Skip to content

Commit 1cffa9f

Browse files
committed
Allow text in children of Text components
1 parent 8a54f68 commit 1cffa9f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/rules/no-raw-text.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ const elementName = (node) => {
2727
return reversedIdentifiers.reverse().join('.');
2828
};
2929

30+
const hasAllowedParent = (parent, allowedElements) => {
31+
let curNode = parent;
32+
33+
while (curNode) {
34+
if (curNode.type === 'JSXElement') {
35+
const name = elementName(curNode);
36+
if (allowedElements.includes(name)) {
37+
return true;
38+
}
39+
}
40+
curNode = curNode.parent;
41+
}
42+
43+
return false;
44+
};
45+
3046
function create(context) {
3147
const options = context.options[0] || {};
3248

@@ -50,7 +66,7 @@ function create(context) {
5066

5167
const hasOnlyLineBreak = (value) => /^[\r\n\t\f\v]+$/.test(value.replace(/ /g, ''));
5268

53-
const getValidation = (node) => !allowedElements.includes(elementName(node.parent));
69+
const getValidation = (node) => !hasAllowedParent(node.parent, allowedElements);
5470

5571
return {
5672
Literal(node) {

tests/lib/rules/no-raw-text.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ const tests = {
114114
}
115115
`,
116116
},
117+
{
118+
// Testing nested components inside an allowed component
119+
code: `
120+
<Text>Some text <>More text</></Text>
121+
`,
122+
},
117123
],
118124
invalid: [
119125
{

0 commit comments

Comments
 (0)