From 6642f12787ce8e96443d150059a78a4efc9e8b40 Mon Sep 17 00:00:00 2001 From: Adam Simon Date: Sun, 29 Dec 2024 15:09:40 +0100 Subject: [PATCH] Revert "Further refine handling of await in class fields" (except for new tests) This reverts commit 9e365f71f339360df3d3b74b3df698a4483439b2. --- acorn/src/expression.js | 2 +- acorn/src/state.js | 11 ++++++----- acorn/src/statement.js | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/acorn/src/expression.js b/acorn/src/expression.js index 9a38bfd8..c335a709 100644 --- a/acorn/src/expression.js +++ b/acorn/src/expression.js @@ -1043,7 +1043,7 @@ pp.checkUnreserved = function({start, end, name}) { this.raiseRecoverable(start, "Cannot use 'yield' as identifier inside a generator") if (this.inAsync && name === "await") this.raiseRecoverable(start, "Cannot use 'await' as identifier inside an async function") - if (this.currentScope().inClassFieldInit && name === "arguments") + if (this.currentThisScope().inClassFieldInit && name === "arguments") this.raiseRecoverable(start, "Cannot use 'arguments' in class field initializer") if (this.inClassStaticBlock && (name === "arguments" || name === "await")) this.raise(start, `Cannot use ${name} in class static initialization block`) diff --git a/acorn/src/state.js b/acorn/src/state.js index fc5e21e4..946bf2d8 100644 --- a/acorn/src/state.js +++ b/acorn/src/state.js @@ -99,14 +99,15 @@ export class Parser { get inFunction() { return (this.currentVarScope().flags & SCOPE_FUNCTION) > 0 } - get inGenerator() { return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0 && !this.currentScope().inClassFieldInit } + get inGenerator() { return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0 && !this.currentVarScope().inClassFieldInit } - get inAsync() { return (this.currentScope().flags & SCOPE_ASYNC) > 0 && !this.currentScope().inClassFieldInit } + get inAsync() { return (this.currentVarScope().flags & SCOPE_ASYNC) > 0 && !this.currentVarScope().inClassFieldInit } get canAwait() { + if (this.currentThisScope().inClassFieldInit) return false for (let i = this.scopeStack.length - 1; i >= 0; i--) { let scope = this.scopeStack[i] - if (scope.flags & SCOPE_CLASS_STATIC_BLOCK || scope.inClassFieldInit) return false + if (scope.flags & SCOPE_CLASS_STATIC_BLOCK) return false if (scope.flags & SCOPE_FUNCTION) return (scope.flags & SCOPE_ASYNC) > 0 } return (this.inModule && this.options.ecmaVersion >= 13) || this.options.allowAwaitOutsideFunction @@ -122,8 +123,8 @@ export class Parser { get treatFunctionsAsVar() { return this.treatFunctionsAsVarInScope(this.currentScope()) } get allowNewDotTarget() { - const {flags} = this.currentThisScope() - return (flags & (SCOPE_FUNCTION | SCOPE_CLASS_STATIC_BLOCK)) > 0 || this.currentScope().inClassFieldInit + const {flags, inClassFieldInit} = this.currentThisScope() + return (flags & (SCOPE_FUNCTION | SCOPE_CLASS_STATIC_BLOCK)) > 0 || inClassFieldInit } get inClassStaticBlock() { diff --git a/acorn/src/statement.js b/acorn/src/statement.js index a8cae83a..0dcfd04f 100644 --- a/acorn/src/statement.js +++ b/acorn/src/statement.js @@ -742,7 +742,7 @@ pp.parseClassField = function(field) { if (this.eat(tt.eq)) { // To raise SyntaxError if 'arguments' exists in the initializer. - const scope = this.currentScope() + const scope = this.currentThisScope() const inClassFieldInit = scope.inClassFieldInit scope.inClassFieldInit = true field.value = this.parseMaybeAssign()