tools icon indicating copy to clipboard operation
tools copied to clipboard

feat(rome_js_analyze): implement the organizeImports code action

Open leops opened this issue 3 years ago • 2 comments

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

leops avatar Nov 21 '22 16:11 leops

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...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

netlify[bot] avatar Nov 21 '22 16:11 netlify[bot]

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 avatar Nov 22 '22 15:11 leops

@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 avatar Apr 21 '23 09:04 toxeus

@toxeus https://rome.tools/blog/2023/03/28/rome12/#import-sorting-via-cli-experimental

Conaclos avatar Apr 21 '23 10:04 Conaclos

Thanks @Conaclos! I've tried check --apply but must have overlooked check --apply-unsafe

toxeus avatar Apr 21 '23 12:04 toxeus