diff --git a/docs/options.md b/docs/options.md index 5218b0fb..3db4e59a 100644 --- a/docs/options.md +++ b/docs/options.md @@ -181,6 +181,7 @@ Both the `babel` and `flow` parsers support the same set of JavaScript features Valid options: - `"babel"` (via [@babel/parser](https://github.com/babel/babel/tree/master/packages/babel-parser)) _Named `"babylon"` until v1.16.0_ +- `"babel-flow"` (Same as `"babel"` but enables Flow parsing explicitly to avoid ambiguity) _First available in v1.16.0_ - `"flow"` (via [flow-parser](https://github.com/facebook/flow/tree/master/src/parser)) - `"typescript"` (via [typescript-estree](https://github.com/JamesHenry/typescript-estree)) _First available in v1.4.0_ - `"css"` (via [postcss-scss](https://github.com/postcss/postcss-scss) and [postcss-less](https://github.com/shellscape/postcss-less), autodetects which to use) _First available in v1.7.1_ diff --git a/src/common/internal-plugins.js b/src/common/internal-plugins.js index 03ecc18d..43e85e46 100644 --- a/src/common/internal-plugins.js +++ b/src/common/internal-plugins.js @@ -14,6 +14,11 @@ module.exports = [ get babel() { return eval("require")("../language-js/parser-babylon").parsers.babel; }, + get "babel-flow"() { + return eval("require")("../language-js/parser-babylon").parsers[ + "babel-flow" + ]; + }, get babylon() { return eval("require")("../language-js/parser-babylon").parsers.babel; }, diff --git a/src/language-js/parser-babylon.js b/src/language-js/parser-babylon.js index fe246e45..2b1a09b0 100644 --- a/src/language-js/parser-babylon.js +++ b/src/language-js/parser-babylon.js @@ -18,7 +18,6 @@ function babelOptions(extraOptions, extraPlugins) { allowSuperOutsideMethod: true, plugins: [ "jsx", - "flow", "doExpressions", "objectRestSpread", "classProperties", @@ -45,20 +44,28 @@ function babelOptions(extraOptions, extraPlugins) { ); } -function createParse(parseMethod) { +function createParse(parseMethod, extraPlugins) { return (text, parsers, opts) => { // Inline the require to avoid loading all the JS if we don't use it const babel = require("@babel/parser"); const combinations = [ - babelOptions({ strictMode: true }, ["decorators-legacy"]), - babelOptions({ strictMode: false }, ["decorators-legacy"]), - babelOptions({ strictMode: true }, [ - ["decorators", { decoratorsBeforeExport: false }] - ]), - babelOptions({ strictMode: false }, [ - ["decorators", { decoratorsBeforeExport: false }] - ]) + babelOptions( + { strictMode: true }, + ["decorators-legacy"].concat(extraPlugins) + ), + babelOptions( + { strictMode: false }, + ["decorators-legacy"].concat(extraPlugins) + ), + babelOptions( + { strictMode: true }, + [["decorators", { decoratorsBeforeExport: false }]].concat(extraPlugins) + ), + babelOptions( + { strictMode: false }, + [["decorators", { decoratorsBeforeExport: false }]].concat(extraPlugins) + ) ]; let ast; @@ -82,7 +89,8 @@ function createParse(parseMethod) { }; } -const parse = createParse("parse"); +const parse = createParse("parse", ["flow"]); +const parseFlow = createParse("parse", [["flow", { all: true }]]); const parseExpression = createParse("parseExpression"); function tryCombinations(fn, combinations) { @@ -167,14 +175,14 @@ function assertJsonNode(node, parent) { } const babel = Object.assign({ parse, astFormat: "estree", hasPragma }, locFns); -const babelExpression = Object.assign({}, babel, { - parse: parseExpression -}); +const babelFlow = Object.assign({}, babel, { parse: parseFlow }); +const babelExpression = Object.assign({}, babel, { parse: parseExpression }); // Export as a plugin so we can reuse the same bundle for UMD loading module.exports = { parsers: { babel, + "babel-flow": babelFlow, // aliased to keep backwards compatibility babylon: babel, json: Object.assign({}, babelExpression, { diff --git a/src/main/core-options.js b/src/main/core-options.js index a1f3f2d8..d4582ec3 100644 --- a/src/main/core-options.js +++ b/src/main/core-options.js @@ -130,6 +130,7 @@ const options = { redirect: "babel" }, { value: "babel", since: "1.16.0", description: "JavaScript" }, + { value: "babel-flow", since: "1.16.0", description: "Flow" }, { value: "typescript", since: "1.4.0", description: "TypeScript" }, { value: "css", since: "1.7.1", description: "CSS" }, { diff --git a/tests/empty/__snapshots__/jsfmt.spec.js.snap b/tests/empty/__snapshots__/jsfmt.spec.js.snap index bdef665e..4ec19365 100644 --- a/tests/empty/__snapshots__/jsfmt.spec.js.snap +++ b/tests/empty/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`empty 1`] = ` ====================================options===================================== -parsers: ["flow", "babel", "typescript", "css", "less", "scss", "json", "json5", "json-stringify", "graphql", "markdown", "mdx", "vue", "yaml", "html", "angular"] +parsers: ["flow", "babel", "babel-flow", "typescript", "css", "less", "scss", "json", "json5", "json-stringify", "graphql", "markdown", "mdx", "vue", "yaml", "html", "angular"] printWidth: 80 | printWidth =====================================input====================================== @@ -14,7 +14,7 @@ printWidth: 80 exports[`newline 1`] = ` ====================================options===================================== -parsers: ["flow", "babel", "typescript", "css", "less", "scss", "json", "json5", "json-stringify", "graphql", "markdown", "mdx", "vue", "yaml", "html", "angular"] +parsers: ["flow", "babel", "babel-flow", "typescript", "css", "less", "scss", "json", "json5", "json-stringify", "graphql", "markdown", "mdx", "vue", "yaml", "html", "angular"] printWidth: 80 | printWidth =====================================input====================================== @@ -27,7 +27,7 @@ printWidth: 80 exports[`space 1`] = ` ====================================options===================================== -parsers: ["flow", "babel", "typescript", "css", "less", "scss", "json", "json5", "json-stringify", "graphql", "markdown", "mdx", "vue", "yaml", "html", "angular"] +parsers: ["flow", "babel", "babel-flow", "typescript", "css", "less", "scss", "json", "json5", "json-stringify", "graphql", "markdown", "mdx", "vue", "yaml", "html", "angular"] printWidth: 80 | printWidth =====================================input====================================== @@ -40,7 +40,7 @@ printWidth: 80 exports[`space-newline 1`] = ` ====================================options===================================== -parsers: ["flow", "babel", "typescript", "css", "less", "scss", "json", "json5", "json-stringify", "graphql", "markdown", "mdx", "vue", "yaml", "html", "angular"] +parsers: ["flow", "babel", "babel-flow", "typescript", "css", "less", "scss", "json", "json5", "json-stringify", "graphql", "markdown", "mdx", "vue", "yaml", "html", "angular"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow_all/__snapshots__/jsfmt.spec.js.snap b/tests/flow_all/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..1eadeb60 --- /dev/null +++ b/tests/flow_all/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,15 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`generic.js 1`] = ` +====================================options===================================== +parsers: ["babel-flow", "flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +const Theme = React.createContext<"light" | "dark">("light"); + +=====================================output===================================== +const Theme = React.createContext<"light" | "dark">("light"); + +================================================================================ +`; diff --git a/tests/flow_all/generic.js b/tests/flow_all/generic.js new file mode 100644 index 00000000..72267976 --- /dev/null +++ b/tests/flow_all/generic.js @@ -0,0 +1 @@ +const Theme = React.createContext<"light" | "dark">("light"); diff --git a/tests/flow_all/jsfmt.spec.js b/tests/flow_all/jsfmt.spec.js new file mode 100644 index 00000000..6da232e6 --- /dev/null +++ b/tests/flow_all/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["babel-flow", "flow"]); diff --git a/tests_integration/__tests__/__snapshots__/early-exit.js.snap b/tests_integration/__tests__/__snapshots__/early-exit.js.snap index a6f103c1..4c4d5ad5 100644 --- a/tests_integration/__tests__/__snapshots__/early-exit.js.snap +++ b/tests_integration/__tests__/__snapshots__/early-exit.js.snap @@ -72,7 +72,7 @@ Format options: Defaults to false. --jsx-single-quote Use single quotes in JSX. Defaults to false. - --parser + --parser Which parser to use. --print-width The line length where Prettier will try wrap. Defaults to 80. @@ -162,7 +162,7 @@ exports[`show warning with --help not-found (typo) (stderr) 1`] = ` `; exports[`show warning with --help not-found (typo) (stdout) 1`] = ` -"--parser +"--parser Which parser to use. @@ -170,6 +170,7 @@ Valid options: flow Flow babel JavaScript + babel-flow Flow typescript TypeScript css CSS less Less @@ -220,7 +221,7 @@ Format options: Defaults to false. --jsx-single-quote Use single quotes in JSX. Defaults to false. - --parser + --parser Which parser to use. --print-width The line length where Prettier will try wrap. Defaults to 80. diff --git a/tests_integration/__tests__/__snapshots__/help-options.js.snap b/tests_integration/__tests__/__snapshots__/help-options.js.snap index f2998b4d..fdfe0ba5 100644 --- a/tests_integration/__tests__/__snapshots__/help-options.js.snap +++ b/tests_integration/__tests__/__snapshots__/help-options.js.snap @@ -332,7 +332,7 @@ exports[`show detailed usage with --help no-semi (write) 1`] = `Array []`; exports[`show detailed usage with --help parser (stderr) 1`] = `""`; exports[`show detailed usage with --help parser (stdout) 1`] = ` -"--parser +"--parser Which parser to use. @@ -340,6 +340,7 @@ Valid options: flow Flow babel JavaScript + babel-flow Flow typescript TypeScript css CSS less Less diff --git a/tests_integration/__tests__/__snapshots__/schema.js.snap b/tests_integration/__tests__/__snapshots__/schema.js.snap index d002d832..e72c7c0e 100644 --- a/tests_integration/__tests__/__snapshots__/schema.js.snap +++ b/tests_integration/__tests__/__snapshots__/schema.js.snap @@ -134,6 +134,12 @@ This option cannot be used with --range-start and --range-end.", "babel", ], }, + Object { + "description": "Flow", + "enum": Array [ + "babel-flow", + ], + }, Object { "description": "TypeScript", "enum": Array [ diff --git a/tests_integration/__tests__/__snapshots__/support-info.js.snap b/tests_integration/__tests__/__snapshots__/support-info.js.snap index ac3a87ea..dfa9251b 100644 --- a/tests_integration/__tests__/__snapshots__/support-info.js.snap +++ b/tests_integration/__tests__/__snapshots__/support-info.js.snap @@ -489,7 +489,7 @@ exports[`API getSupportInfo() with version 1.8.2 -> undefined 1`] = ` \\"type\\": \\"boolean\\", }, \\"cursorOffset\\": Object { -@@ -52,37 +81,75 @@ +@@ -52,37 +81,76 @@ \\"start\\": -1, \\"step\\": 1, }, @@ -535,6 +535,7 @@ exports[`API getSupportInfo() with version 1.8.2 -> undefined 1`] = ` \\"flow\\", - \\"babylon\\", + \\"babel\\", ++ \\"babel-flow\\", \\"typescript\\", \\"css\\", \\"less\\", @@ -567,7 +568,7 @@ exports[`API getSupportInfo() with version 1.8.2 -> undefined 1`] = ` \\"range\\": Object { \\"end\\": Infinity, \\"start\\": 0, -@@ -90,14 +157,15 @@ +@@ -90,14 +158,15 @@ }, \\"type\\": \\"int\\", }, @@ -1086,6 +1087,7 @@ exports[`CLI --support-info (stdout) 1`] = ` \\"choices\\": [ { \\"description\\": \\"Flow\\", \\"value\\": \\"flow\\" }, { \\"description\\": \\"JavaScript\\", \\"since\\": \\"1.16.0\\", \\"value\\": \\"babel\\" }, + { \\"description\\": \\"Flow\\", \\"since\\": \\"1.16.0\\", \\"value\\": \\"babel-flow\\" }, { \\"description\\": \\"TypeScript\\", \\"since\\": \\"1.4.0\\", diff --git a/website/static/worker.js b/website/static/worker.js index df63bb58..87902c01 100644 --- a/website/static/worker.js +++ b/website/static/worker.js @@ -17,6 +17,10 @@ var parsers = { importScriptOnce("lib/parser-babylon.js"); return prettierPlugins.babylon.parsers.babel; }, + get "babel-flow"() { + importScriptOnce("lib/parser-babylon.js"); + return prettierPlugins.babylon.parsers["babel-flow"]; + }, // backward compatibility get babylon() { importScriptOnce("lib/parser-babylon.js");