From 52fb88455014ab1c08125dbec3fd6c087740a4be Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Mon, 17 Dec 2018 16:27:56 +0800 Subject: [PATCH] Strips flow directive fully (#9197) --- packages/babel-plugin-transform-flow-strip-types/src/index.js | 4 ++-- .../test/fixtures/strip-types/strip-comment-noflow/input.js | 2 ++ .../test/fixtures/strip-types/strip-comment-noflow/output.js | 1 + .../fixtures/strip-types/strip-comment-strict-local/input.js | 2 ++ .../fixtures/strip-types/strip-comment-strict-local/output.js | 1 + .../test/fixtures/strip-types/strip-comment-strict/input.js | 2 ++ .../test/fixtures/strip-types/strip-comment-strict/output.js | 1 + .../test/fixtures/strip-types/strip-comment-weak/input.js | 2 ++ .../test/fixtures/strip-types/strip-comment-weak/output.js | 1 + 9 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-noflow/input.js create mode 100644 packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-noflow/output.js create mode 100644 packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-strict-local/input.js create mode 100644 packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-strict-local/output.js create mode 100644 packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-strict/input.js create mode 100644 packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-strict/output.js create mode 100644 packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-weak/input.js create mode 100644 packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-weak/output.js 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 7399d65c9f..bf4e5acaff 100644 --- a/packages/babel-plugin-transform-flow-strip-types/src/index.js +++ b/packages/babel-plugin-transform-flow-strip-types/src/index.js @@ -5,7 +5,7 @@ import { types as t } from "@babel/core"; export default declare(api => { api.assertVersion(7); - const FLOW_DIRECTIVE = "@flow"; + const FLOW_DIRECTIVE = /(@flow(\s+(strict(-local)?|weak))?|@noflow)/; let skipStrip = false; @@ -28,7 +28,7 @@ export default declare(api => { if (comments) { for (const comment of (comments: Array)) { - if (comment.value.indexOf(FLOW_DIRECTIVE) >= 0) { + if (FLOW_DIRECTIVE.test(comment.value)) { directiveFound = true; // remove flow directive diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-noflow/input.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-noflow/input.js new file mode 100644 index 0000000000..5d6ff3552a --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-noflow/input.js @@ -0,0 +1,2 @@ +// @noflow +const a = 1; \ No newline at end of file diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-noflow/output.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-noflow/output.js new file mode 100644 index 0000000000..6d3cec7eee --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-noflow/output.js @@ -0,0 +1 @@ +const a = 1; \ No newline at end of file diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-strict-local/input.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-strict-local/input.js new file mode 100644 index 0000000000..02271a151c --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-strict-local/input.js @@ -0,0 +1,2 @@ +// @flow strict-local +const a = 1; \ No newline at end of file diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-strict-local/output.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-strict-local/output.js new file mode 100644 index 0000000000..6d3cec7eee --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-strict-local/output.js @@ -0,0 +1 @@ +const a = 1; \ No newline at end of file diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-strict/input.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-strict/input.js new file mode 100644 index 0000000000..08a31836de --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-strict/input.js @@ -0,0 +1,2 @@ +// @flow strict +const a = 1; \ No newline at end of file diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-strict/output.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-strict/output.js new file mode 100644 index 0000000000..6d3cec7eee --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-strict/output.js @@ -0,0 +1 @@ +const a = 1; \ No newline at end of file diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-weak/input.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-weak/input.js new file mode 100644 index 0000000000..1e784b3507 --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-weak/input.js @@ -0,0 +1,2 @@ +// @flow weak +const a = 1; \ No newline at end of file diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-weak/output.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-weak/output.js new file mode 100644 index 0000000000..6d3cec7eee --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-comment-weak/output.js @@ -0,0 +1 @@ +const a = 1; \ No newline at end of file