Skip to content
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

Merged
merged 3 commits into from
Dec 5, 2022

Conversation

leops
Copy link
Contributor

@leops leops commented Dec 1, 2022

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.

@leops leops added the A-Linter Area: linter label Dec 1, 2022
@leops leops requested review from xunilrj, ematipico and a team as code owners December 1, 2022 16:58
@netlify
Copy link

netlify bot commented Dec 1, 2022

Deploy Preview for docs-rometools canceled.

Name Link
🔨 Latest commit 3fcd0d2
🔍 Latest deploy log https://app.netlify.com/sites/docs-rometools/deploys/638a222a3dece200091de003

@github-actions
Copy link

github-actions bot commented Dec 1, 2022

Parser conformance results on ubuntu-latest

js/262

Test result main count This PR count Difference
Total 45879 45879 0
Passed 44936 44936 0
Failed 943 943 0
Panics 0 0 0
Coverage 97.94% 97.94% 0.00%

jsx/babel

Test result main count This PR count Difference
Total 39 39 0
Passed 36 36 0
Failed 3 3 0
Panics 0 0 0
Coverage 92.31% 92.31% 0.00%

symbols/microsoft

Test result main count This PR count Difference
Total 5946 5946 0
Passed 1757 1757 0
Failed 4189 4189 0
Panics 0 0 0
Coverage 29.55% 29.55% 0.00%

ts/babel

Test result main count This PR count Difference
Total 588 588 0
Passed 519 519 0
Failed 69 69 0
Panics 0 0 0
Coverage 88.27% 88.27% 0.00%

ts/microsoft

Test result main count This PR count Difference
Total 16257 16257 0
Passed 12397 12397 0
Failed 3860 3860 0
Panics 0 0 0
Coverage 76.26% 76.26% 0.00%

let leading_piece = leading_trivia.first()?;

if !leading_piece.is_newline() {
return None;
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't it a programming error if we reach None? How about adding a debug_assert here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In theory yes, the newline_source token comes immediately after a single-line trivia piece so I would expect it to start with a newline trivia piece. I wasn't sure if that's always guaranteed to be the case though, so just in case I added a fallback where the newline trivia piece is created manually using the \n character

@leops leops merged commit aae38ae into main Dec 5, 2022
@leops leops deleted the feature/organize-import-specifiers branch December 5, 2022 10:59
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-Linter Area: linter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants