eslint-plugin-react-hooks-extra 1.0.0 → 1.5.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.cjs +210 -0
- package/dist/index.d.mts +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +210 -0
- package/dist/index.mjs +207 -0
- package/package.json +65 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Eva1ent
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@eslint-react/core');
|
|
4
|
+
var shared = require('@eslint-react/shared');
|
|
5
|
+
var ast = require('@eslint-react/ast');
|
|
6
|
+
var jsx = require('@eslint-react/jsx');
|
|
7
|
+
var tools = require('@eslint-react/tools');
|
|
8
|
+
var _var = require('@eslint-react/var');
|
|
9
|
+
|
|
10
|
+
var name = "eslint-plugin-react-hooks-extra";
|
|
11
|
+
var version = "1.5.0-beta.0";
|
|
12
|
+
|
|
13
|
+
const createRule = shared.createRuleForPlugin("hooks-extra");
|
|
14
|
+
|
|
15
|
+
const RULE_NAME$3 = "ensure-custom-hooks-using-other-hooks";
|
|
16
|
+
var ensureCustomHooksUsingOtherHooks = createRule({
|
|
17
|
+
name: RULE_NAME$3,
|
|
18
|
+
meta: {
|
|
19
|
+
type: "problem",
|
|
20
|
+
docs: {
|
|
21
|
+
description: "enforce custom hooks using other hooks",
|
|
22
|
+
requiresTypeChecking: false
|
|
23
|
+
},
|
|
24
|
+
schema: [],
|
|
25
|
+
messages: {
|
|
26
|
+
ENSURE_CUSTOM_HOOKS_USING_OTHER_HOOKS: "Custom hooks {{name}} should use other hooks"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
defaultOptions: [],
|
|
30
|
+
create (context) {
|
|
31
|
+
const { ctx, listeners } = core.useHookCollector();
|
|
32
|
+
return {
|
|
33
|
+
...listeners,
|
|
34
|
+
"Program:exit" (node) {
|
|
35
|
+
const allHooks = ctx.getAllHooks(node);
|
|
36
|
+
for (const { name, hookCalls, node } of allHooks.values()){
|
|
37
|
+
if (hookCalls.length > 0) continue;
|
|
38
|
+
context.report({
|
|
39
|
+
data: {
|
|
40
|
+
name: name.value
|
|
41
|
+
},
|
|
42
|
+
messageId: "ENSURE_CUSTOM_HOOKS_USING_OTHER_HOOKS",
|
|
43
|
+
node
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
var match;
|
|
52
|
+
function n(n,t){(null==t||t>n.length)&&(t=n.length);for(var r=0,e=new Array(t);r<t;r++)e[r]=n[r];return e}function t(t,r){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(e)return (e=e.call(t)).next.bind(e);if(Array.isArray(t)||(e=function(t,r){if(t){if("string"==typeof t)return n(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);return "Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?n(t,r):void 0}}(t))||r&&t&&"number"==typeof t.length){e&&(t=e);var u=0;return function(){return u>=t.length?{done:!0}:{done:!1,value:t[u++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var r=Symbol.for("@ts-pattern/matcher"),e=Symbol.for("@ts-pattern/isVariadic"),u="@ts-pattern/anonymous-select-key",i=function(n){return Boolean(n&&"object"==typeof n)},o=function(n){return n&&!!n[r]},c=function n(u,c,a){if(o(u)){var f=u[r]().match(c),s=f.matched,l=f.selections;return s&&l&&Object.keys(l).forEach(function(n){return a(n,l[n])}),s}if(i(u)){if(!i(c))return !1;if(Array.isArray(u)){if(!Array.isArray(c))return !1;for(var h,v=[],g=[],m=[],p=t(u.keys());!(h=p()).done;){var y=u[h.value];o(y)&&y[e]?m.push(y):m.length?g.push(y):v.push(y);}if(m.length){if(m.length>1)throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");if(c.length<v.length+g.length)return !1;var d=c.slice(0,v.length),b=0===g.length?[]:c.slice(-g.length),w=c.slice(v.length,0===g.length?Infinity:-g.length);return v.every(function(t,r){return n(t,d[r],a)})&&g.every(function(t,r){return n(t,b[r],a)})&&(0===m.length||n(m[0],w,a))}return u.length===c.length&&u.every(function(t,r){return n(t,c[r],a)})}return Object.keys(u).every(function(t){var e,i=u[t];return (t in c||o(e=i)&&"optional"===e[r]().matcherType)&&n(i,c[t],a)})}return Object.is(c,u)},a=function n(t){var e,u,c;return i(t)?o(t)?null!=(e=null==(u=(c=t[r]()).getSelectionKeys)?void 0:u.call(c))?e:[]:Array.isArray(t)?f(t,n):f(Object.values(t),n):[]},f=function(n,t){return n.reduce(function(n,r){return n.concat(t(r))},[])};function l(n){return Object.assign(n,{optional:function(){return v(n)},and:function(t){return p(n,t)},or:function(t){return y(n,t)},select:function(t){return void 0===t?b(n):b(t,n)}})}function v(n){var t;return l(((t={})[r]=function(){return {match:function(t){var r={},e=function(n,t){r[n]=t;};return void 0===t?(a(n).forEach(function(n){return e(n,void 0)}),{matched:!0,selections:r}):{matched:c(n,t,e),selections:r}},getSelectionKeys:function(){return a(n)},matcherType:"optional"}},t))}function p(){var n,t=[].slice.call(arguments);return l(((n={})[r]=function(){return {match:function(n){var r={},e=function(n,t){r[n]=t;};return {matched:t.every(function(t){return c(t,n,e)}),selections:r}},getSelectionKeys:function(){return f(t,a)},matcherType:"and"}},n))}function y(){var n,t=[].slice.call(arguments);return l(((n={})[r]=function(){return {match:function(n){var r={},e=function(n,t){r[n]=t;};return f(t,a).forEach(function(n){return e(n,void 0)}),{matched:t.some(function(t){return c(t,n,e)}),selections:r}},getSelectionKeys:function(){return f(t,a)},matcherType:"or"}},n))}function d(n){var t;return (t={})[r]=function(){return {match:function(t){return {matched:Boolean(n(t))}}}},t}function b(){var n,t=[].slice.call(arguments),e="string"==typeof t[0]?t[0]:void 0,i=2===t.length?t[1]:"string"==typeof t[0]?void 0:t[0];return l(((n={})[r]=function(){return {match:function(n){var t,r=((t={})[null!=e?e:u]=n,t);return {matched:void 0===i||c(i,n,function(n,t){r[n]=t;}),selections:r}},getSelectionKeys:function(){return [null!=e?e:u].concat(void 0===i?[]:a(i))}}},n))}function w(n){return "number"==typeof n}function S(n){return "string"==typeof n}function j(n){return "bigint"==typeof n}l(d(function(n){return !0}));(function n(t){return Object.assign(l(t),{startsWith:function(r){return n(p(t,(e=r,d(function(n){return S(n)&&n.startsWith(e)}))));var e;},endsWith:function(r){return n(p(t,(e=r,d(function(n){return S(n)&&n.endsWith(e)}))));var e;},minLength:function(r){return n(p(t,function(n){return d(function(t){return S(t)&&t.length>=n})}(r)))},maxLength:function(r){return n(p(t,function(n){return d(function(t){return S(t)&&t.length<=n})}(r)))},includes:function(r){return n(p(t,(e=r,d(function(n){return S(n)&&n.includes(e)}))));var e;},regex:function(r){return n(p(t,(e=r,d(function(n){return S(n)&&Boolean(n.match(e))}))));var e;}})})(d(S));(function n(t){return Object.assign(l(t),{between:function(r,e){return n(p(t,function(n,t){return d(function(r){return w(r)&&n<=r&&t>=r})}(r,e)))},lt:function(r){return n(p(t,function(n){return d(function(t){return w(t)&&t<n})}(r)))},gt:function(r){return n(p(t,function(n){return d(function(t){return w(t)&&t>n})}(r)))},lte:function(r){return n(p(t,function(n){return d(function(t){return w(t)&&t<=n})}(r)))},gte:function(r){return n(p(t,function(n){return d(function(t){return w(t)&&t>=n})}(r)))},int:function(){return n(p(t,d(function(n){return w(n)&&Number.isInteger(n)})))},finite:function(){return n(p(t,d(function(n){return w(n)&&Number.isFinite(n)})))},positive:function(){return n(p(t,d(function(n){return w(n)&&n>0})))},negative:function(){return n(p(t,d(function(n){return w(n)&&n<0})))}})})(d(w));(function n(t){return Object.assign(l(t),{between:function(r,e){return n(p(t,function(n,t){return d(function(r){return j(r)&&n<=r&&t>=r})}(r,e)))},lt:function(r){return n(p(t,function(n){return d(function(t){return j(t)&&t<n})}(r)))},gt:function(r){return n(p(t,function(n){return d(function(t){return j(t)&&t>n})}(r)))},lte:function(r){return n(p(t,function(n){return d(function(t){return j(t)&&t<=n})}(r)))},gte:function(r){return n(p(t,function(n){return d(function(t){return j(t)&&t>=n})}(r)))},positive:function(){return n(p(t,d(function(n){return j(n)&&n>0})))},negative:function(){return n(p(t,d(function(n){return j(n)&&n<0})))}})})(d(j));l(d(function(n){return "boolean"==typeof n}));l(d(function(n){return "symbol"==typeof n}));l(d(function(n){return null==n}));var I={matched:!1,value:void 0},_=/*#__PURE__*/function(){function n(n,t){this.input=void 0,this.state=void 0,this.input=n,this.state=t;}var t=n.prototype;return t.with=function(){var t=this,r=[].slice.call(arguments);if(this.state.matched)return this;var e=r[r.length-1],i=[r[0]],o=void 0;3===r.length&&"function"==typeof r[1]?o=r[1]:r.length>2&&i.push.apply(i,r.slice(1,r.length-1));var a=!1,f={},s=function(n,t){a=!0,f[n]=t;},l=!i.some(function(n){return c(n,t.input,s)})||o&&!Boolean(o(this.input))?I:{matched:!0,value:e(a?u in f?f[u]:f:this.input,this.input)};return new n(this.input,l)},t.when=function(t,r){if(this.state.matched)return this;var e=Boolean(t(this.input));return new n(this.input,e?{matched:!0,value:r(this.input,this.input)}:I)},t.otherwise=function(n){return this.state.matched?this.state.value:n(this.input)},t.exhaustive=function(){if(this.state.matched)return this.state.value;var n;try{n=JSON.stringify(this.input);}catch(t){n=this.input;}throw new Error("Pattern matching error: no pattern matches value "+n)},t.run=function(){return this.exhaustive()},t.returnType=function(){return this},n}();match = function(n){return new _(n,I)};
|
|
53
|
+
|
|
54
|
+
const RULE_NAME$2 = "ensure-use-callback-has-non-empty-deps";
|
|
55
|
+
var ensureUseCallbackHasNonEmptyDeps = createRule({
|
|
56
|
+
name: RULE_NAME$2,
|
|
57
|
+
meta: {
|
|
58
|
+
type: "problem",
|
|
59
|
+
docs: {
|
|
60
|
+
description: "enforce `useCallback` has non-empty dependencies array",
|
|
61
|
+
requiresTypeChecking: false
|
|
62
|
+
},
|
|
63
|
+
schema: [],
|
|
64
|
+
messages: {
|
|
65
|
+
ENSURE_USE_CALLBACK_HAS_NON_EMPTY_DEPS: "`useCallback` should have a non-empty dependencies array"
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
defaultOptions: [],
|
|
69
|
+
create (context) {
|
|
70
|
+
const alias = shared.parseSchema(shared.ESLintSettingsSchema, context.settings).eslintReact?.reactHooks?.alias?.useCallback ?? [];
|
|
71
|
+
const pragma = jsx.getPragmaFromContext(context);
|
|
72
|
+
return {
|
|
73
|
+
CallExpression (node) {
|
|
74
|
+
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
75
|
+
if (!core.isReactHookCall(node)) return;
|
|
76
|
+
if (!core.isUseCallbackCall(node, context, pragma) && !alias.some(tools.F.flip(core.isReactHookCallWithNameLoose)(node))) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const [_, deps] = node.arguments;
|
|
80
|
+
if (!deps) {
|
|
81
|
+
context.report({
|
|
82
|
+
messageId: "ENSURE_USE_CALLBACK_HAS_NON_EMPTY_DEPS",
|
|
83
|
+
node
|
|
84
|
+
});
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const maybeDescriptor = tools.F.pipe(match(deps).with({
|
|
88
|
+
type: ast.NodeType.ArrayExpression
|
|
89
|
+
}, tools.O.some).with({
|
|
90
|
+
type: ast.NodeType.Identifier
|
|
91
|
+
}, (n)=>{
|
|
92
|
+
return tools.F.pipe(_var.findVariable(n.name, initialScope), tools.O.flatMap(_var.getVariableInit(0)), tools.O.filter(ast.is(ast.NodeType.ArrayExpression)));
|
|
93
|
+
}).otherwise(tools.O.none), tools.O.filter((x)=>x.elements.length === 0), tools.O.map(()=>({
|
|
94
|
+
node,
|
|
95
|
+
messageId: "ENSURE_USE_CALLBACK_HAS_NON_EMPTY_DEPS"
|
|
96
|
+
})));
|
|
97
|
+
tools.O.map(maybeDescriptor, context.report);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const RULE_NAME$1 = "ensure-use-memo-has-non-empty-deps";
|
|
104
|
+
var ensureUseMemoHasNonEmptyDeps = createRule({
|
|
105
|
+
name: RULE_NAME$1,
|
|
106
|
+
meta: {
|
|
107
|
+
type: "problem",
|
|
108
|
+
docs: {
|
|
109
|
+
description: "enforce `useMemo` has non-empty dependencies array",
|
|
110
|
+
requiresTypeChecking: false
|
|
111
|
+
},
|
|
112
|
+
schema: [],
|
|
113
|
+
messages: {
|
|
114
|
+
ENSURE_USE_MEMO_HAS_NON_EMPTY_DEPS: "`useMemo` should have a non-empty dependencies array"
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
defaultOptions: [],
|
|
118
|
+
create (context) {
|
|
119
|
+
const alias = shared.parseSchema(shared.ESLintSettingsSchema, context.settings).eslintReact?.reactHooks?.alias?.useMemo ?? [];
|
|
120
|
+
const pragma = jsx.getPragmaFromContext(context);
|
|
121
|
+
return {
|
|
122
|
+
CallExpression (node) {
|
|
123
|
+
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
124
|
+
if (!core.isReactHookCall(node)) return;
|
|
125
|
+
if (!core.isUseMemoCall(node, context, pragma) && !alias.some(tools.F.flip(core.isReactHookCallWithNameLoose)(node))) return;
|
|
126
|
+
const [_, deps] = node.arguments;
|
|
127
|
+
if (!deps) {
|
|
128
|
+
context.report({
|
|
129
|
+
messageId: "ENSURE_USE_MEMO_HAS_NON_EMPTY_DEPS",
|
|
130
|
+
node
|
|
131
|
+
});
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const maybeDescriptor = tools.F.pipe(match(deps).with({
|
|
135
|
+
type: ast.NodeType.ArrayExpression
|
|
136
|
+
}, tools.O.some).with({
|
|
137
|
+
type: ast.NodeType.Identifier
|
|
138
|
+
}, (n)=>{
|
|
139
|
+
return tools.F.pipe(_var.findVariable(n.name, initialScope), tools.O.flatMap(_var.getVariableInit(0)), tools.O.filter(ast.is(ast.NodeType.ArrayExpression)));
|
|
140
|
+
}).otherwise(tools.O.none), tools.O.filter((x)=>x.elements.length === 0), tools.O.map(()=>({
|
|
141
|
+
node,
|
|
142
|
+
messageId: "ENSURE_USE_MEMO_HAS_NON_EMPTY_DEPS"
|
|
143
|
+
})));
|
|
144
|
+
tools.O.map(maybeDescriptor, context.report);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
// Ported from https://github.com/jsx-eslint/eslint-plugin-react/pull/3579/commits/ebb739a0fe99a2ee77055870bfda9f67a2691374
|
|
151
|
+
const RULE_NAME = "prefer-use-state-lazy-initialization";
|
|
152
|
+
// variables should be defined here
|
|
153
|
+
const ALLOW_LIST = Object.freeze([
|
|
154
|
+
"Boolean",
|
|
155
|
+
"String",
|
|
156
|
+
"Number"
|
|
157
|
+
]);
|
|
158
|
+
// rule takes inspiration from https://github.com/facebook/react/issues/26520
|
|
159
|
+
var preferUseStateLazyInitialization = createRule({
|
|
160
|
+
name: RULE_NAME,
|
|
161
|
+
meta: {
|
|
162
|
+
type: "problem",
|
|
163
|
+
docs: {
|
|
164
|
+
description: "disallow function calls in `useState` that aren't wrapped in an initializer function",
|
|
165
|
+
requiresTypeChecking: false
|
|
166
|
+
},
|
|
167
|
+
schema: [],
|
|
168
|
+
messages: {
|
|
169
|
+
PREFER_USE_STATE_LAZY_INITIALIZATION: "To prevent re-computation, consider using lazy initial state for useState calls that involve function calls. Ex: `useState(() => getValue())`"
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
defaultOptions: [],
|
|
173
|
+
create (context) {
|
|
174
|
+
const alias = shared.parseSchema(shared.ESLintSettingsSchema, context.settings).eslintReact?.reactHooks?.alias?.useState ?? [];
|
|
175
|
+
const pragma = jsx.getPragmaFromContext(context);
|
|
176
|
+
return {
|
|
177
|
+
CallExpression (node) {
|
|
178
|
+
if (!core.isReactHookCall(node)) return;
|
|
179
|
+
if (!core.isUseStateCall(node, context, pragma) && !alias.some(tools.F.flip(core.isReactHookCallWithNameLoose)(node))) return;
|
|
180
|
+
const [useStateInput] = node.arguments;
|
|
181
|
+
if (!useStateInput) return;
|
|
182
|
+
const nestedCallExpressions = ast.getNestedCallExpressions(useStateInput);
|
|
183
|
+
const hasFunctionCall = nestedCallExpressions.some((n)=>{
|
|
184
|
+
return "name" in n.callee && !ALLOW_LIST.includes(n.callee.name);
|
|
185
|
+
});
|
|
186
|
+
if (!hasFunctionCall) return;
|
|
187
|
+
context.report({
|
|
188
|
+
node: useStateInput,
|
|
189
|
+
messageId: "PREFER_USE_STATE_LAZY_INITIALIZATION"
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
// Workaround for @typescript-eslint/utils's TS2742 error.
|
|
197
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
198
|
+
const meta = {
|
|
199
|
+
name,
|
|
200
|
+
version
|
|
201
|
+
};
|
|
202
|
+
const rules = {
|
|
203
|
+
"ensure-custom-hooks-using-other-hooks": ensureCustomHooksUsingOtherHooks,
|
|
204
|
+
"ensure-use-callback-has-non-empty-deps": ensureUseCallbackHasNonEmptyDeps,
|
|
205
|
+
"ensure-use-memo-has-non-empty-deps": ensureUseMemoHasNonEmptyDeps,
|
|
206
|
+
"prefer-use-state-lazy-initialization": preferUseStateLazyInitialization
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
exports.meta = meta;
|
|
210
|
+
exports.rules = rules;
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
+
|
|
3
|
+
declare const meta: {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly version: string;
|
|
6
|
+
};
|
|
7
|
+
declare const rules: {
|
|
8
|
+
readonly "ensure-custom-hooks-using-other-hooks": ESLintUtils.RuleModule<"ENSURE_CUSTOM_HOOKS_USING_OTHER_HOOKS", [], ESLintUtils.RuleListener>;
|
|
9
|
+
readonly "ensure-use-callback-has-non-empty-deps": ESLintUtils.RuleModule<"ENSURE_USE_CALLBACK_HAS_NON_EMPTY_DEPS", [], ESLintUtils.RuleListener>;
|
|
10
|
+
readonly "ensure-use-memo-has-non-empty-deps": ESLintUtils.RuleModule<"ENSURE_USE_MEMO_HAS_NON_EMPTY_DEPS", [], ESLintUtils.RuleListener>;
|
|
11
|
+
readonly "prefer-use-state-lazy-initialization": ESLintUtils.RuleModule<"PREFER_USE_STATE_LAZY_INITIALIZATION", [], ESLintUtils.RuleListener>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export { meta, rules };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
+
|
|
3
|
+
declare const meta: {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly version: string;
|
|
6
|
+
};
|
|
7
|
+
declare const rules: {
|
|
8
|
+
readonly "ensure-custom-hooks-using-other-hooks": ESLintUtils.RuleModule<"ENSURE_CUSTOM_HOOKS_USING_OTHER_HOOKS", [], ESLintUtils.RuleListener>;
|
|
9
|
+
readonly "ensure-use-callback-has-non-empty-deps": ESLintUtils.RuleModule<"ENSURE_USE_CALLBACK_HAS_NON_EMPTY_DEPS", [], ESLintUtils.RuleListener>;
|
|
10
|
+
readonly "ensure-use-memo-has-non-empty-deps": ESLintUtils.RuleModule<"ENSURE_USE_MEMO_HAS_NON_EMPTY_DEPS", [], ESLintUtils.RuleListener>;
|
|
11
|
+
readonly "prefer-use-state-lazy-initialization": ESLintUtils.RuleModule<"PREFER_USE_STATE_LAZY_INITIALIZATION", [], ESLintUtils.RuleListener>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export { meta, rules };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@eslint-react/core');
|
|
4
|
+
var shared = require('@eslint-react/shared');
|
|
5
|
+
var ast = require('@eslint-react/ast');
|
|
6
|
+
var jsx = require('@eslint-react/jsx');
|
|
7
|
+
var tools = require('@eslint-react/tools');
|
|
8
|
+
var _var = require('@eslint-react/var');
|
|
9
|
+
|
|
10
|
+
var name = "eslint-plugin-react-hooks-extra";
|
|
11
|
+
var version = "1.5.0-beta.0";
|
|
12
|
+
|
|
13
|
+
const createRule = shared.createRuleForPlugin("hooks-extra");
|
|
14
|
+
|
|
15
|
+
const RULE_NAME$3 = "ensure-custom-hooks-using-other-hooks";
|
|
16
|
+
var ensureCustomHooksUsingOtherHooks = createRule({
|
|
17
|
+
name: RULE_NAME$3,
|
|
18
|
+
meta: {
|
|
19
|
+
type: "problem",
|
|
20
|
+
docs: {
|
|
21
|
+
description: "enforce custom hooks using other hooks",
|
|
22
|
+
requiresTypeChecking: false
|
|
23
|
+
},
|
|
24
|
+
schema: [],
|
|
25
|
+
messages: {
|
|
26
|
+
ENSURE_CUSTOM_HOOKS_USING_OTHER_HOOKS: "Custom hooks {{name}} should use other hooks"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
defaultOptions: [],
|
|
30
|
+
create (context) {
|
|
31
|
+
const { ctx, listeners } = core.useHookCollector();
|
|
32
|
+
return {
|
|
33
|
+
...listeners,
|
|
34
|
+
"Program:exit" (node) {
|
|
35
|
+
const allHooks = ctx.getAllHooks(node);
|
|
36
|
+
for (const { name, hookCalls, node } of allHooks.values()){
|
|
37
|
+
if (hookCalls.length > 0) continue;
|
|
38
|
+
context.report({
|
|
39
|
+
data: {
|
|
40
|
+
name: name.value
|
|
41
|
+
},
|
|
42
|
+
messageId: "ENSURE_CUSTOM_HOOKS_USING_OTHER_HOOKS",
|
|
43
|
+
node
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
var match;
|
|
52
|
+
function n(n,t){(null==t||t>n.length)&&(t=n.length);for(var r=0,e=new Array(t);r<t;r++)e[r]=n[r];return e}function t(t,r){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(e)return (e=e.call(t)).next.bind(e);if(Array.isArray(t)||(e=function(t,r){if(t){if("string"==typeof t)return n(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);return "Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?n(t,r):void 0}}(t))||r&&t&&"number"==typeof t.length){e&&(t=e);var u=0;return function(){return u>=t.length?{done:!0}:{done:!1,value:t[u++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var r=Symbol.for("@ts-pattern/matcher"),e=Symbol.for("@ts-pattern/isVariadic"),u="@ts-pattern/anonymous-select-key",i=function(n){return Boolean(n&&"object"==typeof n)},o=function(n){return n&&!!n[r]},c=function n(u,c,a){if(o(u)){var f=u[r]().match(c),s=f.matched,l=f.selections;return s&&l&&Object.keys(l).forEach(function(n){return a(n,l[n])}),s}if(i(u)){if(!i(c))return !1;if(Array.isArray(u)){if(!Array.isArray(c))return !1;for(var h,v=[],g=[],m=[],p=t(u.keys());!(h=p()).done;){var y=u[h.value];o(y)&&y[e]?m.push(y):m.length?g.push(y):v.push(y);}if(m.length){if(m.length>1)throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");if(c.length<v.length+g.length)return !1;var d=c.slice(0,v.length),b=0===g.length?[]:c.slice(-g.length),w=c.slice(v.length,0===g.length?Infinity:-g.length);return v.every(function(t,r){return n(t,d[r],a)})&&g.every(function(t,r){return n(t,b[r],a)})&&(0===m.length||n(m[0],w,a))}return u.length===c.length&&u.every(function(t,r){return n(t,c[r],a)})}return Object.keys(u).every(function(t){var e,i=u[t];return (t in c||o(e=i)&&"optional"===e[r]().matcherType)&&n(i,c[t],a)})}return Object.is(c,u)},a=function n(t){var e,u,c;return i(t)?o(t)?null!=(e=null==(u=(c=t[r]()).getSelectionKeys)?void 0:u.call(c))?e:[]:Array.isArray(t)?f(t,n):f(Object.values(t),n):[]},f=function(n,t){return n.reduce(function(n,r){return n.concat(t(r))},[])};function l(n){return Object.assign(n,{optional:function(){return v(n)},and:function(t){return p(n,t)},or:function(t){return y(n,t)},select:function(t){return void 0===t?b(n):b(t,n)}})}function v(n){var t;return l(((t={})[r]=function(){return {match:function(t){var r={},e=function(n,t){r[n]=t;};return void 0===t?(a(n).forEach(function(n){return e(n,void 0)}),{matched:!0,selections:r}):{matched:c(n,t,e),selections:r}},getSelectionKeys:function(){return a(n)},matcherType:"optional"}},t))}function p(){var n,t=[].slice.call(arguments);return l(((n={})[r]=function(){return {match:function(n){var r={},e=function(n,t){r[n]=t;};return {matched:t.every(function(t){return c(t,n,e)}),selections:r}},getSelectionKeys:function(){return f(t,a)},matcherType:"and"}},n))}function y(){var n,t=[].slice.call(arguments);return l(((n={})[r]=function(){return {match:function(n){var r={},e=function(n,t){r[n]=t;};return f(t,a).forEach(function(n){return e(n,void 0)}),{matched:t.some(function(t){return c(t,n,e)}),selections:r}},getSelectionKeys:function(){return f(t,a)},matcherType:"or"}},n))}function d(n){var t;return (t={})[r]=function(){return {match:function(t){return {matched:Boolean(n(t))}}}},t}function b(){var n,t=[].slice.call(arguments),e="string"==typeof t[0]?t[0]:void 0,i=2===t.length?t[1]:"string"==typeof t[0]?void 0:t[0];return l(((n={})[r]=function(){return {match:function(n){var t,r=((t={})[null!=e?e:u]=n,t);return {matched:void 0===i||c(i,n,function(n,t){r[n]=t;}),selections:r}},getSelectionKeys:function(){return [null!=e?e:u].concat(void 0===i?[]:a(i))}}},n))}function w(n){return "number"==typeof n}function S(n){return "string"==typeof n}function j(n){return "bigint"==typeof n}l(d(function(n){return !0}));(function n(t){return Object.assign(l(t),{startsWith:function(r){return n(p(t,(e=r,d(function(n){return S(n)&&n.startsWith(e)}))));var e;},endsWith:function(r){return n(p(t,(e=r,d(function(n){return S(n)&&n.endsWith(e)}))));var e;},minLength:function(r){return n(p(t,function(n){return d(function(t){return S(t)&&t.length>=n})}(r)))},maxLength:function(r){return n(p(t,function(n){return d(function(t){return S(t)&&t.length<=n})}(r)))},includes:function(r){return n(p(t,(e=r,d(function(n){return S(n)&&n.includes(e)}))));var e;},regex:function(r){return n(p(t,(e=r,d(function(n){return S(n)&&Boolean(n.match(e))}))));var e;}})})(d(S));(function n(t){return Object.assign(l(t),{between:function(r,e){return n(p(t,function(n,t){return d(function(r){return w(r)&&n<=r&&t>=r})}(r,e)))},lt:function(r){return n(p(t,function(n){return d(function(t){return w(t)&&t<n})}(r)))},gt:function(r){return n(p(t,function(n){return d(function(t){return w(t)&&t>n})}(r)))},lte:function(r){return n(p(t,function(n){return d(function(t){return w(t)&&t<=n})}(r)))},gte:function(r){return n(p(t,function(n){return d(function(t){return w(t)&&t>=n})}(r)))},int:function(){return n(p(t,d(function(n){return w(n)&&Number.isInteger(n)})))},finite:function(){return n(p(t,d(function(n){return w(n)&&Number.isFinite(n)})))},positive:function(){return n(p(t,d(function(n){return w(n)&&n>0})))},negative:function(){return n(p(t,d(function(n){return w(n)&&n<0})))}})})(d(w));(function n(t){return Object.assign(l(t),{between:function(r,e){return n(p(t,function(n,t){return d(function(r){return j(r)&&n<=r&&t>=r})}(r,e)))},lt:function(r){return n(p(t,function(n){return d(function(t){return j(t)&&t<n})}(r)))},gt:function(r){return n(p(t,function(n){return d(function(t){return j(t)&&t>n})}(r)))},lte:function(r){return n(p(t,function(n){return d(function(t){return j(t)&&t<=n})}(r)))},gte:function(r){return n(p(t,function(n){return d(function(t){return j(t)&&t>=n})}(r)))},positive:function(){return n(p(t,d(function(n){return j(n)&&n>0})))},negative:function(){return n(p(t,d(function(n){return j(n)&&n<0})))}})})(d(j));l(d(function(n){return "boolean"==typeof n}));l(d(function(n){return "symbol"==typeof n}));l(d(function(n){return null==n}));var I={matched:!1,value:void 0},_=/*#__PURE__*/function(){function n(n,t){this.input=void 0,this.state=void 0,this.input=n,this.state=t;}var t=n.prototype;return t.with=function(){var t=this,r=[].slice.call(arguments);if(this.state.matched)return this;var e=r[r.length-1],i=[r[0]],o=void 0;3===r.length&&"function"==typeof r[1]?o=r[1]:r.length>2&&i.push.apply(i,r.slice(1,r.length-1));var a=!1,f={},s=function(n,t){a=!0,f[n]=t;},l=!i.some(function(n){return c(n,t.input,s)})||o&&!Boolean(o(this.input))?I:{matched:!0,value:e(a?u in f?f[u]:f:this.input,this.input)};return new n(this.input,l)},t.when=function(t,r){if(this.state.matched)return this;var e=Boolean(t(this.input));return new n(this.input,e?{matched:!0,value:r(this.input,this.input)}:I)},t.otherwise=function(n){return this.state.matched?this.state.value:n(this.input)},t.exhaustive=function(){if(this.state.matched)return this.state.value;var n;try{n=JSON.stringify(this.input);}catch(t){n=this.input;}throw new Error("Pattern matching error: no pattern matches value "+n)},t.run=function(){return this.exhaustive()},t.returnType=function(){return this},n}();match = function(n){return new _(n,I)};
|
|
53
|
+
|
|
54
|
+
const RULE_NAME$2 = "ensure-use-callback-has-non-empty-deps";
|
|
55
|
+
var ensureUseCallbackHasNonEmptyDeps = createRule({
|
|
56
|
+
name: RULE_NAME$2,
|
|
57
|
+
meta: {
|
|
58
|
+
type: "problem",
|
|
59
|
+
docs: {
|
|
60
|
+
description: "enforce `useCallback` has non-empty dependencies array",
|
|
61
|
+
requiresTypeChecking: false
|
|
62
|
+
},
|
|
63
|
+
schema: [],
|
|
64
|
+
messages: {
|
|
65
|
+
ENSURE_USE_CALLBACK_HAS_NON_EMPTY_DEPS: "`useCallback` should have a non-empty dependencies array"
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
defaultOptions: [],
|
|
69
|
+
create (context) {
|
|
70
|
+
const alias = shared.parseSchema(shared.ESLintSettingsSchema, context.settings).eslintReact?.reactHooks?.alias?.useCallback ?? [];
|
|
71
|
+
const pragma = jsx.getPragmaFromContext(context);
|
|
72
|
+
return {
|
|
73
|
+
CallExpression (node) {
|
|
74
|
+
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
75
|
+
if (!core.isReactHookCall(node)) return;
|
|
76
|
+
if (!core.isUseCallbackCall(node, context, pragma) && !alias.some(tools.F.flip(core.isReactHookCallWithNameLoose)(node))) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const [_, deps] = node.arguments;
|
|
80
|
+
if (!deps) {
|
|
81
|
+
context.report({
|
|
82
|
+
messageId: "ENSURE_USE_CALLBACK_HAS_NON_EMPTY_DEPS",
|
|
83
|
+
node
|
|
84
|
+
});
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const maybeDescriptor = tools.F.pipe(match(deps).with({
|
|
88
|
+
type: ast.NodeType.ArrayExpression
|
|
89
|
+
}, tools.O.some).with({
|
|
90
|
+
type: ast.NodeType.Identifier
|
|
91
|
+
}, (n)=>{
|
|
92
|
+
return tools.F.pipe(_var.findVariable(n.name, initialScope), tools.O.flatMap(_var.getVariableInit(0)), tools.O.filter(ast.is(ast.NodeType.ArrayExpression)));
|
|
93
|
+
}).otherwise(tools.O.none), tools.O.filter((x)=>x.elements.length === 0), tools.O.map(()=>({
|
|
94
|
+
node,
|
|
95
|
+
messageId: "ENSURE_USE_CALLBACK_HAS_NON_EMPTY_DEPS"
|
|
96
|
+
})));
|
|
97
|
+
tools.O.map(maybeDescriptor, context.report);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const RULE_NAME$1 = "ensure-use-memo-has-non-empty-deps";
|
|
104
|
+
var ensureUseMemoHasNonEmptyDeps = createRule({
|
|
105
|
+
name: RULE_NAME$1,
|
|
106
|
+
meta: {
|
|
107
|
+
type: "problem",
|
|
108
|
+
docs: {
|
|
109
|
+
description: "enforce `useMemo` has non-empty dependencies array",
|
|
110
|
+
requiresTypeChecking: false
|
|
111
|
+
},
|
|
112
|
+
schema: [],
|
|
113
|
+
messages: {
|
|
114
|
+
ENSURE_USE_MEMO_HAS_NON_EMPTY_DEPS: "`useMemo` should have a non-empty dependencies array"
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
defaultOptions: [],
|
|
118
|
+
create (context) {
|
|
119
|
+
const alias = shared.parseSchema(shared.ESLintSettingsSchema, context.settings).eslintReact?.reactHooks?.alias?.useMemo ?? [];
|
|
120
|
+
const pragma = jsx.getPragmaFromContext(context);
|
|
121
|
+
return {
|
|
122
|
+
CallExpression (node) {
|
|
123
|
+
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
124
|
+
if (!core.isReactHookCall(node)) return;
|
|
125
|
+
if (!core.isUseMemoCall(node, context, pragma) && !alias.some(tools.F.flip(core.isReactHookCallWithNameLoose)(node))) return;
|
|
126
|
+
const [_, deps] = node.arguments;
|
|
127
|
+
if (!deps) {
|
|
128
|
+
context.report({
|
|
129
|
+
messageId: "ENSURE_USE_MEMO_HAS_NON_EMPTY_DEPS",
|
|
130
|
+
node
|
|
131
|
+
});
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const maybeDescriptor = tools.F.pipe(match(deps).with({
|
|
135
|
+
type: ast.NodeType.ArrayExpression
|
|
136
|
+
}, tools.O.some).with({
|
|
137
|
+
type: ast.NodeType.Identifier
|
|
138
|
+
}, (n)=>{
|
|
139
|
+
return tools.F.pipe(_var.findVariable(n.name, initialScope), tools.O.flatMap(_var.getVariableInit(0)), tools.O.filter(ast.is(ast.NodeType.ArrayExpression)));
|
|
140
|
+
}).otherwise(tools.O.none), tools.O.filter((x)=>x.elements.length === 0), tools.O.map(()=>({
|
|
141
|
+
node,
|
|
142
|
+
messageId: "ENSURE_USE_MEMO_HAS_NON_EMPTY_DEPS"
|
|
143
|
+
})));
|
|
144
|
+
tools.O.map(maybeDescriptor, context.report);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
// Ported from https://github.com/jsx-eslint/eslint-plugin-react/pull/3579/commits/ebb739a0fe99a2ee77055870bfda9f67a2691374
|
|
151
|
+
const RULE_NAME = "prefer-use-state-lazy-initialization";
|
|
152
|
+
// variables should be defined here
|
|
153
|
+
const ALLOW_LIST = Object.freeze([
|
|
154
|
+
"Boolean",
|
|
155
|
+
"String",
|
|
156
|
+
"Number"
|
|
157
|
+
]);
|
|
158
|
+
// rule takes inspiration from https://github.com/facebook/react/issues/26520
|
|
159
|
+
var preferUseStateLazyInitialization = createRule({
|
|
160
|
+
name: RULE_NAME,
|
|
161
|
+
meta: {
|
|
162
|
+
type: "problem",
|
|
163
|
+
docs: {
|
|
164
|
+
description: "disallow function calls in `useState` that aren't wrapped in an initializer function",
|
|
165
|
+
requiresTypeChecking: false
|
|
166
|
+
},
|
|
167
|
+
schema: [],
|
|
168
|
+
messages: {
|
|
169
|
+
PREFER_USE_STATE_LAZY_INITIALIZATION: "To prevent re-computation, consider using lazy initial state for useState calls that involve function calls. Ex: `useState(() => getValue())`"
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
defaultOptions: [],
|
|
173
|
+
create (context) {
|
|
174
|
+
const alias = shared.parseSchema(shared.ESLintSettingsSchema, context.settings).eslintReact?.reactHooks?.alias?.useState ?? [];
|
|
175
|
+
const pragma = jsx.getPragmaFromContext(context);
|
|
176
|
+
return {
|
|
177
|
+
CallExpression (node) {
|
|
178
|
+
if (!core.isReactHookCall(node)) return;
|
|
179
|
+
if (!core.isUseStateCall(node, context, pragma) && !alias.some(tools.F.flip(core.isReactHookCallWithNameLoose)(node))) return;
|
|
180
|
+
const [useStateInput] = node.arguments;
|
|
181
|
+
if (!useStateInput) return;
|
|
182
|
+
const nestedCallExpressions = ast.getNestedCallExpressions(useStateInput);
|
|
183
|
+
const hasFunctionCall = nestedCallExpressions.some((n)=>{
|
|
184
|
+
return "name" in n.callee && !ALLOW_LIST.includes(n.callee.name);
|
|
185
|
+
});
|
|
186
|
+
if (!hasFunctionCall) return;
|
|
187
|
+
context.report({
|
|
188
|
+
node: useStateInput,
|
|
189
|
+
messageId: "PREFER_USE_STATE_LAZY_INITIALIZATION"
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
// Workaround for @typescript-eslint/utils's TS2742 error.
|
|
197
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
198
|
+
const meta = {
|
|
199
|
+
name,
|
|
200
|
+
version
|
|
201
|
+
};
|
|
202
|
+
const rules = {
|
|
203
|
+
"ensure-custom-hooks-using-other-hooks": ensureCustomHooksUsingOtherHooks,
|
|
204
|
+
"ensure-use-callback-has-non-empty-deps": ensureUseCallbackHasNonEmptyDeps,
|
|
205
|
+
"ensure-use-memo-has-non-empty-deps": ensureUseMemoHasNonEmptyDeps,
|
|
206
|
+
"prefer-use-state-lazy-initialization": preferUseStateLazyInitialization
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
exports.meta = meta;
|
|
210
|
+
exports.rules = rules;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { useHookCollector, isReactHookCall, isUseCallbackCall, isReactHookCallWithNameLoose, isUseMemoCall, isUseStateCall } from '@eslint-react/core';
|
|
2
|
+
import { createRuleForPlugin, parseSchema, ESLintSettingsSchema } from '@eslint-react/shared';
|
|
3
|
+
import { NodeType, is, getNestedCallExpressions } from '@eslint-react/ast';
|
|
4
|
+
import { getPragmaFromContext } from '@eslint-react/jsx';
|
|
5
|
+
import { F, O } from '@eslint-react/tools';
|
|
6
|
+
import { findVariable, getVariableInit } from '@eslint-react/var';
|
|
7
|
+
|
|
8
|
+
var name = "eslint-plugin-react-hooks-extra";
|
|
9
|
+
var version = "1.5.0-beta.0";
|
|
10
|
+
|
|
11
|
+
const createRule = createRuleForPlugin("hooks-extra");
|
|
12
|
+
|
|
13
|
+
const RULE_NAME$3 = "ensure-custom-hooks-using-other-hooks";
|
|
14
|
+
var ensureCustomHooksUsingOtherHooks = createRule({
|
|
15
|
+
name: RULE_NAME$3,
|
|
16
|
+
meta: {
|
|
17
|
+
type: "problem",
|
|
18
|
+
docs: {
|
|
19
|
+
description: "enforce custom hooks using other hooks",
|
|
20
|
+
requiresTypeChecking: false
|
|
21
|
+
},
|
|
22
|
+
schema: [],
|
|
23
|
+
messages: {
|
|
24
|
+
ENSURE_CUSTOM_HOOKS_USING_OTHER_HOOKS: "Custom hooks {{name}} should use other hooks"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
defaultOptions: [],
|
|
28
|
+
create (context) {
|
|
29
|
+
const { ctx, listeners } = useHookCollector();
|
|
30
|
+
return {
|
|
31
|
+
...listeners,
|
|
32
|
+
"Program:exit" (node) {
|
|
33
|
+
const allHooks = ctx.getAllHooks(node);
|
|
34
|
+
for (const { name, hookCalls, node } of allHooks.values()){
|
|
35
|
+
if (hookCalls.length > 0) continue;
|
|
36
|
+
context.report({
|
|
37
|
+
data: {
|
|
38
|
+
name: name.value
|
|
39
|
+
},
|
|
40
|
+
messageId: "ENSURE_CUSTOM_HOOKS_USING_OTHER_HOOKS",
|
|
41
|
+
node
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
var match;
|
|
50
|
+
function n(n,t){(null==t||t>n.length)&&(t=n.length);for(var r=0,e=new Array(t);r<t;r++)e[r]=n[r];return e}function t(t,r){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(e)return (e=e.call(t)).next.bind(e);if(Array.isArray(t)||(e=function(t,r){if(t){if("string"==typeof t)return n(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);return "Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?n(t,r):void 0}}(t))||r&&t&&"number"==typeof t.length){e&&(t=e);var u=0;return function(){return u>=t.length?{done:!0}:{done:!1,value:t[u++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var r=Symbol.for("@ts-pattern/matcher"),e=Symbol.for("@ts-pattern/isVariadic"),u="@ts-pattern/anonymous-select-key",i=function(n){return Boolean(n&&"object"==typeof n)},o=function(n){return n&&!!n[r]},c=function n(u,c,a){if(o(u)){var f=u[r]().match(c),s=f.matched,l=f.selections;return s&&l&&Object.keys(l).forEach(function(n){return a(n,l[n])}),s}if(i(u)){if(!i(c))return !1;if(Array.isArray(u)){if(!Array.isArray(c))return !1;for(var h,v=[],g=[],m=[],p=t(u.keys());!(h=p()).done;){var y=u[h.value];o(y)&&y[e]?m.push(y):m.length?g.push(y):v.push(y);}if(m.length){if(m.length>1)throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");if(c.length<v.length+g.length)return !1;var d=c.slice(0,v.length),b=0===g.length?[]:c.slice(-g.length),w=c.slice(v.length,0===g.length?Infinity:-g.length);return v.every(function(t,r){return n(t,d[r],a)})&&g.every(function(t,r){return n(t,b[r],a)})&&(0===m.length||n(m[0],w,a))}return u.length===c.length&&u.every(function(t,r){return n(t,c[r],a)})}return Object.keys(u).every(function(t){var e,i=u[t];return (t in c||o(e=i)&&"optional"===e[r]().matcherType)&&n(i,c[t],a)})}return Object.is(c,u)},a=function n(t){var e,u,c;return i(t)?o(t)?null!=(e=null==(u=(c=t[r]()).getSelectionKeys)?void 0:u.call(c))?e:[]:Array.isArray(t)?f(t,n):f(Object.values(t),n):[]},f=function(n,t){return n.reduce(function(n,r){return n.concat(t(r))},[])};function l(n){return Object.assign(n,{optional:function(){return v(n)},and:function(t){return p(n,t)},or:function(t){return y(n,t)},select:function(t){return void 0===t?b(n):b(t,n)}})}function v(n){var t;return l(((t={})[r]=function(){return {match:function(t){var r={},e=function(n,t){r[n]=t;};return void 0===t?(a(n).forEach(function(n){return e(n,void 0)}),{matched:!0,selections:r}):{matched:c(n,t,e),selections:r}},getSelectionKeys:function(){return a(n)},matcherType:"optional"}},t))}function p(){var n,t=[].slice.call(arguments);return l(((n={})[r]=function(){return {match:function(n){var r={},e=function(n,t){r[n]=t;};return {matched:t.every(function(t){return c(t,n,e)}),selections:r}},getSelectionKeys:function(){return f(t,a)},matcherType:"and"}},n))}function y(){var n,t=[].slice.call(arguments);return l(((n={})[r]=function(){return {match:function(n){var r={},e=function(n,t){r[n]=t;};return f(t,a).forEach(function(n){return e(n,void 0)}),{matched:t.some(function(t){return c(t,n,e)}),selections:r}},getSelectionKeys:function(){return f(t,a)},matcherType:"or"}},n))}function d(n){var t;return (t={})[r]=function(){return {match:function(t){return {matched:Boolean(n(t))}}}},t}function b(){var n,t=[].slice.call(arguments),e="string"==typeof t[0]?t[0]:void 0,i=2===t.length?t[1]:"string"==typeof t[0]?void 0:t[0];return l(((n={})[r]=function(){return {match:function(n){var t,r=((t={})[null!=e?e:u]=n,t);return {matched:void 0===i||c(i,n,function(n,t){r[n]=t;}),selections:r}},getSelectionKeys:function(){return [null!=e?e:u].concat(void 0===i?[]:a(i))}}},n))}function w(n){return "number"==typeof n}function S(n){return "string"==typeof n}function j(n){return "bigint"==typeof n}l(d(function(n){return !0}));(function n(t){return Object.assign(l(t),{startsWith:function(r){return n(p(t,(e=r,d(function(n){return S(n)&&n.startsWith(e)}))));var e;},endsWith:function(r){return n(p(t,(e=r,d(function(n){return S(n)&&n.endsWith(e)}))));var e;},minLength:function(r){return n(p(t,function(n){return d(function(t){return S(t)&&t.length>=n})}(r)))},maxLength:function(r){return n(p(t,function(n){return d(function(t){return S(t)&&t.length<=n})}(r)))},includes:function(r){return n(p(t,(e=r,d(function(n){return S(n)&&n.includes(e)}))));var e;},regex:function(r){return n(p(t,(e=r,d(function(n){return S(n)&&Boolean(n.match(e))}))));var e;}})})(d(S));(function n(t){return Object.assign(l(t),{between:function(r,e){return n(p(t,function(n,t){return d(function(r){return w(r)&&n<=r&&t>=r})}(r,e)))},lt:function(r){return n(p(t,function(n){return d(function(t){return w(t)&&t<n})}(r)))},gt:function(r){return n(p(t,function(n){return d(function(t){return w(t)&&t>n})}(r)))},lte:function(r){return n(p(t,function(n){return d(function(t){return w(t)&&t<=n})}(r)))},gte:function(r){return n(p(t,function(n){return d(function(t){return w(t)&&t>=n})}(r)))},int:function(){return n(p(t,d(function(n){return w(n)&&Number.isInteger(n)})))},finite:function(){return n(p(t,d(function(n){return w(n)&&Number.isFinite(n)})))},positive:function(){return n(p(t,d(function(n){return w(n)&&n>0})))},negative:function(){return n(p(t,d(function(n){return w(n)&&n<0})))}})})(d(w));(function n(t){return Object.assign(l(t),{between:function(r,e){return n(p(t,function(n,t){return d(function(r){return j(r)&&n<=r&&t>=r})}(r,e)))},lt:function(r){return n(p(t,function(n){return d(function(t){return j(t)&&t<n})}(r)))},gt:function(r){return n(p(t,function(n){return d(function(t){return j(t)&&t>n})}(r)))},lte:function(r){return n(p(t,function(n){return d(function(t){return j(t)&&t<=n})}(r)))},gte:function(r){return n(p(t,function(n){return d(function(t){return j(t)&&t>=n})}(r)))},positive:function(){return n(p(t,d(function(n){return j(n)&&n>0})))},negative:function(){return n(p(t,d(function(n){return j(n)&&n<0})))}})})(d(j));l(d(function(n){return "boolean"==typeof n}));l(d(function(n){return "symbol"==typeof n}));l(d(function(n){return null==n}));var I={matched:!1,value:void 0},_=/*#__PURE__*/function(){function n(n,t){this.input=void 0,this.state=void 0,this.input=n,this.state=t;}var t=n.prototype;return t.with=function(){var t=this,r=[].slice.call(arguments);if(this.state.matched)return this;var e=r[r.length-1],i=[r[0]],o=void 0;3===r.length&&"function"==typeof r[1]?o=r[1]:r.length>2&&i.push.apply(i,r.slice(1,r.length-1));var a=!1,f={},s=function(n,t){a=!0,f[n]=t;},l=!i.some(function(n){return c(n,t.input,s)})||o&&!Boolean(o(this.input))?I:{matched:!0,value:e(a?u in f?f[u]:f:this.input,this.input)};return new n(this.input,l)},t.when=function(t,r){if(this.state.matched)return this;var e=Boolean(t(this.input));return new n(this.input,e?{matched:!0,value:r(this.input,this.input)}:I)},t.otherwise=function(n){return this.state.matched?this.state.value:n(this.input)},t.exhaustive=function(){if(this.state.matched)return this.state.value;var n;try{n=JSON.stringify(this.input);}catch(t){n=this.input;}throw new Error("Pattern matching error: no pattern matches value "+n)},t.run=function(){return this.exhaustive()},t.returnType=function(){return this},n}();match = function(n){return new _(n,I)};
|
|
51
|
+
|
|
52
|
+
const RULE_NAME$2 = "ensure-use-callback-has-non-empty-deps";
|
|
53
|
+
var ensureUseCallbackHasNonEmptyDeps = createRule({
|
|
54
|
+
name: RULE_NAME$2,
|
|
55
|
+
meta: {
|
|
56
|
+
type: "problem",
|
|
57
|
+
docs: {
|
|
58
|
+
description: "enforce `useCallback` has non-empty dependencies array",
|
|
59
|
+
requiresTypeChecking: false
|
|
60
|
+
},
|
|
61
|
+
schema: [],
|
|
62
|
+
messages: {
|
|
63
|
+
ENSURE_USE_CALLBACK_HAS_NON_EMPTY_DEPS: "`useCallback` should have a non-empty dependencies array"
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
defaultOptions: [],
|
|
67
|
+
create (context) {
|
|
68
|
+
const alias = parseSchema(ESLintSettingsSchema, context.settings).eslintReact?.reactHooks?.alias?.useCallback ?? [];
|
|
69
|
+
const pragma = getPragmaFromContext(context);
|
|
70
|
+
return {
|
|
71
|
+
CallExpression (node) {
|
|
72
|
+
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
73
|
+
if (!isReactHookCall(node)) return;
|
|
74
|
+
if (!isUseCallbackCall(node, context, pragma) && !alias.some(F.flip(isReactHookCallWithNameLoose)(node))) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const [_, deps] = node.arguments;
|
|
78
|
+
if (!deps) {
|
|
79
|
+
context.report({
|
|
80
|
+
messageId: "ENSURE_USE_CALLBACK_HAS_NON_EMPTY_DEPS",
|
|
81
|
+
node
|
|
82
|
+
});
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const maybeDescriptor = F.pipe(match(deps).with({
|
|
86
|
+
type: NodeType.ArrayExpression
|
|
87
|
+
}, O.some).with({
|
|
88
|
+
type: NodeType.Identifier
|
|
89
|
+
}, (n)=>{
|
|
90
|
+
return F.pipe(findVariable(n.name, initialScope), O.flatMap(getVariableInit(0)), O.filter(is(NodeType.ArrayExpression)));
|
|
91
|
+
}).otherwise(O.none), O.filter((x)=>x.elements.length === 0), O.map(()=>({
|
|
92
|
+
node,
|
|
93
|
+
messageId: "ENSURE_USE_CALLBACK_HAS_NON_EMPTY_DEPS"
|
|
94
|
+
})));
|
|
95
|
+
O.map(maybeDescriptor, context.report);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const RULE_NAME$1 = "ensure-use-memo-has-non-empty-deps";
|
|
102
|
+
var ensureUseMemoHasNonEmptyDeps = createRule({
|
|
103
|
+
name: RULE_NAME$1,
|
|
104
|
+
meta: {
|
|
105
|
+
type: "problem",
|
|
106
|
+
docs: {
|
|
107
|
+
description: "enforce `useMemo` has non-empty dependencies array",
|
|
108
|
+
requiresTypeChecking: false
|
|
109
|
+
},
|
|
110
|
+
schema: [],
|
|
111
|
+
messages: {
|
|
112
|
+
ENSURE_USE_MEMO_HAS_NON_EMPTY_DEPS: "`useMemo` should have a non-empty dependencies array"
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
defaultOptions: [],
|
|
116
|
+
create (context) {
|
|
117
|
+
const alias = parseSchema(ESLintSettingsSchema, context.settings).eslintReact?.reactHooks?.alias?.useMemo ?? [];
|
|
118
|
+
const pragma = getPragmaFromContext(context);
|
|
119
|
+
return {
|
|
120
|
+
CallExpression (node) {
|
|
121
|
+
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
122
|
+
if (!isReactHookCall(node)) return;
|
|
123
|
+
if (!isUseMemoCall(node, context, pragma) && !alias.some(F.flip(isReactHookCallWithNameLoose)(node))) return;
|
|
124
|
+
const [_, deps] = node.arguments;
|
|
125
|
+
if (!deps) {
|
|
126
|
+
context.report({
|
|
127
|
+
messageId: "ENSURE_USE_MEMO_HAS_NON_EMPTY_DEPS",
|
|
128
|
+
node
|
|
129
|
+
});
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const maybeDescriptor = F.pipe(match(deps).with({
|
|
133
|
+
type: NodeType.ArrayExpression
|
|
134
|
+
}, O.some).with({
|
|
135
|
+
type: NodeType.Identifier
|
|
136
|
+
}, (n)=>{
|
|
137
|
+
return F.pipe(findVariable(n.name, initialScope), O.flatMap(getVariableInit(0)), O.filter(is(NodeType.ArrayExpression)));
|
|
138
|
+
}).otherwise(O.none), O.filter((x)=>x.elements.length === 0), O.map(()=>({
|
|
139
|
+
node,
|
|
140
|
+
messageId: "ENSURE_USE_MEMO_HAS_NON_EMPTY_DEPS"
|
|
141
|
+
})));
|
|
142
|
+
O.map(maybeDescriptor, context.report);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// Ported from https://github.com/jsx-eslint/eslint-plugin-react/pull/3579/commits/ebb739a0fe99a2ee77055870bfda9f67a2691374
|
|
149
|
+
const RULE_NAME = "prefer-use-state-lazy-initialization";
|
|
150
|
+
// variables should be defined here
|
|
151
|
+
const ALLOW_LIST = Object.freeze([
|
|
152
|
+
"Boolean",
|
|
153
|
+
"String",
|
|
154
|
+
"Number"
|
|
155
|
+
]);
|
|
156
|
+
// rule takes inspiration from https://github.com/facebook/react/issues/26520
|
|
157
|
+
var preferUseStateLazyInitialization = createRule({
|
|
158
|
+
name: RULE_NAME,
|
|
159
|
+
meta: {
|
|
160
|
+
type: "problem",
|
|
161
|
+
docs: {
|
|
162
|
+
description: "disallow function calls in `useState` that aren't wrapped in an initializer function",
|
|
163
|
+
requiresTypeChecking: false
|
|
164
|
+
},
|
|
165
|
+
schema: [],
|
|
166
|
+
messages: {
|
|
167
|
+
PREFER_USE_STATE_LAZY_INITIALIZATION: "To prevent re-computation, consider using lazy initial state for useState calls that involve function calls. Ex: `useState(() => getValue())`"
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
defaultOptions: [],
|
|
171
|
+
create (context) {
|
|
172
|
+
const alias = parseSchema(ESLintSettingsSchema, context.settings).eslintReact?.reactHooks?.alias?.useState ?? [];
|
|
173
|
+
const pragma = getPragmaFromContext(context);
|
|
174
|
+
return {
|
|
175
|
+
CallExpression (node) {
|
|
176
|
+
if (!isReactHookCall(node)) return;
|
|
177
|
+
if (!isUseStateCall(node, context, pragma) && !alias.some(F.flip(isReactHookCallWithNameLoose)(node))) return;
|
|
178
|
+
const [useStateInput] = node.arguments;
|
|
179
|
+
if (!useStateInput) return;
|
|
180
|
+
const nestedCallExpressions = getNestedCallExpressions(useStateInput);
|
|
181
|
+
const hasFunctionCall = nestedCallExpressions.some((n)=>{
|
|
182
|
+
return "name" in n.callee && !ALLOW_LIST.includes(n.callee.name);
|
|
183
|
+
});
|
|
184
|
+
if (!hasFunctionCall) return;
|
|
185
|
+
context.report({
|
|
186
|
+
node: useStateInput,
|
|
187
|
+
messageId: "PREFER_USE_STATE_LAZY_INITIALIZATION"
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// Workaround for @typescript-eslint/utils's TS2742 error.
|
|
195
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
196
|
+
const meta = {
|
|
197
|
+
name,
|
|
198
|
+
version
|
|
199
|
+
};
|
|
200
|
+
const rules = {
|
|
201
|
+
"ensure-custom-hooks-using-other-hooks": ensureCustomHooksUsingOtherHooks,
|
|
202
|
+
"ensure-use-callback-has-non-empty-deps": ensureUseCallbackHasNonEmptyDeps,
|
|
203
|
+
"ensure-use-memo-has-non-empty-deps": ensureUseMemoHasNonEmptyDeps,
|
|
204
|
+
"prefer-use-state-lazy-initialization": preferUseStateLazyInitialization
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
export { meta, rules };
|
package/package.json
CHANGED
|
@@ -1,12 +1,70 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-react-hooks-extra",
|
|
3
|
-
"version": "1.0.0",
|
|
4
|
-
"description": "",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
"version": "1.5.0-beta.0",
|
|
4
|
+
"description": "ESLint React's ESLint plugin for React Hooks related rules.",
|
|
5
|
+
"homepage": "https://github.com/rel1cx/eslint-react",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/rel1cx/eslint-react/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/Rel1cx/eslint-react",
|
|
12
|
+
"directory": "packages/plugins/eslint-plugin-react-hooks-extra"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "Eva1ent<let@ik.me>",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"import": {
|
|
20
|
+
"types": "./dist/index.d.mts",
|
|
21
|
+
"default": "./dist/index.mjs"
|
|
22
|
+
},
|
|
23
|
+
"require": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"./package.json": "./package.json"
|
|
29
|
+
},
|
|
30
|
+
"main": "dist/index.js",
|
|
31
|
+
"module": "dist/index.mjs",
|
|
32
|
+
"types": "dist/index.d.ts",
|
|
33
|
+
"files": [
|
|
34
|
+
"./dist",
|
|
35
|
+
"./package.json"
|
|
36
|
+
],
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@typescript-eslint/parser": ">=6.19.1",
|
|
39
|
+
"@typescript-eslint/scope-manager": ">=6.19.1",
|
|
40
|
+
"@typescript-eslint/type-utils": ">=6.19.1",
|
|
41
|
+
"@typescript-eslint/types": ">=6.19.1",
|
|
42
|
+
"@typescript-eslint/utils": ">=6.19.1",
|
|
43
|
+
"string-ts": "2.0.0",
|
|
44
|
+
"@eslint-react/ast": "1.5.0-beta.0",
|
|
45
|
+
"@eslint-react/core": "1.5.0-beta.0",
|
|
46
|
+
"@eslint-react/jsx": "1.5.0-beta.0",
|
|
47
|
+
"@eslint-react/shared": "1.5.0-beta.0",
|
|
48
|
+
"@eslint-react/tools": "1.5.0-beta.0",
|
|
49
|
+
"@eslint-react/types": "1.5.0-beta.0",
|
|
50
|
+
"@eslint-react/var": "1.5.0-beta.0"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"@typescript-eslint/parser": ">=6.19.1",
|
|
54
|
+
"eslint": ">=8.56.0",
|
|
55
|
+
"typescript": ">=5.3.3"
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"bun": ">=1.0.15",
|
|
59
|
+
"node": ">=18.18.0"
|
|
60
|
+
},
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public"
|
|
63
|
+
},
|
|
9
64
|
"scripts": {
|
|
10
|
-
"
|
|
65
|
+
"build": "rollup -c rollup.config.ts --configPlugin swc3 && cp dist/index.d.ts dist/index.d.mts",
|
|
66
|
+
"build:docs": "typedoc",
|
|
67
|
+
"lint:publish": "publint",
|
|
68
|
+
"lint:type": "tsc --noEmit"
|
|
11
69
|
}
|
|
12
70
|
}
|