Skip to content

Commit 7a7bb99

Browse files
TildaDaresljharb
authored andcommitted
[Fix] jsx-no-literals: properly error on children with noAttributeStrings: true
1 parent aac7fb9 commit 7a7bb99

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
99
* [`jsx-newline`]: add `allowMultiline` option when prevent option is true ([#3311][] @TildaDares)
1010

1111
### Fixed
12+
* [`jsx-no-literals`]: properly error on children with noAttributeStrings: true ([#3317][] @TildaDares)
13+
14+
### Changed
1215
* [Refactor] [`jsx-indent-props`]: improved readability of the checkNodesIndent function ([#3315][] @caroline223)
1316

17+
[#3317]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3317
1418
[#3315]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3315
1519
[#3311]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3311
1620

lib/rules/jsx-no-literals.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ module.exports = {
7070
config.allowedStrings = new Set(config.allowedStrings.map(trimIfString));
7171

7272
function defaultMessageId() {
73-
if (config.noAttributeStrings) {
73+
const ancestorIsJSXElement = arguments.length >= 1 && arguments[0];
74+
if (config.noAttributeStrings && !ancestorIsJSXElement) {
7475
return 'noStringsInAttributes';
7576
}
7677
if (config.noStrings) {
@@ -79,17 +80,6 @@ module.exports = {
7980
return 'literalNotInJSXExpression';
8081
}
8182

82-
function reportLiteralNode(node, messageId) {
83-
messageId = messageId || defaultMessageId();
84-
85-
report(context, messages[messageId], messageId, {
86-
node,
87-
data: {
88-
text: context.getSourceCode().getText(node).trim(),
89-
},
90-
});
91-
}
92-
9383
function getParentIgnoringBinaryExpressions(node) {
9484
let current = node;
9585
while (current.parent.type === 'BinaryExpression') {
@@ -107,7 +97,7 @@ module.exports = {
10797
function isParentNodeStandard() {
10898
if (!/^[\s]+$/.test(node.value) && typeof node.value === 'string' && parent.type.includes('JSX')) {
10999
if (config.noAttributeStrings) {
110-
return parent.type === 'JSXAttribute';
100+
return parent.type === 'JSXAttribute' || parent.type === 'JSXElement';
111101
}
112102
if (!config.noAttributeStrings) {
113103
return parent.type !== 'JSXAttribute';
@@ -146,6 +136,18 @@ module.exports = {
146136
return parentType === 'JSXFragment' || parentType === 'JSXElement' || grandParentType === 'JSXElement';
147137
}
148138

139+
function reportLiteralNode(node, messageId) {
140+
const ancestorIsJSXElement = hasJSXElementParentOrGrandParent(node);
141+
messageId = messageId || defaultMessageId(ancestorIsJSXElement);
142+
143+
report(context, messages[messageId], messageId, {
144+
node,
145+
data: {
146+
text: context.getSourceCode().getText(node).trim(),
147+
},
148+
});
149+
}
150+
149151
// --------------------------------------------------------------------------
150152
// Public
151153
// --------------------------------------------------------------------------

tests/lib/rules/jsx-no-literals.js

+38
Original file line numberDiff line numberDiff line change
@@ -613,5 +613,43 @@ ruleTester.run('jsx-no-literals', rule, {
613613
},
614614
],
615615
},
616+
{
617+
code: 'export const WithChildren = ({}) => <div>baz bob</div>;',
618+
options: [{ noAttributeStrings: true }],
619+
errors: [
620+
{
621+
messageId: 'literalNotInJSXExpression',
622+
data: { text: 'baz bob' },
623+
},
624+
],
625+
},
626+
{
627+
code: 'export const WithAttributes = ({}) => <div title="foo bar" />;',
628+
options: [{ noAttributeStrings: true }],
629+
errors: [
630+
{
631+
messageId: 'noStringsInAttributes',
632+
data: { text: '"foo bar"' },
633+
},
634+
],
635+
},
636+
{
637+
code: `
638+
export const WithAttributesAndChildren = ({}) => (
639+
<div title="foo bar">baz bob</div>
640+
);
641+
`,
642+
options: [{ noAttributeStrings: true }],
643+
errors: [
644+
{
645+
messageId: 'noStringsInAttributes',
646+
data: { text: '"foo bar"' },
647+
},
648+
{
649+
messageId: 'literalNotInJSXExpression',
650+
data: { text: 'baz bob' },
651+
},
652+
],
653+
},
616654
]),
617655
});

0 commit comments

Comments
 (0)