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

[import/order] Add ability to sort groups in order of nested groups #2721

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 70 additions & 1 deletion docs/rules/order.md
Expand Up @@ -88,7 +88,9 @@ This rule supports the following options:

How groups are defined, and the order to respect. `groups` must be an array of `string` or [`string`]. The only allowed `string`s are:
`"builtin"`, `"external"`, `"internal"`, `"unknown"`, `"parent"`, `"sibling"`, `"index"`, `"object"`, `"type"`.
The enforced order is the same as the order of each element in a group. Omitted types are implicitly grouped together as the last element. Example:
By default, the order of elements in a group is ignored and can be mingled
together (see `intraGroupOrder`). Omitted types are implicitly grouped together as the last element.
Example:
```ts
[
'builtin', // Built-in types are first
Expand Down Expand Up @@ -323,6 +325,73 @@ import React, { PureComponent } from 'react';
import { compose, apply } from 'xcompose';
```

### `intraGroupOrdering: true|false`:

* default: `false`
* Will be changed to `true` in the next major release.

Enforces or forbids the ordering of imports within a group. This option applies
slightly differently depending on whether the import is a `type` import or a
regular import.

#### Regular imports
When nested groups have been defined in `groups` (ie. `['builtin', ['parent', 'sibling']]`),
regular imports are sorted in the order of the elements in the nested group. So
in the case of `['builtin', ['parent', 'sibling']]`, parents would be sorted
above sibling imports but no newline would separate them.

When `intraGroupOrdering` is set to `true`, the following will be invalid:

```ts
/* eslint import/order: ["error", {"intraGroupOrdering": true, groups: ['builtin', ['parent', 'sibling']]}] */
import { fs } from 'fs';
import { Select } from './Select';
import { Button } from '../Button';
```

While this will be valid:

```ts
/* eslint import/order: ["error", {"intraGroupOrdering": true, groups: ['builtin', ['parent', 'sibling']]}] */
import { fs } from 'fs';
import { Button } from '../Button';
import { Select } from './Select';
```

When alphabetization is enabled, imports with will be sorted in alphabetical
order within each sub group before the sub group is sorted relative to the order
of the nested group.

#### Type imports
When `intraGroupOrdering` is set to `true`, type imports are sorted in the order
of the original groups array but flattened. So in the case of
`['builtin', ['parent', 'sibling']]`, the order would be [`builtin`, `parent`, `sibling`].

When `intraGroupOrdering` is set to `true`, the following will be invalid:

```ts
/* eslint import/order: ["error", {"intraGroupOrdering": true, groups: ['type', 'builtin', ['parent', 'sibling']]}] */
import type { SelectProps } from './Select';
import type { ButtonProps } from '../Button';
import type { Dirent } from 'fs';

import fs from 'fs';
import { Button } from '../Button';
import { Select } from './Select';
```

While this will be valid:
```ts
/* eslint import/order: ["error", {"intraGroupOrdering": true, groups: ['type', 'builtin', ['parent', 'sibling']]}] */
import type { Dirent } from 'fs';
import type { ButtonProps } from '../Button';
import type { SelectProps } from './Select';

import fs from 'fs';
import { Button } from '../Button';
import { Select } from './Select';
```

### `warnOnUnassignedImports: true|false`:

* default: `false`
Expand Down