|
| 1 | +import { Rule } from "eslint"; |
| 2 | +import { TSESTree } from "@typescript-eslint/utils"; |
| 3 | +import { Identifier, ImportDeclaration, ImportSpecifier } from "estree-jsx"; |
| 4 | +import { |
| 5 | + checkMatchingImportSpecifier, |
| 6 | + getFromPackage, |
| 7 | + pfPackageMatches, |
| 8 | +} from "."; |
| 9 | + |
| 10 | +interface Renames { |
| 11 | + [currentName: string]: string; |
| 12 | +} |
| 13 | + |
| 14 | +function formatDefaultMessage(oldName: string, newName: string) { |
| 15 | + return `${oldName} has been renamed to ${newName}.`; |
| 16 | +} |
| 17 | + |
| 18 | +export function renameInterface( |
| 19 | + interfaceRenames: Renames, |
| 20 | + componentRenames: Renames, |
| 21 | + packageName = "@patternfly/react-core" |
| 22 | +) { |
| 23 | + return function (context: Rule.RuleContext) { |
| 24 | + const oldNames = Object.keys(interfaceRenames); |
| 25 | + const { imports } = getFromPackage(context, packageName, oldNames); |
| 26 | + |
| 27 | + if (imports.length === 0) { |
| 28 | + return {}; |
| 29 | + } |
| 30 | + |
| 31 | + const shouldRenameIdentifier = (identifier: Identifier) => { |
| 32 | + const matchingImport = imports.find( |
| 33 | + (specifier) => specifier.local.name === identifier.name |
| 34 | + ); |
| 35 | + |
| 36 | + if (!matchingImport) { |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + return matchingImport.local.name === matchingImport.imported.name; |
| 41 | + }; |
| 42 | + |
| 43 | + const replaceIdentifier = (identifier: Identifier) => { |
| 44 | + const oldName = identifier.name; |
| 45 | + const newName = interfaceRenames[oldName]; |
| 46 | + |
| 47 | + context.report({ |
| 48 | + node: identifier, |
| 49 | + message: formatDefaultMessage(oldName, newName), |
| 50 | + fix(fixer) { |
| 51 | + return fixer.replaceText(identifier, newName); |
| 52 | + }, |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + return { |
| 57 | + ImportDeclaration(node: ImportDeclaration) { |
| 58 | + if (!pfPackageMatches(packageName, node.source.value)) { |
| 59 | + return; |
| 60 | + } |
| 61 | + for (const oldName of Object.keys(componentRenames)) { |
| 62 | + const newName = componentRenames[oldName]; |
| 63 | + const importSource = node.source.raw; |
| 64 | + const importSourceHasComponentName = importSource?.includes(oldName); |
| 65 | + const newImportDeclaration = importSource?.replace(oldName, newName); |
| 66 | + |
| 67 | + if (newImportDeclaration && importSourceHasComponentName) { |
| 68 | + context.report({ |
| 69 | + node, |
| 70 | + message: formatDefaultMessage(oldName, newName), |
| 71 | + fix: (fixer) => |
| 72 | + fixer.replaceText(node.source, newImportDeclaration), |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
| 76 | + }, |
| 77 | + ImportSpecifier(node: ImportSpecifier) { |
| 78 | + if (!checkMatchingImportSpecifier(node, imports)) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + const oldName = node.imported.name; |
| 83 | + const newName = interfaceRenames[oldName]; |
| 84 | + |
| 85 | + context.report({ |
| 86 | + node, |
| 87 | + message: formatDefaultMessage(oldName, newName), |
| 88 | + fix(fixer) { |
| 89 | + return fixer.replaceText(node.imported, newName); |
| 90 | + }, |
| 91 | + }); |
| 92 | + }, |
| 93 | + TSTypeReference(node: TSESTree.TSTypeReference) { |
| 94 | + if (node.typeName.type === "Identifier") { |
| 95 | + shouldRenameIdentifier(node.typeName) && |
| 96 | + replaceIdentifier(node.typeName); |
| 97 | + } |
| 98 | + }, |
| 99 | + TSInterfaceHeritage(node: TSESTree.TSInterfaceHeritage) { |
| 100 | + if (node.expression.type === "Identifier") { |
| 101 | + shouldRenameIdentifier(node.expression) && |
| 102 | + replaceIdentifier(node.expression); |
| 103 | + } |
| 104 | + }, |
| 105 | + }; |
| 106 | + }; |
| 107 | +} |
0 commit comments