Skip to content

Commit 60b9670

Browse files
authored
[Babel 8] Remove enums option of flow plugin (#16792)
1 parent 63e8c44 commit 60b9670

92 files changed

Lines changed: 1656 additions & 30 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/babel-parser/src/plugins/flow/index.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,6 @@ export default (superClass: typeof Parser) =>
311311
return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
312312
}
313313

314-
shouldParseEnums(): boolean {
315-
return !!this.getPluginOption("flow", "enums");
316-
}
317-
318314
finishToken(type: TokenType, val: any): void {
319315
if (
320316
type !== tt.string &&
@@ -1954,7 +1950,10 @@ export default (superClass: typeof Parser) =>
19541950
this.next();
19551951
return this.flowParseInterface(node);
19561952
}
1957-
} else if (this.shouldParseEnums() && this.isContextual(tt._enum)) {
1953+
} else if (
1954+
(process.env.BABEL_8_BREAKING || this.plugins.get("flow").enums) &&
1955+
this.isContextual(tt._enum)
1956+
) {
19581957
const node = this.startNode();
19591958
this.next();
19601959
return this.flowParseEnumDeclaration(node);
@@ -2007,7 +2006,8 @@ export default (superClass: typeof Parser) =>
20072006
const { type } = this.state;
20082007
if (
20092008
tokenIsFlowInterfaceOrTypeOrOpaque(type) ||
2010-
(this.shouldParseEnums() && type === tt._enum)
2009+
((process.env.BABEL_8_BREAKING || this.plugins.get("flow").enums) &&
2010+
type === tt._enum)
20112011
) {
20122012
return !this.state.containsEsc;
20132013
}
@@ -2018,7 +2018,8 @@ export default (superClass: typeof Parser) =>
20182018
const { type } = this.state;
20192019
if (
20202020
tokenIsFlowInterfaceOrTypeOrOpaque(type) ||
2021-
(this.shouldParseEnums() && type === tt._enum)
2021+
((process.env.BABEL_8_BREAKING || this.plugins.get("flow").enums) &&
2022+
type === tt._enum)
20222023
) {
20232024
return this.state.containsEsc;
20242025
}
@@ -2027,7 +2028,10 @@ export default (superClass: typeof Parser) =>
20272028
}
20282029

20292030
parseExportDefaultExpression() {
2030-
if (this.shouldParseEnums() && this.isContextual(tt._enum)) {
2031+
if (
2032+
(process.env.BABEL_8_BREAKING || this.plugins.get("flow").enums) &&
2033+
this.isContextual(tt._enum)
2034+
) {
20312035
const node = this.startNode();
20322036
this.next();
20332037
return this.flowParseEnumDeclaration(node);
@@ -2276,7 +2280,10 @@ export default (superClass: typeof Parser) =>
22762280
this.next();
22772281
// @ts-expect-error: refine typings
22782282
return this.flowParseInterface(declarationNode);
2279-
} else if (this.shouldParseEnums() && this.isContextual(tt._enum)) {
2283+
} else if (
2284+
(process.env.BABEL_8_BREAKING || this.plugins.get("flow").enums) &&
2285+
this.isContextual(tt._enum)
2286+
) {
22802287
node.exportKind = "value";
22812288
const declarationNode = this.startNode();
22822289
this.next();

packages/babel-parser/src/typings.d.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,14 @@ export interface RecordAndTuplePluginOptions {
7777
syntaxType: "bar" | "hash";
7878
}
7979

80-
export interface FlowPluginOptions {
81-
all?: boolean;
82-
enums?: boolean;
83-
}
80+
export type FlowPluginOptions = BABEL_8_BREAKING extends true
81+
? {
82+
all?: boolean;
83+
enums?: boolean;
84+
}
85+
: {
86+
all?: boolean;
87+
};
8488

8589
export interface TypeScriptPluginOptions {
8690
dts?: boolean;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
enum E of boolean {
2+
A = true,
3+
B = false,
4+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":46}},
4+
"program": {
5+
"type": "Program",
6+
"start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":46}},
7+
"sourceType": "script",
8+
"interpreter": null,
9+
"body": [
10+
{
11+
"type": "EnumDeclaration",
12+
"start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":46}},
13+
"id": {
14+
"type": "Identifier",
15+
"start":5,"end":6,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":6,"index":6},"identifierName":"E"},
16+
"name": "E"
17+
},
18+
"body": {
19+
"type": "EnumBooleanBody",
20+
"start":7,"end":46,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":4,"column":1,"index":46}},
21+
"hasUnknownMembers": false,
22+
"explicitType": true,
23+
"members": [
24+
{
25+
"type": "EnumBooleanMember",
26+
"start":22,"end":30,"loc":{"start":{"line":2,"column":2,"index":22},"end":{"line":2,"column":10,"index":30}},
27+
"id": {
28+
"type": "Identifier",
29+
"start":22,"end":23,"loc":{"start":{"line":2,"column":2,"index":22},"end":{"line":2,"column":3,"index":23},"identifierName":"A"},
30+
"name": "A"
31+
},
32+
"init": {
33+
"type": "BooleanLiteral",
34+
"start":26,"end":30,"loc":{"start":{"line":2,"column":6,"index":26},"end":{"line":2,"column":10,"index":30}},
35+
"value": true
36+
}
37+
},
38+
{
39+
"type": "EnumBooleanMember",
40+
"start":34,"end":43,"loc":{"start":{"line":3,"column":2,"index":34},"end":{"line":3,"column":11,"index":43}},
41+
"id": {
42+
"type": "Identifier",
43+
"start":34,"end":35,"loc":{"start":{"line":3,"column":2,"index":34},"end":{"line":3,"column":3,"index":35},"identifierName":"B"},
44+
"name": "B"
45+
},
46+
"init": {
47+
"type": "BooleanLiteral",
48+
"start":38,"end":43,"loc":{"start":{"line":3,"column":6,"index":38},"end":{"line":3,"column":11,"index":43}},
49+
"value": false
50+
}
51+
}
52+
]
53+
}
54+
}
55+
],
56+
"directives": []
57+
}
58+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
enum E {
2+
A = true,
3+
B = false,
4+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":35,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":35}},
4+
"program": {
5+
"type": "Program",
6+
"start":0,"end":35,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":35}},
7+
"sourceType": "script",
8+
"interpreter": null,
9+
"body": [
10+
{
11+
"type": "EnumDeclaration",
12+
"start":0,"end":35,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":35}},
13+
"id": {
14+
"type": "Identifier",
15+
"start":5,"end":6,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":6,"index":6},"identifierName":"E"},
16+
"name": "E"
17+
},
18+
"body": {
19+
"type": "EnumBooleanBody",
20+
"start":7,"end":35,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":4,"column":1,"index":35}},
21+
"hasUnknownMembers": false,
22+
"explicitType": false,
23+
"members": [
24+
{
25+
"type": "EnumBooleanMember",
26+
"start":11,"end":19,"loc":{"start":{"line":2,"column":2,"index":11},"end":{"line":2,"column":10,"index":19}},
27+
"id": {
28+
"type": "Identifier",
29+
"start":11,"end":12,"loc":{"start":{"line":2,"column":2,"index":11},"end":{"line":2,"column":3,"index":12},"identifierName":"A"},
30+
"name": "A"
31+
},
32+
"init": {
33+
"type": "BooleanLiteral",
34+
"start":15,"end":19,"loc":{"start":{"line":2,"column":6,"index":15},"end":{"line":2,"column":10,"index":19}},
35+
"value": true
36+
}
37+
},
38+
{
39+
"type": "EnumBooleanMember",
40+
"start":23,"end":32,"loc":{"start":{"line":3,"column":2,"index":23},"end":{"line":3,"column":11,"index":32}},
41+
"id": {
42+
"type": "Identifier",
43+
"start":23,"end":24,"loc":{"start":{"line":3,"column":2,"index":23},"end":{"line":3,"column":3,"index":24},"identifierName":"B"},
44+
"name": "B"
45+
},
46+
"init": {
47+
"type": "BooleanLiteral",
48+
"start":27,"end":32,"loc":{"start":{"line":3,"column":6,"index":27},"end":{"line":3,"column":11,"index":32}},
49+
"value": false
50+
}
51+
}
52+
]
53+
}
54+
}
55+
],
56+
"directives": []
57+
}
58+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
enum E of boolean {
2+
A,
3+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":26,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":26}},
4+
"errors": [
5+
"SyntaxError: Boolean enum members need to be initialized. Use either `A = true,` or `A = false,` in enum `E`. (2:2)"
6+
],
7+
"program": {
8+
"type": "Program",
9+
"start":0,"end":26,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":26}},
10+
"sourceType": "script",
11+
"interpreter": null,
12+
"body": [
13+
{
14+
"type": "EnumDeclaration",
15+
"start":0,"end":26,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":26}},
16+
"id": {
17+
"type": "Identifier",
18+
"start":5,"end":6,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":6,"index":6},"identifierName":"E"},
19+
"name": "E"
20+
},
21+
"body": {
22+
"type": "EnumBooleanBody",
23+
"start":7,"end":26,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":3,"column":1,"index":26}},
24+
"hasUnknownMembers": false,
25+
"explicitType": true,
26+
"members": []
27+
}
28+
}
29+
],
30+
"directives": []
31+
}
32+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
enum E {
2+
A,
3+
B = true,
4+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":27,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":27}},
4+
"errors": [
5+
"SyntaxError: Boolean enum members need to be initialized. Use either `A = true,` or `A = false,` in enum `E`. (2:2)"
6+
],
7+
"program": {
8+
"type": "Program",
9+
"start":0,"end":27,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":27}},
10+
"sourceType": "script",
11+
"interpreter": null,
12+
"body": [
13+
{
14+
"type": "EnumDeclaration",
15+
"start":0,"end":27,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":27}},
16+
"id": {
17+
"type": "Identifier",
18+
"start":5,"end":6,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":6,"index":6},"identifierName":"E"},
19+
"name": "E"
20+
},
21+
"body": {
22+
"type": "EnumBooleanBody",
23+
"start":7,"end":27,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":4,"column":1,"index":27}},
24+
"hasUnknownMembers": false,
25+
"explicitType": false,
26+
"members": [
27+
{
28+
"type": "EnumBooleanMember",
29+
"start":16,"end":24,"loc":{"start":{"line":3,"column":2,"index":16},"end":{"line":3,"column":10,"index":24}},
30+
"id": {
31+
"type": "Identifier",
32+
"start":16,"end":17,"loc":{"start":{"line":3,"column":2,"index":16},"end":{"line":3,"column":3,"index":17},"identifierName":"B"},
33+
"name": "B"
34+
},
35+
"init": {
36+
"type": "BooleanLiteral",
37+
"start":20,"end":24,"loc":{"start":{"line":3,"column":6,"index":20},"end":{"line":3,"column":10,"index":24}},
38+
"value": true
39+
}
40+
}
41+
]
42+
}
43+
}
44+
],
45+
"directives": []
46+
}
47+
}

0 commit comments

Comments
 (0)