feat(rome_js_analyze): implement the organizeImports code action
Summary
Fixes #3462
This is an initial implementation of import sorting, exposed through the source.organizeImports code action. For now, the grouping of individual imports is not configurable and the rule will only reorder imports within the same "group", blocks of adjacent import nodes separated by empty lines or non-import module items (export or statement nodes). The rule also makes no attempts at merging imports from the same source (these will be preserved as individual imports and retain their original order relative to each other).
Test Plan
I've added a few snapshot tests that should cover everything that's currently supported by this version of the rule
Deploy Preview for docs-rometools ready!
| Name | Link |
|---|---|
| Latest commit | c47907e7e2099b994381be33befd944447123670 |
| Latest deploy log | https://app.netlify.com/sites/docs-rometools/deploys/637ce2ed3cdb210009f97425 |
| Deploy Preview | https://deploy-preview-3818--docs-rometools.netlify.app/playground |
| Preview on mobile | Toggle QR Code...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify site settings.
Update: I've improved the way the rule handles trivia to avoid having to create (or remove) newlines trivia pieces by recycling the trivia from existing tokens instead. I've also used this refactor to implement support for file-level comments by preserving the order of some trivia pieces that are separated from the rest of the group by an empty line, for instance:
// this comment will stay in place
// this comment will be moved with the import below
import b from 'b';
import a from 'a';
@leops IIUC this patch doesn't enable users to apply sorting via CLI. Is there plans to extend the CLI to allow for that?
@toxeus https://rome.tools/blog/2023/03/28/rome12/#import-sorting-via-cli-experimental
Thanks @Conaclos! I've tried check --apply but must have overlooked check --apply-unsafe