Skip to content

Commit 49381fd

Browse files
feat(extensions): add autofix support for consistent file extensions in import paths
1 parent 6e49a58 commit 49381fd

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, a
7676
| [consistent-type-specifier-style](docs/rules/consistent-type-specifier-style.md) | Enforce or ban the use of inline type-only markers for named imports. | | | | 🔧 | | |
7777
| [dynamic-import-chunkname](docs/rules/dynamic-import-chunkname.md) | Enforce a leading comment with the webpackChunkName for dynamic imports. | | | | | 💡 | |
7878
| [exports-last](docs/rules/exports-last.md) | Ensure all exports appear after other statements. | | | | | | |
79-
| [extensions](docs/rules/extensions.md) | Ensure consistent use of file extension within the import path. | | | | | | |
79+
| [extensions](docs/rules/extensions.md) | Ensure consistent use of file extension within the import path. | | | | 🔧 | | |
8080
| [first](docs/rules/first.md) | Ensure all imports appear before other statements. | | | | 🔧 | | |
8181
| [group-exports](docs/rules/group-exports.md) | Prefer named exports to be grouped together in a single export declaration | | | | | | |
8282
| [imports-first](docs/rules/imports-first.md) | Replaced by `import/first`. | | | | 🔧 | ||

docs/rules/extensions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# import/extensions
22

3+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
4+
35
<!-- end auto-generated rule header -->
46

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

src/rules/extensions.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function buildProperties(context) {
4646
defaultConfig: 'never',
4747
pattern: {},
4848
ignorePackages: false,
49+
fix: false,
4950
};
5051

5152
context.options.forEach((obj) => {
@@ -72,6 +73,11 @@ function buildProperties(context) {
7273
result.ignorePackages = obj.ignorePackages;
7374
}
7475

76+
// If fix is provided, transfer it to result
77+
if (obj.fix !== undefined) {
78+
result.fix = obj.fix;
79+
}
80+
7581
if (obj.checkTypeImports !== undefined) {
7682
result.checkTypeImports = obj.checkTypeImports;
7783
}
@@ -97,7 +103,7 @@ module.exports = {
97103
description: 'Ensure consistent use of file extension within the import path.',
98104
url: docsUrl('extensions'),
99105
},
100-
106+
fixable: 'code',
101107
schema: {
102108
anyOf: [
103109
{
@@ -225,13 +231,33 @@ module.exports = {
225231
node: source,
226232
message:
227233
`Missing file extension ${extension ? `"${extension}" ` : ''}for "${importPathWithQueryString}"`,
234+
...props.fix ? {
235+
fix(fixer) {
236+
if (!extension) { return fixer; }
237+
return fixer.replaceText(
238+
source,
239+
JSON.stringify(`${importPathWithQueryString}.${extension}`),
240+
);
241+
},
242+
} : {},
228243
});
229244
}
230245
} else if (extension) {
231246
if (isUseOfExtensionForbidden(extension) && isResolvableWithoutExtension(importPath)) {
232247
context.report({
233248
node: source,
234249
message: `Unexpected use of file extension "${extension}" for "${importPathWithQueryString}"`,
250+
...props.fix
251+
? {
252+
fix(fixer) {
253+
return fixer.replaceText(
254+
source,
255+
JSON.stringify(
256+
importPath.slice(0, -(extension.length + 1)),
257+
),
258+
);
259+
},
260+
} : {},
235261
});
236262
}
237263
}

0 commit comments

Comments
 (0)