Skip to content

Commit

Permalink
Add specifier-order rule (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
stormwarning authored Dec 13, 2024
1 parent cdeccec commit ba66f25
Show file tree
Hide file tree
Showing 26 changed files with 1,249 additions and 389 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-glasses-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-import-sorting': minor
---

Add new `specifier-order` rule for sorting named imports
38 changes: 18 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
## eslint-plugin-import-sorting
# eslint-plugin-import-sorting

Enforce a convention in the order of `import` statements, inspired by [isort](https://timothycrosley.github.io/isort/#how-does-isort-work)’s grouping style:
Enforce a convention in the order of `import` statements, inspired by
[isort](https://timothycrosley.github.io/isort/#how-does-isort-work)’s grouping style:

1. Node standard modules
2. Framework 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.
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.
Install the plugin, and ESLint if it 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.
Include the plugin in the `plugins` key of your ESLint config and enable the
rules.

```js
// eslint.config.js
Expand All @@ -31,26 +36,19 @@ export default [
'import-sorting': importSortingPlugin,
},
rules: {
'import-sorting/order': 'warn',
'import-sorting/order': 'error',
},
},
]
```

<details>
<summary>Legacy config example</summary>
<!-- begin auto-generated rules list -->

```js
// .eslintrc.js

module.exports = {
plugins: ['import-sorting'],
rules: {
'import-sorting/order': 'warn',
},
}
```
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).

</details>
| Name | Description | 🔧 |
| :----------------------------------------------- | :------------------------------------------ | :- |
| [order](docs/rules/order.md) | Consistently order `import` statements. | 🔧 |
| [specifier-order](docs/rules/specifier-order.md) | Consistently order named import specifiers. | 🔧 |

See the [order](https://github.com/stormwarning/eslint-plugin-import-sorting/blob/main/docs/rules/order.md) rule docs for more configuration options.
<!-- end auto-generated rules list -->
26 changes: 15 additions & 11 deletions docs/rules/order.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# import-sorting/order
# Consistently order `import` statements (`import-sorting/order`)

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

Enforce a convention in the order of `import` statements.
<!-- end auto-generated rule header -->

The grouping order is as follows:

Expand All @@ -24,13 +24,17 @@ constitutes an “internal” module.
For example:

```js
settings: {
// Group official React packages together.
'import-sorting/framework-patterns': /^react(\/|-dom|-router|$)/.source,
// Group aliased imports together.
'import-sorting/internal-patterns': /^~/.source,
},
rules: {
'import-sorting/order': 'error',
},
export default [
{
settings: {
// Group official React packages together.
'import-sorting/framework-patterns': /^react(\/|-dom|-router|$)/.source,
// Group aliased imports together.
'import-sorting/internal-patterns': /^~/.source,
},
rules: {
'import-sorting/order': 'error',
},
},
]
```
18 changes: 18 additions & 0 deletions docs/rules/specifier-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Consistently order named import specifiers (`import-sorting/specifier-order`)

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->

Specifiers are sorted naturally, the same as imports within groups. `type`
keywords are ignored during sorting.

```js
export default [
{
rules: {
'import-sorting/specifier-order': 'error',
},
},
]
```
Loading

0 comments on commit ba66f25

Please sign in to comment.