You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+37-22Lines changed: 37 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -326,46 +326,48 @@ Workaround to make the next section to appear in the table of contents.
326
326
327
327
## Custom grouping
328
328
329
-
There is **one** option (see [Not for everyone]) called `groups` that allows you to:
329
+
There is **one** option (see [Not for everyone]) called `groups` that is useful for a bunch of different use cases.
330
330
331
-
- Move `src/Button`, `@company/Button` and similar out of the (third party) “packages” group, into their own group.
331
+
`groups` is an array of arrays of strings:
332
+
333
+
```ts
334
+
typeOptions= {
335
+
groups:Array<Array<string>>;
336
+
};
337
+
```
338
+
339
+
Each string is a regex (with the [`u` flag]). The regexes decide which imports go where. (Remember to escape backslashes – it’s `"\\w"`, not `"\w"`, for example.)
340
+
341
+
The inner arrays are joined with one newline; the outer arrays are joined with two – creating a blank line. That’s why there are two levels of arrays – it lets you choose where to have blank lines.
342
+
343
+
Here are some things you can do:
344
+
345
+
- Move non-standard import paths like `src/Button` and `@company/Button` out of the (third party) “packages” group, into their own group.
332
346
- Move `react` first.
333
-
- Avoid blank lines between imports by using a single group.
347
+
-[Avoid blank lines between imports](#how-do-i-remove-all-blank-lines-between-imports) by using a single inner array.
334
348
- Make a separate group for style imports.
335
349
- Separate `./` and `../` imports.
336
350
- Not use groups at all and only sort alphabetically.
337
351
338
-
> If you’re looking at custom grouping because you want to move `src/Button`, `@company/Button`and similar – also consider using names that do not look like npm packages, such as `@/Button` and `~company/Button`. Then you won’t need to customize the grouping at all, and as a bonus things might be less confusing for other people working on the code base.
352
+
> If you’re looking at custom grouping because you want to move non-standard import paths like `src/Button` (with no leading `./` or `../`) and `@company/Button`– consider instead using names that do not look like npm packages, such as `@/Button` and `~company/Button`. Then you won’t need to customize the grouping at all, and as a bonus things might be less confusing for other people working on the code base.
339
353
>
340
354
> See [issue #31] for some tips on what you can do if you have very complex requirements.
341
355
>
342
356
> Note: For exports the grouping is manual using comments – see [exports].
343
357
344
-
`groups` is an array of arrays of strings:
345
-
346
-
```ts
347
-
typeOptions= {
348
-
groups:Array<Array<string>>;
349
-
};
350
-
```
351
-
352
-
Each string is a regex (with the `u` flag) and defines a group. (Remember to escape backslashes – it’s `"\\w"`, not `"\w"`, for example.)
353
-
354
-
Each `import` is matched against _all_ regexes on the `from` string. The import ends up in the group with **the longest match.** In case of a tie, the **first** matching group wins.
358
+
Each `import` is matched against _all_ regexes on the `from` string. The import ends up at the regex with **the longest match.** In case of a tie, the **first** matching regex wins.
355
359
356
-
> If an import ends up in the wrong group – try making the desired group regex match more of the `from` string, or use negative lookahead (`(?!x)`) to exclude things from other groups.
360
+
> If an import ends up in the wrong place – try making the desired regex match more of the `from` string, or use negative lookahead (`(?!x)`) to exclude things from other groups.
357
361
358
-
Imports that don’t match any regex are grouped together last.
362
+
Imports that don’t match any regex are put together last.
359
363
360
364
Side effect imports have `\u0000`_prepended_ to their `from` string (starts with `\u0000`). You can match them with `"^\\u0000"`.
361
365
362
-
Type imports have `\u0000`_appended_ to their `from` string (ends with `\u0000`). You can match them with `"\\u0000$"` – but you probably need more than that to avoid them also being matched by other groups.
363
-
364
-
The inner arrays are joined with one newline; the outer arrays are joined with two (creating a blank line).
366
+
Type imports have `\u0000`_appended_ to their `from` string (ends with `\u0000`). You can match them with `"\\u0000$"` – but you probably need more than that to avoid them also being matched by other regexes.
365
367
366
-
Every group is sorted internally as mentioned in [Sort order]. Side effect imports are always placed first in the group and keep their internal order. It’s recommended to keep side effect imports in their own group.
368
+
All imports that match the same regex are sorted internally as mentioned in [Sort order].
367
369
368
-
These are the default groups:
370
+
This is the default value for the `groups` option:
369
371
370
372
```js
371
373
[
@@ -668,10 +670,23 @@ If you’d like to enforce grouping, though, you could still use `eslint-plugin-
This folder serves both as examples on how to configure the plugin for different outcomes things, as well as tests.
4
+
5
+
**Where you want to go is to the [.eslintrc.js](./.eslintrc.js) config file.** There you’ll find a bunch of different ways to configure the plugin, with comments.
6
+
7
+
Most other files in this directory are test cases and aren’t super interesting to look at. You can open [test/__snapshots__/examples.test.js.snap](../test/__snapshots__/examples.test.js.snap) to see what they look like after running `eslint --fix`.
0 commit comments