Skip to content

Commit

Permalink
Fix await binding error within static block (babel#13088)
Browse files Browse the repository at this point in the history
* fix: allow await within SCOPE_FUNCTION under static block

* perf: scan scopeStack for once

* add new test case

* chore: update allowlist
  • Loading branch information
JLHwung authored and nicolo-ribaudo committed Jul 30, 2021
1 parent b4757bf commit bb3722c
Show file tree
Hide file tree
Showing 15 changed files with 1,091 additions and 400 deletions.
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/expression.js
Expand Up @@ -2385,7 +2385,7 @@ export default class ExpressionParser extends LValParser {
if (this.prodParam.hasAwait) {
this.raise(startLoc, Errors.AwaitBindingIdentifier);
return;
} else if (this.scope.inStaticBlock && !this.scope.inNonArrowFunction) {
} else if (this.scope.inStaticBlock) {
this.raise(startLoc, Errors.AwaitBindingIdentifierInStaticBlock);
return;
} else {
Expand Down
11 changes: 10 additions & 1 deletion packages/babel-parser/src/util/scope.js
Expand Up @@ -65,7 +65,16 @@ export default class ScopeHandler<IScope: Scope = Scope> {
return (flags & SCOPE_CLASS) > 0 && (flags & SCOPE_FUNCTION) === 0;
}
get inStaticBlock() {
return (this.currentThisScopeFlags() & SCOPE_STATIC_BLOCK) > 0;
for (let i = this.scopeStack.length - 1; ; i--) {
const { flags } = this.scopeStack[i];
if (flags & SCOPE_STATIC_BLOCK) {
return true;
}
if (flags & (SCOPE_VAR | SCOPE_CLASS)) {
// function body, module body, class property initializers
return false;
}
}
}
get inNonArrowFunction() {
return (this.currentThisScopeFlags() & SCOPE_FUNCTION) > 0;
Expand Down
@@ -0,0 +1,3 @@
var C;
// await in body is allowed
C = class { static { () => await } };
@@ -0,0 +1,100 @@
{
"type": "File",
"start":0,"end":72,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":37}},
"program": {
"type": "Program",
"start":0,"end":72,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":37}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}},
"trailingComments": [
{
"type": "CommentLine",
"value": " await in body is allowed",
"start":7,"end":34,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":27}}
}
],
"declarations": [
{
"type": "VariableDeclarator",
"start":4,"end":5,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":5}},
"id": {
"type": "Identifier",
"start":4,"end":5,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":5},"identifierName":"C"},
"name": "C"
},
"init": null
}
],
"kind": "var"
},
{
"type": "ExpressionStatement",
"start":35,"end":72,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":37}},
"leadingComments": [
{
"type": "CommentLine",
"value": " await in body is allowed",
"start":7,"end":34,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":27}}
}
],
"expression": {
"type": "AssignmentExpression",
"start":35,"end":71,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":36}},
"operator": "=",
"left": {
"type": "Identifier",
"start":35,"end":36,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":1},"identifierName":"C"},
"name": "C"
},
"right": {
"type": "ClassExpression",
"start":39,"end":71,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":36}},
"id": null,
"superClass": null,
"body": {
"type": "ClassBody",
"start":45,"end":71,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":36}},
"body": [
{
"type": "StaticBlock",
"start":47,"end":69,"loc":{"start":{"line":3,"column":12},"end":{"line":3,"column":34}},
"body": [
{
"type": "ExpressionStatement",
"start":56,"end":67,"loc":{"start":{"line":3,"column":21},"end":{"line":3,"column":32}},
"expression": {
"type": "ArrowFunctionExpression",
"start":56,"end":67,"loc":{"start":{"line":3,"column":21},"end":{"line":3,"column":32}},
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "Identifier",
"start":62,"end":67,"loc":{"start":{"line":3,"column":27},"end":{"line":3,"column":32},"identifierName":"await"},
"name": "await"
}
}
}
]
}
]
}
}
}
}
],
"directives": []
},
"comments": [
{
"type": "CommentLine",
"value": " await in body is allowed",
"start":7,"end":34,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":27}}
}
]
}
@@ -0,0 +1,7 @@
var C;
// await is not allowed in async arrow
C = class { static { async (await) => {} } };

C = class { static { async (x = await) => {} } };

C = class { static { async ({ [await]: x }) => {} } };
@@ -0,0 +1,251 @@
{
"type": "File",
"start":0,"end":198,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":54}},
"errors": [
"SyntaxError: Can not use 'await' as identifier inside a static block. (3:28)",
"SyntaxError: Can not use 'await' as identifier inside a static block. (5:32)",
"SyntaxError: Can not use 'await' as identifier inside a static block. (7:31)"
],
"program": {
"type": "Program",
"start":0,"end":198,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":54}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}},
"trailingComments": [
{
"type": "CommentLine",
"value": " await is not allowed in async arrow",
"start":7,"end":45,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":38}}
}
],
"declarations": [
{
"type": "VariableDeclarator",
"start":4,"end":5,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":5}},
"id": {
"type": "Identifier",
"start":4,"end":5,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":5},"identifierName":"C"},
"name": "C"
},
"init": null
}
],
"kind": "var"
},
{
"type": "ExpressionStatement",
"start":46,"end":91,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":45}},
"leadingComments": [
{
"type": "CommentLine",
"value": " await is not allowed in async arrow",
"start":7,"end":45,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":38}}
}
],
"expression": {
"type": "AssignmentExpression",
"start":46,"end":90,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":44}},
"operator": "=",
"left": {
"type": "Identifier",
"start":46,"end":47,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":1},"identifierName":"C"},
"name": "C"
},
"right": {
"type": "ClassExpression",
"start":50,"end":90,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":44}},
"id": null,
"superClass": null,
"body": {
"type": "ClassBody",
"start":56,"end":90,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":44}},
"body": [
{
"type": "StaticBlock",
"start":58,"end":88,"loc":{"start":{"line":3,"column":12},"end":{"line":3,"column":42}},
"body": [
{
"type": "ExpressionStatement",
"start":67,"end":86,"loc":{"start":{"line":3,"column":21},"end":{"line":3,"column":40}},
"expression": {
"type": "ArrowFunctionExpression",
"start":67,"end":86,"loc":{"start":{"line":3,"column":21},"end":{"line":3,"column":40}},
"id": null,
"generator": false,
"async": true,
"params": [
{
"type": "Identifier",
"start":74,"end":79,"loc":{"start":{"line":3,"column":28},"end":{"line":3,"column":33},"identifierName":"await"},
"name": "await"
}
],
"body": {
"type": "BlockStatement",
"start":84,"end":86,"loc":{"start":{"line":3,"column":38},"end":{"line":3,"column":40}},
"body": [],
"directives": []
}
}
}
]
}
]
}
}
}
},
{
"type": "ExpressionStatement",
"start":93,"end":142,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":49}},
"expression": {
"type": "AssignmentExpression",
"start":93,"end":141,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":48}},
"operator": "=",
"left": {
"type": "Identifier",
"start":93,"end":94,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":1},"identifierName":"C"},
"name": "C"
},
"right": {
"type": "ClassExpression",
"start":97,"end":141,"loc":{"start":{"line":5,"column":4},"end":{"line":5,"column":48}},
"id": null,
"superClass": null,
"body": {
"type": "ClassBody",
"start":103,"end":141,"loc":{"start":{"line":5,"column":10},"end":{"line":5,"column":48}},
"body": [
{
"type": "StaticBlock",
"start":105,"end":139,"loc":{"start":{"line":5,"column":12},"end":{"line":5,"column":46}},
"body": [
{
"type": "ExpressionStatement",
"start":114,"end":137,"loc":{"start":{"line":5,"column":21},"end":{"line":5,"column":44}},
"expression": {
"type": "ArrowFunctionExpression",
"start":114,"end":137,"loc":{"start":{"line":5,"column":21},"end":{"line":5,"column":44}},
"id": null,
"generator": false,
"async": true,
"params": [
{
"type": "AssignmentPattern",
"start":121,"end":130,"loc":{"start":{"line":5,"column":28},"end":{"line":5,"column":37}},
"left": {
"type": "Identifier",
"start":121,"end":122,"loc":{"start":{"line":5,"column":28},"end":{"line":5,"column":29},"identifierName":"x"},
"name": "x"
},
"right": {
"type": "Identifier",
"start":125,"end":130,"loc":{"start":{"line":5,"column":32},"end":{"line":5,"column":37},"identifierName":"await"},
"name": "await"
}
}
],
"body": {
"type": "BlockStatement",
"start":135,"end":137,"loc":{"start":{"line":5,"column":42},"end":{"line":5,"column":44}},
"body": [],
"directives": []
}
}
}
]
}
]
}
}
}
},
{
"type": "ExpressionStatement",
"start":144,"end":198,"loc":{"start":{"line":7,"column":0},"end":{"line":7,"column":54}},
"expression": {
"type": "AssignmentExpression",
"start":144,"end":197,"loc":{"start":{"line":7,"column":0},"end":{"line":7,"column":53}},
"operator": "=",
"left": {
"type": "Identifier",
"start":144,"end":145,"loc":{"start":{"line":7,"column":0},"end":{"line":7,"column":1},"identifierName":"C"},
"name": "C"
},
"right": {
"type": "ClassExpression",
"start":148,"end":197,"loc":{"start":{"line":7,"column":4},"end":{"line":7,"column":53}},
"id": null,
"superClass": null,
"body": {
"type": "ClassBody",
"start":154,"end":197,"loc":{"start":{"line":7,"column":10},"end":{"line":7,"column":53}},
"body": [
{
"type": "StaticBlock",
"start":156,"end":195,"loc":{"start":{"line":7,"column":12},"end":{"line":7,"column":51}},
"body": [
{
"type": "ExpressionStatement",
"start":165,"end":193,"loc":{"start":{"line":7,"column":21},"end":{"line":7,"column":49}},
"expression": {
"type": "ArrowFunctionExpression",
"start":165,"end":193,"loc":{"start":{"line":7,"column":21},"end":{"line":7,"column":49}},
"id": null,
"generator": false,
"async": true,
"params": [
{
"type": "ObjectPattern",
"start":172,"end":186,"loc":{"start":{"line":7,"column":28},"end":{"line":7,"column":42}},
"properties": [
{
"type": "ObjectProperty",
"start":174,"end":184,"loc":{"start":{"line":7,"column":30},"end":{"line":7,"column":40}},
"method": false,
"computed": true,
"key": {
"type": "Identifier",
"start":175,"end":180,"loc":{"start":{"line":7,"column":31},"end":{"line":7,"column":36},"identifierName":"await"},
"name": "await"
},
"shorthand": false,
"value": {
"type": "Identifier",
"start":183,"end":184,"loc":{"start":{"line":7,"column":39},"end":{"line":7,"column":40},"identifierName":"x"},
"name": "x"
}
}
]
}
],
"body": {
"type": "BlockStatement",
"start":191,"end":193,"loc":{"start":{"line":7,"column":47},"end":{"line":7,"column":49}},
"body": [],
"directives": []
}
}
}
]
}
]
}
}
}
}
],
"directives": []
},
"comments": [
{
"type": "CommentLine",
"value": " await is not allowed in async arrow",
"start":7,"end":45,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":38}}
}
]
}
Expand Up @@ -7,3 +7,5 @@ C = class { static { function f(await) {} } };
C = class { static { function f(x = await) {} } };

C = class { static { function f({ [await]: x }) {} } };
// await in function expression is allowed
C = class { static { (function await() {}) } };

0 comments on commit bb3722c

Please sign in to comment.