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

fix: disallow expression after binding identifier of #11355

Merged
merged 1 commit into from Apr 21, 2020
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
14 changes: 14 additions & 0 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -3456,4 +3456,18 @@ export default (superClass: Class<Parser>): Class<Parser> =>
});
return this.finishNode(node, "EnumDeclaration");
}

updateContext(prevType: TokenType): void {
if (
this.match(tt.name) &&
this.state.value === "of" &&
prevType === tt.name &&
this.input.slice(this.state.lastTokStart, this.state.lastTokEnd) ===
"interface"
) {
this.state.exprAllowed = false;
} else {
super.updateContext(prevType);
}
}
};
5 changes: 4 additions & 1 deletion packages/babel-parser/src/tokenizer/context.js
Expand Up @@ -59,7 +59,10 @@ tt.name.updateContext = function(prevType) {
let allowed = false;
if (prevType !== tt.dot) {
if (
(this.state.value === "of" && !this.state.exprAllowed) ||
(this.state.value === "of" &&
!this.state.exprAllowed &&
prevType !== tt._function &&
prevType !== tt._class) ||
(this.state.value === "yield" && this.prodParam.hasYield)
) {
allowed = true;
Expand Down
@@ -0,0 +1,2 @@
class of<T> {}

@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"jsx",
"flow"
]
}
@@ -0,0 +1,40 @@
{
"type": "File",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"program": {
"type": "Program",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"id": {
"type": "Identifier",
"start":6,"end":8,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":8},"identifierName":"of"},
"name": "of"
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start":8,"end":11,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":11}},
"params": [
{
"type": "TypeParameter",
"start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10}},
"name": "T",
"variance": null
}
]
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":12,"end":14,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":14}},
"body": []
}
}
],
"directives": []
}
}
@@ -0,0 +1,2 @@
function of<T>() {}

@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"jsx",
"flow"
]
}
@@ -0,0 +1,43 @@
{
"type": "File",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
"program": {
"type": "Program",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
"id": {
"type": "Identifier",
"start":9,"end":11,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":11},"identifierName":"of"},
"name": "of"
},
"generator": false,
"async": false,
"typeParameters": {
"type": "TypeParameterDeclaration",
"start":11,"end":14,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":14}},
"params": [
{
"type": "TypeParameter",
"start":12,"end":13,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":13}},
"name": "T",
"variance": null
}
]
},
"params": [],
"body": {
"type": "BlockStatement",
"start":17,"end":19,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":19}},
"body": [],
"directives": []
}
}
],
"directives": []
}
}
@@ -0,0 +1,2 @@
interface of<T> {}

@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"jsx",
"flow"
]
}
@@ -0,0 +1,46 @@
{
"type": "File",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"program": {
"type": "Program",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "InterfaceDeclaration",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"id": {
"type": "Identifier",
"start":10,"end":12,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":12},"identifierName":"of"},
"name": "of"
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start":12,"end":15,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":15}},
"params": [
{
"type": "TypeParameter",
"start":13,"end":14,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":14}},
"name": "T",
"variance": null
}
]
},
"extends": [],
"implements": [],
"mixins": [],
"body": {
"type": "ObjectTypeAnnotation",
"start":16,"end":18,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":18}},
"callProperties": [],
"properties": [],
"indexers": [],
"internalSlots": [],
"exact": false
}
}
],
"directives": []
}
}