diff --git a/packages/eslint-plugin-pf-codemods/src/rules/helpers/getFromPackage.ts b/packages/eslint-plugin-pf-codemods/src/rules/helpers/getFromPackage.ts index 8ff133149..c310fa2ac 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/helpers/getFromPackage.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/helpers/getFromPackage.ts @@ -81,7 +81,7 @@ export function getFromPackage( specifierNames.includes(specifier.imported.name) ); const specifiedExports = exports.filter((specifier) => - specifierNames.includes(specifier.exported.name) + specifierNames.includes(specifier.local.name) ); return { imports: specifiedImports, exports: specifiedExports }; diff --git a/packages/eslint-plugin-pf-codemods/src/rules/helpers/renameInterface.ts b/packages/eslint-plugin-pf-codemods/src/rules/helpers/renameInterface.ts index 9e2d9c015..ecfdd3f96 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/helpers/renameInterface.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/helpers/renameInterface.ts @@ -1,13 +1,27 @@ import { Rule } from "eslint"; import { TSESTree } from "@typescript-eslint/utils"; -import { Identifier, ImportDeclaration, ImportSpecifier } from "estree-jsx"; +import { + ExportSpecifier, + Identifier, + ImportDeclaration, + ImportSpecifier, +} from "estree-jsx"; import { checkMatchingImportSpecifier, getFromPackage, pfPackageMatches, } from "."; -interface Renames { +interface InterfaceRenames { + [currentName: string]: + | string + | { + newName: string; + message?: string; + }; +} + +interface ComponentRenames { [currentName: string]: string; } @@ -16,37 +30,45 @@ function formatDefaultMessage(oldName: string, newName: string) { } export function renameInterface( - interfaceRenames: Renames, - componentRenames: Renames, + interfaceRenames: InterfaceRenames, + componentRenames: ComponentRenames = {}, packageName = "@patternfly/react-core" ) { return function (context: Rule.RuleContext) { const oldNames = Object.keys(interfaceRenames); - const { imports } = getFromPackage(context, packageName, oldNames); + const { imports, exports } = getFromPackage(context, packageName, oldNames); - if (imports.length === 0) { + if (!imports.length && !exports.length) { return {}; } const shouldRenameIdentifier = (identifier: Identifier) => { const matchingImport = imports.find( - (specifier) => specifier.local.name === identifier.name + (specifier) => + specifier.imported.name === identifier.name && + specifier.imported.name === specifier.local.name ); - if (!matchingImport) { - return false; + return !!matchingImport; + }; + + const getRenameInfo = (oldName: string) => { + const newName = interfaceRenames[oldName]; + + if (typeof newName === "string") { + return { newName }; } - return matchingImport.local.name === matchingImport.imported.name; + return newName; }; const replaceIdentifier = (identifier: Identifier) => { const oldName = identifier.name; - const newName = interfaceRenames[oldName]; + const { newName, message } = getRenameInfo(oldName); context.report({ node: identifier, - message: formatDefaultMessage(oldName, newName), + message: message ?? formatDefaultMessage(oldName, newName), fix(fixer) { return fixer.replaceText(identifier, newName); }, @@ -79,16 +101,17 @@ export function renameInterface( return; } - const oldName = node.imported.name; - const newName = interfaceRenames[oldName]; - - context.report({ - node, - message: formatDefaultMessage(oldName, newName), - fix(fixer) { - return fixer.replaceText(node.imported, newName); - }, - }); + replaceIdentifier(node.imported); + }, + ExportSpecifier(node: ExportSpecifier) { + if ( + exports.some( + (specifier) => specifier.local.name === node.local.name + ) || + shouldRenameIdentifier(node.local) + ) { + replaceIdentifier(node.local); + } }, TSTypeReference(node: TSESTree.TSTypeReference) { if (node.typeName.type === "Identifier") { diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/componentGroupsInvalidObjectPropsRenameToMissingPageProps/component-groups-invalidObjectProps-rename-to-missingPageProps.test.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/componentGroupsInvalidObjectPropsRenameToMissingPageProps/component-groups-invalidObjectProps-rename-to-missingPageProps.test.ts index ef3893d7c..82306ac39 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/componentGroupsInvalidObjectPropsRenameToMissingPageProps/component-groups-invalidObjectProps-rename-to-missingPageProps.test.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/componentGroupsInvalidObjectPropsRenameToMissingPageProps/component-groups-invalidObjectProps-rename-to-missingPageProps.test.ts @@ -35,7 +35,7 @@ ruleTester.run( errors: [ { message, - type: "ImportSpecifier", + type: "Identifier", }, { message, @@ -60,7 +60,7 @@ ruleTester.run( errors: [ { message, - type: "ImportSpecifier", + type: "Identifier", }, ], }, @@ -75,7 +75,7 @@ ruleTester.run( }, { message, - type: "ImportSpecifier", + type: "Identifier", }, ], }, @@ -89,7 +89,7 @@ ruleTester.run( }, { message, - type: "ImportSpecifier", + type: "Identifier", }, ], }, @@ -103,7 +103,7 @@ ruleTester.run( }, { message, - type: "ImportSpecifier", + type: "Identifier", }, ], }, diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.test.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.test.ts index 2d3b1ed05..6af1c9e33 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.test.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.test.ts @@ -20,11 +20,11 @@ ruleTester.run("formFiledGroupHeaderTitleTextObject-renamed", rule, { errors: [ { message: errorMessage, - type: "ImportSpecifier", + type: "Identifier", }, { message: errorMessage, - type: "TSTypeReference", + type: "Identifier", }, ], }, @@ -34,11 +34,11 @@ ruleTester.run("formFiledGroupHeaderTitleTextObject-renamed", rule, { errors: [ { message: errorMessage, - type: "ImportSpecifier", + type: "Identifier", }, { message: errorMessage, - type: "TSInterfaceHeritage", + type: "Identifier", }, ], }, @@ -48,11 +48,11 @@ ruleTester.run("formFiledGroupHeaderTitleTextObject-renamed", rule, { errors: [ { message: errorMessage, - type: "ImportSpecifier", + type: "Identifier", }, { message: errorMessage, - type: "ExportSpecifier", + type: "Identifier", }, ], }, @@ -62,7 +62,7 @@ ruleTester.run("formFiledGroupHeaderTitleTextObject-renamed", rule, { errors: [ { message: errorMessage, - type: "ExportSpecifier", + type: "Identifier", }, ], }, diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.ts index c93f95747..2d16cc002 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.ts @@ -1,87 +1,13 @@ -import { AST, Rule } from "eslint"; -import { ImportSpecifier, ExportSpecifier, Identifier, Node } from "estree-jsx"; -import { getFromPackage } from "../../helpers"; +import { renameInterface } from "../../helpers"; // https://github.com/patternfly/patternfly-react/pull/10016 module.exports = { meta: { fixable: "code" }, - create: function (context: Rule.RuleContext) { - const { imports, exports } = getFromPackage( - context, - "@patternfly/react-core" - ); - - const previousName = "FormFiledGroupHeaderTitleTextObject"; - const newName = "FormFieldGroupHeaderTitleTextObject"; - - const interfaceImport = imports.find( - (specifier) => specifier.imported.name === previousName - ); - - const interfaceExport = exports.find( - (specifier) => specifier.local.name === previousName - ); - - if (!interfaceImport && !interfaceExport) { - return {}; - } - - const hasAlias = (specifier: ImportSpecifier) => - specifier.local.name !== specifier.imported.name; - - const reportMessage = - "There was a typo in FormFiledGroupHeaderTitleTextObject interface. It was renamed to the intended FormFieldGroupHeaderTitleTextObject."; - - const callContextReport = (node: Node, nodeToReplace: Node | AST.Token) => { - context.report({ - node, - message: reportMessage, - fix(fixer) { - return fixer.replaceText(nodeToReplace, newName); - }, - }); - }; - - return { - ImportSpecifier(node: ImportSpecifier) { - if ( - interfaceImport && - node.imported.name === interfaceImport.imported.name - ) { - callContextReport(node, node.imported); - } - }, - ExportSpecifier(node: ExportSpecifier) { - if (interfaceExport && node.local.name === interfaceExport.local.name) { - callContextReport(node, node.local); - } - - if ( - interfaceImport && - !hasAlias(interfaceImport) && - node.local.name === previousName - ) { - callContextReport(node, node.local); - } - }, - TSTypeReference(node: { typeName: Identifier }) { - if ( - interfaceImport && - !hasAlias(interfaceImport) && - node.typeName.name === previousName - ) { - callContextReport(node as unknown as Node, node.typeName); - } - }, - TSInterfaceHeritage(node: { expression: Identifier }) { - if ( - interfaceImport && - !hasAlias(interfaceImport) && - node.expression.name === previousName - ) { - callContextReport(node as unknown as Node, node.expression); - } - }, - }; - }, + create: renameInterface({ + FormFiledGroupHeaderTitleTextObject: { + newName: "FormFieldGroupHeaderTitleTextObject", + message: + "There was a typo in FormFiledGroupHeaderTitleTextObject interface. It was renamed to the intended FormFieldGroupHeaderTitleTextObject.", + }, + }), };