fix CallExpression detection

This commit is contained in:
dakkar 2024-10-16 14:13:05 +01:00
parent 82674d8718
commit dba3277200
2 changed files with 8 additions and 3 deletions

View File

@ -40,9 +40,13 @@ function walkDown(locale, path) {
* to the MemberExpressions * to the MemberExpressions
*/ */
function findCallExpression(node) { function findCallExpression(node) {
if (node.type === 'CallExpression') return node if (!node.parent) return null;
if (node.parent?.type === 'CallExpression') return node.parent;
if (node.parent?.type === 'MemberExpression') return findCallExpression(node.parent); // the second half of this guard protects from cases like
// `foo(one.two.three)` where the CallExpression is parent of the
// MemberExpressions, but via `arguments`, not `callee`
if (node.parent.type === 'CallExpression' && node.parent.callee === node) return node.parent;
if (node.parent.type === 'MemberExpression') return findCallExpression(node.parent);
return null; return null;
} }

View File

@ -15,6 +15,7 @@ ruleTester.run(
{code: 'i18n.tsx.foo.baz(1)', options: [locale] }, {code: 'i18n.tsx.foo.baz(1)', options: [locale] },
{code: 'whatever.i18n.ts.blah.blah', options: [locale] }, {code: 'whatever.i18n.ts.blah.blah', options: [locale] },
{code: 'whatever.i18n.tsx.does.not.matter', options: [locale] }, {code: 'whatever.i18n.tsx.does.not.matter', options: [locale] },
{code: 'whatever(i18n.ts.foo.bar)', options: [locale] },
], ],
invalid: [ invalid: [
{code: 'i18n.ts.not', options: [locale], errors: 1 }, {code: 'i18n.ts.not', options: [locale], errors: 1 },