-
-
Notifications
You must be signed in to change notification settings - Fork 729
refactor(linter/plugins): reduce repeat code initializing tokensWithComments
#16017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(linter/plugins): reduce repeat code initializing tokensWithComments
#16017
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
|
cc @lilnasy |
Merge activity
|
There was a problem hiding this 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 refactors repeated code by extracting the initialization logic for tokensWithComments into a dedicated helper function. The same code pattern was duplicated across 8 different functions (getTokens, getFirstToken, getFirstTokens, getLastTokens, getTokenBefore, getTokensBefore, getTokenAfter, getTokensAfter), which merged tokens and comments arrays and sorted them. Now all call sites use the new initTokensWithComments() helper function.
- Introduced new
initTokensWithComments()helper function with proper JSDoc documentation - Replaced 8 instances of duplicate inline code with calls to the new helper
- Added defensive
debugAssertIsNonNullchecks after callinginitTokensWithComments()at each call site
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…Comments` (#16017) The same code was repeated in many places. Move it into a function. Function is only called when `tokensWithComments` is not already initialized (cold path), so no function call cost in majority of cases. In Rust, the pattern of moving code which is rarely called into a separate function is usually positive for perf. Not sure whether JS engine works the same way, but it might well be that V8 makes inlining decisions in much the same way.
f7c7ff0 to
d185615
Compare

The same code was repeated in many places. Move it into a function.
Function is only called when
tokensWithCommentsis not already initialized (cold path), so no function call cost in majority of cases. In Rust, the pattern of moving code which is rarely called into a separate function is usually positive for perf. Not sure whether JS engine works the same way, but it might well be that V8 makes inlining decisions in much the same way.