π Search Terms
organize
sort
import
imports
organizeimports
eslint
β
Viability Checklist
β Suggestion
The organizeImports code action should accept a customizable sort order.
The eslint import plugin is very popular. It has this rule, which allows you to set the preferred ordering of imports.
The default example config for this rule is ["builtin", "external", "internal", "parent", "sibling", "index", "object", "type"]. However, the default ordering provided by tsserver does not follow this order.
Ideally, developers would be able to specify their import order in their .eslintrc file, and the organizeImports action would obey that order. I think the most reasonable implementation for this would be that the organizeImport command could take an extra argument that specifies a desired import ordering and editors could handle sending that order in with the organize import action LSP request.
π Motivating Example
Whenever I use the organizeImports action, I inevitably end up with linter errors saying 'foo' import should occur before import of 'bar' (eslint import/order).
Imagine starting with this file
import net from 'net';
import chalk from 'chalk';
net.createServer();
console.log(chalk.red('a'));
and your eslint is configured with this setting
"import/order": [
"error",
{ "groups": ["builtin", "external", "parent", "sibling", "index"] }
]
Running the organizeImports action will mutate the file to this
import chalk from 'chalk';
import net from 'net';
net.createServer();
console.log(chalk.red('a'));
And there will now be an eslint error. The import sorter is doing an alphabetical sort without regard for the fact that net is builtin and chalk is external.
Now you need to either fix this manually or use the eslint auto-fix feature to fix the import order (something like npx eslint --no-eslintrc --parser @typescript-eslint/parser --plugin import --fix --rule '"import/order": [ "error", { "groups": ["builtin", "external", "parent", "sibling", "index"] } ]' <filename>).
π» Use Cases
1. What do you want to use this for?
This is used for auto-organizing imports and not having to worry about lint errors resulting from import ordering that should have been fixed by organizeImports in the first place.
2. What shortcomings exist with current approaches?
The current organizeImports does not work properly with eslint, and it is not customizable.
3. What workarounds are you using in the meantime?
For now I just manually quick-fix all the import errors one by one after doing the organizeImports action.
Related Issues
neoclide/coc.nvim#5136
#30430
π Search Terms
organize
sort
import
imports
organizeimports
eslint
β Viability Checklist
β Suggestion
The
organizeImportscode action should accept a customizable sort order.The eslint import plugin is very popular. It has this rule, which allows you to set the preferred ordering of imports.
The default example config for this rule is
["builtin", "external", "internal", "parent", "sibling", "index", "object", "type"]. However, the default ordering provided bytsserverdoes not follow this order.Ideally, developers would be able to specify their import order in their
.eslintrcfile, and theorganizeImportsaction would obey that order. I think the most reasonable implementation for this would be that theorganizeImportcommand could take an extra argument that specifies a desired import ordering and editors could handle sending that order in with the organize import action LSP request.π Motivating Example
Whenever I use the
organizeImportsaction, I inevitably end up with linter errors saying'foo' import should occur before import of 'bar' (eslint import/order).Imagine starting with this file
and your eslint is configured with this setting
Running the
organizeImportsaction will mutate the file to thisAnd there will now be an eslint error. The import sorter is doing an alphabetical sort without regard for the fact that
netis builtin andchalkis external.Now you need to either fix this manually or use the eslint auto-fix feature to fix the import order (something like
npx eslint --no-eslintrc --parser @typescript-eslint/parser --plugin import --fix --rule '"import/order": [ "error", { "groups": ["builtin", "external", "parent", "sibling", "index"] } ]' <filename>).π» Use Cases
1. What do you want to use this for?
This is used for auto-organizing imports and not having to worry about lint errors resulting from import ordering that should have been fixed by
organizeImportsin the first place.2. What shortcomings exist with current approaches?
The current
organizeImportsdoes not work properly with eslint, and it is not customizable.3. What workarounds are you using in the meantime?
For now I just manually quick-fix all the import errors one by one after doing the
organizeImportsaction.Related Issues
neoclide/coc.nvim#5136
#30430