Skip to content

Commit

Permalink
fix: scope.inAsync should exclude reference in class property initial…
Browse files Browse the repository at this point in the history
…izers
  • Loading branch information
JLHwung committed Dec 30, 2019
1 parent 0238244 commit 72211fa
Show file tree
Hide file tree
Showing 15 changed files with 1,166 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/babel-parser/src/util/scope.js
Expand Up @@ -56,7 +56,18 @@ export default class ScopeHandler<IScope: Scope = Scope> {
return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0;
}
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 @@
class C {
#p = async () => await 42;
}
@@ -0,0 +1,3 @@
{
"plugins": ["classPrivateProperties"]
}

0 comments on commit 72211fa

Please sign in to comment.