diff --git a/eslint-plugin-expensify/no-use-state-initializer-functions.js b/eslint-plugin-expensify/no-use-state-initializer-functions.js index b14ad88..f788e2c 100644 --- a/eslint-plugin-expensify/no-use-state-initializer-functions.js +++ b/eslint-plugin-expensify/no-use-state-initializer-functions.js @@ -28,10 +28,12 @@ module.exports = { const firstArg = node.arguments[0]; - // Return early if the first argument is not a function call or a member expression with a function call + // Return early if the first argument is not a function call, member expression with a function call, or conditional expression with function calls if ( firstArg.type !== 'CallExpression' && !(firstArg.type === 'MemberExpression' && firstArg.object.type === 'CallExpression') + && !(firstArg.type === 'ConditionalExpression' + && (firstArg.consequent.type === 'CallExpression' || firstArg.alternate.type === 'CallExpression')) ) { return; } @@ -64,10 +66,12 @@ module.exports = { return; // Valid case, do nothing } - // If it's a direct function call or a member expression with a function call, report it + // If it's a direct function call, member expression with a function call, or conditional expression with function calls, report it if ( firstArg.type === 'CallExpression' || (firstArg.type === 'MemberExpression' && firstArg.object.type === 'CallExpression') + || (firstArg.type === 'ConditionalExpression' + && (firstArg.consequent.type === 'CallExpression' || firstArg.alternate.type === 'CallExpression')) ) { context.report({ node: firstArg, 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 7f4f919..3cc06ea 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 @@ -23,6 +23,18 @@ ruleTester.run('no-use-state-initializer-functions', rule, { useState(() => testFunc().value); `, }, + { + // Calling a callback should be valid + code: ` + useState(condition ? testFunc : testFunc); + `, + }, + { + // Calling a callback should be valid + code: ` + useState(condition ? () => testFunc() : () => testFunc()); + `, + }, ], invalid: [ { @@ -47,5 +59,27 @@ ruleTester.run('no-use-state-initializer-functions', rule, { }, ], }, + { + // Calling a function should be invalid + code: ` + useState(condition ? testFunc() : testFunc()); + `, + errors: [ + { + message, + }, + ], + }, + { + // Calling a function should be invalid + code: ` + useState(condition ? (() => testFunc())() : (() => testFunc())()); + `, + errors: [ + { + message, + }, + ], + }, ], });