question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow sorting side-effect imports?

See original GitHub issue

The 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:26 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
haxxxtoncommented, Aug 13, 2021

@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 ❤️

1reaction
lydellcommented, Aug 12, 2021

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)?

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found