-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from rezkiy37/feature/no-use-state-initialize…
…r-functions no-use-state-initializer-functions rule
- Loading branch information
Showing
4 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
eslint-plugin-expensify/no-use-state-initializer-functions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const message = require('./CONST').MESSAGE.NO_USE_STATE_INITIALIZER_CALL_FUNCTION; | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', // This is a potential performance issue | ||
docs: { | ||
description: | ||
'Disallow direct function calls in useState initializer', | ||
category: 'Best Practices', | ||
recommended: false, | ||
}, | ||
schema: [], // No options for this rule | ||
messages: { | ||
noDirectCall: message, | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
// Return early if the function being called is not `useState` | ||
if ( | ||
node.callee.type !== 'Identifier' | ||
|| node.callee.name !== 'useState' | ||
|| node.arguments.length === 0 | ||
) { | ||
return; | ||
} | ||
|
||
const firstArg = node.arguments[0]; | ||
|
||
// 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; | ||
} | ||
|
||
context.report({ | ||
node: firstArg, | ||
messageId: 'noDirectCall', | ||
}); | ||
}, | ||
|
||
// Handle cases where the initializer is passed as a function reference | ||
VariableDeclarator(node) { | ||
if ( | ||
!node.init | ||
|| node.init.type !== 'CallExpression' | ||
|| node.init.callee.name !== 'useState' | ||
|| node.init.arguments.length === 0 | ||
) { | ||
return; | ||
} | ||
|
||
const firstArg = node.init.arguments[0]; | ||
|
||
// Return early if the first argument is a function reference (valid case) | ||
if ( | ||
firstArg.type === 'Identifier' // e.g., `getChatTabBrickRoad` | ||
|| (firstArg.type === 'ArrowFunctionExpression' | ||
&& firstArg.body.type !== 'CallExpression') // e.g., `() => getChatTabBrickRoad` | ||
) { | ||
return; // Valid case, do nothing | ||
} | ||
|
||
// 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, | ||
messageId: 'noDirectCall', | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
85 changes: 85 additions & 0 deletions
85
eslint-plugin-expensify/tests/no-use-state-initializer-functions.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../no-use-state-initializer-functions'); | ||
const message = require('../CONST').MESSAGE.NO_USE_STATE_INITIALIZER_CALL_FUNCTION; | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
sourceType: 'module', | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-use-state-initializer-functions', rule, { | ||
valid: [ | ||
{ | ||
// Calling a callback should be valid | ||
code: ` | ||
useState(() => testFunc()); | ||
`, | ||
}, | ||
{ | ||
// Calling a callback should be valid | ||
code: ` | ||
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: [ | ||
{ | ||
// Calling a function should be invalid | ||
code: ` | ||
useState(testFunc()); | ||
`, | ||
errors: [ | ||
{ | ||
message, | ||
}, | ||
], | ||
}, | ||
{ | ||
// Calling a function should be invalid | ||
code: ` | ||
useState(testFunc().value); | ||
`, | ||
errors: [ | ||
{ | ||
message, | ||
}, | ||
], | ||
}, | ||
{ | ||
// 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, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters