eslint-plugin-react-web-api 2.0.0-next.8 → 2.0.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/README.md +20 -17
- package/dist/index.d.ts +16 -45
- package/dist/index.js +604 -697
- package/package.json +17 -26
package/dist/index.js
CHANGED
|
@@ -1,737 +1,644 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import * as AST from
|
|
3
|
-
import * as
|
|
4
|
-
import {
|
|
5
|
-
import * as VAR from
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
1
|
+
import { DEFAULT_ESLINT_REACT_SETTINGS, getConfigAdapters, getDocsUrl } from "@eslint-react/shared";
|
|
2
|
+
import * as AST from "@eslint-react/ast";
|
|
3
|
+
import * as ER from "@eslint-react/core";
|
|
4
|
+
import { or, unit } from "@eslint-react/eff";
|
|
5
|
+
import * as VAR from "@eslint-react/var";
|
|
6
|
+
import { AST_NODE_TYPES, ESLintUtils } from "@typescript-eslint/utils";
|
|
7
|
+
import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
8
|
+
import { P, isMatching, match } from "ts-pattern";
|
|
9
|
+
import { AST_NODE_TYPES as AST_NODE_TYPES$1 } from "@typescript-eslint/types";
|
|
9
10
|
|
|
11
|
+
//#region rolldown:runtime
|
|
10
12
|
var __defProp = Object.defineProperty;
|
|
11
|
-
var __export = (
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
var __export = (all) => {
|
|
14
|
+
let target = {};
|
|
15
|
+
for (var name$2 in all) __defProp(target, name$2, {
|
|
16
|
+
get: all[name$2],
|
|
17
|
+
enumerable: true
|
|
18
|
+
});
|
|
19
|
+
return target;
|
|
14
20
|
};
|
|
15
21
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
__export(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/configs/recommended.ts
|
|
24
|
+
var recommended_exports = /* @__PURE__ */ __export({
|
|
25
|
+
name: () => name$1,
|
|
26
|
+
rules: () => rules,
|
|
27
|
+
settings: () => settings
|
|
22
28
|
});
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
};
|
|
30
|
-
var settings = {
|
|
31
|
-
"react-x": DEFAULT_ESLINT_REACT_SETTINGS
|
|
29
|
+
const name$1 = "react-web-api/recommended";
|
|
30
|
+
const rules = {
|
|
31
|
+
"react-web-api/no-leaked-event-listener": "warn",
|
|
32
|
+
"react-web-api/no-leaked-interval": "warn",
|
|
33
|
+
"react-web-api/no-leaked-resize-observer": "warn",
|
|
34
|
+
"react-web-api/no-leaked-timeout": "warn"
|
|
32
35
|
};
|
|
36
|
+
const settings = { "react-x": DEFAULT_ESLINT_REACT_SETTINGS };
|
|
33
37
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
var
|
|
37
|
-
var
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const pReturn = AST.findParentNode(node, AST.is(AST_NODE_TYPES$1.ReturnStatement));
|
|
48
|
-
const pFunction = AST.findParentNode(node, AST.isFunction);
|
|
49
|
-
const pFunctionOfReturn = AST.findParentNode(pReturn, AST.isFunction);
|
|
50
|
-
if (pFunction !== pFunctionOfReturn) return false;
|
|
51
|
-
return isFunctionOfUseEffectSetup(pFunction);
|
|
52
|
-
}
|
|
53
|
-
function isFunctionOfComponentDidMount(node) {
|
|
54
|
-
return AST.isFunction(node) && ER4.isComponentDidMount(node.parent) && node.parent.value === node;
|
|
55
|
-
}
|
|
56
|
-
function isFunctionOfComponentWillUnmount(node) {
|
|
57
|
-
return AST.isFunction(node) && ER4.isComponentWillUnmount(node.parent) && node.parent.value === node;
|
|
58
|
-
}
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region package.json
|
|
40
|
+
var name = "eslint-plugin-react-web-api";
|
|
41
|
+
var version = "2.0.0";
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/utils/create-rule.ts
|
|
45
|
+
const createRule = ESLintUtils.RuleCreator(getDocsUrl("web-api"));
|
|
46
|
+
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/utils/is-conditional.ts
|
|
49
|
+
const isControlFlow = or(AST.isLoop, AST.isOneOf([AST_NODE_TYPES$1.IfStatement, AST_NODE_TYPES$1.SwitchStatement]));
|
|
50
|
+
const isConditional = or(isControlFlow, AST.isOneOf([AST_NODE_TYPES$1.LogicalExpression, AST_NODE_TYPES$1.ConditionalExpression]));
|
|
59
51
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/rules/no-leaked-event-listener.ts
|
|
54
|
+
const RULE_NAME$3 = "no-leaked-event-listener";
|
|
55
|
+
const RULE_FEATURES$3 = [];
|
|
56
|
+
const defaultOptions = {
|
|
57
|
+
capture: false,
|
|
58
|
+
signal: unit
|
|
67
59
|
};
|
|
68
|
-
function getCallKind(node) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
default:
|
|
75
|
-
return "other";
|
|
76
|
-
}
|
|
60
|
+
function getCallKind$3(node) {
|
|
61
|
+
switch (true) {
|
|
62
|
+
case node.callee.type === AST_NODE_TYPES.Identifier && isMatching(P.union("addEventListener", "removeEventListener", "abort"))(node.callee.name): return node.callee.name;
|
|
63
|
+
case node.callee.type === AST_NODE_TYPES.MemberExpression && node.callee.property.type === AST_NODE_TYPES.Identifier && isMatching(P.union("addEventListener", "removeEventListener", "abort"))(node.callee.property.name): return node.callee.property.name;
|
|
64
|
+
default: return "other";
|
|
65
|
+
}
|
|
77
66
|
}
|
|
78
|
-
function getFunctionKind(node) {
|
|
79
|
-
|
|
67
|
+
function getFunctionKind$1(node) {
|
|
68
|
+
return ER.getPhaseKindOfFunction(node) ?? "other";
|
|
80
69
|
}
|
|
81
70
|
function getSignalValueExpression(node, initialScope) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
return node;
|
|
89
|
-
default:
|
|
90
|
-
return _;
|
|
91
|
-
}
|
|
71
|
+
if (node == null) return unit;
|
|
72
|
+
switch (node.type) {
|
|
73
|
+
case AST_NODE_TYPES.Identifier: return getSignalValueExpression(VAR.getVariableDefinitionNode(VAR.findVariable(node, initialScope), 0), initialScope);
|
|
74
|
+
case AST_NODE_TYPES.MemberExpression: return node;
|
|
75
|
+
default: return unit;
|
|
76
|
+
}
|
|
92
77
|
}
|
|
93
78
|
function getOptions(node, initialScope) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
return getOpts(node);
|
|
79
|
+
function findProp(properties, propName) {
|
|
80
|
+
return VAR.findProperty(propName, properties, initialScope);
|
|
81
|
+
}
|
|
82
|
+
function getPropValue(prop, filter = (a) => true) {
|
|
83
|
+
if (prop?.type !== AST_NODE_TYPES.Property) return unit;
|
|
84
|
+
const { value } = prop;
|
|
85
|
+
let v = value;
|
|
86
|
+
switch (value.type) {
|
|
87
|
+
case AST_NODE_TYPES.Literal:
|
|
88
|
+
v = value.value;
|
|
89
|
+
break;
|
|
90
|
+
default:
|
|
91
|
+
v = getStaticValue(value, initialScope)?.value;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
return filter(v) ? v : unit;
|
|
95
|
+
}
|
|
96
|
+
function getOpts(node$1) {
|
|
97
|
+
switch (node$1.type) {
|
|
98
|
+
case AST_NODE_TYPES.Identifier: {
|
|
99
|
+
const variable = VAR.findVariable(node$1, initialScope);
|
|
100
|
+
const variableNode = VAR.getVariableDefinitionNode(variable, 0);
|
|
101
|
+
if (variableNode?.type === AST_NODE_TYPES.ObjectExpression) return getOpts(variableNode);
|
|
102
|
+
return defaultOptions;
|
|
103
|
+
}
|
|
104
|
+
case AST_NODE_TYPES.Literal: return {
|
|
105
|
+
...defaultOptions,
|
|
106
|
+
capture: Boolean(node$1.value)
|
|
107
|
+
};
|
|
108
|
+
case AST_NODE_TYPES.ObjectExpression: {
|
|
109
|
+
const pCapture = findProp(node$1.properties, "capture");
|
|
110
|
+
const vCapture = !!getPropValue(pCapture);
|
|
111
|
+
const pSignal = findProp(node$1.properties, "signal");
|
|
112
|
+
const vSignal = pSignal?.type === AST_NODE_TYPES.Property ? getSignalValueExpression(pSignal.value, initialScope) : unit;
|
|
113
|
+
return {
|
|
114
|
+
capture: vCapture,
|
|
115
|
+
signal: vSignal
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
default: return defaultOptions;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return getOpts(node);
|
|
139
122
|
}
|
|
140
123
|
var no_leaked_event_listener_default = createRule({
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
124
|
+
meta: {
|
|
125
|
+
type: "problem",
|
|
126
|
+
docs: {
|
|
127
|
+
description: "Prevents leaked `addEventListener` in a component or custom Hook.",
|
|
128
|
+
[Symbol.for("rule_features")]: RULE_FEATURES$3
|
|
129
|
+
},
|
|
130
|
+
messages: {
|
|
131
|
+
expectedRemoveEventListenerInCleanup: "An 'addEventListener' in '{{effectMethodKind}}' should have a corresponding 'removeEventListener' in its cleanup function.",
|
|
132
|
+
expectedRemoveEventListenerInUnmount: "An 'addEventListener' in 'componentDidMount' should have a corresponding 'removeEventListener' in 'componentWillUnmount' method.",
|
|
133
|
+
unexpectedInlineFunction: "A/an '{{eventMethodKind}}' should not have an inline listener function."
|
|
134
|
+
},
|
|
135
|
+
schema: []
|
|
136
|
+
},
|
|
137
|
+
name: RULE_NAME$3,
|
|
138
|
+
create: create$3,
|
|
139
|
+
defaultOptions: []
|
|
157
140
|
});
|
|
158
|
-
function create(context) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
continue;
|
|
262
|
-
}
|
|
263
|
-
if (rEntries.some((rEntry) => isInverseEntry(aEntry, rEntry))) {
|
|
264
|
-
continue;
|
|
265
|
-
}
|
|
266
|
-
switch (aEntry.phase) {
|
|
267
|
-
case "setup":
|
|
268
|
-
case "cleanup":
|
|
269
|
-
context.report({
|
|
270
|
-
messageId: "expectedRemoveEventListenerInCleanup",
|
|
271
|
-
node: aEntry.node,
|
|
272
|
-
data: {
|
|
273
|
-
effectMethodKind: "useEffect"
|
|
274
|
-
}
|
|
275
|
-
});
|
|
276
|
-
continue;
|
|
277
|
-
case "mount":
|
|
278
|
-
case "unmount":
|
|
279
|
-
context.report({
|
|
280
|
-
messageId: "expectedRemoveEventListenerInUnmount",
|
|
281
|
-
node: aEntry.node
|
|
282
|
-
});
|
|
283
|
-
continue;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
};
|
|
141
|
+
function create$3(context) {
|
|
142
|
+
if (!context.sourceCode.text.includes("addEventListener")) return {};
|
|
143
|
+
if (!/use\w*Effect|componentDidMount|componentWillUnmount/u.test(context.sourceCode.text)) return {};
|
|
144
|
+
const fEntries = [];
|
|
145
|
+
const aEntries = [];
|
|
146
|
+
const rEntries = [];
|
|
147
|
+
const abortedSignals = [];
|
|
148
|
+
function isSameObject(a, b) {
|
|
149
|
+
switch (true) {
|
|
150
|
+
case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return AST.isNodeEqual(a.object, b.object);
|
|
151
|
+
default: return false;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function isInverseEntry(aEntry, rEntry) {
|
|
155
|
+
const { type: aType, callee: aCallee, capture: aCapture, listener: aListener, phase: aPhase } = aEntry;
|
|
156
|
+
const { type: rType, callee: rCallee, capture: rCapture, listener: rListener, phase: rPhase } = rEntry;
|
|
157
|
+
if (!ER.isInversePhase(aPhase, rPhase)) return false;
|
|
158
|
+
return isSameObject(aCallee, rCallee) && AST.isNodeEqual(aListener, rListener) && VAR.isNodeValueEqual(aType, rType, [context.sourceCode.getScope(aType), context.sourceCode.getScope(rType)]) && aCapture === rCapture;
|
|
159
|
+
}
|
|
160
|
+
function checkInlineFunction(node, callKind, options) {
|
|
161
|
+
const listener = node.arguments.at(1);
|
|
162
|
+
if (!AST.isFunction(listener)) return;
|
|
163
|
+
if (options.signal != null) return;
|
|
164
|
+
context.report({
|
|
165
|
+
messageId: "unexpectedInlineFunction",
|
|
166
|
+
node: listener,
|
|
167
|
+
data: { eventMethodKind: callKind }
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
[":function"](node) {
|
|
172
|
+
const kind = getFunctionKind$1(node);
|
|
173
|
+
fEntries.push({
|
|
174
|
+
kind,
|
|
175
|
+
node
|
|
176
|
+
});
|
|
177
|
+
},
|
|
178
|
+
[":function:exit"]() {
|
|
179
|
+
fEntries.pop();
|
|
180
|
+
},
|
|
181
|
+
["CallExpression"](node) {
|
|
182
|
+
const fKind = fEntries.findLast((x) => x.kind !== "other")?.kind;
|
|
183
|
+
if (fKind == null) return;
|
|
184
|
+
if (!ER.ComponentPhaseRelevance.has(fKind)) return;
|
|
185
|
+
match(getCallKind$3(node)).with("addEventListener", (callKind) => {
|
|
186
|
+
const [type, listener, options] = node.arguments;
|
|
187
|
+
if (type == null || listener == null) return;
|
|
188
|
+
const opts = options == null ? defaultOptions : getOptions(options, context.sourceCode.getScope(options));
|
|
189
|
+
const { callee } = node;
|
|
190
|
+
checkInlineFunction(node, callKind, opts);
|
|
191
|
+
aEntries.push({
|
|
192
|
+
...opts,
|
|
193
|
+
kind: "addEventListener",
|
|
194
|
+
type,
|
|
195
|
+
node,
|
|
196
|
+
callee,
|
|
197
|
+
listener,
|
|
198
|
+
phase: fKind
|
|
199
|
+
});
|
|
200
|
+
}).with("removeEventListener", (callKind) => {
|
|
201
|
+
const [type, listener, options] = node.arguments;
|
|
202
|
+
if (type == null || listener == null) return;
|
|
203
|
+
const opts = options == null ? defaultOptions : getOptions(options, context.sourceCode.getScope(options));
|
|
204
|
+
const { callee } = node;
|
|
205
|
+
checkInlineFunction(node, callKind, opts);
|
|
206
|
+
rEntries.push({
|
|
207
|
+
...opts,
|
|
208
|
+
kind: "removeEventListener",
|
|
209
|
+
type,
|
|
210
|
+
node,
|
|
211
|
+
callee,
|
|
212
|
+
listener,
|
|
213
|
+
phase: fKind
|
|
214
|
+
});
|
|
215
|
+
}).with("abort", () => {
|
|
216
|
+
abortedSignals.push(node.callee);
|
|
217
|
+
}).otherwise(() => null);
|
|
218
|
+
},
|
|
219
|
+
["Program:exit"]() {
|
|
220
|
+
for (const aEntry of aEntries) {
|
|
221
|
+
const signal = aEntry.signal;
|
|
222
|
+
if (signal != null && abortedSignals.some((a) => isSameObject(a, signal))) continue;
|
|
223
|
+
if (rEntries.some((rEntry) => isInverseEntry(aEntry, rEntry))) continue;
|
|
224
|
+
switch (aEntry.phase) {
|
|
225
|
+
case "setup":
|
|
226
|
+
case "cleanup":
|
|
227
|
+
context.report({
|
|
228
|
+
messageId: "expectedRemoveEventListenerInCleanup",
|
|
229
|
+
node: aEntry.node,
|
|
230
|
+
data: { effectMethodKind: "useEffect" }
|
|
231
|
+
});
|
|
232
|
+
continue;
|
|
233
|
+
case "mount":
|
|
234
|
+
case "unmount":
|
|
235
|
+
context.report({
|
|
236
|
+
messageId: "expectedRemoveEventListenerInUnmount",
|
|
237
|
+
node: aEntry.node
|
|
238
|
+
});
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
};
|
|
288
244
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
245
|
+
|
|
246
|
+
//#endregion
|
|
247
|
+
//#region src/rules/no-leaked-interval.ts
|
|
248
|
+
const RULE_NAME$2 = "no-leaked-interval";
|
|
249
|
+
const RULE_FEATURES$2 = [];
|
|
250
|
+
function getCallKind$2(node) {
|
|
251
|
+
switch (true) {
|
|
252
|
+
case node.callee.type === AST_NODE_TYPES.Identifier && isMatching(P.union("setInterval", "clearInterval"))(node.callee.name): return node.callee.name;
|
|
253
|
+
case node.callee.type === AST_NODE_TYPES.MemberExpression && node.callee.property.type === AST_NODE_TYPES.Identifier && isMatching(P.union("setInterval", "clearInterval"))(node.callee.property.name): return node.callee.property.name;
|
|
254
|
+
default: return "other";
|
|
255
|
+
}
|
|
300
256
|
}
|
|
301
257
|
var no_leaked_interval_default = createRule({
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
258
|
+
meta: {
|
|
259
|
+
type: "problem",
|
|
260
|
+
docs: {
|
|
261
|
+
description: "Prevents leaked `setInterval` in a component or custom Hook.",
|
|
262
|
+
[Symbol.for("rule_features")]: RULE_FEATURES$2
|
|
263
|
+
},
|
|
264
|
+
messages: {
|
|
265
|
+
expectedClearIntervalInCleanup: "A 'setInterval' created in '{{ kind }}' must be cleared with 'clearInterval' in the cleanup function.",
|
|
266
|
+
expectedClearIntervalInUnmount: "A 'setInterval' created in '{{ kind }}' must be cleared with 'clearInterval' in the 'componentWillUnmount' method.",
|
|
267
|
+
expectedIntervalId: "A 'setInterval' must be assigned to a variable for proper cleanup."
|
|
268
|
+
},
|
|
269
|
+
schema: []
|
|
270
|
+
},
|
|
271
|
+
name: RULE_NAME$2,
|
|
272
|
+
create: create$2,
|
|
273
|
+
defaultOptions: []
|
|
318
274
|
});
|
|
319
|
-
function
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
continue;
|
|
403
|
-
case "mount":
|
|
404
|
-
case "unmount":
|
|
405
|
-
context.report({
|
|
406
|
-
messageId: "expectedClearIntervalInUnmount",
|
|
407
|
-
node: sEntry.node,
|
|
408
|
-
data: {
|
|
409
|
-
kind: "componentDidMount"
|
|
410
|
-
}
|
|
411
|
-
});
|
|
412
|
-
continue;
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
};
|
|
275
|
+
function create$2(context) {
|
|
276
|
+
if (!context.sourceCode.text.includes("setInterval")) return {};
|
|
277
|
+
const fEntries = [];
|
|
278
|
+
const sEntries = [];
|
|
279
|
+
const cEntries = [];
|
|
280
|
+
function isInverseEntry(a, b) {
|
|
281
|
+
return ER.isInstanceIdEqual(context, a.timerId, b.timerId);
|
|
282
|
+
}
|
|
283
|
+
return {
|
|
284
|
+
[":function"](node) {
|
|
285
|
+
const kind = ER.getPhaseKindOfFunction(node) ?? "other";
|
|
286
|
+
fEntries.push({
|
|
287
|
+
kind,
|
|
288
|
+
node
|
|
289
|
+
});
|
|
290
|
+
},
|
|
291
|
+
[":function:exit"]() {
|
|
292
|
+
fEntries.pop();
|
|
293
|
+
},
|
|
294
|
+
["CallExpression"](node) {
|
|
295
|
+
switch (getCallKind$2(node)) {
|
|
296
|
+
case "setInterval": {
|
|
297
|
+
const fEntry = fEntries.findLast((x) => x.kind !== "other");
|
|
298
|
+
if (fEntry == null) break;
|
|
299
|
+
if (!ER.ComponentPhaseRelevance.has(fEntry.kind)) break;
|
|
300
|
+
const intervalIdNode = VAR.findAssignmentTarget(node);
|
|
301
|
+
if (intervalIdNode == null) {
|
|
302
|
+
context.report({
|
|
303
|
+
messageId: "expectedIntervalId",
|
|
304
|
+
node
|
|
305
|
+
});
|
|
306
|
+
break;
|
|
307
|
+
}
|
|
308
|
+
sEntries.push({
|
|
309
|
+
kind: "interval",
|
|
310
|
+
node,
|
|
311
|
+
callee: node.callee,
|
|
312
|
+
phase: fEntry.kind,
|
|
313
|
+
timerId: intervalIdNode
|
|
314
|
+
});
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
case "clearInterval": {
|
|
318
|
+
const fEntry = fEntries.findLast((x) => x.kind !== "other");
|
|
319
|
+
if (fEntry == null) break;
|
|
320
|
+
if (!ER.ComponentPhaseRelevance.has(fEntry.kind)) break;
|
|
321
|
+
const [intervalIdNode] = node.arguments;
|
|
322
|
+
if (intervalIdNode == null) break;
|
|
323
|
+
cEntries.push({
|
|
324
|
+
kind: "interval",
|
|
325
|
+
node,
|
|
326
|
+
callee: node.callee,
|
|
327
|
+
phase: fEntry.kind,
|
|
328
|
+
timerId: intervalIdNode
|
|
329
|
+
});
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
},
|
|
334
|
+
["Program:exit"]() {
|
|
335
|
+
for (const sEntry of sEntries) {
|
|
336
|
+
if (cEntries.some((cEntry) => isInverseEntry(sEntry, cEntry))) continue;
|
|
337
|
+
switch (sEntry.phase) {
|
|
338
|
+
case "setup":
|
|
339
|
+
case "cleanup":
|
|
340
|
+
context.report({
|
|
341
|
+
messageId: "expectedClearIntervalInCleanup",
|
|
342
|
+
node: sEntry.node,
|
|
343
|
+
data: { kind: "useEffect" }
|
|
344
|
+
});
|
|
345
|
+
continue;
|
|
346
|
+
case "mount":
|
|
347
|
+
case "unmount":
|
|
348
|
+
context.report({
|
|
349
|
+
messageId: "expectedClearIntervalInUnmount",
|
|
350
|
+
node: sEntry.node,
|
|
351
|
+
data: { kind: "componentDidMount" }
|
|
352
|
+
});
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
};
|
|
417
358
|
}
|
|
418
|
-
|
|
419
|
-
|
|
359
|
+
|
|
360
|
+
//#endregion
|
|
361
|
+
//#region src/rules/no-leaked-resize-observer.ts
|
|
362
|
+
const RULE_NAME$1 = "no-leaked-resize-observer";
|
|
363
|
+
const RULE_FEATURES$1 = [];
|
|
420
364
|
function isNewResizeObserver(node) {
|
|
421
|
-
|
|
365
|
+
return node?.type === AST_NODE_TYPES.NewExpression && node.callee.type === AST_NODE_TYPES.Identifier && node.callee.name === "ResizeObserver";
|
|
422
366
|
}
|
|
423
367
|
function isFromObserver(context, node) {
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
return false;
|
|
434
|
-
}
|
|
368
|
+
switch (true) {
|
|
369
|
+
case node.type === AST_NODE_TYPES.Identifier: {
|
|
370
|
+
const initialScope = context.sourceCode.getScope(node);
|
|
371
|
+
const object = VAR.getVariableDefinitionNode(VAR.findVariable(node, initialScope), 0);
|
|
372
|
+
return isNewResizeObserver(object);
|
|
373
|
+
}
|
|
374
|
+
case node.type === AST_NODE_TYPES.MemberExpression: return isFromObserver(context, node.object);
|
|
375
|
+
default: return false;
|
|
376
|
+
}
|
|
435
377
|
}
|
|
436
|
-
function
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
default:
|
|
443
|
-
return "other";
|
|
444
|
-
}
|
|
378
|
+
function getCallKind$1(context, node) {
|
|
379
|
+
switch (true) {
|
|
380
|
+
case node.callee.type === AST_NODE_TYPES.Identifier && isMatching(P.union("observe", "unobserve", "disconnect"))(node.callee.name) && isFromObserver(context, node.callee): return node.callee.name;
|
|
381
|
+
case node.callee.type === AST_NODE_TYPES.MemberExpression && node.callee.property.type === AST_NODE_TYPES.Identifier && isMatching(P.union("observe", "unobserve", "disconnect"))(node.callee.property.name) && isFromObserver(context, node.callee): return node.callee.property.name;
|
|
382
|
+
default: return "other";
|
|
383
|
+
}
|
|
445
384
|
}
|
|
446
|
-
function
|
|
447
|
-
|
|
385
|
+
function getFunctionKind(node) {
|
|
386
|
+
return ER.getPhaseKindOfFunction(node) ?? "other";
|
|
448
387
|
}
|
|
449
388
|
var no_leaked_resize_observer_default = createRule({
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
389
|
+
meta: {
|
|
390
|
+
type: "problem",
|
|
391
|
+
docs: {
|
|
392
|
+
description: "Prevents leaked `ResizeObserver` in a component or custom Hook.",
|
|
393
|
+
[Symbol.for("rule_features")]: RULE_FEATURES$1
|
|
394
|
+
},
|
|
395
|
+
messages: {
|
|
396
|
+
expectedDisconnectInControlFlow: "Dynamically added 'ResizeObserver.observe' should be cleared all at once using 'ResizeObserver.disconnect' in the cleanup function.",
|
|
397
|
+
expectedDisconnectOrUnobserveInCleanup: "A 'ResizeObserver' instance created in 'useEffect' must be disconnected in the cleanup function.",
|
|
398
|
+
unexpectedFloatingInstance: "A 'ResizeObserver' instance created in component or custom Hook must be assigned to a variable for proper cleanup."
|
|
399
|
+
},
|
|
400
|
+
schema: []
|
|
401
|
+
},
|
|
402
|
+
name: RULE_NAME$1,
|
|
403
|
+
create: create$1,
|
|
404
|
+
defaultOptions: []
|
|
466
405
|
});
|
|
467
|
-
function
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
}
|
|
570
|
-
for (const oEntry of oentries) {
|
|
571
|
-
if (uentries.some((uEntry) => ER4.isInstanceIdEqual(context, uEntry.element, oEntry.element))) {
|
|
572
|
-
continue;
|
|
573
|
-
}
|
|
574
|
-
context.report({ messageId: "expectedDisconnectOrUnobserveInCleanup", node: oEntry.node });
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
};
|
|
406
|
+
function create$1(context) {
|
|
407
|
+
if (!context.sourceCode.text.includes("ResizeObserver")) return {};
|
|
408
|
+
const fEntries = [];
|
|
409
|
+
const observers = [];
|
|
410
|
+
const oEntries = [];
|
|
411
|
+
const uEntries = [];
|
|
412
|
+
const dEntries = [];
|
|
413
|
+
return {
|
|
414
|
+
[":function"](node) {
|
|
415
|
+
const kind = getFunctionKind(node);
|
|
416
|
+
fEntries.push({
|
|
417
|
+
kind,
|
|
418
|
+
node
|
|
419
|
+
});
|
|
420
|
+
},
|
|
421
|
+
[":function:exit"]() {
|
|
422
|
+
fEntries.pop();
|
|
423
|
+
},
|
|
424
|
+
["CallExpression"](node) {
|
|
425
|
+
if (node.callee.type !== AST_NODE_TYPES.MemberExpression) return;
|
|
426
|
+
const fKind = fEntries.findLast((x) => x.kind !== "other")?.kind;
|
|
427
|
+
if (fKind == null || !ER.ComponentPhaseRelevance.has(fKind)) return;
|
|
428
|
+
const { object } = node.callee;
|
|
429
|
+
match(getCallKind$1(context, node)).with("disconnect", () => {
|
|
430
|
+
dEntries.push({
|
|
431
|
+
kind: "disconnect",
|
|
432
|
+
node,
|
|
433
|
+
callee: node.callee,
|
|
434
|
+
observer: object,
|
|
435
|
+
observerKind: "ResizeObserver",
|
|
436
|
+
phase: fKind
|
|
437
|
+
});
|
|
438
|
+
}).with("observe", () => {
|
|
439
|
+
const [element] = node.arguments;
|
|
440
|
+
if (element == null) return;
|
|
441
|
+
oEntries.push({
|
|
442
|
+
kind: "observe",
|
|
443
|
+
node,
|
|
444
|
+
callee: node.callee,
|
|
445
|
+
element,
|
|
446
|
+
observer: object,
|
|
447
|
+
observerKind: "ResizeObserver",
|
|
448
|
+
phase: fKind
|
|
449
|
+
});
|
|
450
|
+
}).with("unobserve", () => {
|
|
451
|
+
const [element] = node.arguments;
|
|
452
|
+
if (element == null) return;
|
|
453
|
+
uEntries.push({
|
|
454
|
+
kind: "unobserve",
|
|
455
|
+
node,
|
|
456
|
+
callee: node.callee,
|
|
457
|
+
element,
|
|
458
|
+
observer: object,
|
|
459
|
+
observerKind: "ResizeObserver",
|
|
460
|
+
phase: fKind
|
|
461
|
+
});
|
|
462
|
+
}).otherwise(() => null);
|
|
463
|
+
},
|
|
464
|
+
["NewExpression"](node) {
|
|
465
|
+
const fEntry = fEntries.findLast((x) => x.kind !== "other");
|
|
466
|
+
if (fEntry == null) return;
|
|
467
|
+
if (!ER.ComponentPhaseRelevance.has(fEntry.kind)) return;
|
|
468
|
+
if (!isNewResizeObserver(node)) return;
|
|
469
|
+
const id = ER.getInstanceId(node);
|
|
470
|
+
if (id == null) {
|
|
471
|
+
context.report({
|
|
472
|
+
messageId: "unexpectedFloatingInstance",
|
|
473
|
+
node
|
|
474
|
+
});
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
observers.push({
|
|
478
|
+
id,
|
|
479
|
+
node,
|
|
480
|
+
phase: fEntry.kind,
|
|
481
|
+
phaseNode: fEntry.node
|
|
482
|
+
});
|
|
483
|
+
},
|
|
484
|
+
["Program:exit"]() {
|
|
485
|
+
for (const { id, node, phaseNode } of observers) {
|
|
486
|
+
if (dEntries.some((e) => ER.isInstanceIdEqual(context, e.observer, id))) continue;
|
|
487
|
+
const oentries = oEntries.filter((e) => ER.isInstanceIdEqual(context, e.observer, id));
|
|
488
|
+
const uentries = uEntries.filter((e) => ER.isInstanceIdEqual(context, e.observer, id));
|
|
489
|
+
const isDynamic = (node$1) => node$1?.type === AST_NODE_TYPES.CallExpression || isConditional(node$1);
|
|
490
|
+
const isPhaseNode = (node$1) => node$1 === phaseNode;
|
|
491
|
+
if (oentries.some((e) => !isPhaseNode(AST.findParentNode(e.node, or(isDynamic, isPhaseNode))))) {
|
|
492
|
+
context.report({
|
|
493
|
+
messageId: "expectedDisconnectInControlFlow",
|
|
494
|
+
node
|
|
495
|
+
});
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
498
|
+
for (const oEntry of oentries) {
|
|
499
|
+
if (uentries.some((uEntry) => ER.isInstanceIdEqual(context, uEntry.element, oEntry.element))) continue;
|
|
500
|
+
context.report({
|
|
501
|
+
messageId: "expectedDisconnectOrUnobserveInCleanup",
|
|
502
|
+
node: oEntry.node
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
};
|
|
579
508
|
}
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
509
|
+
|
|
510
|
+
//#endregion
|
|
511
|
+
//#region src/rules/no-leaked-timeout.ts
|
|
512
|
+
const RULE_NAME = "no-leaked-timeout";
|
|
513
|
+
const RULE_FEATURES = [];
|
|
514
|
+
function getCallKind(node) {
|
|
515
|
+
switch (true) {
|
|
516
|
+
case node.callee.type === AST_NODE_TYPES.Identifier && isMatching(P.union("setTimeout", "clearTimeout"))(node.callee.name): return node.callee.name;
|
|
517
|
+
case node.callee.type === AST_NODE_TYPES.MemberExpression && node.callee.property.type === AST_NODE_TYPES.Identifier && isMatching(P.union("setTimeout", "clearTimeout"))(node.callee.property.name): return node.callee.property.name;
|
|
518
|
+
default: return "other";
|
|
519
|
+
}
|
|
591
520
|
}
|
|
592
521
|
var no_leaked_timeout_default = createRule({
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
522
|
+
meta: {
|
|
523
|
+
type: "problem",
|
|
524
|
+
docs: {
|
|
525
|
+
description: "Prevents leaked `setTimeout` in a component or custom Hook.",
|
|
526
|
+
[Symbol.for("rule_features")]: RULE_FEATURES
|
|
527
|
+
},
|
|
528
|
+
messages: {
|
|
529
|
+
expectedClearTimeoutInCleanup: "A 'setTimeout' created in '{{ kind }}' must be cleared with 'clearTimeout' in the cleanup function.",
|
|
530
|
+
expectedClearTimeoutInUnmount: "A 'setTimeout' created in '{{ kind }}' must be cleared with 'clearTimeout' in the 'componentWillUnmount' method.",
|
|
531
|
+
expectedTimeoutId: "A 'setTimeout' must be assigned to a variable for proper cleanup."
|
|
532
|
+
},
|
|
533
|
+
schema: []
|
|
534
|
+
},
|
|
535
|
+
name: RULE_NAME,
|
|
536
|
+
create,
|
|
537
|
+
defaultOptions: []
|
|
609
538
|
});
|
|
610
|
-
function
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
data: {
|
|
690
|
-
kind: "componentDidMount"
|
|
691
|
-
}
|
|
692
|
-
});
|
|
693
|
-
continue;
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
};
|
|
539
|
+
function create(context) {
|
|
540
|
+
if (!context.sourceCode.text.includes("setTimeout")) return {};
|
|
541
|
+
const fEntries = [];
|
|
542
|
+
const sEntries = [];
|
|
543
|
+
const rEntries = [];
|
|
544
|
+
function isInverseEntry(a, b) {
|
|
545
|
+
return ER.isInstanceIdEqual(context, a.timerId, b.timerId);
|
|
546
|
+
}
|
|
547
|
+
return {
|
|
548
|
+
[":function"](node) {
|
|
549
|
+
const kind = ER.getPhaseKindOfFunction(node) ?? "other";
|
|
550
|
+
fEntries.push({
|
|
551
|
+
kind,
|
|
552
|
+
node
|
|
553
|
+
});
|
|
554
|
+
},
|
|
555
|
+
[":function:exit"]() {
|
|
556
|
+
fEntries.pop();
|
|
557
|
+
},
|
|
558
|
+
["CallExpression"](node) {
|
|
559
|
+
const fEntry = fEntries.findLast((f) => f.kind !== "other");
|
|
560
|
+
if (!ER.ComponentPhaseRelevance.has(fEntry?.kind)) return;
|
|
561
|
+
switch (getCallKind(node)) {
|
|
562
|
+
case "setTimeout": {
|
|
563
|
+
const timeoutIdNode = VAR.findAssignmentTarget(node);
|
|
564
|
+
if (timeoutIdNode == null) {
|
|
565
|
+
context.report({
|
|
566
|
+
messageId: "expectedTimeoutId",
|
|
567
|
+
node
|
|
568
|
+
});
|
|
569
|
+
break;
|
|
570
|
+
}
|
|
571
|
+
sEntries.push({
|
|
572
|
+
kind: "timeout",
|
|
573
|
+
node,
|
|
574
|
+
callee: node.callee,
|
|
575
|
+
phase: fEntry.kind,
|
|
576
|
+
timerId: timeoutIdNode
|
|
577
|
+
});
|
|
578
|
+
break;
|
|
579
|
+
}
|
|
580
|
+
case "clearTimeout": {
|
|
581
|
+
const [timeoutIdNode] = node.arguments;
|
|
582
|
+
if (timeoutIdNode == null) break;
|
|
583
|
+
rEntries.push({
|
|
584
|
+
kind: "timeout",
|
|
585
|
+
node,
|
|
586
|
+
callee: node.callee,
|
|
587
|
+
phase: fEntry.kind,
|
|
588
|
+
timerId: timeoutIdNode
|
|
589
|
+
});
|
|
590
|
+
break;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
},
|
|
594
|
+
["Program:exit"]() {
|
|
595
|
+
for (const sEntry of sEntries) {
|
|
596
|
+
if (rEntries.some((rEntry) => isInverseEntry(sEntry, rEntry))) continue;
|
|
597
|
+
switch (sEntry.phase) {
|
|
598
|
+
case "setup":
|
|
599
|
+
case "cleanup":
|
|
600
|
+
context.report({
|
|
601
|
+
messageId: "expectedClearTimeoutInCleanup",
|
|
602
|
+
node: sEntry.node,
|
|
603
|
+
data: { kind: "useEffect" }
|
|
604
|
+
});
|
|
605
|
+
continue;
|
|
606
|
+
case "mount":
|
|
607
|
+
case "unmount":
|
|
608
|
+
context.report({
|
|
609
|
+
messageId: "expectedClearTimeoutInUnmount",
|
|
610
|
+
node: sEntry.node,
|
|
611
|
+
data: { kind: "componentDidMount" }
|
|
612
|
+
});
|
|
613
|
+
continue;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
};
|
|
698
618
|
}
|
|
699
619
|
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
620
|
+
//#endregion
|
|
621
|
+
//#region src/plugin.ts
|
|
622
|
+
const plugin = {
|
|
623
|
+
meta: {
|
|
624
|
+
name,
|
|
625
|
+
version
|
|
626
|
+
},
|
|
627
|
+
rules: {
|
|
628
|
+
"no-leaked-event-listener": no_leaked_event_listener_default,
|
|
629
|
+
"no-leaked-interval": no_leaked_interval_default,
|
|
630
|
+
"no-leaked-resize-observer": no_leaked_resize_observer_default,
|
|
631
|
+
"no-leaked-timeout": no_leaked_timeout_default
|
|
632
|
+
}
|
|
712
633
|
};
|
|
713
634
|
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
}
|
|
721
|
-
};
|
|
722
|
-
}
|
|
723
|
-
function makeLegacyConfig({ rules: rules2 }) {
|
|
724
|
-
return {
|
|
725
|
-
plugins: ["react-web-api"],
|
|
726
|
-
rules: rules2
|
|
727
|
-
};
|
|
728
|
-
}
|
|
729
|
-
var index_default = {
|
|
730
|
-
...plugin,
|
|
731
|
-
configs: {
|
|
732
|
-
["recommended"]: makeConfig(recommended_exports),
|
|
733
|
-
["recommended-legacy"]: makeLegacyConfig(recommended_exports)
|
|
734
|
-
}
|
|
635
|
+
//#endregion
|
|
636
|
+
//#region src/index.ts
|
|
637
|
+
const { toFlatConfig } = getConfigAdapters("react-web-api", plugin);
|
|
638
|
+
var src_default = {
|
|
639
|
+
...plugin,
|
|
640
|
+
configs: { ["recommended"]: toFlatConfig(recommended_exports) }
|
|
735
641
|
};
|
|
736
642
|
|
|
737
|
-
|
|
643
|
+
//#endregion
|
|
644
|
+
export { src_default as default };
|