Skip to content

fix: add test for failing regression and fix program.comments not sorted by range#198

Merged
NullVoxPopuli merged 2 commits intoember-tooling:mainfrom
johanrd:test/program-comments-sort-order
Apr 24, 2026
Merged

fix: add test for failing regression and fix program.comments not sorted by range#198
NullVoxPopuli merged 2 commits intoember-tooling:mainfrom
johanrd:test/program-comments-sort-order

Conversation

@johanrd
Copy link
Copy Markdown
Contributor

@johanrd johanrd commented Apr 24, 2026

Follow-up to #196.

Observed

With #196 applied, program.comments comes out unsorted whenever a JS /* */ comment sits between two templates:

const X = <template>
  {{! glimmer comment at 22 }}
</template>;
/* js comment at 56 */
const Y = 1;
// ast.comments: [[65,87] js, [23,51] glimmer]  ← unsorted

ESLint's SourceCode builds tokensAndComments = sortedMerge(ast.tokens, ast.comments) and the token-store's createIndexMap(tokens, comments) — both assume each input array is sorted by range[0] (espree, @typescript-eslint/parser, @babel/eslint-parser all honor that invariant). With unsorted comments, the index map points at the wrong token indices for positions whose nearest comment was iterated out of order, which surfaces as wrong answers from sourceCode.getTokenBefore(commentNode) / getTokenAfter(commentNode):

getTokenBefore(glimmer@[23,51]) getTokenAfter(glimmer@[23,51])
#196 as-is ; @ [63,64] (wrong — after the comment) const @ [88,93] (skips </template>)
with this PR <template> @ [10,20] </template> @ [52,63]

Commits

  1. test:tests/program-comments-sort-order.test.js, two tests that pin the invariant:
    • Structural: ast.comments.map(c => c.range[0]) equals its sorted copy.
    • End-to-end via Linter: for the {{! }} comment, getTokenBefore / getTokenAfter return tokens that bracket the comment (before-end ≤ comment-start, after-start ≥ comment-end).
  2. fix: — sort the concatenated array after appending Glimmer comments in gjs-gts-parser.js (programNode.comments = […TS, …glimmer].sort((a, b) => a.range[0] - b.range[0])).

Verification

  • Both tests fail on the test: commit alone, pass after fix:.
  • npm run lint and prettier --check clean.

Review comment companion

I also ran eslint-plugin-ember#2733's five-test directive suite against #196 + this fix — all 5 pass.

@johanrd johanrd mentioned this pull request Apr 24, 2026
4 tasks
@johanrd johanrd force-pushed the test/program-comments-sort-order branch from ce0502a to b46414f Compare April 24, 2026 07:24
johanrd added 2 commits April 24, 2026 15:14
ESLint's SourceCode builds both `tokensAndComments = sortedMerge(tokens,
comments)` and the token-store's `createIndexMap(tokens, comments)`
assuming each input array is sorted by `range[0]`. Every standard JS
parser (espree, @babel/eslint-parser, @typescript-eslint/parser) honors
that invariant.

When a .gts file has a JS /* */ comment interleaved between templates,
TS-parser comments are spread into program.comments first and Glimmer
template comments get appended — producing an array whose order doesn't
match range order:

  const X = <template>{{! glimmer at 22 }}</template>;
  /* js at 56 */
  const Y = 1;

  ast.comments: [[65,87] js, [23,51] glimmer]  ← unsorted

The downstream effect is that `sourceCode.getTokenBefore(glimmer)` /
`getTokenAfter(glimmer)` return wrong tokens:

  before: ";"     @ [63,64]   (wrong — that's *after* the comment)
  after:  "const" @ [88,93]   (skips </template>)

Adds two tests:

  1. ast.comments is sorted by range[0] — direct structural assertion.
  2. getTokenBefore / getTokenAfter on a Glimmer comment return
     source-adjacent tokens — end-to-end through Linter.

Both currently fail; they'll pass once program.comments is sorted.
ESLint's SourceCode.tokensAndComments merge and createIndexMap both
assume comments are sorted by range[0]; appending Glimmer comments after
the TS-parser comments breaks that invariant when a JS block comment
sits between templates, producing wrong results from
getTokenBefore/getTokenAfter on template comments.
@johanrd johanrd force-pushed the test/program-comments-sort-order branch from b46414f to 20410ac Compare April 24, 2026 13:19
@johanrd johanrd changed the title test: failing regression — program.comments not sorted by range fix: add test for failing regression and fix program.comments not sorted by range Apr 24, 2026
@NullVoxPopuli NullVoxPopuli merged commit 29bae56 into ember-tooling:main Apr 24, 2026
36 checks passed
@github-actions github-actions Bot mentioned this pull request Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants