feat: add babel-flow (#5685)

master
Ika 2018-12-29 21:35:47 +08:00 committed by GitHub
parent 284764f030
commit 7c4cebeaa5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 70 additions and 24 deletions

View File

@ -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_

View File

@ -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;
},

View File

@ -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, {

View File

@ -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" },
{

View File

@ -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======================================

View File

@ -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");
================================================================================
`;

View File

@ -0,0 +1 @@
const Theme = React.createContext<"light" | "dark">("light");

View File

@ -0,0 +1 @@
run_spec(__dirname, ["babel-flow", "flow"]);

View File

@ -72,7 +72,7 @@ Format options:
Defaults to false.
--jsx-single-quote Use single quotes in JSX.
Defaults to false.
--parser <flow|babel|typescript|css|less|scss|json|json5|json-stringify|graphql|markdown|mdx|vue|yaml|html|angular>
--parser <flow|babel|babel-flow|typescript|css|less|scss|json|json5|json-stringify|graphql|markdown|mdx|vue|yaml|html|angular>
Which parser to use.
--print-width <int> 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 <flow|babel|typescript|css|less|scss|json|json5|json-stringify|graphql|markdown|mdx|vue|yaml|html|angular>
"--parser <flow|babel|babel-flow|typescript|css|less|scss|json|json5|json-stringify|graphql|markdown|mdx|vue|yaml|html|angular>
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 <flow|babel|typescript|css|less|scss|json|json5|json-stringify|graphql|markdown|mdx|vue|yaml|html|angular>
--parser <flow|babel|babel-flow|typescript|css|less|scss|json|json5|json-stringify|graphql|markdown|mdx|vue|yaml|html|angular>
Which parser to use.
--print-width <int> The line length where Prettier will try wrap.
Defaults to 80.

View File

@ -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 <flow|babel|typescript|css|less|scss|json|json5|json-stringify|graphql|markdown|mdx|vue|yaml|html|angular>
"--parser <flow|babel|babel-flow|typescript|css|less|scss|json|json5|json-stringify|graphql|markdown|mdx|vue|yaml|html|angular>
Which parser to use.
@ -340,6 +340,7 @@ Valid options:
flow Flow
babel JavaScript
babel-flow Flow
typescript TypeScript
css CSS
less Less

View File

@ -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 [

View File

@ -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\\",

View File

@ -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");