-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathprefer-use-state-lazy-initialization.js
82 lines (73 loc) · 2.55 KB
/
prefer-use-state-lazy-initialization.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* @fileoverview Detects function calls in useState and suggests using lazy initialization instead.
* @author Patrick Gillespie
*/
'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, // URL to the documentation page for this rule
},
fixable: null, // Or `code` or `whitespace`
schema: [], // Add a schema if the rule has options
messages: {
useLazyInitialization:
'To prevent re-computation, consider using lazy initial state for useState calls that involve function calls. Ex: useState(() => getValue())',
},
},
// rule takes inspiration from https://github.com/facebook/react/issues/26520
create(context) {
// variables should be defined here
const ALLOW_LIST = Object.freeze(['Boolean', 'String']);
//----------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------
// any helper functions should go here or else delete this section
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) {
// @ts-ignore
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' });
}
}
}
},
};
},
};