Skip to content

Commit

Permalink
Catch untranslated text in conditional and logical expressions (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandernanberg authored Dec 22, 2024
1 parent f3fde5b commit e12e430
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,48 @@ ruleTester.run('no-untranslated-strings', rule, {
},
{
code: `
<>Hello world</>
<>Hello world</>;
`,
errors: [
{
messageId: 'unwrappedString',
},
],
},
{
code: `
<span>{foo ? 'bar' : 'baz'}</span>;
`,
errors: [
{
messageId: 'unwrappedString',
},
],
},
{
code: `
<span>{foo ?? 'bar'}</span>;
`,
errors: [
{
messageId: 'unwrappedString',
},
],
},
{
code: `
<span>{foo || 'bar'}</span>;
`,
errors: [
{
messageId: 'unwrappedString',
},
],
},
{
code: `
const foo = true;
<span>{foo && 'bar'}</span>;
`,
errors: [
{
Expand Down Expand Up @@ -219,5 +260,20 @@ ruleTester.run('no-untranslated-strings', rule, {
foo.bar('Baz')
`,
},
{
code: `
const foo = isGreeting ? 'Hello' : 'Goodbye'
`,
},
{
code: `
const foo = helloGreeting ?? 'goodbye'
`,
},
{
code: `
const foo = helloGreeting || 'goodbye'
`,
},
],
});
14 changes: 14 additions & 0 deletions packages/eslint-plugin-fbtee/src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ export function resolveNodeValue(
return resolveNodeValue(node.expression);
}

if (node.type === 'ConditionalExpression') {
const consequent = resolveNodeValue(node.consequent);
const alternate = resolveNodeValue(node.alternate);

return consequent || alternate || null;
}

if (node.type === 'LogicalExpression') {
const left = resolveNodeValue(node.left);
const right = resolveNodeValue(node.right);

return left || right || null;
}

return null;
}

Expand Down

0 comments on commit e12e430

Please sign in to comment.