From f4b868dbaf3dc462deb324521187feb6a6462fa2 Mon Sep 17 00:00:00 2001 From: adamviktora Date: Wed, 21 Aug 2024 16:21:37 +0200 Subject: [PATCH] refactor: get rid of nodeIsComponentNamed helper --- .../src/rules/helpers/index.ts | 1 - .../src/rules/helpers/nodeIsComponentNamed.ts | 9 --------- .../emptyStateHeader-move-into-emptyState.ts | 13 +++++++++---- .../loginMainFooterLinksItem-structure-updated.ts | 9 +++++++-- 4 files changed, 16 insertions(+), 16 deletions(-) delete mode 100644 packages/eslint-plugin-pf-codemods/src/rules/helpers/nodeIsComponentNamed.ts diff --git a/packages/eslint-plugin-pf-codemods/src/rules/helpers/index.ts b/packages/eslint-plugin-pf-codemods/src/rules/helpers/index.ts index 196081667..d428715be 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/helpers/index.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/helpers/index.ts @@ -21,7 +21,6 @@ export * from "./importAndExport"; export * from "./includesImport"; export * from "./interfaces"; export * from "./JSXAttributes"; -export * from "./nodeIsComponentNamed"; export * from "./nodeMatches"; export * from "./pfPackageMatches"; export * from "./removeElement"; diff --git a/packages/eslint-plugin-pf-codemods/src/rules/helpers/nodeIsComponentNamed.ts b/packages/eslint-plugin-pf-codemods/src/rules/helpers/nodeIsComponentNamed.ts deleted file mode 100644 index 991a6073b..000000000 --- a/packages/eslint-plugin-pf-codemods/src/rules/helpers/nodeIsComponentNamed.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { JSXElement } from "estree-jsx"; - -export function nodeIsComponentNamed(node: JSXElement, componentName: string) { - if (node.openingElement.name.type === "JSXIdentifier") { - return node.openingElement.name.name === componentName; - } - - return false; -} diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/emptyStateHeaderMoveIntoEmptyState/emptyStateHeader-move-into-emptyState.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/emptyStateHeaderMoveIntoEmptyState/emptyStateHeader-move-into-emptyState.ts index 655ba2302..67fe9e1ea 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/emptyStateHeaderMoveIntoEmptyState/emptyStateHeader-move-into-emptyState.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/emptyStateHeaderMoveIntoEmptyState/emptyStateHeader-move-into-emptyState.ts @@ -7,6 +7,7 @@ import { Node, } from "estree-jsx"; import { + checkMatchingJSXOpeningElement, getAttribute, getAttributeText, getAttributeValueText, @@ -14,8 +15,6 @@ import { getDefaultImportsFromPackage, getExpression, getFromPackage, - includesImport, - nodeIsComponentNamed, getChildrenAsAttributeValueText, getRemoveElementFixes, } from "../../helpers"; @@ -149,13 +148,19 @@ module.exports = { const pkg = "@patternfly/react-core"; const { imports } = getFromPackage(context, pkg); - if (!includesImport(imports, "EmptyState")) { + const emptyStateImport = imports.find( + (specifier) => specifier.imported.name === "EmptyState" + ); + + if (!emptyStateImport) { return {}; } return { JSXElement(node: JSXElement) { - if (!nodeIsComponentNamed(node, "EmptyState")) { + if ( + !checkMatchingJSXOpeningElement(node.openingElement, emptyStateImport) + ) { return; } diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/loginMainFooterLinksItemStructureUpdated/loginMainFooterLinksItem-structure-updated.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/loginMainFooterLinksItemStructureUpdated/loginMainFooterLinksItem-structure-updated.ts index 383037b1f..567403a8b 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/loginMainFooterLinksItemStructureUpdated/loginMainFooterLinksItem-structure-updated.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/loginMainFooterLinksItemStructureUpdated/loginMainFooterLinksItem-structure-updated.ts @@ -1,12 +1,12 @@ import { Rule } from "eslint"; import { JSXElement } from "estree-jsx"; import { + checkMatchingJSXOpeningElement, getAttribute, getAttributeText, getExpression, getFromPackage, includesImport, - nodeIsComponentNamed, } from "../../helpers"; // https://github.com/patternfly/patternfly-react/pull/10107 @@ -25,7 +25,12 @@ module.exports = { return { JSXElement(node: JSXElement) { - if (!nodeIsComponentNamed(node, "LoginMainFooterLinksItem")) { + if ( + !checkMatchingJSXOpeningElement( + node.openingElement, + loginMainFooterLinksItemImport + ) + ) { return; }