From 46d2f69ec61c7dc2fde2f029212aa6e4d69c4757 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Wed, 23 Jun 2021 17:12:02 +0200 Subject: [PATCH] Only forbid await inside class field init values So that it can be used in computed properties in classes. Makes the test262 suite run again. Issue #1048 --- acorn/src/scopeflags.js | 3 +-- acorn/src/state.js | 11 ++++------- acorn/src/statement.js | 7 +------ 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/acorn/src/scopeflags.js b/acorn/src/scopeflags.js index 81c8c92d3..d8b5b8d27 100644 --- a/acorn/src/scopeflags.js +++ b/acorn/src/scopeflags.js @@ -8,8 +8,7 @@ export const SCOPE_ARROW = 16, SCOPE_SIMPLE_CATCH = 32, SCOPE_SUPER = 64, - SCOPE_DIRECT_SUPER = 128, - SCOPE_CLASS = 256 + SCOPE_DIRECT_SUPER = 128 export function functionFlags(async, generator) { return SCOPE_FUNCTION | (async ? SCOPE_ASYNC : 0) | (generator ? SCOPE_GENERATOR : 0) diff --git a/acorn/src/state.js b/acorn/src/state.js index c742460cc..563847672 100644 --- a/acorn/src/state.js +++ b/acorn/src/state.js @@ -3,10 +3,7 @@ import {types as tt} from "./tokentype.js" import {lineBreak} from "./whitespace.js" import {getOptions} from "./options.js" import {wordsRegexp} from "./util.js" -import { - SCOPE_TOP, SCOPE_FUNCTION, SCOPE_ASYNC, SCOPE_CLASS, SCOPE_GENERATOR, - SCOPE_SUPER, SCOPE_DIRECT_SUPER -} from "./scopeflags.js" +import {SCOPE_TOP, SCOPE_FUNCTION, SCOPE_ASYNC, SCOPE_GENERATOR, SCOPE_SUPER, SCOPE_DIRECT_SUPER} from "./scopeflags.js" export class Parser { constructor(options, input, startPos) { @@ -105,9 +102,9 @@ export class Parser { get inAsync() { return (this.currentVarScope().flags & SCOPE_ASYNC) > 0 && !this.currentVarScope().inClassFieldInit } get canAwait() { for (let i = this.scopeStack.length - 1; i >= 0; i--) { - let {flags} = this.scopeStack[i] - if (flags & SCOPE_FUNCTION) return (flags & SCOPE_ASYNC) > 0 - if (flags & SCOPE_CLASS) return false + let scope = this.scopeStack[i] + if (scope.inClassFieldInit) return false + if (scope.flags & SCOPE_FUNCTION) return (scope.flags & SCOPE_ASYNC) > 0 } return (this.inModule && this.options.ecmaVersion >= 13) || this.options.allowAwaitOutsideFunction } diff --git a/acorn/src/statement.js b/acorn/src/statement.js index 5063d149a..a9eb51e1a 100644 --- a/acorn/src/statement.js +++ b/acorn/src/statement.js @@ -4,10 +4,7 @@ import {lineBreak, skipWhiteSpace} from "./whitespace.js" import {isIdentifierStart, isIdentifierChar, keywordRelationalOperator} from "./identifier.js" import {has} from "./util.js" import {DestructuringErrors} from "./parseutil.js" -import { - functionFlags, SCOPE_SIMPLE_CATCH, SCOPE_CLASS, BIND_SIMPLE_CATCH, - BIND_LEXICAL, BIND_VAR, BIND_FUNCTION -} from "./scopeflags.js" +import {functionFlags, SCOPE_SIMPLE_CATCH, BIND_SIMPLE_CATCH, BIND_LEXICAL, BIND_VAR, BIND_FUNCTION} from "./scopeflags.js" const pp = Parser.prototype @@ -584,7 +581,6 @@ pp.parseClass = function(node, isStatement) { let hadConstructor = false classBody.body = [] this.expect(tt.braceL) - this.enterScope(SCOPE_CLASS) while (this.type !== tt.braceR) { const element = this.parseClassElement(node.superClass !== null) if (element) { @@ -601,7 +597,6 @@ pp.parseClass = function(node, isStatement) { this.next() node.body = this.finishNode(classBody, "ClassBody") this.exitClassBody() - this.exitScope() return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression") }