diff --git a/eslint-plugin-expensify/no-use-state-initializer-functions.js b/eslint-plugin-expensify/no-use-state-initializer-functions.js index 817d84e..b14ad88 100644 --- a/eslint-plugin-expensify/no-use-state-initializer-functions.js +++ b/eslint-plugin-expensify/no-use-state-initializer-functions.js @@ -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; } @@ -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', diff --git a/eslint-plugin-expensify/tests/no-use-state-initializer-functions.test.js b/eslint-plugin-expensify/tests/no-use-state-initializer-functions.test.js index cdd078b..7f4f919 100644 --- a/eslint-plugin-expensify/tests/no-use-state-initializer-functions.test.js +++ b/eslint-plugin-expensify/tests/no-use-state-initializer-functions.test.js @@ -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: [ { @@ -30,5 +36,16 @@ ruleTester.run('no-use-state-initializer-functions', rule, { }, ], }, + { + // Calling a function should be invalid + code: ` + useState(testFunc().value); + `, + errors: [ + { + message, + }, + ], + }, ], });