Skip to content

Commit

Permalink
handle member expressions with function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
rezkiy37 committed Nov 29, 2024
1 parent c3663ae commit 86151f0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
14 changes: 10 additions & 4 deletions eslint-plugin-expensify/no-use-state-initializer-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ module.exports = {

const firstArg = node.arguments[0];

// Return early if the first argument is not a function call
if (firstArg.type !== 'CallExpression') {
// Return early if the first argument is not a function call or a member expression with a function call
if (
firstArg.type !== 'CallExpression'
&& !(firstArg.type === 'MemberExpression' && firstArg.object.type === 'CallExpression')
) {
return;
}

Expand Down Expand Up @@ -61,8 +64,11 @@ module.exports = {
return; // Valid case, do nothing
}

// If it's a direct function call, report it
if (firstArg.type === 'CallExpression') {
// If it's a direct function call or a member expression with a function call, report it
if (
firstArg.type === 'CallExpression'
|| (firstArg.type === 'MemberExpression' && firstArg.object.type === 'CallExpression')
) {
context.report({
node: firstArg,
messageId: 'noDirectCall',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ ruleTester.run('no-use-state-initializer-functions', rule, {
useState(() => testFunc());
`,
},
{
// Calling a callback should be valid
code: `
useState(() => testFunc().value);
`,
},
],
invalid: [
{
Expand All @@ -30,5 +36,16 @@ ruleTester.run('no-use-state-initializer-functions', rule, {
},
],
},
{
// Calling a function should be invalid
code: `
useState(testFunc().value);
`,
errors: [
{
message,
},
],
},
],
});

0 comments on commit 86151f0

Please sign in to comment.