From 2aaee8b8d4231d34f5ea3d54910ebd445d886d67 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Tue, 16 Aug 2016 07:30:04 -0700 Subject: [PATCH] Strip flow-only class props without needing transform-class-properties. (#3655) --- .../src/index.js | 13 +++++++++++-- .../fixtures/regression/class-prop-types/actual.js | 3 +++ .../regression/class-prop-types/expected.js | 3 +++ .../regression/class-prop-types/options.json | 7 +++++++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 packages/babel-plugin-transform-flow-strip-types/test/fixtures/regression/class-prop-types/actual.js create mode 100644 packages/babel-plugin-transform-flow-strip-types/test/fixtures/regression/class-prop-types/expected.js create mode 100644 packages/babel-plugin-transform-flow-strip-types/test/fixtures/regression/class-prop-types/options.json diff --git a/packages/babel-plugin-transform-flow-strip-types/src/index.js b/packages/babel-plugin-transform-flow-strip-types/src/index.js index 11933b0ff8..80b52476d7 100644 --- a/packages/babel-plugin-transform-flow-strip-types/src/index.js +++ b/packages/babel-plugin-transform-flow-strip-types/src/index.js @@ -26,8 +26,17 @@ export default function ({ types: t }) { if (!path.node.value) path.remove(); }, - Class({ node }) { - node.implements = null; + Class(path) { + path.node.implements = null; + + // We do this here instead of in a `ClassProperty` visitor because the class transform + // would transform the class before we reached the class property. + path.get("body.body").forEach((child) => { + if (child.isClassProperty()) { + child.node.typeAnnotation = null; + if (!child.node.value) child.remove(); + } + }); }, Function({ node }) { diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/regression/class-prop-types/actual.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/regression/class-prop-types/actual.js new file mode 100644 index 0000000000..11afecb563 --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/regression/class-prop-types/actual.js @@ -0,0 +1,3 @@ +class Test { + prop: string; +} diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/regression/class-prop-types/expected.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/regression/class-prop-types/expected.js new file mode 100644 index 0000000000..a63c4ab872 --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/regression/class-prop-types/expected.js @@ -0,0 +1,3 @@ +let Test = function Test() { + babelHelpers.classCallCheck(this, Test); +}; diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/regression/class-prop-types/options.json b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/regression/class-prop-types/options.json new file mode 100644 index 0000000000..e62c3dc66b --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/regression/class-prop-types/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "transform-flow-strip-types", + "transform-es2015-classes", + "external-helpers" + ] +}