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: Class Field Initializer should not allow await expression as immediate child #10946

Merged
merged 3 commits into from Dec 31, 2019
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
15 changes: 14 additions & 1 deletion packages/babel-parser/src/util/scope.js
Expand Up @@ -55,8 +55,21 @@ export default class ScopeHandler<IScope: Scope = Scope> {
get inGenerator() {
return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0;
}
// the following loop always exit because SCOPE_PROGRAM is SCOPE_VAR
// $FlowIgnore
get inAsync() {
return (this.currentVarScope().flags & SCOPE_ASYNC) > 0;
for (let i = this.scopeStack.length - 1; ; i--) {
const scope = this.scopeStack[i];
const isVarScope = scope.flags & SCOPE_VAR;
const isClassScope = scope.flags & SCOPE_CLASS;
if (isClassScope && !isVarScope) {
// If it meets a class scope before a var scope, it means it is a class property initializer
// which does not have an [Await] parameter in its grammar
return false;
} else if (isVarScope) {
return (scope.flags & SCOPE_ASYNC) > 0;
}
}
}
get allowSuper() {
return (this.currentThisScope().flags & SCOPE_SUPER) > 0;
Expand Down
@@ -0,0 +1,3 @@
() => class {
async m() { await 42 }
}
@@ -0,0 +1,210 @@
{
"type": "File",
"start": 0,
"end": 40,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 40,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 40,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 0,
"end": 40,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "ClassExpression",
"start": 6,
"end": 40,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 3,
"column": 1
}
},
"id": null,
"superClass": null,
"body": {
"type": "ClassBody",
"start": 12,
"end": 40,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 16,
"end": 38,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 24
}
},
"static": false,
"key": {
"type": "Identifier",
"start": 22,
"end": 23,
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 9
},
"identifierName": "m"
},
"name": "m"
},
"computed": false,
"kind": "method",
"id": null,
"generator": false,
"async": true,
"params": [],
"body": {
"type": "BlockStatement",
"start": 26,
"end": 38,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 24
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 28,
"end": 36,
"loc": {
"start": {
"line": 2,
"column": 14
},
"end": {
"line": 2,
"column": 22
}
},
"expression": {
"type": "AwaitExpression",
"start": 28,
"end": 36,
"loc": {
"start": {
"line": 2,
"column": 14
},
"end": {
"line": 2,
"column": 22
}
},
"argument": {
"type": "NumericLiteral",
"start": 34,
"end": 36,
"loc": {
"start": {
"line": 2,
"column": 20
},
"end": {
"line": 2,
"column": 22
}
},
"extra": {
"rawValue": 42,
"raw": "42"
},
"value": 42
}
}
}
],
"directives": []
}
}
]
}
}
}
}
],
"directives": []
}
}
@@ -0,0 +1,3 @@
async () => class {
[await 42]() { }
}