spec: disable await binding identifier within static block (#12661)

This commit is contained in:
Huáng Jùnliàng
2021-02-01 15:55:43 -05:00
committed by GitHub
parent d0a965b71f
commit ecfe20395b
11 changed files with 1128 additions and 84 deletions

View File

@@ -14,6 +14,8 @@ export const ErrorMessages = Object.freeze({
"Async functions can only be declared at the top level or inside a block",
AwaitBindingIdentifier:
"Can not use 'await' as identifier inside an async function",
AwaitBindingIdentifierInStaticBlock:
"Can not use 'await' as identifier inside a static block",
AwaitExpressionFormalParameter:
"await is not allowed in async function parameters",
AwaitNotInAsyncContext:

View File

@@ -2320,6 +2320,9 @@ export default class ExpressionParser extends LValParser {
if (this.prodParam.hasAwait) {
this.raise(startLoc, Errors.AwaitBindingIdentifier);
return;
} else if (this.scope.inStaticBlock && !this.scope.inNonArrowFunction) {
this.raise(startLoc, Errors.AwaitBindingIdentifierInStaticBlock);
return;
} else {
this.expressionScope.recordAsyncArrowParametersError(
startLoc,

View File

@@ -20,6 +20,7 @@ import {
SCOPE_FUNCTION,
SCOPE_OTHER,
SCOPE_SIMPLE_CATCH,
SCOPE_STATIC_BLOCK,
SCOPE_SUPER,
CLASS_ELEMENT_OTHER,
CLASS_ELEMENT_INSTANCE_GETTER,
@@ -1519,10 +1520,7 @@ export default class StatementParser extends ExpressionParser {
) {
this.expectPlugin("classStaticBlock", member.start);
// Start a new lexical scope
this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
// Start a new expression scope, this is required for parsing edge cases like:
// async (x = class { static { await; } }) => {}
this.expressionScope.enter(newExpressionScope());
this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
// Start a new scope with regard to loop labels
const oldLabels = this.state.labels;
this.state.labels = [];
@@ -1532,7 +1530,6 @@ export default class StatementParser extends ExpressionParser {
const body = (member.body = []);
this.parseBlockOrModuleBlockBody(body, undefined, false, tt.braceR);
this.prodParam.exit();
this.expressionScope.exit();
this.scope.exit();
this.state.labels = oldLabels;
classBody.body.push(this.finishNode<N.StaticBlock>(member, "StaticBlock"));