117616: Replace all the usages of the import

This commit is contained in:
Alexandre Vryghem
2024-08-29 11:12:23 +02:00
parent f10447b8d3
commit 39fec7ce64

View File

@@ -4,6 +4,7 @@ import {
TSESLint, TSESLint,
TSESTree, TSESTree,
} from '@typescript-eslint/utils'; } from '@typescript-eslint/utils';
import { Scope } from '@typescript-eslint/utils/ts-eslint';
import { import {
DSpaceESLintRuleInfo, DSpaceESLintRuleInfo,
@@ -125,16 +126,22 @@ function handleUnaliasedImport(context: TSESLint.RuleContext<Message, unknown[]>
if (hasAliasedImport) { if (hasAliasedImport) {
context.report({ context.report({
messageId: Message.MULTIPLE_ALIASES, messageId: Message.MULTIPLE_ALIASES,
data: { local: option.local },
node: node, node: node,
fix(fixer: TSESLint.RuleFixer) { fix(fixer: TSESLint.RuleFixer) {
const fixes: TSESLint.RuleFix[] = [];
const commaAfter = context.sourceCode.getTokenAfter(node, { const commaAfter = context.sourceCode.getTokenAfter(node, {
filter: (token: TSESTree.Token) => token.value === ',', filter: (token: TSESTree.Token) => token.value === ',',
}); });
if (commaAfter) { if (commaAfter) {
return fixer.removeRange([node.range[0], commaAfter.range[1]]); fixes.push(fixer.removeRange([node.range[0], commaAfter.range[1]]));
} else { } else {
return fixer.remove(node); fixes.push(fixer.remove(node));
} }
fixes.push(...retrieveUsageReplacementFixes(context, fixer, node, option.local));
return fixes;
}, },
}); });
} else if (node.local.name === node.imported.name) { } else if (node.local.name === node.imported.name) {
@@ -142,7 +149,12 @@ function handleUnaliasedImport(context: TSESLint.RuleContext<Message, unknown[]>
messageId: Message.NO_ALIAS, messageId: Message.NO_ALIAS,
node: node, node: node,
fix(fixer: TSESLint.RuleFixer) { fix(fixer: TSESLint.RuleFixer) {
return fixer.replaceText(node.local, `${option.imported} as ${option.local}`); const fixes: TSESLint.RuleFix[] = [];
fixes.push(fixer.replaceText(node.local, `${option.imported} as ${option.local}`));
fixes.push(...retrieveUsageReplacementFixes(context, fixer, node, option.local));
return fixes;
}, },
}); });
} else { } else {
@@ -151,8 +163,25 @@ function handleUnaliasedImport(context: TSESLint.RuleContext<Message, unknown[]>
data: { local: option.local }, data: { local: option.local },
node: node, node: node,
fix(fixer: TSESLint.RuleFixer) { fix(fixer: TSESLint.RuleFixer) {
return fixer.replaceText(node.local, option.local); const fixes: TSESLint.RuleFix[] = [];
fixes.push(fixer.replaceText(node.local, option.local));
fixes.push(...retrieveUsageReplacementFixes(context, fixer, node, option.local));
return fixes;
}, },
}); });
} }
} }
/**
* Generates the {@link TSESLint.RuleFix}s for all the usages of the incorrect import.
*
* @param context The current {@link TSESLint.RuleContext}
* @param fixer The instance {@link TSESLint.RuleFixer}
* @param node The node which needs to be replaced
* @param newAlias The new import name
*/
function retrieveUsageReplacementFixes(context: TSESLint.RuleContext<Message, unknown[]>, fixer: TSESLint.RuleFixer, node: TSESTree.ImportSpecifier, newAlias: string): TSESLint.RuleFix[] {
return context.sourceCode.getDeclaredVariables(node)[0].references.map((reference: Scope.Reference) => fixer.replaceText(reference.identifier, newAlias));
}