From 43aa61d6bedfe0025ceb2f5f3512b3c66bc6c604 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Wed, 16 May 2018 15:06:35 -0700 Subject: [PATCH] Make the typescript preset require a filename unless the user configured it for general use. --- .../babel-plugin-syntax-flow/src/index.js | 10 +++++ packages/babel-plugin-syntax-jsx/src/index.js | 10 +++++ .../src/index.js | 30 ++++++++++++- .../imports/elide-preact-no/options.json | 2 +- .../imports/elide-react-no-2/options.json | 2 +- .../imports/elide-react-no/options.json | 2 +- packages/babel-preset-react/package.json | 1 - packages/babel-preset-react/src/index.js | 2 - packages/babel-preset-typescript/src/index.js | 44 ++++++++++++++++--- packages/babel-standalone/test/babel.js | 2 +- 10 files changed, 91 insertions(+), 14 deletions(-) diff --git a/packages/babel-plugin-syntax-flow/src/index.js b/packages/babel-plugin-syntax-flow/src/index.js index 855586638b..a7fcf7203a 100644 --- a/packages/babel-plugin-syntax-flow/src/index.js +++ b/packages/babel-plugin-syntax-flow/src/index.js @@ -9,6 +9,16 @@ export default declare((api, options) => { return { manipulateOptions(opts, parserOpts) { + // If the file has already enabled TS, assume that this is not a + // valid Flowtype file. + if ( + parserOpts.plugins.some( + p => (Array.isArray(p) ? p[0] : p) === "typescript", + ) + ) { + return; + } + parserOpts.plugins.push(["flow", { all }]); }, }; diff --git a/packages/babel-plugin-syntax-jsx/src/index.js b/packages/babel-plugin-syntax-jsx/src/index.js index 3a17865e6d..33b751f6c7 100644 --- a/packages/babel-plugin-syntax-jsx/src/index.js +++ b/packages/babel-plugin-syntax-jsx/src/index.js @@ -5,6 +5,16 @@ export default declare(api => { return { manipulateOptions(opts, parserOpts) { + // If the Typescript plugin already ran, it will have decided whether + // or not this is a TSX file. + if ( + parserOpts.plugins.some( + p => (Array.isArray(p) ? p[0] : p) === "typescript", + ) + ) { + return; + } + parserOpts.plugins.push("jsx"); }, }; diff --git a/packages/babel-plugin-syntax-typescript/src/index.js b/packages/babel-plugin-syntax-typescript/src/index.js index 09be2c66a8..d804ab18bf 100644 --- a/packages/babel-plugin-syntax-typescript/src/index.js +++ b/packages/babel-plugin-syntax-typescript/src/index.js @@ -1,15 +1,43 @@ import { declare } from "@babel/helper-plugin-utils"; -export default declare(api => { +function removePlugin(plugins, name) { + const indices = []; + plugins.forEach((plugin, i) => { + const n = Array.isArray(plugin) ? plugin[0] : plugin; + + if (n === name) { + indices.unshift(i); + } + }); + + for (const i of indices) { + plugins.splice(i, 1); + } +} + +export default declare((api, { isTSX }) => { api.assertVersion(7); return { manipulateOptions(opts, parserOpts) { + const { plugins } = parserOpts; + // If the Flow syntax plugin already ran, remove it since Typescript + // takes priority. + removePlugin(plugins, "flow"); + + // If the JSX syntax plugin already ran, remomove it because JSX handling + // in TS depends on the extensions, and is purely dependent on 'isTSX'. + removePlugin(plugins, "jsx"); + parserOpts.plugins.push( "typescript", "objectRestSpread", "classProperties", ); + + if (isTSX) { + parserOpts.plugins.push("jsx"); + } }, }; }); diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact-no/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact-no/options.json index bba55263ce..5bd8727ebb 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact-no/options.json +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact-no/options.json @@ -1,3 +1,3 @@ { - "plugins": ["syntax-jsx", ["transform-typescript", { "jsxPragma": "h" }]] + "plugins": [["transform-typescript", { "jsxPragma": "h", "isTSX": true }]] } diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/options.json index 2552742a15..20970e7cd0 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/options.json +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/options.json @@ -1,3 +1,3 @@ { - "plugins": ["syntax-jsx", "transform-typescript"] + "plugins": [["transform-typescript", { "isTSX": true }]] } diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/options.json index 2552742a15..20970e7cd0 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/options.json +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/options.json @@ -1,3 +1,3 @@ { - "plugins": ["syntax-jsx", "transform-typescript"] + "plugins": [["transform-typescript", { "isTSX": true }]] } diff --git a/packages/babel-preset-react/package.json b/packages/babel-preset-react/package.json index 7826d39ed2..e92bcb5c47 100644 --- a/packages/babel-preset-react/package.json +++ b/packages/babel-preset-react/package.json @@ -9,7 +9,6 @@ "main": "lib/index.js", "dependencies": { "@babel/helper-plugin-utils": "7.0.0-beta.47", - "@babel/plugin-syntax-jsx": "7.0.0-beta.47", "@babel/plugin-transform-react-display-name": "7.0.0-beta.47", "@babel/plugin-transform-react-jsx": "7.0.0-beta.47", "@babel/plugin-transform-react-jsx-self": "7.0.0-beta.47", diff --git a/packages/babel-preset-react/src/index.js b/packages/babel-preset-react/src/index.js index d13753fdc5..f99fe36e0b 100644 --- a/packages/babel-preset-react/src/index.js +++ b/packages/babel-preset-react/src/index.js @@ -1,6 +1,5 @@ import { declare } from "@babel/helper-plugin-utils"; import transformReactJSX from "@babel/plugin-transform-react-jsx"; -import transformSyntaxJSX from "@babel/plugin-syntax-jsx"; import transformReactDisplayName from "@babel/plugin-transform-react-display-name"; import transformReactJSXSource from "@babel/plugin-transform-react-jsx-source"; import transformReactJSXSelf from "@babel/plugin-transform-react-jsx-self"; @@ -27,7 +26,6 @@ export default declare((api, opts) => { transformReactJSX, { pragma, pragmaFrag, throwIfNamespace, useBuiltIns }, ], - transformSyntaxJSX, transformReactDisplayName, development && transformReactJSXSource, diff --git a/packages/babel-preset-typescript/src/index.js b/packages/babel-preset-typescript/src/index.js index 7e90b338ea..e6d12dbffe 100644 --- a/packages/babel-preset-typescript/src/index.js +++ b/packages/babel-preset-typescript/src/index.js @@ -1,10 +1,42 @@ import { declare } from "@babel/helper-plugin-utils"; import transformTypeScript from "@babel/plugin-transform-typescript"; -export default declare((api, { jsxPragma }) => { - api.assertVersion(7); +export default declare( + (api, { jsxPragma, allExtensions = false, isTSX = false }) => { + api.assertVersion(7); - return { - plugins: [[transformTypeScript, { jsxPragma }]], - }; -}); + if (typeof allExtensions !== "boolean") { + throw new Error(".allExtensions must be a boolean, or undefined"); + } + if (typeof isTSX !== "boolean") { + throw new Error(".allExtensions must be a boolean, or undefined"); + } + + if (isTSX && !allExtensions) { + throw new Error("isTSX:true requires allExtensions:true"); + } + + return { + overrides: allExtensions + ? [ + { + plugins: [[transformTypeScript, { jsxPragma, isTSX }]], + }, + ] + : [ + { + // Only set 'test' if explicitly requested, since it requires that + // Babel is being called` + test: /\.ts$/, + plugins: [[transformTypeScript, { jsxPragma }]], + }, + { + // Only set 'test' if explicitly requested, since it requires that + // Babel is being called` + test: /\.tsx$/, + plugins: [[transformTypeScript, { jsxPragma, isTSX: true }]], + }, + ], + }; + }, +); diff --git a/packages/babel-standalone/test/babel.js b/packages/babel-standalone/test/babel.js index 46f9187288..056bfafca8 100644 --- a/packages/babel-standalone/test/babel.js +++ b/packages/babel-standalone/test/babel.js @@ -23,7 +23,7 @@ }); it("handles the typescript preset", () => { const output = Babel.transform("var a: string;", { - presets: ["typescript"], + presets: [["typescript", { allExtensions: true }]], }).code; expect(output).toBe("var a;"); });