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: docs/rules/extensions.md
+51-4Lines changed: 51 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,13 +10,13 @@ In order to provide a consistent use of file extensions across your code base, t
10
10
11
11
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.
By providing an object you can configure each extension separately.
18
18
19
-
```json
19
+
```jsonc
20
20
"import/extensions": [<severity>, {
21
21
<extension>:"never"|"always"|"ignorePackages"
22
22
}]
@@ -26,7 +26,7 @@ By providing an object you can configure each extension separately.
26
26
27
27
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.
28
28
29
-
```json
29
+
```jsonc
30
30
"import/extensions": [
31
31
<severity>,
32
32
"never"|"always"|"ignorePackages",
@@ -40,7 +40,7 @@ For example, `["error", "never", { "svg": "always" }]` would require that all ex
40
40
41
41
`ignorePackages` can be set as a separate boolean option like this:
42
42
43
-
```json
43
+
```jsonc
44
44
"import/extensions": [
45
45
<severity>,
46
46
"never"|"always"|"ignorePackages",
@@ -58,6 +58,50 @@ Default value of `ignorePackages` is `false`.
58
58
59
59
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`.
60
60
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.
|`action`| ☑️ |`"enforce" \| "ignore"`| What action to take on imports whose specifiers match `pattern`|
104
+
61
105
### Exception
62
106
63
107
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';
190
234
If you are not concerned about a consistent usage of file extension.
191
235
192
236
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.
0 commit comments