Skip to content

Commit 9204f7e

Browse files
committed
Improve groups doucmentation
After feedback in #140.
1 parent d7b449c commit 9204f7e

File tree

3 files changed

+45
-23
lines changed

3 files changed

+45
-23
lines changed

README.md

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -326,46 +326,48 @@ Workaround to make the next section to appear in the table of contents.
326326

327327
## Custom grouping
328328

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.
330330

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+
type Options = {
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.
332346
- 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.
334348
- Make a separate group for style imports.
335349
- Separate `./` and `../` imports.
336350
- Not use groups at all and only sort alphabetically.
337351

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.
339353
>
340354
> See [issue #31] for some tips on what you can do if you have very complex requirements.
341355
>
342356
> Note: For exports the grouping is manual using comments – see [exports].
343357
344-
`groups` is an array of arrays of strings:
345-
346-
```ts
347-
type Options = {
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.
355359

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.
357361
358-
Imports that don’t match any regex are grouped together last.
362+
Imports that don’t match any regex are put together last.
359363

360364
Side effect imports have `\u0000` _prepended_ to their `from` string (starts with `\u0000`). You can match them with `"^\\u0000"`.
361365

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.
365367

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].
367369

368-
These are the default groups:
370+
This is the default value for the `groups` option:
369371

370372
```js
371373
[
@@ -668,10 +670,23 @@ If you’d like to enforce grouping, though, you could still use `eslint-plugin-
668670

669671
Source: https://dprint.dev/plugins/typescript/config/
670672

673+
### How do I remove all blank lines between imports?
674+
675+
Use [custom grouping], setting the `groups` option to only have a single inner array.
676+
677+
For example, here’s the default value but changed to a single inner array:
678+
679+
```js
680+
[["^\\u0000", "^node:", "^@?\\w", "^", "^\\."]];
681+
```
682+
683+
(By default, each string is in its _own_ array (that’s 5 inner arrays) – causing a blank line between each.)
684+
671685
## License
672686

673687
[MIT](LICENSE)
674688

689+
[`u` flag]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode
675690
[autofix]: #can-i-use-this-without-autofix
676691
[comment-handling]: #comment-and-whitespace-handling
677692
[custom grouping]: #custom-grouping

examples/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Examples
2+
3+
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`.

test/examples.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe("examples", () => {
2727

2828
for (const item of output) {
2929
const name = path.basename(item.filePath);
30-
if (!name.startsWith(".")) {
30+
if (!(name.startsWith(".") || name === "README.md")) {
3131
test(`${name}`, () => {
3232
expect(item).toMatchObject({
3333
messages: [],

0 commit comments

Comments
 (0)