Skip to content

Commit df5a08b

Browse files
committed
fixing bug where the text content had to be a string literal
1 parent 536ae9f commit df5a08b

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

lib/util/hasTextContentChild.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { TSESTree } from "@typescript-eslint/types";
55

66
/**
7-
* hasTextContentChild - determines if a component has text content as a child e.g. <Button>Hello</Button>
7+
* hasTextContentChild - determines if a component has text content as a child, e.g., <Button>Hello</Button>, <Button>{'Hello'}</Button>, <Button>{myFunc()}</Button>, or <Button>{myVar}</Button>
88
* @param {*} node JSXElement
99
* @returns boolean
1010
*/
@@ -14,12 +14,28 @@ const hasTextContentChild = (node?: TSESTree.JSXElement) => {
1414
return false;
1515
}
1616

17-
if (node.children == null || node.children == undefined || node.children.length === 0) {
17+
if (!node.children || node.children.length === 0) {
1818
return false;
1919
}
2020

2121
const result = node.children.filter(element => {
22-
return element.type === "JSXText" && element.value.trim().length > 0;
22+
// Check for JSXText with non-whitespace content
23+
if (element.type === "JSXText" && element.value.trim().length > 0) {
24+
return true;
25+
}
26+
27+
// Check for JSXExpressionContainer with valid expression content
28+
if (
29+
element.type === "JSXExpressionContainer" &&
30+
element.expression &&
31+
((element.expression.type === "Literal" && String(element.expression.value).trim().length > 0) ||
32+
element.expression.type === "CallExpression" ||
33+
element.expression.type === "Identifier")
34+
) {
35+
return true;
36+
}
37+
38+
return false;
2339
});
2440

2541
return result.length !== 0;

tests/lib/rules/utils/hasTextContentChild.test.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe("hasTextContentChild", () => {
2222
expect(hasTextContentChild(node)).toBe(false);
2323
});
2424

25-
it("should return false when node.children has no JSXText elements with non-whitespace content", () => {
25+
it("should return false when node.children has no JSXText or relevant JSXExpressionContainer content", () => {
2626
const node: TSESTree.JSXElement = {
2727
children: [{ type: "JSXElement" }, { type: "JSXExpressionContainer" }]
2828
} as any;
@@ -42,4 +42,32 @@ describe("hasTextContentChild", () => {
4242
} as any;
4343
expect(hasTextContentChild(node)).toBe(false);
4444
});
45+
46+
it("should return true when node.children has JSXExpressionContainer with a literal string", () => {
47+
const node: TSESTree.JSXElement = {
48+
children: [{ type: "JSXExpressionContainer", expression: { type: "Literal", value: "Hello" } }]
49+
} as any;
50+
expect(hasTextContentChild(node)).toBe(true);
51+
});
52+
53+
it("should return true when node.children has JSXExpressionContainer with a function call", () => {
54+
const node: TSESTree.JSXElement = {
55+
children: [{ type: "JSXExpressionContainer", expression: { type: "CallExpression", callee: { name: "myFunc" } } }]
56+
} as any;
57+
expect(hasTextContentChild(node)).toBe(true);
58+
});
59+
60+
it("should return true when node.children has JSXExpressionContainer with an identifier (variable)", () => {
61+
const node: TSESTree.JSXElement = {
62+
children: [{ type: "JSXExpressionContainer", expression: { type: "Identifier", name: "myVar" } }]
63+
} as any;
64+
expect(hasTextContentChild(node)).toBe(true);
65+
});
66+
67+
it("should return false when node.children has JSXExpressionContainer with an empty string literal", () => {
68+
const node: TSESTree.JSXElement = {
69+
children: [{ type: "JSXExpressionContainer", expression: { type: "Literal", value: "" } }]
70+
} as any;
71+
expect(hasTextContentChild(node)).toBe(false);
72+
});
4573
});

0 commit comments

Comments
 (0)