diff --git a/changelog_unreleased/flow/10397.md b/changelog_unreleased/flow/10397.md new file mode 100644 index 000000000..3df2ddcc3 --- /dev/null +++ b/changelog_unreleased/flow/10397.md @@ -0,0 +1,15 @@ +#### Support `this` type annotation in functions via `babel-flow` (#10397 by @sosukesuzuki) + +`this` type annotation is supported since [Babel 7.13](https://babeljs.io/blog/2021/02/22/7.13.0#new-flow-features-12193httpsgithubcombabelbabelpull12193-12234httpsgithubcombabelbabelpull12234). + +```js +// Input +var foo: (this: boolean) => void; + +// Prettier stable +SyntaxError: Unexpected token, expected ")" (1:15) + +// Prettier main +var foo: (this: boolean) => void; + +``` diff --git a/package.json b/package.json index aba976076..7fcdf433a 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "dependencies": { "@angular/compiler": "11.2.1", "@babel/code-frame": "7.12.13", - "@babel/parser": "7.12.17", + "@babel/parser": "7.13.4", "@glimmer/syntax": "0.77.1", "@iarna/toml": "2.2.5", "@typescript-eslint/typescript-estree": "4.15.1", diff --git a/tests/flow/this-annotation/__snapshots__/jsfmt.spec.js.snap b/tests/flow/this-annotation/__snapshots__/jsfmt.spec.js.snap index 6c97fb86a..12da03297 100644 --- a/tests/flow/this-annotation/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/this-annotation/__snapshots__/jsfmt.spec.js.snap @@ -54,17 +54,6 @@ function baz(this: number, ...a) {} ================================================================================ `; -exports[`function_type.js - {"trailingComma":"all"} [babel-flow] format 1`] = ` -"Unexpected token, expected \\")\\" (3:16) - 1 | // @flow - 2 | -> 3 | type T = (this : number, a : string, b : number) => void - | ^ - 4 | - 5 | type U = (this : number, ...c : any) => void - 6 |" -`; - exports[`function_type.js - {"trailingComma":"all"} format 1`] = ` ====================================options===================================== parsers: ["flow", "babel-flow"] @@ -92,17 +81,6 @@ type V = (this: number) => void; ================================================================================ `; -exports[`line_break.js - {"trailingComma":"all"} [babel-flow] format 1`] = ` -"Unexpected token, expected \\")\\" (3:15) - 1 | // @flow - 2 | -> 3 | type T = (this: boolean, - | ^ - 4 | a: number, - 5 | - 6 | b: number," -`; - exports[`line_break.js - {"trailingComma":"all"} format 1`] = ` ====================================options===================================== parsers: ["flow", "babel-flow"] @@ -195,17 +173,6 @@ class A { ================================================================================ `; -exports[`union_type.js - {"trailingComma":"all"} [babel-flow] format 1`] = ` -"Unexpected token, expected \\")\\" (4:7) - 2 | - 3 | type A = ( -> 4 | this: | SupperLongLongLongLongLongLongLongLongLongLongLongType | FooBarBazLorem12345, - | ^ - 5 | b: number, - 6 | ) => boolean; - 7 |" -`; - exports[`union_type.js - {"trailingComma":"all"} format 1`] = ` ====================================options===================================== parsers: ["flow", "babel-flow"] diff --git a/tests/flow/this-annotation/jsfmt.spec.js b/tests/flow/this-annotation/jsfmt.spec.js index 4d93126a0..eaab82f9e 100644 --- a/tests/flow/this-annotation/jsfmt.spec.js +++ b/tests/flow/this-annotation/jsfmt.spec.js @@ -1,6 +1,3 @@ run_spec(__dirname, ["flow", "babel-flow"], { trailingComma: "all", - errors: { - "babel-flow": ["function_type.js", "line_break.js", "union_type.js"], - }, }); diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js index ea8baaed3..f97e8a702 100644 --- a/tests_config/run_spec.js +++ b/tests_config/run_spec.js @@ -47,6 +47,27 @@ const unstableTests = new Map( }) ); +// This is temporary. +// This will be removed once https://github.com/babel/babel/issues/12915 is fixed +const unstableAstTests = new Map( + [ + [ + "flow/function-parentheses/single.js", + (options) => + options.parser.startsWith("babel") && options.arrowParens !== "avoid", + ], + [ + "flow/function-parentheses/test.js", + (options) => options.parser.startsWith("babel"), + ], + ].map((fixture) => { + const [file, isUnstable = () => true] = Array.isArray(fixture) + ? fixture + : [fixture]; + return [path.join(__dirname, "../tests/", file), isUnstable]; + }) +); + const espreeDisabledTests = new Set( [ // These tests only work for `babel` @@ -65,6 +86,16 @@ const isUnstable = (filename, options) => { return testFunction(options); }; +const isAstUnstable = (filename, options) => { + const testFunction = unstableAstTests.get(filename); + + if (!testFunction) { + return false; + } + + return testFunction(options); +}; + const shouldThrowOnFormat = (filename, options) => { const { errors = {} } = options; if (errors === true) { @@ -293,13 +324,18 @@ function runTest({ }); } + const isAstUnstableTest = isAstUnstable(filename, formatOptions); // Some parsers skip parsing empty files if (formatResult.changed && code.trim()) { test(`[${parser}] compare AST`, () => { const { input, output } = formatResult; const originalAst = parse(input, formatOptions); const formattedAst = parse(output, formatOptions); - expect(formattedAst).toEqual(originalAst); + if (isAstUnstableTest) { + expect(formattedAst).not.toEqual(originalAst); + } else { + expect(formattedAst).toEqual(originalAst); + } }); } diff --git a/yarn.lock b/yarn.lock index 2ecab2ab4..250b1c09f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -267,15 +267,10 @@ chalk "^2.0.0" js-tokens "^4.0.0" -"@babel/parser@7.12.17": - version "7.12.17" - resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.12.17.tgz#bc85d2d47db38094e5bb268fc761716e7d693848" - integrity sha512-r1yKkiUTYMQ8LiEI0UcQx5ETw5dpTLn9wijn9hk6KkTtOK95FndDN10M+8/s6k/Ymlbivw0Av9q4SlgF80PtHg== - -"@babel/parser@^7.1.0", "@babel/parser@^7.12.13", "@babel/parser@^7.12.17", "@babel/parser@^7.7.0": - version "7.12.17" - resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.12.17.tgz#bc85d2d47db38094e5bb268fc761716e7d693848" - integrity sha512-r1yKkiUTYMQ8LiEI0UcQx5ETw5dpTLn9wijn9hk6KkTtOK95FndDN10M+8/s6k/Ymlbivw0Av9q4SlgF80PtHg== +"@babel/parser@7.13.4", "@babel/parser@^7.1.0", "@babel/parser@^7.12.13", "@babel/parser@^7.12.17", "@babel/parser@^7.7.0": + version "7.13.4" + resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.13.4.tgz#340211b0da94a351a6f10e63671fa727333d13ab" + integrity sha512-uvoOulWHhI+0+1f9L4BoozY7U5cIkZ9PgJqvb041d6vypgUmtVPG4vmGm4pSggjl8BELzvHyUeJSUyEMY6b+qA== "@babel/plugin-proposal-async-generator-functions@^7.12.13": version "7.12.13" @@ -3078,7 +3073,6 @@ eslint-plugin-jest@24.1.5: "eslint-plugin-prettier-internal-rules@link:scripts/tools/eslint-plugin-prettier-internal-rules": version "0.0.0" - uid "" eslint-plugin-react@7.22.0: version "7.22.0"