Repository navigation
fix: comments in loc.tokens hide parentheses from hasParens - #1438
Merged
eventualbuddha merged 2 commits intoAug 10, 2026
Merged
Conversation
The babel, babel-ts, typescript and flow parsers include comment tokens in
loc.tokens; esprima and acorn do not. FastPath#getPrevToken and #getNextToken
indexed that array directly, so a comment sitting between a node and its
enclosing parenthesis was returned instead of the parenthesis. hasParens then
concluded the node was unparenthesised and the printer emitted a second pair,
breaking the documented print(parse(source)).code === source identity on
completely unmodified input.
const a = (
// c
1
);
printed as `(1)` under babel, typescript, flow and babel-ts, and correctly under
esprima and acorn.
Both lookaheads now skip comment tokens, and return null rather than a comment
if that is all there is. Measured against main across 5,076 generated inputs
spanning six parsers, twenty expressions, six comment forms and eight
enclosing contexts: 2,540 identity violations fixed, zero regressions.
Fixes benjamn#1423
Collaborator
|
Published in v0.23.20. |
This was referenced Aug 14, 2026
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.
Fixes #1423.
The bug
FastPath#getPrevTokenand#getNextTokenindexloc.tokensdirectly. The babel, babel-ts, typescript and flow parsers put comment tokens into that array; esprima and acorn do not. So a comment sitting between a node and its enclosing parenthesis is returned instead of the parenthesis,hasParens()concludes the node is unparenthesised, and the printer emits a second pair.This breaks the identity documented in the README on completely unmodified source:
The issue as reported used a 60-line JSX and ternary example. Neither is required — the four lines above are enough, and the parser split is the whole story.
Token dump for that input under babel, where
init.loc.start.tokenis5:getPrevTokenreturns index4, the comment, rather than index3, the(. Under esprima the comment is absent fromtokensand the same lookahead lands on the parenthesis.Both directions are affected, so trailing comments break too:
The change
Both lookaheads now walk past comment tokens, and return
nullrather than a comment if that is all that remains.hasParens()already treatsnullas "no parentheses", so that path is unchanged — but neither function can now hand a caller something that is not syntax.Comment tokens are identified by their string
type(CommentLine/CommentBlock), which is how all four affected parsers emit them; syntactic tokens carry a token-type object instead.Verification
Against
mainacross 5,076 generated inputs — six parsers, twenty expression forms, six comment placements, eight enclosing contexts:npm run mocha: 776 passing, up from 751.npm run lint: clean.Tests
Added to
test/comments.ts, which already runs each case against acorn, babel, esprima, flow and typescript. Five placements: leading line, trailing line, leading block, trailing block, and both sides.Worth noting what happens with the fix reverted but the tests kept: 15 of the 25 instances fail, and the 10 that pass are exactly the acorn and esprima ones. The suite demonstrates the parser split on its own.
Related
#1129 has been open since 2022 and also touches
lib/fast-path.ts, for #575. It changesneedsParens()rather than the token lookaheads. I applied its hunk to currentmainand confirmed it does not address this issue, so the two should not conflict.