Skip to content

Commit

Permalink
Remove flow plugin from babel parser (#14314)
Browse files Browse the repository at this point in the history
Co-authored-by: Georgii Dolzhykov <thorn.mailbox@gmail.com>
  • Loading branch information
fisker and thorn0 committed Feb 10, 2023
1 parent 6b40e2e commit add8435
Show file tree
Hide file tree
Showing 34 changed files with 760 additions and 573 deletions.
3 changes: 3 additions & 0 deletions changelog_unreleased/javascript/14314.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#### [BREAKING] Remove Flow syntax support from `babel` parser (#14314 by @fisker, @thorn0)

For historical reasons, Prettier used to recognize Flow syntax in JS files when the `parser` option was set to `babel` even if the file didn't include the `@flow` pragma. This support was limited and bad for performance, so it has been removed in Prettier main. Prettier with the `babel` parser still automatically switches to the Flow syntax if it finds the `@flow` pragma or the file has the `.flow.js` extension.
8 changes: 5 additions & 3 deletions src/language-js/parse/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,9 @@ const allowedMessageCodes = new Set([
"DuplicateExport",
]);

const babelParserOptionsCombinations = [appendPlugins(["jsx"])];
const babel = createBabelParser({
optionsCombinations: [appendPlugins(["jsx", "flow"])],
optionsCombinations: babelParserOptionsCombinations,
});
const babelTs = createBabelParser({
optionsCombinations: [
Expand All @@ -235,7 +236,6 @@ const babelTSExpression = createBabelParser({
isExpression: true,
optionsCombinations: [appendPlugins(["typescript"])],
});

const babelFlow = createBabelParser({
optionsCombinations: [
appendPlugins([
Expand All @@ -246,7 +246,9 @@ const babelFlow = createBabelParser({
],
});
const babelEstree = createBabelParser({
optionsCombinations: [appendPlugins(["jsx", "flow", "estree"])],
optionsCombinations: babelParserOptionsCombinations.map((options) =>
appendPlugins(["estree"], options)
),
});

// Export as a plugin so we can reuse the same bundle for UMD loading
Expand Down
26 changes: 13 additions & 13 deletions tests/config/format-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,19 +218,6 @@ function runSpec(fixtures, parsers, options) {
const allParsers = [...parsers];

if (!IS_ERROR_TESTS) {
if (
parsers.includes("typescript") &&
!parsers.includes("babel-ts") &&
!IS_TYPESCRIPT_ONLY_TEST &&
!babelTsDisabledTest.has(dirname)
) {
allParsers.push("babel-ts");
}

if (parsers.includes("flow") && !parsers.includes("babel-flow")) {
allParsers.push("babel-flow");
}

if (
parsers.includes("babel") &&
(isTestDirectory(dirname, "js") || isTestDirectory(dirname, "jsx"))
Expand All @@ -246,6 +233,19 @@ function runSpec(fixtures, parsers, options) {
}
}

if (
parsers.includes("typescript") &&
!parsers.includes("babel-ts") &&
!IS_TYPESCRIPT_ONLY_TEST &&
!babelTsDisabledTest.has(dirname)
) {
allParsers.push("babel-ts");
}

if (parsers.includes("flow") && !parsers.includes("babel-flow")) {
allParsers.push("babel-flow");
}

if (parsers.includes("babel") && !parsers.includes("__babel_estree")) {
allParsers.push("__babel_estree");
}
Expand Down
117 changes: 117 additions & 0 deletions tests/format/flow/decorator/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`comments.js format 1`] = `
====================================options=====================================
parsers: ["babel-flow"]
printWidth: 80
| printWidth
=====================================input======================================
class Something {
@Annotateme()
// comment
static property: Array<string>;
}
=====================================output=====================================
class Something {
@Annotateme()
// comment
static property: Array<string>;
}
================================================================================
`;
exports[`mobx.js format 1`] = `
====================================options=====================================
parsers: ["babel-flow"]
printWidth: 80
| printWidth
=====================================input======================================
import {observable} from "mobx";
@observer class OrderLine {
@observable price:number = 0;
@observable amount:number = 1;
constructor(price) {
this.price = price;
}
@computed get total() {
return this.price * this.amount;
}
@action.bound setPrice(price) {
this.price = price;
}
@computed
get total() {
return this.price * this.amount;
}
@action.bound
setPrice(price) {
this.price = price;
}
@computed @computed @computed @computed @computed @computed @computed get total() {
return this.price * this.amount;
}
@action handleDecrease = (event: React.ChangeEvent<HTMLInputElement>) => this.count--;
@action handleSomething = (event: React.ChangeEvent<HTMLInputElement>) => doSomething();
}
=====================================output=====================================
import { observable } from "mobx";
@observer
class OrderLine {
@observable price: number = 0;
@observable amount: number = 1;
constructor(price) {
this.price = price;
}
@computed get total() {
return this.price * this.amount;
}
@action.bound setPrice(price) {
this.price = price;
}
@computed
get total() {
return this.price * this.amount;
}
@action.bound
setPrice(price) {
this.price = price;
}
@computed
@computed
@computed
@computed
@computed
@computed
@computed
get total() {
return this.price * this.amount;
}
@action handleDecrease = (event: React.ChangeEvent<HTMLInputElement>) =>
this.count--;
@action handleSomething = (event: React.ChangeEvent<HTMLInputElement>) =>
doSomething();
}
================================================================================
`;
File renamed without changes.
2 changes: 2 additions & 0 deletions tests/format/flow/decorator/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// TODO: Run tests with `flow` parser https://github.com/facebook/flow/issues/9003
run_spec(import.meta, ["babel-flow"]);
36 changes: 36 additions & 0 deletions tests/format/flow/decorator/mobx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {observable} from "mobx";

@observer class OrderLine {
@observable price:number = 0;
@observable amount:number = 1;

constructor(price) {
this.price = price;
}

@computed get total() {
return this.price * this.amount;
}

@action.bound setPrice(price) {
this.price = price;
}

@computed
get total() {
return this.price * this.amount;
}

@action.bound
setPrice(price) {
this.price = price;
}

@computed @computed @computed @computed @computed @computed @computed get total() {
return this.price * this.amount;
}

@action handleDecrease = (event: React.ChangeEvent<HTMLInputElement>) => this.count--;

@action handleSomething = (event: React.ChangeEvent<HTMLInputElement>) => doSomething();
}
98 changes: 85 additions & 13 deletions tests/format/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -937,34 +937,34 @@ export * as ns from "mod";
================================================================================
`;
exports[`flow.js [acorn] format 1`] = `
"Unexpected token (3:17)
exports[`flow.js [__babel_estree] format 1`] = `
"Unexpected token, expected "," (3:17)
1 | // https://babeljs.io/docs/en/babel-preset-flow
2 |
> 3 | function foo(one: any, two: number, three?): string {}
| ^
4 |"
`;
exports[`flow.js [espree] format 1`] = `
"Unexpected token : (3:17)
exports[`flow.js [acorn] format 1`] = `
"Unexpected token (3:17)
1 | // https://babeljs.io/docs/en/babel-preset-flow
2 |
> 3 | function foo(one: any, two: number, three?): string {}
| ^
4 |"
`;
exports[`flow.js [meriyah] format 1`] = `
"Expected ')' (3:17)
exports[`flow.js [babel] format 1`] = `
"Unexpected token, expected "," (3:17)
1 | // https://babeljs.io/docs/en/babel-preset-flow
2 |
> 3 | function foo(one: any, two: number, three?): string {}
| ^
4 |"
`;
exports[`flow.js format 1`] = `
exports[`flow.js [babel-flow] format 1`] = `
====================================options=====================================
parsers: ["babel", "babel-ts", "babel-flow"]
printWidth: 80
Expand All @@ -982,6 +982,42 @@ function foo(one: any, two: number, three?): string {}
================================================================================
`;
exports[`flow.js [babel-ts] format 1`] = `
====================================options=====================================
parsers: ["babel", "babel-ts", "babel-flow"]
printWidth: 80
| printWidth
=====================================input======================================
// https://babeljs.io/docs/en/babel-preset-flow
function foo(one: any, two: number, three?): string {}
=====================================output=====================================
// https://babeljs.io/docs/en/babel-preset-flow
function foo(one: any, two: number, three?): string {}
================================================================================
`;
exports[`flow.js [espree] format 1`] = `
"Unexpected token : (3:17)
1 | // https://babeljs.io/docs/en/babel-preset-flow
2 |
> 3 | function foo(one: any, two: number, three?): string {}
| ^
4 |"
`;
exports[`flow.js [meriyah] format 1`] = `
"Expected ')' (3:17)
1 | // https://babeljs.io/docs/en/babel-preset-flow
2 |
> 3 | function foo(one: any, two: number, three?): string {}
| ^
4 |"
`;
exports[`function-bind.js [acorn] format 1`] = `
"Unexpected token (3:5)
1 | // https://babeljs.io/docs/en/babel-plugin-proposal-function-bind
Expand Down Expand Up @@ -2381,7 +2417,7 @@ function test(param = throw new Error("required!")) {
================================================================================
`;
exports[`typescript.js [acorn] format 1`] = `
exports[`typescript.js [__babel_estree] format 1`] = `
"Unexpected token (3:8)
1 | // https://babeljs.io/docs/en/babel-preset-typescript
2 |
Expand All @@ -2390,25 +2426,25 @@ exports[`typescript.js [acorn] format 1`] = `
4 |"
`;
exports[`typescript.js [espree] format 1`] = `
"Unexpected token : (3:8)
exports[`typescript.js [acorn] format 1`] = `
"Unexpected token (3:8)
1 | // https://babeljs.io/docs/en/babel-preset-typescript
2 |
> 3 | const x: number = 0;
| ^
4 |"
`;
exports[`typescript.js [meriyah] format 1`] = `
"Missing initializer in const declaration (3:8)
exports[`typescript.js [babel] format 1`] = `
"Unexpected token (3:8)
1 | // https://babeljs.io/docs/en/babel-preset-typescript
2 |
> 3 | const x: number = 0;
| ^
4 |"
`;
exports[`typescript.js format 1`] = `
exports[`typescript.js [babel-flow] format 1`] = `
====================================options=====================================
parsers: ["babel", "babel-ts", "babel-flow"]
printWidth: 80
Expand All @@ -2426,6 +2462,42 @@ const x: number = 0;
================================================================================
`;
exports[`typescript.js [babel-ts] format 1`] = `
====================================options=====================================
parsers: ["babel", "babel-ts", "babel-flow"]
printWidth: 80
| printWidth
=====================================input======================================
// https://babeljs.io/docs/en/babel-preset-typescript
const x: number = 0;
=====================================output=====================================
// https://babeljs.io/docs/en/babel-preset-typescript
const x: number = 0;
================================================================================
`;
exports[`typescript.js [espree] format 1`] = `
"Unexpected token : (3:8)
1 | // https://babeljs.io/docs/en/babel-preset-typescript
2 |
> 3 | const x: number = 0;
| ^
4 |"
`;
exports[`typescript.js [meriyah] format 1`] = `
"Missing initializer in const declaration (3:8)
1 | // https://babeljs.io/docs/en/babel-preset-typescript
2 |
> 3 | const x: number = 0;
| ^
4 |"
`;
exports[`v8intrinsic.js [acorn] format 1`] = `
"Unexpected token (3:1)
1 | // https://github.com/babel/babel/pull/10148
Expand Down

0 comments on commit add8435

Please sign in to comment.