Skip to content

Commit

Permalink
refactor(EmptyState): extract getChildrenAsAttributeValueText helper
Browse files Browse the repository at this point in the history
  • Loading branch information
adamviktora committed Mar 21, 2024
1 parent c1136d4 commit 5e6ff93
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
47 changes: 46 additions & 1 deletion packages/eslint-plugin-pf-codemods/src/rules/helpers/getText.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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[])}</>}`;
}
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 5e6ff93

Please sign in to comment.