Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* [`jsx-newline`]: add `allowMultiline` option when prevent option is true ([#3311][] @TildaDares)

### Fixed
* [`jsx-no-literals`]: properly error on children with noAttributeStrings: true ([#3317][] @TildaDares)

### Changed
* [Refactor] [`jsx-indent-props`]: improved readability of the checkNodesIndent function ([#3315][] @caroline223)

[#3317]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3317
[#3315]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3315
[#3311]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3311

Expand Down
28 changes: 15 additions & 13 deletions lib/rules/jsx-no-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ module.exports = {
config.allowedStrings = new Set(config.allowedStrings.map(trimIfString));

function defaultMessageId() {
if (config.noAttributeStrings) {
const ancestorIsJSXElement = arguments.length >= 1 && arguments[0];
if (config.noAttributeStrings && !ancestorIsJSXElement) {
return 'noStringsInAttributes';
}
if (config.noStrings) {
Expand All @@ -79,17 +80,6 @@ module.exports = {
return 'literalNotInJSXExpression';
}

function reportLiteralNode(node, messageId) {
messageId = messageId || defaultMessageId();

report(context, messages[messageId], messageId, {
node,
data: {
text: context.getSourceCode().getText(node).trim(),
},
});
}

function getParentIgnoringBinaryExpressions(node) {
let current = node;
while (current.parent.type === 'BinaryExpression') {
Expand All @@ -107,7 +97,7 @@ module.exports = {
function isParentNodeStandard() {
if (!/^[\s]+$/.test(node.value) && typeof node.value === 'string' && parent.type.includes('JSX')) {
if (config.noAttributeStrings) {
return parent.type === 'JSXAttribute';
return parent.type === 'JSXAttribute' || parent.type === 'JSXElement';
}
if (!config.noAttributeStrings) {
return parent.type !== 'JSXAttribute';
Expand Down Expand Up @@ -146,6 +136,18 @@ module.exports = {
return parentType === 'JSXFragment' || parentType === 'JSXElement' || grandParentType === 'JSXElement';
}

function reportLiteralNode(node, messageId) {
const ancestorIsJSXElement = hasJSXElementParentOrGrandParent(node);
messageId = messageId || defaultMessageId(ancestorIsJSXElement);

report(context, messages[messageId], messageId, {
node,
data: {
text: context.getSourceCode().getText(node).trim(),
},
});
}

// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------
Expand Down
38 changes: 38 additions & 0 deletions tests/lib/rules/jsx-no-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,5 +613,43 @@ ruleTester.run('jsx-no-literals', rule, {
},
],
},
{
code: 'export const WithChildren = ({}) => <div>baz bob</div>;',
options: [{ noAttributeStrings: true }],
errors: [
{
messageId: 'literalNotInJSXExpression',
data: { text: 'baz bob' },
},
],
},
{
code: 'export const WithAttributes = ({}) => <div title="foo bar" />;',
options: [{ noAttributeStrings: true }],
errors: [
{
messageId: 'noStringsInAttributes',
data: { text: '"foo bar"' },
},
],
},
{
code: `
export const WithAttributesAndChildren = ({}) => (
<div title="foo bar">baz bob</div>
);
`,
options: [{ noAttributeStrings: true }],
errors: [
{
messageId: 'noStringsInAttributes',
data: { text: '"foo bar"' },
},
{
messageId: 'literalNotInJSXExpression',
data: { text: 'baz bob' },
},
],
},
]),
});