-
Notifications
You must be signed in to change notification settings - Fork 45.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ESLint rule for detecting function calls inside useState (for recommending lazy initialization) #26822
Closed
patorjk
wants to merge
1
commit into
facebook:main
from
patorjk:eslint-prefer-use-state-lazy-initialization
Closed
ESLint rule for detecting function calls inside useState (for recommending lazy initialization) #26822
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
129 changes: 129 additions & 0 deletions
129
...es/eslint-plugin-react-hooks/__tests__/ESLintRulePreferUseStateLazyInitialization-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,129 @@ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../src/PreferUseStateLazyInitialization'); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester(); | ||
ruleTester.run('prefer-use-state-lazy-initialization', rule, { | ||
valid: [ | ||
// give me some code that won't trigger a warning | ||
'useState()', | ||
'useState("")', | ||
'useState(true)', | ||
'useState(false)', | ||
'useState(null)', | ||
'useState(undefined)', | ||
'useState(1)', | ||
'useState("test")', | ||
'useState(value)', | ||
'useState(object.value)', | ||
'useState(1 || 2)', | ||
'useState(1 || 2 || 3 < 4)', | ||
'useState(1 && 2)', | ||
'useState(1 < 2)', | ||
'useState(1 < 2 ? 3 : 4)', | ||
'useState(1 == 2 ? 3 : 4)', | ||
'useState(1 === 2 ? 3 : 4)', | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'useState(1 || getValue())', | ||
errors: [ | ||
{ | ||
message: rule.meta.messages.useLazyInitialization, | ||
type: 'CallExpression', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'useState(2 < getValue())', | ||
errors: [ | ||
{ | ||
message: rule.meta.messages.useLazyInitialization, | ||
type: 'CallExpression', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'useState(getValue())', | ||
errors: [ | ||
{ | ||
message: rule.meta.messages.useLazyInitialization, | ||
type: 'CallExpression', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'useState(getValue(1, 2, 3))', | ||
errors: [ | ||
{ | ||
message: rule.meta.messages.useLazyInitialization, | ||
type: 'CallExpression', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'useState(a ? b : c())', | ||
errors: [ | ||
{ | ||
message: rule.meta.messages.useLazyInitialization, | ||
type: 'CallExpression', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'useState(a() ? b : c)', | ||
errors: [ | ||
{ | ||
message: rule.meta.messages.useLazyInitialization, | ||
type: 'CallExpression', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'useState(a ? (b ? b1() : b2) : c)', | ||
errors: [ | ||
{ | ||
message: rule.meta.messages.useLazyInitialization, | ||
type: 'CallExpression', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'useState(a() && b)', | ||
errors: [ | ||
{ | ||
message: rule.meta.messages.useLazyInitialization, | ||
type: 'CallExpression', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'useState(a && b())', | ||
errors: [ | ||
{ | ||
message: rule.meta.messages.useLazyInitialization, | ||
type: 'CallExpression', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'useState(a() && b())', | ||
errors: [ | ||
{ | ||
message: rule.meta.messages.useLazyInitialization, | ||
type: 'CallExpression', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
72 changes: 72 additions & 0 deletions
72
packages/eslint-plugin-react-hooks/src/PreferUseStateLazyInitialization.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,72 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
'use strict'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
//------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||
// Rule Definition | ||||||||||||||||||||||||||||||||||||||||||||||||
//------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
/** @type {import('eslint').Rule.RuleModule} */ | ||||||||||||||||||||||||||||||||||||||||||||||||
module.exports = { | ||||||||||||||||||||||||||||||||||||||||||||||||
meta: { | ||||||||||||||||||||||||||||||||||||||||||||||||
type: 'suggestion', | ||||||||||||||||||||||||||||||||||||||||||||||||
docs: { | ||||||||||||||||||||||||||||||||||||||||||||||||
description: | ||||||||||||||||||||||||||||||||||||||||||||||||
"Disallow function calls in useState that aren't wrapped in an initializer function", | ||||||||||||||||||||||||||||||||||||||||||||||||
recommended: false, | ||||||||||||||||||||||||||||||||||||||||||||||||
url: null, | ||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||
fixable: null, | ||||||||||||||||||||||||||||||||||||||||||||||||
schema: [], | ||||||||||||||||||||||||||||||||||||||||||||||||
messages: { | ||||||||||||||||||||||||||||||||||||||||||||||||
useLazyInitialization: | ||||||||||||||||||||||||||||||||||||||||||||||||
'To prevent re-computation, consider using lazy initial state for useState calls that involve function calls. Ex: useState(() => getValue())', | ||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
create(context) { | ||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think code will be clearer if you just ban all calls inside useState
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
const ALLOW_LIST = Object.freeze(['Boolean', 'String']); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
//---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||
// Helpers | ||||||||||||||||||||||||||||||||||||||||||||||||
//---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
const hasFunctionCall = node => { | ||||||||||||||||||||||||||||||||||||||||||||||||
if ( | ||||||||||||||||||||||||||||||||||||||||||||||||
node.type === 'CallExpression' && | ||||||||||||||||||||||||||||||||||||||||||||||||
ALLOW_LIST.indexOf(node.callee.name) === -1 | ||||||||||||||||||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||||||||||||||||||
return true; | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
if (node.type === 'ConditionalExpression') { | ||||||||||||||||||||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||||||||||||||||||||
hasFunctionCall(node.test) || | ||||||||||||||||||||||||||||||||||||||||||||||||
hasFunctionCall(node.consequent) || | ||||||||||||||||||||||||||||||||||||||||||||||||
hasFunctionCall(node.alternate) | ||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
if ( | ||||||||||||||||||||||||||||||||||||||||||||||||
node.type === 'LogicalExpression' || | ||||||||||||||||||||||||||||||||||||||||||||||||
node.type === 'BinaryExpression' | ||||||||||||||||||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||||||||||||||||||
return hasFunctionCall(node.left) || hasFunctionCall(node.right); | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
return false; | ||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
//---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||
// Public | ||||||||||||||||||||||||||||||||||||||||||||||||
//---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||||||||||||||||||||||
CallExpression(node) { | ||||||||||||||||||||||||||||||||||||||||||||||||
if (node.callee && node.callee.name === 'useState') { | ||||||||||||||||||||||||||||||||||||||||||||||||
if (node.arguments.length > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||
const useStateInput = node.arguments[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||
if (hasFunctionCall(useStateInput)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
context.report({node, messageId: 'useLazyInitialization'}); | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you should get two errors. Because you have
two function calls.