Browse documentation

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 using selects Module. Outside functions and class static blocks, an await using declaration or a for (await using … of …) head makes a detected entry a Module, as a top-level for await does, in a block as at the top level. A C-style for (await using x = …; …; …) head counts too: its LexicalDeclaration is [+Await] grammar as well (§14.7.4 sec-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.meta is available and await is not an identifier. Under --compat-non-strict-mode the detected Module gets its strict reparse, as any detected Module does.
  • await and using split by a line terminator are not await using. §14.3.1 has AwaitUsingDeclarationHead : await [no LineTerminator here] using, and the ForDeclaration the same; a binding after using must be on its line too. Split, await starts an await expression, ADR 0140's ambiguous await, which keeps its Module-first reading and Script retry unchanged: await using followed by x = null on the next line is a Module that awaits using, and await followed by using x = null on 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-level await using ends 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-level await using declaration or for (await using … of …) head is now module syntax there too.
  • using without await is not module syntax. A UsingDeclaration takes ?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-level using stays a Script and keeps that SyntaxError, and one in a block runs as a Script.
  • Explicit goals do not change. --source-type=script, a .cjs entry, the REPL, eval code and ShadowRealm.prototype.evaluate refuse an await using declaration outside async functions, a block's included, in the parser with 'await using' is only valid in async functions and modules, as they refuse the for head. --source-type=module, .mjs and .mts accept both forms.
  • A class static block refuses it in every goal. §15.7.1 makes it a SyntaxError if a ClassStaticBlockStatementList Contains await. The parser refuses an await using declaration there with await is not allowed in a class static block, as it refuses the for await and for (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-host mode without --source-type detects 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 using declaration in a block of Script code or in a class static block is gone; both are ordinary SyntaxErrors from the parser.
  • The source pipeline's unit tests, tests/language/source-type-detection/await-using-entry.js and for-await-using-entry.js, tests/built-ins/ShadowRealm/prototype/evaluate.js and scripts/test-cli.ts check the detection, the refusals and each detected entry point.