Skip to content

Commit 14cb948

Browse files
lnhrdtJounQin
andauthored
fix(extensions): always calculate fix option (#393)
Co-authored-by: JounQin <[email protected]>
1 parent 1089d9f commit 14cb948

File tree

4 files changed

+98
-4
lines changed

4 files changed

+98
-4
lines changed

.changeset/angry-lions-learn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-import-x": patch
3+
---
4+
5+
fix(extensions): always calculate `fix` option

docs/rules/extensions.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44

55
<!-- end auto-generated rule header -->
66

7+
> [!NOTE]
8+
>
9+
> This rule is only fixable when the `fix` option is set to `true` for compatibility, otherwise `suggestions` will be provided instead.
10+
>
11+
> Example:
12+
>
13+
> ```json
14+
> "import-x/extensions": ["error", "never", { "fix": true }]
15+
> ```
16+
>
17+
> It will change to be automatically fixable in the next major version.
18+
719
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.
820
921
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.

src/rules/extensions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ function buildProperties(context: RuleContext<MessageId, Options>) {
132132
continue
133133
}
134134

135+
if (obj.fix != null) {
136+
result.fix = Boolean(obj.fix)
137+
}
138+
135139
// If this is not the new structure, transfer all props to result.pattern
136140
if (
137141
(!('pattern' in obj) || obj.pattern == null) &&
@@ -156,10 +160,6 @@ function buildProperties(context: RuleContext<MessageId, Options>) {
156160
result.checkTypeImports = obj.checkTypeImports
157161
}
158162

159-
if (obj.fix != null) {
160-
result.fix = Boolean(obj.fix)
161-
}
162-
163163
if (Array.isArray(obj.pathGroupOverrides)) {
164164
result.pathGroupOverrides = obj.pathGroupOverrides
165165
}

test/rules/extensions.spec.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,83 @@ ruleTester.run('extensions', rule, {
173173
],
174174

175175
invalid: [
176+
tInvalid({
177+
name: 'extensions should provide suggestions by default',
178+
code: 'import a from "./foo.js"',
179+
options: ['never'],
180+
errors: [
181+
{
182+
messageId: 'unexpected',
183+
data: { extension: 'js', importPath: './foo.js' },
184+
line: 1,
185+
column: 15,
186+
suggestions: [
187+
{
188+
messageId: 'removeUnexpected',
189+
data: {
190+
extension: 'js',
191+
importPath: './foo.js',
192+
fixedImportPath: './foo',
193+
},
194+
output: 'import a from "./foo"',
195+
},
196+
],
197+
},
198+
],
199+
}),
200+
tInvalid({
201+
name: 'extensions should autofix when fix is set to true',
202+
code: 'import a from "./foo.js"',
203+
options: ['never', { fix: true }],
204+
errors: [
205+
{
206+
messageId: 'unexpected',
207+
data: { extension: 'js', importPath: './foo.js' },
208+
line: 1,
209+
column: 15,
210+
},
211+
],
212+
output: 'import a from "./foo"',
213+
}),
214+
tInvalid({
215+
name: 'extensions should autofix when fix is set to true and a pattern object is provided',
216+
code: 'import a from "./foo.js"',
217+
options: ['never', { fix: true, pattern: {} }],
218+
errors: [
219+
{
220+
messageId: 'unexpected',
221+
data: { extension: 'js', importPath: './foo.js' },
222+
line: 1,
223+
column: 15,
224+
},
225+
],
226+
output: 'import a from "./foo"',
227+
}),
228+
tInvalid({
229+
name: 'extensions should not autofix when fix is set to false',
230+
code: 'import a from "./foo.js"',
231+
options: ['never', { fix: false }],
232+
errors: [
233+
{
234+
messageId: 'unexpected',
235+
data: { extension: 'js', importPath: './foo.js' },
236+
line: 1,
237+
column: 15,
238+
suggestions: [
239+
{
240+
messageId: 'removeUnexpected',
241+
data: {
242+
extension: 'js',
243+
importPath: './foo.js',
244+
fixedImportPath: './foo',
245+
},
246+
output: 'import a from "./foo"',
247+
},
248+
],
249+
},
250+
],
251+
output: null,
252+
}),
176253
tInvalid({
177254
code: 'import a from "a/index.js"',
178255
errors: [

0 commit comments

Comments
 (0)