Skip to content

Commit

Permalink
Add "allowDeclareFields" option to flow-strip-types
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Feb 27, 2020
1 parent c79edab commit 195b16a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 14 deletions.
35 changes: 23 additions & 12 deletions packages/babel-plugin-transform-flow-strip-types/src/index.js
Expand Up @@ -2,13 +2,15 @@ import { declare } from "@babel/helper-plugin-utils";
import syntaxFlow from "@babel/plugin-syntax-flow";
import { types as t } from "@babel/core";

export default declare(api => {
export default declare((api, opts) => {
api.assertVersion(7);

const FLOW_DIRECTIVE = /(@flow(\s+(strict(-local)?|weak))?|@noflow)/;

let skipStrip = false;

const { requireDirective = false, allowDeclareFields = false } = opts;

return {
name: "transform-flow-strip-types",
inherits: syntaxFlow,
Expand All @@ -20,7 +22,6 @@ export default declare(api => {
file: {
ast: { comments },
},
opts,
},
) {
skipStrip = false;
Expand All @@ -42,7 +43,7 @@ export default declare(api => {
}
}

if (!directiveFound && opts.requireDirective) {
if (!directiveFound && requireDirective) {
skipStrip = true;
}
},
Expand Down Expand Up @@ -74,13 +75,6 @@ export default declare(api => {
path.remove();
},

ClassProperty(path) {
if (skipStrip) return;
path.node.variance = null;
path.node.typeAnnotation = null;
if (!path.node.value) path.remove();
},

ClassPrivateProperty(path) {
if (skipStrip) return;
path.node.typeAnnotation = null;
Expand All @@ -94,8 +88,25 @@ export default declare(api => {
// 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();
const { node } = child;

if (!allowDeclareFields && node.declare) {
throw child.buildCodeFrameError(
`The 'declare' modifier is only allowed when the ` +
`'allowDeclareFields' option of ` +
`@babel/plugin-transform-flow-strip-types or ` +
`@babel/preset-flow is enabled.`,
);
}

if (node.declare) {
child.remove();
} else if (!allowDeclareFields && !node.value && !node.decorators) {
child.remove();
} else {
node.variance = null;
node.typeAnnotation = null;
}
}
});
},
Expand Down
@@ -0,0 +1,3 @@
class A {
declare x;
}
@@ -0,0 +1,7 @@
{
"plugins": [
"transform-flow-strip-types",
"syntax-class-properties"
],
"throws": "The 'declare' modifier is only allowed when the 'allowDeclareFields' option of @babel/plugin-transform-flow-strip-types or @babel/preset-flow is enabled."
}
@@ -0,0 +1,3 @@
class A {
declare x;
}
@@ -0,0 +1,6 @@
{
"plugins": [
["transform-flow-strip-types", { "allowDeclareFields": true }],
"syntax-class-properties"
]
}
@@ -0,0 +1 @@
class A {}
4 changes: 2 additions & 2 deletions packages/babel-preset-flow/src/index.js
@@ -1,10 +1,10 @@
import { declare } from "@babel/helper-plugin-utils";
import transformFlowStripTypes from "@babel/plugin-transform-flow-strip-types";

export default declare((api, { all }) => {
export default declare((api, { all, allowDeclareFields }) => {
api.assertVersion(7);

return {
plugins: [[transformFlowStripTypes, { all }]],
plugins: [[transformFlowStripTypes, { all, allowDeclareFields }]],
};
});

0 comments on commit 195b16a

Please sign in to comment.