Skip to content

Commit d5f2950

Browse files
Xunnamiusljharb
authored andcommitted
[Docs] extensions, order: improve documentation
1 parent a20d843 commit d5f2950

File tree

4 files changed

+815
-163
lines changed

4 files changed

+815
-163
lines changed

.markdownlint.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"line-length": false,
3+
"no-duplicate-heading": {
4+
"siblings_only": true
5+
},
36
"ul-indent": {
47
"start_indent": 1,
58
"start_indented": true

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
66

77
## [Unreleased]
88

9+
### Changed
10+
- [Docs] `extensions`, `order`: improve documentation ([#3106], thanks [@Xunnamius])
11+
912
## [2.31.0] - 2024-10-03
1013

1114
### Added
@@ -1152,6 +1155,7 @@ for info on changes for earlier releases.
11521155

11531156
[`memo-parser`]: ./memo-parser/README.md
11541157

1158+
[#3106]: https://github.com/import-js/eslint-plugin-import/pull/3106
11551159
[#3073]: https://github.com/import-js/eslint-plugin-import/pull/3073
11561160
[#3072]: https://github.com/import-js/eslint-plugin-import/pull/3072
11571161
[#3071]: https://github.com/import-js/eslint-plugin-import/pull/3071
@@ -2018,6 +2022,7 @@ for info on changes for earlier releases.
20182022
[@wtgtybhertgeghgtwtg]: https://github.com/wtgtybhertgeghgtwtg
20192023
[@xM8WVqaG]: https://github.com/xM8WVqaG
20202024
[@xpl]: https://github.com/xpl
2025+
[@Xunnamius]: https://github.com/Xunnamius
20212026
[@yesl-kim]: https://github.com/yesl-kim
20222027
[@yndajas]: https://github.com/yndajas
20232028
[@yordis]: https://github.com/yordis

docs/rules/extensions.md

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ In order to provide a consistent use of file extensions across your code base, t
1010

1111
This rule either takes one string option, one object option, or a string and an object option. If it is the string `"never"` (the default value), then the rule forbids the use for any extension. If it is the string `"always"`, then the rule enforces the use of extensions for all import statements. If it is the string `"ignorePackages"`, then the rule enforces the use of extensions for all import statements except package imports.
1212

13-
```json
13+
```jsonc
1414
"import/extensions": [<severity>, "never" | "always" | "ignorePackages"]
1515
```
1616

1717
By providing an object you can configure each extension separately.
1818

19-
```json
19+
```jsonc
2020
"import/extensions": [<severity>, {
2121
<extension>: "never" | "always" | "ignorePackages"
2222
}]
@@ -26,7 +26,7 @@ By providing an object you can configure each extension separately.
2626

2727
By providing both a string and an object, the string will set the default setting for all extensions, and the object can be used to set granular overrides for specific extensions.
2828

29-
```json
29+
```jsonc
3030
"import/extensions": [
3131
<severity>,
3232
"never" | "always" | "ignorePackages",
@@ -40,7 +40,7 @@ For example, `["error", "never", { "svg": "always" }]` would require that all ex
4040

4141
`ignorePackages` can be set as a separate boolean option like this:
4242

43-
```json
43+
```jsonc
4444
"import/extensions": [
4545
<severity>,
4646
"never" | "always" | "ignorePackages",
@@ -58,6 +58,50 @@ Default value of `ignorePackages` is `false`.
5858

5959
By default, `import type` and `export type` style imports/exports are ignored. If you want to check them as well, you can set the `checkTypeImports` option to `true`.
6060

61+
Unfortunately, in more advanced linting setups, such as when employing custom specifier aliases (e.g. you're using `eslint-import-resolver-alias`, `paths` in `tsconfig.json`, etc), this rule can be too coarse-grained when determining which imports to ignore and on which to enforce the config.
62+
This is especially troublesome if you have import specifiers that [look like externals or builtins](./order.md#how-imports-are-grouped).
63+
64+
Set `pathGroupOverrides` to force this rule to always ignore certain imports and never ignore others.
65+
`pathGroupOverrides` accepts an array of one or more [`PathGroupOverride`](#pathgroupoverride) objects.
66+
67+
For example:
68+
69+
```jsonc
70+
"import/extensions": [
71+
<severity>,
72+
"never" | "always" | "ignorePackages",
73+
{
74+
ignorePackages: true | false,
75+
pattern: {
76+
<extension>: "never" | "always" | "ignorePackages"
77+
},
78+
pathGroupOverrides: [
79+
{
80+
pattern: "package-name-to-ignore",
81+
action: "ignore",
82+
},
83+
{
84+
pattern: "bespoke+alias:{*,*/**}",
85+
action: "enforce",
86+
}
87+
]
88+
}
89+
]
90+
```
91+
92+
> \[!NOTE]
93+
>
94+
> `pathGroupOverrides` is inspired by [`pathGroups` in `'import/order'`](./order.md#pathgroups) and shares a similar interface.
95+
> If you're using `pathGroups` already, you may find `pathGroupOverrides` very useful.
96+
97+
### `PathGroupOverride`
98+
99+
| property | required | type | description |
100+
| :--------------: | :------: | :---------------------: | --------------------------------------------------------------- |
101+
| `pattern` | ☑️ | `string` | [Minimatch pattern][16] for specifier matching |
102+
| `patternOptions` | | `object` | [Minimatch options][17]; default: `{nocomment: true}` |
103+
| `action` | ☑️ | `"enforce" \| "ignore"` | What action to take on imports whose specifiers match `pattern` |
104+
61105
### Exception
62106

63107
When disallowing the use of certain extensions this rule makes an exception and allows the use of extension when the file would not be resolvable without extension.
@@ -190,3 +234,6 @@ export type { Foo } from './foo';
190234
If you are not concerned about a consistent usage of file extension.
191235

192236
In the future, when this rule supports native node ESM resolution, and the plugin is configured to use native rather than transpiled ESM (a config option that is not yet available) - setting this to `always` will have no effect.
237+
238+
[16]: https://www.npmjs.com/package/minimatch#features
239+
[17]: https://www.npmjs.com/package/minimatch#options

0 commit comments

Comments
 (0)