Detection reads a top-level await using as module syntax
Date: 2026-12-01
Area: language, source pipeline
Refines: ADR 0140's list of what makes a detected entry a
Module, which gains this one, and ADR 0227's
rule that a top-level await using does not count for its Module reading; their other decisions stand.
Related: #2492,
#2493,
#2494, #2480
Context
ECMA-262 has no detection: the host names the goal. Explicit Resource Management (the ES2027 draft, main snapshot
7cff4afe1dd8ee44d877de8dd7e6853f71cf1fd1) adds two forms that exist only under [+Await]. An
AwaitUsingDeclaration is a LexicalDeclaration only with [+Await] (§14.3.1 sec-let-and-const-declarations), and
the await using ForDeclaration needs [+Using, +Await] (§14.7.5 sec-for-in-and-for-of-statements). A
ScriptBody is StatementList[~Yield, ~Await, ~Return] (§16.1 sec-scripts) and a ModuleItem is
StatementListItem[~Yield, +Await, ~Return] (§16.2 sec-modules). Outside functions, both forms therefore parse only
as Module code, in a nested block as at the top level, and they await at the top level as a top-level await does.
ADR 0140 makes a detected entry a Module when it has a top-level import or export declaration or a valid top-level
await expression, and #2480 added a top-level for await. A detected entry whose only module syntax was await using
settled on Script, which refused it: await using x = …; with Using declarations are not allowed at the top level of a script, for (await using x of …) with 'for (await using ...)' is only valid in async functions and modules, and
{ await using x = …; } reached the compiler and failed with internal: await compiled outside an async function.
The same entry runs with --source-type=module. The maintainer agreed on 2026-12-01 that such an entry is a Module.
Decision
- A top-level
await usingselects Module. Outside functions and class static blocks, anawait usingdeclaration or afor (await using … of …)head makes a detected entry a Module, as a top-levelfor awaitdoes, in a block as at the top level. A C-stylefor (await using x = …; …; …)head counts too: itsLexicalDeclarationis[+Await]grammar as well (§14.7.4sec-for-statement). Inside a function, async or not, it is that function's and does not count. - It is read as a Module from the first parse. The parser settles the goal at the end of the parse
(
SettleDetectedModuleGoal), so the entry's Module rules apply to the parse that found it:import.metais available andawaitis not an identifier. Under--compat-non-strict-modethe detected Module gets its strict reparse, as any detected Module does. awaitandusingsplit by a line terminator are notawait using. §14.3.1 hasAwaitUsingDeclarationHead : await [no LineTerminator here] using, and theForDeclarationthe same; a binding afterusingmust be on its line too. Split,awaitstarts an await expression, ADR 0140's ambiguous await, which keeps its Module-first reading and Script retry unchanged:await usingfollowed byx = nullon the next line is a Module that awaitsusing, andawaitfollowed byusing x = nullon the next line parses in neither goal.- The ambiguous await Script retry cannot take it. Like an import or export declaration and a top-level
for await, a top-levelawait usingends that retry, so the Module reading's error stands. - ADR 0227's Module reading counts it. Under
--compat-annex-b, a Script reading that fails after an HTML-like comment is read as a Module, which counts when it has module syntax; a top-levelawait usingdeclaration orfor (await using … of …)head is now module syntax there too. usingwithoutawaitis not module syntax. AUsingDeclarationtakes?Await(§14.3.1), so it parses in Script code; only §16.1.1's early error refuses it at a Script's top level. A detected entry with a top-levelusingstays a Script and keeps thatSyntaxError, and one in a block runs as a Script.- Explicit goals do not change.
--source-type=script, a.cjsentry, the REPL, eval code andShadowRealm.prototype.evaluaterefuse anawait usingdeclaration outside async functions, a block's included, in the parser with'await using' is only valid in async functions and modules, as they refuse theforhead.--source-type=module,.mjsand.mtsaccept both forms. - A class static block refuses it in every goal. §15.7.1 makes it a SyntaxError if a
ClassStaticBlockStatementListContainsawait. The parser refuses anawait usingdeclaration there withawait is not allowed in a class static block, as it refuses thefor awaitandfor (await using … of …)heads.
Consequences
- Every entry point that detects (ADR 0140's table: the runner's file, directory, stdin and sandbox entries, the test
runner, the benchmark runner, the bundler and
GocciaScriptLoaderBare) settles its goal in the source pipeline, so each one follows. The test262 runner's cases name their goal, so their results do not change; its--eval-hostmode without--source-typedetects through the source pipeline and follows too. - Detection's maximum parse counts (ADR 0140) gain no new case: the goal settles in the first parse.
- The internal compiler error for an
await usingdeclaration in a block of Script code or in a class static block is gone; both are ordinarySyntaxErrors from the parser. - The source pipeline's unit tests,
tests/language/source-type-detection/await-using-entry.jsandfor-await-using-entry.js,tests/built-ins/ShadowRealm/prototype/evaluate.jsandscripts/test-cli.tscheck the detection, the refusals and each detected entry point.