Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flow Enums with unknown members support #12193

Merged
merged 2 commits into from Feb 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/babel-generator/src/generators/flow.ts
Expand Up @@ -195,6 +195,10 @@ function enumBody(context: any, node: any) {
context.print(member, node);
context.newline();
}
if (node.hasUnknownMembers) {
context.token("...");
context.newline();
}
context.dedent();
context.token("}");
}
Expand Down
Expand Up @@ -34,3 +34,9 @@ enum E of symbol {
A,
B,
}

enum E {
A,
B,
...
}
Expand Up @@ -34,3 +34,8 @@ enum E of symbol {
A,
B,
}
enum E {
A,
B,
...
}
24 changes: 18 additions & 6 deletions packages/babel-parser/src/plugins/flow/index.js
Expand Up @@ -3282,10 +3282,13 @@ export default (superClass: Class<Parser>): Class<Parser> =>
enumName: string,
explicitType: EnumExplicitType,
}): {|
booleanMembers: Array<N.Node>,
numberMembers: Array<N.Node>,
stringMembers: Array<N.Node>,
defaultedMembers: Array<N.Node>,
members: {|
booleanMembers: Array<N.Node>,
numberMembers: Array<N.Node>,
stringMembers: Array<N.Node>,
defaultedMembers: Array<N.Node>,
|},
hasUnknownMembers: boolean,
|} {
const seenNames = new Set();
const members = {
Expand All @@ -3294,7 +3297,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
stringMembers: [],
defaultedMembers: [],
};
let hasUnknownMembers = false;
while (!this.match(tt.braceR)) {
if (this.eat(tt.ellipsis)) {
hasUnknownMembers = true;
break;
}
const memberNode = this.startNode();
const { id, init } = this.flowEnumMemberRaw();
const memberName = id.name;
Expand Down Expand Up @@ -3371,7 +3379,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.expect(tt.comma);
}
}
return members;
return { members, hasUnknownMembers };
}

flowEnumStringMembers(
Expand Down Expand Up @@ -3438,7 +3446,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
flowEnumBody(node: N.Node, { enumName, nameLoc }): N.Node {
const explicitType = this.flowEnumParseExplicitType({ enumName });
this.expect(tt.braceL);
const members = this.flowEnumMembers({ enumName, explicitType });
const { members, hasUnknownMembers } = this.flowEnumMembers({
enumName,
explicitType,
});
node.hasUnknownMembers = hasUnknownMembers;

switch (explicitType) {
case "boolean":
Expand Down
Expand Up @@ -48,7 +48,8 @@
"value": false
}
}
]
],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -19,6 +19,7 @@
"type": "EnumBooleanBody",
"start":7,"end":35,"loc":{"start":{"line":1,"column":7},"end":{"line":4,"column":1}},
"explicitType": false,
"hasUnknownMembers": false,
"members": [
{
"type": "EnumBooleanMember",
Expand Down
Expand Up @@ -22,7 +22,8 @@
"type": "EnumBooleanBody",
"start":7,"end":26,"loc":{"start":{"line":1,"column":7},"end":{"line":3,"column":1}},
"explicitType": true,
"members": []
"members": [],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -22,6 +22,7 @@
"type": "EnumBooleanBody",
"start":7,"end":27,"loc":{"start":{"line":1,"column":7},"end":{"line":4,"column":1}},
"explicitType": false,
"hasUnknownMembers": false,
"members": [
{
"type": "EnumBooleanMember",
Expand Down
Expand Up @@ -41,7 +41,8 @@
"name": "A"
}
}
]
],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -19,7 +19,8 @@
"type": "EnumStringBody",
"start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}},
"explicitType": false,
"members": []
"members": [],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -19,7 +19,8 @@
"type": "EnumStringBody",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":2,"column":1}},
"explicitType": false,
"members": []
"members": [],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -25,7 +25,8 @@
"type": "EnumStringBody",
"start":14,"end":16,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":16}},
"explicitType": false,
"members": []
"members": [],
"hasUnknownMembers": false
}
}
},
Expand All @@ -44,7 +45,8 @@
"type": "EnumStringBody",
"start":40,"end":42,"loc":{"start":{"line":3,"column":22},"end":{"line":3,"column":24}},
"explicitType": false,
"members": []
"members": [],
"hasUnknownMembers": false
}
}
}
Expand Down
Expand Up @@ -22,7 +22,8 @@
"type": "EnumStringBody",
"start":7,"end":29,"loc":{"start":{"line":1,"column":7},"end":{"line":5,"column":1}},
"explicitType": false,
"members": []
"members": [],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -22,7 +22,8 @@
"type": "EnumStringBody",
"start":7,"end":31,"loc":{"start":{"line":1,"column":7},"end":{"line":4,"column":1}},
"explicitType": false,
"members": []
"members": [],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -22,7 +22,8 @@
"type": "EnumStringBody",
"start":7,"end":17,"loc":{"start":{"line":1,"column":7},"end":{"line":2,"column":1}},
"explicitType": false,
"members": []
"members": [],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -22,7 +22,8 @@
"type": "EnumStringBody",
"start":7,"end":32,"loc":{"start":{"line":1,"column":7},"end":{"line":3,"column":1}},
"explicitType": true,
"members": []
"members": [],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -21,7 +21,8 @@
"body": {
"type": "EnumSymbolBody",
"start":7,"end":29,"loc":{"start":{"line":1,"column":7},"end":{"line":3,"column":1}},
"members": []
"members": [],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -22,7 +22,8 @@
"type": "EnumBooleanBody",
"start":7,"end":30,"loc":{"start":{"line":1,"column":7},"end":{"line":3,"column":1}},
"explicitType": true,
"members": []
"members": [],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -22,7 +22,8 @@
"type": "EnumStringBody",
"start":7,"end":29,"loc":{"start":{"line":1,"column":7},"end":{"line":3,"column":1}},
"explicitType": true,
"members": []
"members": [],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -22,7 +22,8 @@
"type": "EnumBooleanBody",
"start":7,"end":33,"loc":{"start":{"line":1,"column":7},"end":{"line":3,"column":1}},
"explicitType": true,
"members": []
"members": [],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -22,7 +22,8 @@
"type": "EnumNumberBody",
"start":7,"end":32,"loc":{"start":{"line":1,"column":7},"end":{"line":3,"column":1}},
"explicitType": true,
"members": []
"members": [],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -42,7 +42,8 @@
"name": "bar"
}
}
]
],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -29,7 +29,8 @@
"name": "A"
}
}
]
],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -56,7 +56,8 @@
"value": 2
}
}
]
],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -19,6 +19,7 @@
"type": "EnumNumberBody",
"start":7,"end":28,"loc":{"start":{"line":1,"column":7},"end":{"line":4,"column":1}},
"explicitType": false,
"hasUnknownMembers": false,
"members": [
{
"type": "EnumNumberMember",
Expand Down
Expand Up @@ -22,7 +22,8 @@
"type": "EnumNumberBody",
"start":7,"end":25,"loc":{"start":{"line":1,"column":7},"end":{"line":3,"column":1}},
"explicitType": true,
"members": []
"members": [],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -22,6 +22,7 @@
"type": "EnumNumberBody",
"start":7,"end":24,"loc":{"start":{"line":1,"column":7},"end":{"line":4,"column":1}},
"explicitType": false,
"hasUnknownMembers": false,
"members": [
{
"type": "EnumNumberMember",
Expand Down
Expand Up @@ -22,7 +22,8 @@
"type": "EnumStringBody",
"start":11,"end":14,"loc":{"start":{"line":1,"column":11},"end":{"line":2,"column":1}},
"explicitType": false,
"members": []
"members": [],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -38,7 +38,8 @@
"name": "B"
}
}
]
],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -56,7 +56,8 @@
"value": "b"
}
}
]
],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -38,7 +38,8 @@
"name": "B"
}
}
]
],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -56,7 +56,8 @@
"value": "b"
}
}
]
],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -41,7 +41,8 @@
"name": "C"
}
}
]
],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -59,7 +59,8 @@
"value": "c"
}
}
]
],
"hasUnknownMembers": false
}
}
],
Expand Down
Expand Up @@ -37,7 +37,8 @@
"name": "B"
}
}
]
],
"hasUnknownMembers": false
}
}
],
Expand Down
@@ -0,0 +1,3 @@
enum E {
...,
}