From 3035dbfe32a9f2478de9a9c8d0365cdfaec89ef0 Mon Sep 17 00:00:00 2001 From: vilicvane Date: Tue, 14 Nov 2023 23:15:00 +0800 Subject: [PATCH] Fix replace export all specifiers range --- .../src/library/rules/@scoped-modules.ts | 65 ++++++++++--------- .../export-namespace/1/namespace.ts | 1 + .../export-namespace/1/namespace.ts.fix | 1 + 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/packages/eslint-plugin/src/library/rules/@scoped-modules.ts b/packages/eslint-plugin/src/library/rules/@scoped-modules.ts index 2f90851..a006a89 100644 --- a/packages/eslint-plugin/src/library/rules/@scoped-modules.ts +++ b/packages/eslint-plugin/src/library/rules/@scoped-modules.ts @@ -211,7 +211,7 @@ export default { importSpecifiers, ); - if (missingImportIds.length) { + if (missingImportIds.length > 0) { context.report({ node: context.sourceCode.ast, messageId: 'missingImports', @@ -251,11 +251,11 @@ export default { } function validateFile(dirName: string, fileNames: string[]): void { - const exportSpecifiers = infos - .filter(info => info.type === 'export-all') - .map(info => info.specifier); + const exportAllInfos = infos.filter(info => info.type === 'export-all'); - const expectedExportSpecifiers = fileNames + const exportAllSpecifiers = exportAllInfos.map(info => info.specifier); + + const expectedExportAllSpecifiers = fileNames .map((fileName): string | undefined => { if (fileName.startsWith('.')) { return undefined; @@ -312,40 +312,43 @@ export default { }) .filter((entryName): entryName is string => !!entryName); - const missingExportSpecifiers = difference( - expectedExportSpecifiers, - exportSpecifiers, + const missingSpecifiers = difference( + expectedExportAllSpecifiers, + exportAllSpecifiers, ); - if (missingExportSpecifiers.length) { + if (missingSpecifiers.length > 0) { context.report({ node: context.sourceCode.ast, messageId: 'missingExports', - fix: buildAddMissingExportsFixer(expectedExportSpecifiers), + fix: buildReplaceExportsFixer(expectedExportAllSpecifiers), }); } - } - function buildAddMissingExportsFixer( - specifiers: string[], - ): TSESLint.ReportFixFunction { - return fixer => { - const sourceCodeEnd = context.sourceCode.getText().length; - - const replacement = specifiers - .map(value => `export * from '${value}';`) - .join('\n'); - - return infos.length > 0 - ? fixer.replaceTextRange( - [ - infos[0].statement.range[0], - infos[infos.length - 1].statement.range[1], - ], - replacement, - ) - : fixer.insertTextAfterRange([0, sourceCodeEnd], `${replacement}\n`); - }; + function buildReplaceExportsFixer( + specifiers: string[], + ): TSESLint.ReportFixFunction { + return fixer => { + const sourceCodeEnd = context.sourceCode.getText().length; + + const replacement = specifiers + .map(value => `export * from '${value}';`) + .join('\n'); + + return exportAllInfos.length > 0 + ? fixer.replaceTextRange( + [ + exportAllInfos[0].statement.range[0], + exportAllInfos[exportAllInfos.length - 1].statement.range[1], + ], + replacement, + ) + : fixer.insertTextAfterRange( + [0, sourceCodeEnd], + `${replacement}\n`, + ); + }; + } } function isStringLiteral(node: TSESTree.Node): node is TSESTree.Literal { diff --git a/packages/eslint-plugin/test-cases/scoped-modules/export-namespace/1/namespace.ts b/packages/eslint-plugin/test-cases/scoped-modules/export-namespace/1/namespace.ts index e69de29..3cc4e19 100644 --- a/packages/eslint-plugin/test-cases/scoped-modules/export-namespace/1/namespace.ts +++ b/packages/eslint-plugin/test-cases/scoped-modules/export-namespace/1/namespace.ts @@ -0,0 +1 @@ +import {} from 'irrelevant'; diff --git a/packages/eslint-plugin/test-cases/scoped-modules/export-namespace/1/namespace.ts.fix b/packages/eslint-plugin/test-cases/scoped-modules/export-namespace/1/namespace.ts.fix index a8e4f7c..b0f1765 100644 --- a/packages/eslint-plugin/test-cases/scoped-modules/export-namespace/1/namespace.ts.fix +++ b/packages/eslint-plugin/test-cases/scoped-modules/export-namespace/1/namespace.ts.fix @@ -1 +1,2 @@ +import {} from 'irrelevant'; export * from './bar.js';