Skip to content

Commit

Permalink
docs: Clarify the description of sort-imports options (#18198)
Browse files Browse the repository at this point in the history
* fix sort-imports doc

* fix sort import's test cases

* remove one useless test case

* fix typo

* applied review

* better to clear description

Co-authored-by: Tanuj Kanti <[email protected]>

* Better description for ignoreCase

* write default option at the top of the option.

* Apply suggestions from code review

Co-authored-by: Tanuj Kanti <[email protected]>

---------

Co-authored-by: Tanuj Kanti <[email protected]>
  • Loading branch information
gwBear1 and Tanujkanti4441 committed Mar 22, 2024
1 parent 09bd7fe commit 239a7e2
Showing 1 changed file with 79 additions and 23 deletions.
102 changes: 79 additions & 23 deletions docs/src/rules/sort-imports.md
Expand Up @@ -192,40 +192,64 @@ import {b, a, c} from 'foo.js'

### `ignoreCase`

When `true` the rule ignores the case-sensitivity of the imports local name.
When `false` (default), uppercase letters of the alphabet must always precede lowercase letters.

Examples of **incorrect** code for this rule with the `{ "ignoreCase": true }` option:
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';
```

:::

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';
```

:::

Default is `false`.
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`

Ignores the sorting of import declaration statements.
When `true`, the rule ignores the sorting of import declaration statements. Default is `false`.

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

Expand All @@ -239,18 +263,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,11 +287,20 @@ 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`

Ignores the member sorting within a `multiple` member import declaration.
When `true`, the rule ignores the member sorting within a `multiple` member import declaration. Default is `false`.

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

Expand All @@ -278,6 +313,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 +335,24 @@ 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`

This option takes an array with four predefined elements, the order of elements specifies the order of import styles.

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

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

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

:::

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

### `allowSeparatedGroups`

When `true` the rule checks the sorting of import declaration statements only for those that appear on consecutive lines.
When `true`, the rule checks the sorting of import declaration statements only for those that appear on consecutive lines. Default is `false`.

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 +462,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.

0 comments on commit 239a7e2

Please sign in to comment.