Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Clarify the description of sort-imports options #18198

Merged
merged 9 commits into from
Mar 22, 2024
102 changes: 82 additions & 20 deletions docs/src/rules/sort-imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,39 +192,67 @@ import {b, a, c} from 'foo.js'

### `ignoreCase`

When `true` the rule ignores the case-sensitivity of the imports local name.
Default is `false`.
gwBear1 marked this conversation as resolved.
Show resolved Hide resolved

Examples of **incorrect** code for this rule with the `{ "ignoreCase": true }` option:
When `false` (default), uppercase letters of the alphabet must always precede lowercase letters.

When `true`, the rule ignores the case-sensitivity of the imports local name.

Examples of **incorrect** code for this rule with the default `{ "ignoreCase": false }` option:

::: incorrect

```js
/*eslint sort-imports: ["error", { "ignoreCase": true }]*/

import B from 'foo.js';
/*eslint sort-imports: ["error", { "ignoreCase": false }]*/
import a from 'bar.js';
import B from 'foo.js';
import c from 'baz.js';
```

:::

Examples of **correct** code for this rule with the `{ "ignoreCase": true }` option:
Examples of **correct** code for this rule with the default `{ "ignoreCase": false }` option:

::: correct

```js
/*eslint sort-imports: ["error", { "ignoreCase": true }]*/

import a from 'foo.js';
/*eslint sort-imports: ["error", { "ignoreCase": false }]*/
import B from 'bar.js';
import a from 'foo.js';
import c from 'baz.js';
```

:::

Default is `false`.
Examples of **correct** code for this rule with `{ "ignoreCase": true }` option:

::: correct

```js
/*eslint sort-imports: ["error", { "ignoreCase": true }]*/
import a from 'bar.js';
import B from 'foo.js';
import c from 'baz.js';
```

:::

Examples of **incorrect** code for this rule with the `{ "ignoreCase": true }` option:

::: incorrect

```js
/*eslint sort-imports: ["error", { "ignoreCase": true }]*/
import B from 'foo.js';
import a from 'bar.js';
```

:::

### `ignoreDeclarationSort`

Default is `false`.

Ignores the sorting of import declaration statements.
gwBear1 marked this conversation as resolved.
Show resolved Hide resolved

Examples of **incorrect** code for this rule with the default `{ "ignoreDeclarationSort": false }` option:
Expand All @@ -239,18 +267,20 @@ import a from 'bar.js'

:::

Examples of **correct** code for this rule with the `{ "ignoreDeclarationSort": true }` option:
Examples of **correct** code for this rule with the default `{ "ignoreDeclarationSort": false }` option:

::: correct

```js
/*eslint sort-imports: ["error", { "ignoreDeclarationSort": true }]*/
import a from 'foo.js'
import b from 'bar.js'
/*eslint sort-imports: ["error", { "ignoreDeclarationSort": false }]*/
import a from 'bar.js';
import b from 'foo.js';
```

:::

Examples of **correct** code for this rule with the `{ "ignoreDeclarationSort": true }` option:

::: correct

```js
Expand All @@ -261,10 +291,21 @@ import a from 'bar.js'

:::

Default is `false`.
Examples of **incorrect** code for this rule with the `{ "ignoreDeclarationSort": true }` option:

::: incorrect

```js
/*eslint sort-imports: ["error", { "ignoreDeclarationSort": true }]*/
import {b, a, c} from 'foo.js';
```

:::

### `ignoreMemberSort`

Default is `false`.

Ignores the member sorting within a `multiple` member import declaration.
gwBear1 marked this conversation as resolved.
Show resolved Hide resolved

Examples of **incorrect** code for this rule with the default `{ "ignoreMemberSort": false }` option:
Expand All @@ -278,6 +319,17 @@ import {b, a, c} from 'foo.js'

:::

Examples of **correct** code for this rule with the default `{ "ignoreMemberSort": false }` option:

::: correct

```js
/*eslint sort-imports: ["error", { "ignoreMemberSort": false }]*/
import {a, b, c} from 'foo.js';
```

:::

Examples of **correct** code for this rule with the `{ "ignoreMemberSort": true }` option:

::: correct
Expand All @@ -289,10 +341,22 @@ import {b, a, c} from 'foo.js'

:::

Default is `false`.
Examples of **incorrect** code for this rule with the `{ "ignoreMemberSort": true }` option:

::: incorrect

```js
/*eslint sort-imports: ["error", { "ignoreMemberSort": true }]*/
import b from 'foo.js';
import a from 'bar.js';
```

:::

### `memberSyntaxSortOrder`

Default is `["none", "all", "multiple", "single"]`.
gwBear1 marked this conversation as resolved.
Show resolved Hide resolved

There are four different styles and the default member syntax sort order is:

* `none` - import module without exported bindings.
Expand Down Expand Up @@ -341,10 +405,10 @@ import {a, b} from 'foo.js';

:::

Default is `["none", "all", "multiple", "single"]`.

### `allowSeparatedGroups`

Default is `false`.

When `true` the rule checks the sorting of import declaration statements only for those that appear on consecutive lines.
gwBear1 marked this conversation as resolved.
Show resolved Hide resolved

In other words, a blank line or a comment line or line with any other statement after an import declaration statement will reset the sorting of import declaration statements.
Expand Down Expand Up @@ -404,8 +468,6 @@ import a from 'baz.js';

:::

Default is `false`.

## When Not To Use It

This rule is a formatting preference and not following it won't negatively affect the quality of your code. If alphabetizing imports isn't a part of your coding standards, then you can leave this rule disabled.