From b6a98df77774826cfde315238aa19a30403c428d Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Sat, 14 Dec 2024 08:10:20 +0100 Subject: [PATCH] Make sure await in class field initializers is checked properly FIX: Fix an issue where `await` expressions in class field initializers were inappropriately allowed. Closes https://github.com/acornjs/acorn/issues/1334 --- acorn/src/state.js | 3 ++- test/tests-asyncawait.js | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/acorn/src/state.js b/acorn/src/state.js index c2e411156..946bf2d84 100644 --- a/acorn/src/state.js +++ b/acorn/src/state.js @@ -104,9 +104,10 @@ export class Parser { 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.inClassFieldInit || scope.flags & SCOPE_CLASS_STATIC_BLOCK) 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 diff --git a/test/tests-asyncawait.js b/test/tests-asyncawait.js index ed492e97f..28a212e4e 100644 --- a/test/tests-asyncawait.js +++ b/test/tests-asyncawait.js @@ -3540,3 +3540,5 @@ testFail("async() => await (5) ** 6", "Unexpected token (1:21)", {ecmaVersion: 8 testFail("4 + async() => 2", "Unexpected token (1:12)", {ecmaVersion: 8, loose: false}) 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})