Skip to content

Commit

Permalink
fix: Class Field Initializer should not allow await expression as imm…
Browse files Browse the repository at this point in the history
…ediate child (#10946)

* fix: scope.inAsync should exclude reference in class property initializers

* chore: add test on await in computed class property

* fix flow error :(
  • Loading branch information
JLHwung committed Dec 31, 2019
1 parent 0238244 commit 26eb891
Show file tree
Hide file tree
Showing 17 changed files with 1,348 additions and 1 deletion.
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]() { }
}

0 comments on commit 26eb891

Please sign in to comment.