diff --git a/packages/babel-plugin-transform-flow-strip-types/README.md b/packages/babel-plugin-transform-flow-strip-types/README.md index dfb4c0f03a..9248839270 100644 --- a/packages/babel-plugin-transform-flow-strip-types/README.md +++ b/packages/babel-plugin-transform-flow-strip-types/README.md @@ -55,4 +55,5 @@ require("babel-core").transform("code", { `boolean`, defaults to `false`. Setting this to true will only strip annotations and declarations from files -that contain the `// @flow` directive. +that contain the `// @flow` directive. It will also throw errors for any Flow +annotations found in files without the directive. 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 c0acf431b4..1bec2a5745 100644 --- a/packages/babel-plugin-transform-flow-strip-types/src/index.js +++ b/packages/babel-plugin-transform-flow-strip-types/src/index.js @@ -47,7 +47,13 @@ export default function({ types: t }) { }, Flow(path) { - if (skipStrip) return; + if (skipStrip) { + throw path.buildCodeFrameError( + "A @flow directive is required when using Flow annotations with " + + "babel-preset-react or the `requireDirective` option.", + ); + } + path.remove(); }, diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/requireDirective/true-does-not-throw-with-directive/actual.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/requireDirective/true-does-not-throw-with-directive/actual.js new file mode 100644 index 0000000000..88aebda63c --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/requireDirective/true-does-not-throw-with-directive/actual.js @@ -0,0 +1 @@ +function foo(numVal, strVal) {} diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/requireDirective/true-does-not-throw-with-directive/expected.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/requireDirective/true-does-not-throw-with-directive/expected.js new file mode 100644 index 0000000000..88aebda63c --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/requireDirective/true-does-not-throw-with-directive/expected.js @@ -0,0 +1 @@ +function foo(numVal, strVal) {} diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/requireDirective/true-ignores-without-directive/actual.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/requireDirective/true-throws-if-found-with-directive/actual.js similarity index 100% rename from packages/babel-plugin-transform-flow-strip-types/test/fixtures/requireDirective/true-ignores-without-directive/actual.js rename to packages/babel-plugin-transform-flow-strip-types/test/fixtures/requireDirective/true-throws-if-found-with-directive/actual.js diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/requireDirective/true-throws-if-found-with-directive/options.json b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/requireDirective/true-throws-if-found-with-directive/options.json new file mode 100644 index 0000000000..03baa59e36 --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/requireDirective/true-throws-if-found-with-directive/options.json @@ -0,0 +1,3 @@ +{ + "throws": "A @flow directive is required when using Flow annotations with babel-preset-react or the `requireDirective` option." +} diff --git a/packages/babel-preset-react/README.md b/packages/babel-preset-react/README.md index 93ce543067..dc25e804c9 100644 --- a/packages/babel-preset-react/README.md +++ b/packages/babel-preset-react/README.md @@ -14,8 +14,10 @@ And with the `development` option: - [transform-react-jsx-self](https://babeljs.io/docs/plugins/transform-react-jsx-self/) - [transform-react-jsx-source](https://babeljs.io/docs/plugins/transform-react-jsx-source/) -Note: Flow annotations and declarations will _only_ be removed in files that -have the `// @flow ` directive. +Note: This preset sets the `requireDirective` option on +`transform-flow-strip-types`. This means Flow annotations and declarations +will _only_ be removed in files that have a `// @flow ` directive. It will also +throw errors for any Flow annotations found in files without the directive. ```js // @flow diff --git a/packages/babel-preset-react/test/fixtures/flow/strip-without-directive/expected.js b/packages/babel-preset-react/test/fixtures/flow/strip-without-directive/expected.js deleted file mode 100644 index 6bd1f1803c..0000000000 --- a/packages/babel-preset-react/test/fixtures/flow/strip-without-directive/expected.js +++ /dev/null @@ -1 +0,0 @@ -function foo(numVal: number, strVal: string) {} diff --git a/packages/babel-preset-react/test/fixtures/flow/strip-without-directive/actual.js b/packages/babel-preset-react/test/fixtures/flow/throws-without-directive/actual.js similarity index 100% rename from packages/babel-preset-react/test/fixtures/flow/strip-without-directive/actual.js rename to packages/babel-preset-react/test/fixtures/flow/throws-without-directive/actual.js diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/requireDirective/true-ignores-without-directive/expected.js b/packages/babel-preset-react/test/fixtures/flow/throws-without-directive/expected.js similarity index 100% rename from packages/babel-plugin-transform-flow-strip-types/test/fixtures/requireDirective/true-ignores-without-directive/expected.js rename to packages/babel-preset-react/test/fixtures/flow/throws-without-directive/expected.js diff --git a/packages/babel-preset-react/test/fixtures/flow/throws-without-directive/options.json b/packages/babel-preset-react/test/fixtures/flow/throws-without-directive/options.json new file mode 100644 index 0000000000..03baa59e36 --- /dev/null +++ b/packages/babel-preset-react/test/fixtures/flow/throws-without-directive/options.json @@ -0,0 +1,3 @@ +{ + "throws": "A @flow directive is required when using Flow annotations with babel-preset-react or the `requireDirective` option." +}