Skip to content
Open
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
13 changes: 10 additions & 3 deletions lib/rules/no-literal-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,16 @@ module.exports = {

function filterOutJSX(node) {
if (onlyValidateJSX) {
const isInsideJSX = context
.getAncestors()
.some(item => ['JSXElement', 'JSXFragment'].includes(item.type));
let isInsideJSX = false;
const ancestors = [...context.getAncestors()].reverse();
for (const ancestor of ancestors) {
if (ancestor.type === 'CallExpression') {
break;
} else if (['JSXElement', 'JSXFragment'].includes(ancestor.type)) {
isInsideJSX = true;
break;
}
}

if (!isInsideJSX) return true;

Expand Down
18 changes: 18 additions & 0 deletions tests/lib/rules/no-literal-string/jsx-only.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,26 @@ const cases = {
code: `<div>{[].map((a) => { switch (a) { case 'abc': return null; default: return null; } })}</div>`,
options: [{ mode: 'jsx-only' }],
},
{
code: `<div>{[].map((a) => { return <DIV foo={a} />; })}</div>`,
options: [{ mode: 'jsx-only' }],
},
{
code: `<div>{[].map((a) => { const x="some text"; console.log(x); return <DIV foo={a} />; })}</div>`,
options: [{ mode: 'jsx-only' }],
},
],
invalid: [
{
code: `<div>{[].map(() => { const x="some text"; console.log(x); return <DIV foo="bar">; })}</div>`,
options: [{ mode: 'jsx-only' }],
errors: 1,
},
{
code: `<div>{[].map(() => { const x="some text"; console.log(x); return <div>untranslated text</div> })}</div>`,
options: [{ mode: 'jsx-only' }],
errors: 1,
},
{
...testFile('invalid-jsx-only.jsx'),
options: [{ mode: 'jsx-only' }],
Expand Down