diff --git a/packages/eslint-plugin-pf-codemods/src/rules/helpers/getText.ts b/packages/eslint-plugin-pf-codemods/src/rules/helpers/getText.ts index 53d5b09e8..14f96b2e1 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/helpers/getText.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/helpers/getText.ts @@ -1,6 +1,12 @@ import { Rule } from "eslint"; -import { JSXAttribute, Node } from "estree-jsx"; +import { + JSXAttribute, + JSXElement, + JSXExpressionContainer, + JSXFragment, + Node, +} from "estree-jsx"; export function getAttributeText( context: Rule.RuleContext, @@ -27,3 +33,42 @@ export function getAttributeValueText( export function getNodesText(context: Rule.RuleContext, nodes: Node[]) { return nodes.map((node) => context.getSourceCode().getText(node)).join(""); } + +export function getChildrenAsAttributeValueText( + context: Rule.RuleContext, + children: JSXElement["children"] +) { + if (!children.length) { + return `""`; + } + + // is a single text-only child + if (children.length === 1 && children[0].type === "JSXText") { + const childText = children[0].value.trim(); + + if (childText.includes(`"`)) { + return `{<>${childText}}`; + } + + return `"${childText}"`; + } + + const nonEmptyChildrenNodes = children.filter( + (child) => !(child.type === "JSXText" && child.value.trim() === "") + ); + + if (nonEmptyChildrenNodes.length === 1) { + const singleChild = nonEmptyChildrenNodes[0]; + const singleChildText = context + .getSourceCode() + .getText( + singleChild as JSXExpressionContainer | JSXElement | JSXFragment + ); + + return singleChild.type === "JSXExpressionContainer" + ? singleChildText + : `{${singleChildText}}`; + } + + return `{<>${getNodesText(context, children as Node[])}}`; +} 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 fdf025c92..7ec3c5afa 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 @@ -1,21 +1,15 @@ import { AST, Rule } from "eslint"; -import { - ImportSpecifier, - JSXElement, - JSXExpressionContainer, - JSXFragment, - Node, -} from "estree-jsx"; +import { ImportSpecifier, JSXElement } from "estree-jsx"; import { getAttribute, getAttributeText, getAttributeValueText, getChildElementByName, - getNodesText, getExpression, getFromPackage, includesImport, nodeIsComponentNamed, + getChildrenAsAttributeValueText, } from "../../helpers"; const baseMessage = @@ -95,37 +89,12 @@ module.exports = { titleTextAttribute ); - const getChildrenText = (children: JSXElement["children"]) => { - if (!children.length) { - return ""; - } - - if (children.length === 1 && children[0].type === "JSXText") { - return `"${children[0].value.trim()}"`; - } - - const potentialSingleChild = children.filter( - (child) => !(child.type === "JSXText" && child.value.trim() === "") - ); - - if (potentialSingleChild.length === 1) { - const singleChild = potentialSingleChild[0]; - const singleChildText = context - .getSourceCode() - .getText( - singleChild as JSXExpressionContainer | JSXElement | JSXFragment - ); - - return singleChild.type === "JSXExpressionContainer" - ? singleChildText - : `{${singleChildText}}`; - } - - return `{<>${getNodesText(context, children as Node[])}}`; - }; - const titleText = - titleTextPropValue || `titleText=${getChildrenText(headerChildren)}`; + titleTextPropValue || + `titleText=${getChildrenAsAttributeValueText( + context, + headerChildren + )}`; const iconPropValue = getExpression(headerIconAttribute?.value);