diff --git a/acorn/src/expression.js b/acorn/src/expression.js index c335a709..9a38bfd8 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.currentThisScope().inClassFieldInit && name === "arguments") + if (this.currentScope().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 946bf2d8..fc5e21e4 100644 --- a/acorn/src/state.js +++ b/acorn/src/state.js @@ -99,15 +99,14 @@ export class Parser { get inFunction() { return (this.currentVarScope().flags & SCOPE_FUNCTION) > 0 } - get inGenerator() { return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0 && !this.currentVarScope().inClassFieldInit } + get inGenerator() { return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0 && !this.currentScope().inClassFieldInit } - get inAsync() { return (this.currentVarScope().flags & SCOPE_ASYNC) > 0 && !this.currentVarScope().inClassFieldInit } + get inAsync() { return (this.currentScope().flags & SCOPE_ASYNC) > 0 && !this.currentScope().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) return false + if (scope.flags & SCOPE_CLASS_STATIC_BLOCK || 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 @@ -123,8 +122,8 @@ export class Parser { get treatFunctionsAsVar() { return this.treatFunctionsAsVarInScope(this.currentScope()) } get allowNewDotTarget() { - const {flags, inClassFieldInit} = this.currentThisScope() - return (flags & (SCOPE_FUNCTION | SCOPE_CLASS_STATIC_BLOCK)) > 0 || inClassFieldInit + const {flags} = this.currentThisScope() + return (flags & (SCOPE_FUNCTION | SCOPE_CLASS_STATIC_BLOCK)) > 0 || this.currentScope().inClassFieldInit } get inClassStaticBlock() { diff --git a/acorn/src/statement.js b/acorn/src/statement.js index 0dcfd04f..a8cae83a 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.currentThisScope() + const scope = this.currentScope() const inClassFieldInit = scope.inClassFieldInit scope.inClassFieldInit = true field.value = this.parseMaybeAssign() diff --git a/test/tests-asyncawait.js b/test/tests-asyncawait.js index 28a212e4..dab7a5d0 100644 --- a/test/tests-asyncawait.js +++ b/test/tests-asyncawait.js @@ -3541,4 +3541,11 @@ testFail("4 + async() => 2", "Unexpected token (1:12)", {ecmaVersion: 8, loose: testFail("async function𝐬 f() {}", "Unexpected token (1:17)", {ecmaVersion: 8}) -testFail("async () => class { x = await 1 }", "Cannot use 'await' as identifier inside an async function (1:24)", {ecmaVersion: 2024}) +testFail("async () => class { x = await 1 }", "Unexpected token (1:30)", {ecmaVersion: 2024}) + +test("async () => class { x = async () => await 1 }", {}, {ecmaVersion: 2024}) + +test("async () => class { x = await }", {}, {ecmaVersion: 2024}) + +testFail("async () => class { x = await }", "Cannot use keyword 'await' outside an async function (1:24)", + {ecmaVersion: 2024, sourceType: "module"})