This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
feature(rome_js_analyze): implement import sorting for named specifiers #3916
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
This PR extends the
organizeImports
code action to not only sort whole import nodes, but also reorder the named import specifiers within a given node.The implementation is made quite complex by the necessity of moving nodes along with the relevant comments while remaining syntactically correct, which means inserting or removing comma tokens and newline trivias as nodes get moved to different places.
The current solution correctly handles nodes that were formatted using the Rome Formatter, but the output may be formatted incorrectly when the formatting of the input get more complex (this is only formatting though, it should not result in any syntax errors).
I've also fixed a bug in the
SyntaxTrivia::first
method that was causing it to return a trivia piece with an incorrect source offset.Test Plan
I've added an additional snapshot file for the rule testing both case that are expected to work correctly, as well as more complex patterns that are not currently well supported.