Allow sorting side-effect imports?
See original GitHub issueThe goal
To align with VSCode’s “Organize Imports” and the old TSLint sort order by using ESLint and this plugins’ options.
rules: {
imports: ["error", { groups: /* something that outputs example below */ }]
}
Example output
import '@scoped/package/a.css';
import { a } from '@scoped/package/b';
import '@scoped/package/c.css';
import 'package/d.css';
import { e } from 'package/e';
import 'package/f.css';
import './relative/path/g';
import { h } from './relative/path/h';
import './relative/path/i';
Essentially the only thing that matters for sorting each line is the import path.
The issue
Side-effect imports are sorted separately and can’t be customised https://github.com/lydell/eslint-plugin-simple-import-sort/blob/main/src/shared.js#L709
// If both items are side effect imports, keep their original order.
itemA.isSideEffectImport && itemB.isSideEffectImport
? itemA.index - itemB.index
: // If one of the items is a side effect import, move it first.
itemA.isSideEffectImport
? -1
: itemB.isSideEffectImport
? 1
: // ... rest of compare function
There is currently no way to configure groups so that they output like the example above.
Proposed solution
To remove the special resolution of isSideEffectImport
and instead rely more on groups
.
Removing lines 709 to 716 makes it possible to achieve the goal above with the following configuration:
"groups": [
// \\0? makes named vs unnamed imports irrelevant
[
"^\\0?[^\\.]", // non-relative paths (packages)
"^\\0?\\." // relative paths
]
]
Default behaviour will stay exactly the same.
Explicit configuration is still possible with:
"groups": [
// side effect imports
["^\\0"],
// ... all the rest
]
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:26 (14 by maintainers)
Top Results From Across the Web
Do not reorder side effect imports · Issue #110 - GitHub
A side effect import is an import that only imports a module for its side effects, without importing any values (i.e. where ImportDeclaration. ......
Read more >Sorting your imports with ESLint - DEV Community
The named imports should be sorted in alphabetical order; It should skip a line before relative imports that are in other folders; It...
Read more >Sorting imports on save in React projects with ESLint.
Figuring out how to automatically sort imports on save was much harder than I'd like to admit. ... Side effect imports ["^\\u0000"]
Read more >Do not disturb the position of side-effect imports when sorting ...
Side-effect imports should have an option to be handled differently to regular imports regarding sorting, since due to their very nature the exact...
Read more >eslint-plugin-simple-import-sort - npm
Make sure to remove or disable other sorting rules, such as sort-imports and import/order. {.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@lydell KING! thank you sir, that final set of regex’s was exactly what i was looking for! I really need to up my regex-fu! thank you sir ❤️
But hey, I just realized: Can’t you already do what you want? Use a regex like
'^\\u0000[^.]'
to match side effect imports from packages, and'^\\u0000\\.'
to match local ones (the regexes might need tweaking)?