Permalink
codeql-action/node_modules/eslint-plugin-import/docs/rules/extensions.md
Newer
100644
174 lines (110 sloc)
4.76 KB
Ignoring revisions in .git-blame-ignore-revs.
1
# import/extensions
2
3
<!-- end auto-generated rule header -->
5
Some file resolve algorithms allow you to omit the file extension within the import source path. For example the `node` resolver (which does not yet support ESM/`import`) can resolve `./foo/bar` to the absolute path `/User/someone/foo/bar.js` because the `.js` extension is resolved automatically by default in CJS. Depending on the resolver you can configure more extensions to get resolved automatically.
6
7
In order to provide a consistent use of file extensions across your code base, this rule can enforce or disallow the use of certain file extensions.
8
9
## Rule Details
10
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.
12
14
"import/extensions": [<severity>, "never" | "always" | "ignorePackages"]
15
```
16
17
By providing an object you can configure each extension separately.
18
20
"import/extensions": [<severity>, {
21
<extension>: "never" | "always" | "ignorePackages"
22
}]
23
```
24
25
For example `{ "js": "always", "json": "never" }` would always enforce the use of the `.js` extension but never allow the use of the `.json` extension.
26
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
30
"import/extensions": [
31
<severity>,
32
"never" | "always" | "ignorePackages",
33
{
34
<extension>: "never" | "always" | "ignorePackages"
35
}
36
]
37
```
38
39
For example, `["error", "never", { "svg": "always" }]` would require that all extensions are omitted, except for "svg".
40
41
`ignorePackages` can be set as a separate boolean option like this:
42
44
"import/extensions": [
45
<severity>,
46
"never" | "always" | "ignorePackages",
47
{
48
ignorePackages: true | false,
49
pattern: {
50
<extension>: "never" | "always" | "ignorePackages"
51
}
52
}
53
]
54
```
55
56
In that case, if you still want to specify extensions, you can do so inside the **pattern** property.
57
Default value of `ignorePackages` is `false`.
58
59
### Exception
60
61
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.
62
63
For example, given the following folder structure:
64
66
├── foo
67
│ ├── bar.js
68
│ ├── bar.json
69
```
70
71
and this import statement:
72
73
```js
74
import bar from './foo/bar.json';
75
```
76
77
then the extension can’t be omitted because it would then resolve to `./foo/bar.js`.
78
79
### Examples
80
81
The following patterns are considered problems when configuration set to "never":
82
83
```js
84
import foo from './foo.js';
85
86
import bar from './bar.json';
87
88
import Component from './Component.jsx';
89
90
import express from 'express/index.js';
91
```
92
93
The following patterns are not considered problems when configuration set to "never":
94
95
```js
96
import foo from './foo';
97
98
import bar from './bar';
99
100
import Component from './Component';
101
102
import express from 'express/index';
103
104
import * as path from 'path';
105
```
106
107
The following patterns are considered problems when configuration set to "always":
108
109
```js
110
import foo from './foo';
111
112
import bar from './bar';
113
114
import Component from './Component';
115
116
import foo from '@/foo';
117
```
118
119
The following patterns are not considered problems when configuration set to "always":
120
121
```js
122
import foo from './foo.js';
123
124
import bar from './bar.json';
125
126
import Component from './Component.jsx';
127
128
import * as path from 'path';
129
130
import foo from '@/foo.js';
131
```
132
133
The following patterns are considered problems when configuration set to "ignorePackages":
134
135
```js
136
import foo from './foo';
137
138
import bar from './bar';
139
140
import Component from './Component';
141
142
```
143
144
The following patterns are not considered problems when configuration set to "ignorePackages":
145
146
```js
147
import foo from './foo.js';
148
149
import bar from './bar.json';
150
151
import Component from './Component.jsx';
152
153
import express from 'express';
154
155
import foo from '@/foo'
156
```
157
158
The following patterns are not considered problems when configuration set to `['error', 'always', {ignorePackages: true} ]`:
159
160
```js
161
import Component from './Component.jsx';
162
163
import baz from 'foo/baz.js';
164
165
import express from 'express';
166
167
import foo from '@/foo';
168
```
169
170
## When Not To Use It
171
172
If you are not concerned about a consistent usage of file extension.
173
174
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.