diff --git a/src/rules/extensions.ts b/src/rules/extensions.ts index 7f8c5f82..37bee6a5 100644 --- a/src/rules/extensions.ts +++ b/src/rules/extensions.ts @@ -370,10 +370,13 @@ export default createRule({ }) const fixOrSuggest = { fix(fixer: TSESLint.RuleFixer) { - return fixer.replaceText( - source, - replaceImportPath(source.raw, fixedImportPath), + const newImportPath = replaceImportPath( + source.raw, + fixedImportPath, ) + return source.raw === newImportPath + ? null // Surface that something has gone wrong with extension detection. + : fixer.replaceText(source, newImportPath) }, } context.report({ diff --git a/test/rules/extensions.spec.ts b/test/rules/extensions.spec.ts index f7824036..f75fa395 100644 --- a/test/rules/extensions.spec.ts +++ b/test/rules/extensions.spec.ts @@ -250,6 +250,18 @@ ruleTester.run('extensions', rule, { ], output: null, }), + tInvalid({ + name: 'extensions should not autofix when the fix is identical', + code: 'import foo from "./unresolved"', + options: ['always', { fix: true }], + errors: [ + { + messageId: 'missing', + data: { extension: 'js', importPath: './unresolved' }, + }, + ], + output: null, + }), tInvalid({ code: 'import a from "a/index.js"', errors: [