Skip to content

Commit

Permalink
Rewrite sorting rule and utilities (#18)
Browse files Browse the repository at this point in the history
Copied a bunch of the sorting utilities from [eslint-plugin-perfectionist](https://perfectionist.dev) (thanks for the great plugin, @azat-io!) which should make things easier to follow, and possibly sort out a few hard-to-nail-down issues.

Fixes #16, #17
  • Loading branch information
stormwarning authored Aug 30, 2024
1 parent 10af33a commit f3e79a7
Show file tree
Hide file tree
Showing 25 changed files with 1,011 additions and 974 deletions.
6 changes: 6 additions & 0 deletions .changeset/quick-kangaroos-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"eslint-plugin-import-sorting": minor
---

Group unassigned imports at the top
Unassigned imports are grouped together, but not sorted in case of potential side-effects.
8 changes: 8 additions & 0 deletions .changeset/slimy-dancers-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'eslint-plugin-import-sorting': major
---

Remove deprecated settings

- `import-sorting/known-framework` should be changed to `import-sorting/framework-patterns`
- `import-sorting/known-first-party` should be changed to `import-sorting/internal-patterns`
6 changes: 6 additions & 0 deletions .changeset/tame-actors-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'eslint-plugin-import-sorting': minor
---

Include Bun runtime modules in `builtin` group
Since some builtin modules require a `bun:` or `node:` protocol prefix while others do not, sorting ignores the protocol prefix. (Including it consistently is recommended)
6 changes: 6 additions & 0 deletions .changeset/tough-geckos-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'eslint-plugin-import-sorting': minor
---

Include additional extensions in `style` group
The `style` group now matches imports of Less, Sass, Stylus, and more.
43 changes: 39 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,53 @@ Enforce a convention in the order of `import` statements, inspired by [isort](ht

1. Node standard modules
2. Framework modules
3. Third-party modules
4. First-party modules
3. External modules
4. Internal modules
5. Explicitly local modules

This plugin includes an additional group for “style” imports where the import source ends in `.css` or other style format. Imports are sorted alphabetically, except for local modules, which are sorted by the number of `.` segements in the path first, then alphabetically.

## Usage

Install the plugin, and ESLint if is not already.

```sh
npm install --save-dev eslint eslint-plugin-import-sorting
```

Include the plugin in the `plugins` key of your ESLint config and enable the rule.

```js
// eslint.config.js

import importSortingPlugin from 'eslint-plugin-import-sorting'

export default [
{
plugins: {
'import-sorting': importSortingPlugin,
},
rules: {
'import-sorting/order': 'warn',
},
},
]
```

<details>
<summary>Legacy config example</summary>

```js
rules: {
'import-sorting/order': 'error',
// .eslintrc.js

module.exports = {
plugins: ['import-sorting'],
rules: {
'import-sorting/order': 'warn',
},
}
```

</details>

See the [order](https://github.com/stormwarning/eslint-plugin-import-sorting/blob/main/docs/rules/order.md) rule docs for more configuration options.
18 changes: 14 additions & 4 deletions docs/rules/order.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,31 @@

Enforce a convention in the order of `import` statements.

The grouping order is as follows:

1. Unassigned imports (only grouped, existing order is preserved)
2. Node/Bun standard modules (protocol is ignored when sorting)
3. Framework modules (see below)
4. External modules
5. Internal modules (see below)
6. Explicitly local modules (paths starting with a dot segment)
7. Style imports

## Settings

The framework and first-party groups can be configured by passing a RegEx
The framework and internal groups can be configured by passing a RegEx
string to the different plugin setting. This allows the rule itself to be
implemented by a shared config, while individual projects can determine what
constitutes a “first-party” module.
constitutes an “internal” module.

For example:

```js
settings: {
// Group official React packages together.
'import-sorting/known-framework': /^react(\/|-dom|-router|$)/.source,
'import-sorting/framework-patterns': /^react(\/|-dom|-router|$)/.source,
// Group aliased imports together.
'import-sorting/known-first-party': /^~/.source,
'import-sorting/internal-patterns': /^~/.source,
},
rules: {
'import-sorting/order': 'error',
Expand Down
Loading

0 comments on commit f3e79a7

Please sign in to comment.