fix(parser): support top-level await detection in unambiguous mode#18457
Closed
fix(parser): support top-level await detection in unambiguous mode#18457
Conversation
Add support for detecting ES modules via top-level await in unambiguous parsing mode, conforming with Babel's behavior. In unambiguous mode, when `await <expr>` is encountered at the top level and the expression is unambiguous (not an ambiguous token), the file is detected as an ES module. Ambiguous tokens after `await` that don't definitively indicate ESM: - `+`, `-` (unary vs binary operators) - `(`, `[` (call/member vs grouped expression) - `/` (regex vs division) - `%` (modulo operator) - `;` (semicolon - empty await) - Template literals - `of`, `using` (contextual keywords) - Newlines (ASI boundary) Also fixes Script mode parsing where `await` at top level should always be an identifier (not a keyword), preventing `await / 0 / u` from being incorrectly parsed as `await /0/u` (await + regex). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Merging this PR will not alter performance
Comparing Footnotes
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
await <expr>is encountered at the top level and the expression is unambiguous, the file is detected as an ES moduleawaitat top level should always be an identifierAmbiguous tokens after
await(following Babel'sisAmbiguousPrefixOrIdentifier())These tokens don't definitively indicate ESM:
+,-(unary vs binary operators)(,[(call/member vs grouped expression)/(regex vs division)%(modulo operator);(semicolon - empty await)of,using(contextual keywords)Results
All 4 Babel TLA unambiguous tests now pass:
module- TLA with ESMambiguous-script- TLA with ambiguous tokens (Script detected)ambiguous-modulo- TLA with%operatorambiguous-v8intrinsinc- TLA with V8 intrinsicConformance improvements:
🤖 Generated with Claude Code