Browse documentation

Detection falls back to a Module reading when an HTML-like comment fails the Script reading

Date: 2026-12-01 Area: language, source pipeline Refines: ADR 0222's consequence that an entry whose Script reading fails because of an HTML-like comment reports that SyntaxError, and ADR 0140's detection fallbacks, which gain this one; their other decisions stand. Related: #2473, #2376

Context

ES2026 B.1.1 (sec-html-like-comments) extends the comments of §12.4 (sec-comments) with <!-- and a line-leading -->, "except that this extension is not allowed when parsing source text using the goal symbol Module". So the two goals read the same text differently: in a Script, under --compat-annex-b (ADR 0222), a <!--b ends the line as a comment; in a Module (§16.2.1.7.1 ParseModule) it is a < !--b.

Detection (ADR 0140) reads an entry with no named goal as Script code first and settles on Module when it meets module syntax. Under the flag that first reading reads HTML-like comments. ADR 0222 recorded what follows: an entry such as

let a = 1, b = 2;
if (a <!--b) {}
export {};

fails in its Script reading with Expected ")" after if condition, though as a Module, the goal its export selects, it parses. Without the flag it runs as that Module. The maintainer decided on 2026-12-01 to fall back to the Module reading there.

Decision

  • A Script reading that fails after an HTML-like comment gets a Module reading. With --compat-annex-b, when detection's Script reading of an entry raises a SyntaxError and the lexer had read an HTML-like comment by then, the source is parsed once more with the Module goal, without HTML-like comments. That is the only difference in how the two goals tokenise the text, so a comment is what such a failure turns on. A failure before any HTML-like comment gets no Module reading.
  • The Module reading counts only as a detected Module. It is used when it parses and has what makes a detected entry a Module without the flag: a top-level import or export declaration or a top-level await expression (ADR 0140), or a top-level for await statement, which the engine runs as a Module (HasTopLevelForAwait in Goccia.Eval). A top-level await using does not count, as a detected Script refuses it, and neither does an await in a class static block, an early error. An entry with none of them, such as if (a <!--b) {} alone, was never going to be detected as a Module, and keeps its Script reading's error, as Script code under the flag must read the comment.
  • A line-leading --> never gets that far. B.1.1 reads --> as a comment only after a line terminator (SingleLineHTMLCloseComment, and a MultiLineComment that holds one) or at the start of the source. There a Module reads -- >, and neither form of -- parses: the postfix one has [no LineTerminator here] before it (§13.4 sec-update-expressions), and the prefix one needs a UnaryExpression, which > does not start. So its Module reading always fails and the Script reading's error stands. Only <!-- changes an entry's outcome.
  • When every reading fails, the first error stands. A Module reading that fails, or has no module syntax, is dropped, and the Script reading's SyntaxError is reported as before. An ambiguous top-level await keeps its Script retry (ADR 0140), which comes first; the Module reading follows only when that retry fails too.
  • Nothing else changes. An entry whose Script reading parses is unchanged, including let b = 1; 2 <!--b; export { b };, where the export is part of the comment and the entry settles on Script (ADR 0222). Without the flag no reading reads HTML-like comments, so the fallback never runs. An explicit goal, --source-type, .mjs, .mts and .cjs, parses once as before.

Consequences

  • An entry whose module syntax sits beside <!-- used as operators runs as a Module under --compat-annex-b, as it does without the flag.
  • Detection's maximum parse counts (ADR 0140) gain no new case. Without ambiguous await the fallback is a second parse; with it, the Script retry is the second and the Module reading the third, the maximum ADR 0140 already has.
  • The test262 runner runs each case with the goal its flags name, never through detection, so its results do not change. Its host-eval mode, which detects the goal of the source it reads from standard input with the test262 profile (--compat-annex-b on), now detects such a source as a Module too.
  • The source pipeline's unit tests, tests/language/html-like-comments/annex-b and scripts/test-cli-lexer.ts check the fallback, an entry that is a Script with HTML-like comments, and an entry invalid either way.

Refined by ADR 0230: a top-level await using declaration or for (await using …) head now makes a detected entry a Module, so it counts for the Module reading too.