Skip to content

Commit

Permalink
Fix replace export all specifiers range
Browse files Browse the repository at this point in the history
  • Loading branch information
vilicvane committed Nov 14, 2023
1 parent 1305253 commit 3035dbf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
65 changes: 34 additions & 31 deletions packages/eslint-plugin/src/library/rules/@scoped-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export default {
importSpecifiers,
);

if (missingImportIds.length) {
if (missingImportIds.length > 0) {
context.report({
node: context.sourceCode.ast,
messageId: 'missingImports',
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import {} from 'irrelevant';
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import {} from 'irrelevant';
export * from './bar.js';

0 comments on commit 3035dbf

Please sign in to comment.