Skip to content

Commit

Permalink
chore(helpers): extract findAncestor helper (#603)
Browse files Browse the repository at this point in the history
* chore(helpers): extract findAncestor helper

* Removed extraneous import

* Add back conditionCallback

* Updated typing

* Removed comments
  • Loading branch information
thatblindgeye authored Mar 20, 2024
1 parent 9df19dd commit c7e95cf
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { JSXElement, JSXOpeningElement } from "estree-jsx";

type NodeWithParent = (JSXOpeningElement | JSXElement) & {
parent?: NodeWithParent | null;
};

export function findAncestor(
node: NodeWithParent,
conditionCallback: (_current: NodeWithParent) => boolean
): NodeWithParent | undefined {
if (!node) {
return;
}

let current = node.parent;

while (current) {
if (conditionCallback(current)) {
return current;
}

current = current.parent;
}
}
15 changes: 1 addition & 14 deletions packages/eslint-plugin-pf-codemods/src/rules/helpers/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getFromPackage } from "./getFromPackage";
import { pfPackageMatches } from "./pfPackageMatches";
import { findAncestor } from "./findAncestor";

const evk = require("eslint-visitor-keys");

Expand Down Expand Up @@ -880,17 +881,3 @@ export function findVariableDeclaration(name, scope) {
}
return undefined;
}

export function findAncestor(node, conditionCallback = (_current) => false) {
let current = node?.parent;

while (current) {
if (conditionCallback(current)) {
return current;
}

current = current.parent;
}

return undefined;
}
7 changes: 4 additions & 3 deletions packages/eslint-plugin-pf-codemods/src/rules/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './helpers';
export * from './pfPackageMatches';
export * from './getFromPackage';
export * from "./findAncestor";
export * from "./helpers";
export * from "./pfPackageMatches";
export * from "./getFromPackage";
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getFromPackage, findAncestor } from "../../helpers";
import { Rule } from "eslint";
import { JSXOpeningElement } from "estree-jsx";
import { JSXElement, JSXOpeningElement } from "estree-jsx";

// https://github.com/patternfly/patternfly-react/pull/9876
module.exports = {
Expand All @@ -26,30 +26,39 @@ module.exports = {
attr.type === "JSXAttribute" &&
attr.name.name === "isExpanded"
);
if (attribute) {
context.report({
node,
message:
"The `isExpanded` prop for AccordionToggle has been moved to AccordionItem.",
fix(fixer) {
const accordionItemAncestor = findAncestor(
node,
(current) =>
current?.openingElement?.name?.name === "AccordionItem"
);
const attributeValue = context
.getSourceCode()
.getText(attribute);
return [
fixer.replaceText(attribute, ""),
fixer.insertTextAfter(
accordionItemAncestor.openingElement.name,
` ${attributeValue}`
),
];
},
});

if (!attribute) {
return;
}

context.report({
node,
message:
"The `isExpanded` prop for AccordionToggle has been moved to AccordionItem.",
fix(fixer) {
const accordionItemAncestor = findAncestor(
node,
(current) =>
current.type === "JSXElement" &&
current.openingElement.name.type === "JSXIdentifier" &&
current.openingElement.name.name === "AccordionItem"
);
const attributeValue = context
.getSourceCode()
.getText(attribute);

return accordionItemAncestor &&
accordionItemAncestor.type === "JSXElement"
? [
fixer.replaceText(attribute, ""),
fixer.insertTextAfter(
accordionItemAncestor.openingElement.name,
` ${attributeValue}`
),
]
: [];
},
});
}
},
};
Expand Down

0 comments on commit c7e95cf

Please sign in to comment.