Skip to content

Conversation

@gabritto
Copy link
Member

We shouldn't use file.GetOrCreateToken() for tokens not already present in the file. That function should be use to materialize tokens from the file that are not in the AST.

Noticed this while working on #2308.

Copilot AI review requested due to automatic review settings December 10, 2025 17:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes the misuse of SourceFile.GetOrCreateToken() in the change tracker. The GetOrCreateToken() method is designed to materialize tokens that already exist in the source file (typically discovered through scanning), not for creating new synthetic tokens during code transformations. The PR replaces these incorrect usages with the proper approach of using NodeFactory.NewToken() and manually setting the token's location and parent fields.

Key changes:

  • Replaced three instances of GetOrCreateToken() with NewToken() for synthetic token creation in the change tracker
  • Added clarifying documentation to GetOrCreateToken() explaining its intended use case
  • The new approach correctly creates synthetic tokens by calling NewToken(), then manually setting Loc and Parent fields

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
internal/ls/change/tracker.go Fixed three locations (InsertModifierBefore, endPosForInsertNodeAfter, InsertNodeInListAfter) to create synthetic tokens using NewToken() instead of misusing GetOrCreateToken()
internal/ast/ast.go Added documentation comment clarifying that GetOrCreateToken() should not be used for creating synthetic tokens

@gabritto gabritto added this pull request to the merge queue Dec 10, 2025
Merged via the queue into main with commit caa2977 Dec 10, 2025
22 checks passed
@gabritto gabritto deleted the gabritto/tracker_token branch December 10, 2025 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants