eslint-plugin-react-web-api 5.30.0-beta.2 → 5.30.0-beta.21
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/dist/index.js +291 -185
- package/package.json +10 -10
package/dist/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { DEFAULT_ESLINT_REACT_SETTINGS } from "@eslint-react/shared";
|
|
2
2
|
import * as ESLintUtils from "@typescript-eslint/utils/eslint-utils";
|
|
3
|
+
import { merge } from "@eslint-react/eslint";
|
|
3
4
|
import { Check, Compare, Extract, Traverse } from "@eslint-react/ast";
|
|
4
5
|
import { getFunctionId, isUseEffectCleanupCallback, isUseEffectSetupCallback, isUseRefLikeCall } from "@eslint-react/core";
|
|
5
|
-
import "@eslint-react/eslint";
|
|
6
|
-
import { isAssignmentTargetEqual, isInitializedFromReactNative, isValueEqual, resolve, resolveEnclosingAssignmentTarget, resolveOrigin } from "@eslint-react/var";
|
|
7
6
|
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
8
|
-
import { findVariable, getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
9
7
|
import { P, isMatching, match } from "ts-pattern";
|
|
8
|
+
import { isAssignmentTargetEqual, isInitializedFromReactNative, isProvenanceEqual, resolve, resolveEnclosingAssignmentTarget, resolveOrigin } from "@eslint-react/var";
|
|
9
|
+
import { findVariable, getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
10
10
|
// \0rolldown/runtime.js
|
|
11
11
|
var __defProp = Object.defineProperty;
|
|
12
12
|
var __exportAll = (all, no_symbols) => {
|
|
@@ -20,7 +20,7 @@ var __exportAll = (all, no_symbols) => {
|
|
|
20
20
|
};
|
|
21
21
|
// package.json
|
|
22
22
|
var name$1 = "eslint-plugin-react-web-api";
|
|
23
|
-
var version = "5.30.0-beta.
|
|
23
|
+
var version = "5.30.0-beta.21";
|
|
24
24
|
// src/utils/create-rule.ts
|
|
25
25
|
function getDocsUrl(ruleName) {
|
|
26
26
|
return `https://eslint-react.xyz/docs/rules/web-api-${ruleName}`;
|
|
@@ -96,9 +96,6 @@ dual(2, (a, b) => (data) => a(data) === b(data));
|
|
|
96
96
|
dual(2, (antecedent, consequent) => (data) => !antecedent(data) || consequent(data));
|
|
97
97
|
dual(2, (a, b) => (data) => !a(data) && !b(data));
|
|
98
98
|
dual(2, (a, b) => (data) => !a(data) || !b(data));
|
|
99
|
-
function isString(input) {
|
|
100
|
-
return typeof input === "string";
|
|
101
|
-
}
|
|
102
99
|
function isFunction(input) {
|
|
103
100
|
return typeof input === "function";
|
|
104
101
|
}
|
|
@@ -126,7 +123,163 @@ dual(2, (self, predicate) => {
|
|
|
126
123
|
while (idx < len && predicate(input[idx], idx)) idx++;
|
|
127
124
|
return input.slice(0, idx);
|
|
128
125
|
});
|
|
129
|
-
// src/rules/no-leaked-event-listener/
|
|
126
|
+
// src/rules/no-leaked-event-listener/helpers.ts
|
|
127
|
+
const isUseEffectCallback$5 = or(isUseEffectSetupCallback, isUseEffectCleanupCallback);
|
|
128
|
+
function getEffectContext(node) {
|
|
129
|
+
const fn = Traverse.findParent(node, isUseEffectCallback$5);
|
|
130
|
+
if (fn == null) return null;
|
|
131
|
+
const phase = isUseEffectSetupCallback(fn) ? "setup" : "cleanup";
|
|
132
|
+
const setupFn = phase === "setup" ? fn : Traverse.findParent(fn, Check.isFunction);
|
|
133
|
+
return {
|
|
134
|
+
effect: setupFn == null ? null : Extract.unwrap(setupFn).parent ?? null,
|
|
135
|
+
phase
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function getCallKind$5(node) {
|
|
139
|
+
const name = Extract.getCalleeName(node);
|
|
140
|
+
if (name != null && isMatching(P.union("addEventListener", "removeEventListener"))(name)) return name;
|
|
141
|
+
return "other";
|
|
142
|
+
}
|
|
143
|
+
function isGlobalObject(node) {
|
|
144
|
+
return Check.isIdentifier(node) && (node.name === "window" || node.name === "globalThis" || node.name === "self");
|
|
145
|
+
}
|
|
146
|
+
function isSameObject(a, b, isEqual = Compare.isEqual) {
|
|
147
|
+
switch (true) {
|
|
148
|
+
case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return isEqual(a.object, b.object);
|
|
149
|
+
case a.type === AST_NODE_TYPES.MemberExpression: return isGlobalObject(a.object);
|
|
150
|
+
case b.type === AST_NODE_TYPES.MemberExpression: return isGlobalObject(b.object);
|
|
151
|
+
default: return true;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// src/rules/no-leaked-event-listener/collect.ts
|
|
155
|
+
function createFactCollector() {
|
|
156
|
+
const facts = { calls: [] };
|
|
157
|
+
return {
|
|
158
|
+
facts,
|
|
159
|
+
visitor: { CallExpression(node) {
|
|
160
|
+
const effectContext = getEffectContext(node);
|
|
161
|
+
if (effectContext == null) return;
|
|
162
|
+
facts.calls.push({
|
|
163
|
+
callee: Extract.unwrap(node.callee),
|
|
164
|
+
effect: effectContext.effect,
|
|
165
|
+
enclosingFunction: Traverse.findParent(node, Check.isFunction),
|
|
166
|
+
kind: getCallKind$5(node),
|
|
167
|
+
node,
|
|
168
|
+
phase: effectContext.phase
|
|
169
|
+
});
|
|
170
|
+
} }
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
// src/rules/no-leaked-event-listener/effect.ts
|
|
174
|
+
function getStaticMemberPropertyName(node) {
|
|
175
|
+
const property = Extract.unwrap(node.property);
|
|
176
|
+
if (!node.computed) return Check.isIdentifier(property) ? property.name : null;
|
|
177
|
+
if (property.type === AST_NODE_TYPES.Literal && typeof property.value === "string") return property.value;
|
|
178
|
+
if (property.type === AST_NODE_TYPES.TemplateLiteral && property.expressions.length === 0) return property.quasis[0]?.value.cooked ?? property.quasis[0]?.value.raw ?? null;
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
function isSameEventExpression(a, b, isEqual = Compare.isEqual) {
|
|
182
|
+
const exprA = Extract.unwrap(a);
|
|
183
|
+
const exprB = Extract.unwrap(b);
|
|
184
|
+
if (exprA.type !== AST_NODE_TYPES.MemberExpression) return isEqual(exprA, exprB);
|
|
185
|
+
if (exprB.type !== AST_NODE_TYPES.MemberExpression) return isEqual(exprA, exprB);
|
|
186
|
+
if (exprA.optional !== exprB.optional) return isEqual(exprA, exprB);
|
|
187
|
+
const propertyA = getStaticMemberPropertyName(exprA);
|
|
188
|
+
const propertyB = getStaticMemberPropertyName(exprB);
|
|
189
|
+
if (propertyA == null) return isEqual(exprA, exprB);
|
|
190
|
+
if (propertyB == null) return isEqual(exprA, exprB);
|
|
191
|
+
if (propertyA !== propertyB) return isEqual(exprA, exprB);
|
|
192
|
+
return isSameEventExpression(exprA.object, exprB.object, isEqual);
|
|
193
|
+
}
|
|
194
|
+
function isInverseEntry({ context, addEntry, remEntry, cleanupCalleeEntries }) {
|
|
195
|
+
const { type: aType, callee: aCallee, capture: aCapture, effect: aEffect, listener: aListener, phase: aPhase } = addEntry;
|
|
196
|
+
const { type: rType, callee: rCallee, capture: rCapture, effect: rEffect, listener: rListener, phase: rPhase } = remEntry;
|
|
197
|
+
if (aPhase !== "setup") return false;
|
|
198
|
+
if (!isSameObject(aCallee, rCallee, isSameEventExpression)) return false;
|
|
199
|
+
if (!isSameEventExpression(aListener, rListener)) return false;
|
|
200
|
+
if (!isSameEventExpression(aType, rType, (a, b) => isProvenanceEqual(context, a, b))) return false;
|
|
201
|
+
if (aCapture !== rCapture) return false;
|
|
202
|
+
if (rPhase === "cleanup") return true;
|
|
203
|
+
if (aEffect == null || aEffect !== rEffect) return false;
|
|
204
|
+
const enclosingFunction = remEntry.enclosingFunction;
|
|
205
|
+
if (enclosingFunction == null) return false;
|
|
206
|
+
const callees = cleanupCalleeEntries.get(aEffect);
|
|
207
|
+
if (callees == null) return false;
|
|
208
|
+
const { functions, names } = callees;
|
|
209
|
+
if (functions.has(enclosingFunction)) return true;
|
|
210
|
+
const id = getFunctionId(enclosingFunction);
|
|
211
|
+
if (id == null) return false;
|
|
212
|
+
if (id.type !== AST_NODE_TYPES.Identifier) return false;
|
|
213
|
+
return names.has(id.name);
|
|
214
|
+
}
|
|
215
|
+
function inferEventListenerViolations(context, facts) {
|
|
216
|
+
const violations = [];
|
|
217
|
+
const addEntries = [];
|
|
218
|
+
const removeEntries = [];
|
|
219
|
+
for (const entry of facts.entries) {
|
|
220
|
+
if (entry.signal == null && Check.isFunction(entry.listener)) violations.push({
|
|
221
|
+
kind: "unexpected-inline-function",
|
|
222
|
+
data: { eventMethodKind: entry.kind },
|
|
223
|
+
node: entry.listener
|
|
224
|
+
});
|
|
225
|
+
if (entry.kind === "addEventListener") addEntries.push(entry);
|
|
226
|
+
else removeEntries.push(entry);
|
|
227
|
+
}
|
|
228
|
+
for (const addEntry of addEntries) {
|
|
229
|
+
if (addEntry.signal != null) continue;
|
|
230
|
+
if (removeEntries.some((remEntry) => isInverseEntry({
|
|
231
|
+
context,
|
|
232
|
+
addEntry,
|
|
233
|
+
remEntry,
|
|
234
|
+
cleanupCalleeEntries: facts.cleanupCalleeEntries
|
|
235
|
+
}))) continue;
|
|
236
|
+
const effect = addEntry.effect;
|
|
237
|
+
const effectMethodKind = effect != null && effect.type === AST_NODE_TYPES.CallExpression ? Extract.getCalleeName(effect) ?? "useEffect" : "useEffect";
|
|
238
|
+
violations.push({
|
|
239
|
+
kind: "expected-remove-event-listener-in-cleanup",
|
|
240
|
+
data: { effectMethodKind },
|
|
241
|
+
node: addEntry.node
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
return violations.toSorted((a, b) => a.node.range[0] - b.node.range[0] || a.node.range[1] - b.node.range[1]);
|
|
245
|
+
}
|
|
246
|
+
// src/rules/no-leaked-event-listener/origins.ts
|
|
247
|
+
function resolveCleanupCallee({ context, node }) {
|
|
248
|
+
const callee = Extract.unwrap(node.callee);
|
|
249
|
+
if (callee.type === AST_NODE_TYPES.Identifier) {
|
|
250
|
+
const resolved = resolve(context, callee, { at: -1 });
|
|
251
|
+
const fn = resolved == null ? null : Extract.unwrap(resolved);
|
|
252
|
+
return fn != null && Check.isFunction(fn) ? {
|
|
253
|
+
kind: "function",
|
|
254
|
+
node: fn
|
|
255
|
+
} : {
|
|
256
|
+
kind: "name",
|
|
257
|
+
name: callee.name
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
if (callee.type !== AST_NODE_TYPES.MemberExpression || callee.computed) return null;
|
|
261
|
+
const object = Extract.unwrap(callee.object);
|
|
262
|
+
const property = Extract.unwrap(callee.property);
|
|
263
|
+
if (object.type !== AST_NODE_TYPES.Identifier || property.type !== AST_NODE_TYPES.Identifier) return null;
|
|
264
|
+
const init = resolve(context, object, { at: -1 });
|
|
265
|
+
const objectExpression = init == null ? null : Extract.unwrap(init);
|
|
266
|
+
if (objectExpression?.type !== AST_NODE_TYPES.ObjectExpression) return null;
|
|
267
|
+
for (const prop of objectExpression.properties) {
|
|
268
|
+
if (prop.type !== AST_NODE_TYPES.Property || prop.computed) continue;
|
|
269
|
+
const key = Extract.unwrap(prop.key);
|
|
270
|
+
if (Check.isIdentifier(key) ? key.name !== property.name : key.value !== property.name) continue;
|
|
271
|
+
const value = Extract.unwrap(prop.value);
|
|
272
|
+
if (Check.isFunction(value)) return {
|
|
273
|
+
kind: "function",
|
|
274
|
+
node: value
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
const defaultListenerOptions = {
|
|
280
|
+
capture: false,
|
|
281
|
+
signal: null
|
|
282
|
+
};
|
|
130
283
|
function isSignalPropertyBinding(context, node) {
|
|
131
284
|
const defNode = findVariable(context.sourceCode.getScope(node), node)?.defs.at(-1)?.node;
|
|
132
285
|
if (defNode?.type !== AST_NODE_TYPES.VariableDeclarator || defNode.id.type !== AST_NODE_TYPES.ObjectPattern) return false;
|
|
@@ -139,41 +292,56 @@ function isSignalPropertyBinding(context, node) {
|
|
|
139
292
|
}
|
|
140
293
|
return false;
|
|
141
294
|
}
|
|
142
|
-
function getSignalValueExpression(context, node) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const
|
|
151
|
-
|
|
295
|
+
function getSignalValueExpression({ context, node }) {
|
|
296
|
+
const seen = /* @__PURE__ */ new Set();
|
|
297
|
+
function visit(node) {
|
|
298
|
+
if (node == null) return null;
|
|
299
|
+
if (seen.has(node)) return null;
|
|
300
|
+
seen.add(node);
|
|
301
|
+
switch (node.type) {
|
|
302
|
+
case AST_NODE_TYPES.Identifier: {
|
|
303
|
+
const resolved = resolveOrigin(context, node);
|
|
304
|
+
const unwrapped = resolved == null ? null : Extract.unwrap(resolved);
|
|
305
|
+
if (unwrapped != null && Check.isFunction(unwrapped)) return node;
|
|
306
|
+
if (unwrapped != null && unwrapped.type === AST_NODE_TYPES.NewExpression && isSignalPropertyBinding(context, node)) {
|
|
307
|
+
const newCallee = Extract.unwrap(unwrapped.callee);
|
|
308
|
+
if (newCallee.type === AST_NODE_TYPES.Identifier && newCallee.name === "AbortController") return node;
|
|
309
|
+
}
|
|
310
|
+
return visit(unwrapped);
|
|
152
311
|
}
|
|
153
|
-
return
|
|
312
|
+
case AST_NODE_TYPES.MemberExpression: return node;
|
|
313
|
+
default: return null;
|
|
154
314
|
}
|
|
155
|
-
case AST_NODE_TYPES.MemberExpression: return node;
|
|
156
|
-
default: return null;
|
|
157
315
|
}
|
|
316
|
+
return visit(node);
|
|
158
317
|
}
|
|
159
|
-
|
|
160
|
-
capture: false,
|
|
161
|
-
signal: null
|
|
162
|
-
};
|
|
163
|
-
function getOptions(context, node) {
|
|
318
|
+
function getListenerOptions({ context, node }) {
|
|
164
319
|
function visit(node) {
|
|
165
320
|
switch (node.type) {
|
|
166
321
|
case AST_NODE_TYPES.Identifier: {
|
|
167
322
|
const initNode = resolve(context, node);
|
|
168
323
|
if (initNode?.type === AST_NODE_TYPES.ObjectExpression) return visit(initNode);
|
|
169
|
-
return
|
|
324
|
+
return defaultListenerOptions;
|
|
170
325
|
}
|
|
171
326
|
case AST_NODE_TYPES.Literal: return {
|
|
172
|
-
...
|
|
327
|
+
...defaultListenerOptions,
|
|
173
328
|
capture: Boolean(node.value)
|
|
174
329
|
};
|
|
175
330
|
case AST_NODE_TYPES.ObjectExpression: {
|
|
176
|
-
const
|
|
331
|
+
const findOption = (properties, name) => {
|
|
332
|
+
for (const property of properties) {
|
|
333
|
+
if (property.type === AST_NODE_TYPES.Property) {
|
|
334
|
+
const key = Extract.unwrap(property.key);
|
|
335
|
+
if ((!property.computed && Check.isIdentifier(key) ? key.name : key.type === AST_NODE_TYPES.Literal && typeof key.value === "string" ? key.value : key.type === AST_NODE_TYPES.TemplateLiteral && key.expressions.length === 0 ? key.quasis[0]?.value.cooked ?? key.quasis[0]?.value.raw ?? null : null) === name) return property;
|
|
336
|
+
}
|
|
337
|
+
if (property.type === AST_NODE_TYPES.SpreadElement && property.argument.type === AST_NODE_TYPES.ObjectExpression) {
|
|
338
|
+
const found = findOption(property.argument.properties, name);
|
|
339
|
+
if (found != null) return found;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
return null;
|
|
343
|
+
};
|
|
344
|
+
const pCapture = findOption(node.properties, "capture");
|
|
177
345
|
const vCapture = match(pCapture).with(P.nullish, () => false).with({ type: AST_NODE_TYPES.Property }, (prop) => {
|
|
178
346
|
const value = prop.value;
|
|
179
347
|
switch (value.type) {
|
|
@@ -181,62 +349,78 @@ function getOptions(context, node) {
|
|
|
181
349
|
default: return Boolean(getStaticValue(value, context.sourceCode.getScope(node))?.value);
|
|
182
350
|
}
|
|
183
351
|
}).otherwise(() => false);
|
|
184
|
-
const pSignal =
|
|
352
|
+
const pSignal = findOption(node.properties, "signal");
|
|
185
353
|
return {
|
|
186
354
|
capture: vCapture,
|
|
187
|
-
signal: pSignal?.type === AST_NODE_TYPES.Property ? getSignalValueExpression(
|
|
355
|
+
signal: pSignal?.type === AST_NODE_TYPES.Property ? getSignalValueExpression({
|
|
356
|
+
context,
|
|
357
|
+
node: pSignal.value
|
|
358
|
+
}) : null
|
|
188
359
|
};
|
|
189
360
|
}
|
|
190
|
-
default: return
|
|
361
|
+
default: return defaultListenerOptions;
|
|
191
362
|
}
|
|
192
363
|
}
|
|
193
364
|
return visit(node);
|
|
194
365
|
}
|
|
195
|
-
|
|
196
|
-
const
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
366
|
+
function getListenerArguments({ context, node }) {
|
|
367
|
+
const [type, listener, options] = node.arguments;
|
|
368
|
+
if (type == null || listener == null) return null;
|
|
369
|
+
return {
|
|
370
|
+
type,
|
|
371
|
+
listener,
|
|
372
|
+
options: options == null ? defaultListenerOptions : getListenerOptions({
|
|
373
|
+
context,
|
|
374
|
+
node: options
|
|
375
|
+
})
|
|
376
|
+
};
|
|
202
377
|
}
|
|
203
|
-
function
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
if (init.type !== AST_NODE_TYPES.ObjectExpression) return null;
|
|
226
|
-
for (const prop of init.properties) {
|
|
227
|
-
if (prop.type !== AST_NODE_TYPES.Property || prop.computed) continue;
|
|
228
|
-
const key = Extract.unwrap(prop.key);
|
|
229
|
-
if ((key.type === AST_NODE_TYPES.Identifier ? key.name : isString(key.value) ? key.value : null) !== property.name) continue;
|
|
230
|
-
const value = Extract.unwrap(prop.value);
|
|
231
|
-
if (Check.isFunction(value)) return {
|
|
232
|
-
kind: "function",
|
|
233
|
-
node: value
|
|
234
|
-
};
|
|
235
|
-
}
|
|
236
|
-
return null;
|
|
378
|
+
function isReactNativeMemberExpression(context, node) {
|
|
379
|
+
return node.type === AST_NODE_TYPES.MemberExpression && Check.isIdentifier(node.object) && isInitializedFromReactNative(node.object.name, context.sourceCode.getScope(node));
|
|
380
|
+
}
|
|
381
|
+
function resolveEventListenerFacts(context, facts) {
|
|
382
|
+
const entries = [];
|
|
383
|
+
const cleanupCalleeEntries = /* @__PURE__ */ new Map();
|
|
384
|
+
for (const fact of facts.calls) {
|
|
385
|
+
const { callee, effect, enclosingFunction, kind, node, phase } = fact;
|
|
386
|
+
if (kind === "other") {
|
|
387
|
+
if (phase !== "cleanup" || effect == null) continue;
|
|
388
|
+
const cleanupCallee = resolveCleanupCallee({
|
|
389
|
+
context,
|
|
390
|
+
node
|
|
391
|
+
});
|
|
392
|
+
if (cleanupCallee == null) continue;
|
|
393
|
+
const { functions, names } = getOrInsertComputed(cleanupCalleeEntries, effect, () => ({
|
|
394
|
+
functions: /* @__PURE__ */ new Set(),
|
|
395
|
+
names: /* @__PURE__ */ new Set()
|
|
396
|
+
}));
|
|
397
|
+
if (cleanupCallee.kind === "function") functions.add(cleanupCallee.node);
|
|
398
|
+
else names.add(cleanupCallee.name);
|
|
399
|
+
continue;
|
|
237
400
|
}
|
|
238
|
-
|
|
401
|
+
if (kind === "addEventListener" && isReactNativeMemberExpression(context, callee)) continue;
|
|
402
|
+
const args = getListenerArguments({
|
|
403
|
+
context,
|
|
404
|
+
node
|
|
405
|
+
});
|
|
406
|
+
if (args == null) continue;
|
|
407
|
+
entries.push({
|
|
408
|
+
type: args.type,
|
|
409
|
+
callee,
|
|
410
|
+
capture: args.options.capture,
|
|
411
|
+
effect,
|
|
412
|
+
enclosingFunction,
|
|
413
|
+
kind,
|
|
414
|
+
listener: args.listener,
|
|
415
|
+
node,
|
|
416
|
+
phase,
|
|
417
|
+
signal: args.options.signal
|
|
418
|
+
});
|
|
239
419
|
}
|
|
420
|
+
return {
|
|
421
|
+
entries,
|
|
422
|
+
cleanupCalleeEntries
|
|
423
|
+
};
|
|
240
424
|
}
|
|
241
425
|
var no_leaked_event_listener_default = createRule({
|
|
242
426
|
meta: {
|
|
@@ -248,116 +432,21 @@ var no_leaked_event_listener_default = createRule({
|
|
|
248
432
|
},
|
|
249
433
|
schema: []
|
|
250
434
|
},
|
|
251
|
-
name:
|
|
435
|
+
name: "no-leaked-event-listener",
|
|
252
436
|
create: create$5
|
|
253
437
|
});
|
|
254
438
|
function create$5(context) {
|
|
255
439
|
if (!context.sourceCode.text.includes("addEventListener")) return {};
|
|
256
440
|
if (!/use\w*Effect/u.test(context.sourceCode.text)) return {};
|
|
257
|
-
const
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
switch (true) {
|
|
265
|
-
case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return Compare.isEqual(a.object, b.object);
|
|
266
|
-
case a.type === AST_NODE_TYPES.MemberExpression: return isGlobalObject(a.object);
|
|
267
|
-
case b.type === AST_NODE_TYPES.MemberExpression: return isGlobalObject(b.object);
|
|
268
|
-
default: return true;
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
function isInverseEntry(aEntry, rEntry) {
|
|
272
|
-
const { type: aType, callee: aCallee, capture: aCapture, effect: aEffect, listener: aListener, phase: aPhase } = aEntry;
|
|
273
|
-
const { type: rType, callee: rCallee, capture: rCapture, effect: rEffect, listener: rListener, phase: rPhase } = rEntry;
|
|
274
|
-
if (aPhase !== "setup") return false;
|
|
275
|
-
if (!isSameObject(aCallee, rCallee) || !Compare.isEqual(aListener, rListener) || !isValueEqual(context, aType, rType) || aCapture !== rCapture) return false;
|
|
276
|
-
if (rPhase === "cleanup") return true;
|
|
277
|
-
if (aEffect == null || aEffect !== rEffect) return false;
|
|
278
|
-
const enclosingFunction = Traverse.findParent(rEntry.node, Check.isFunction);
|
|
279
|
-
if (enclosingFunction == null) return false;
|
|
280
|
-
const callees = cleanupCallees.get(aEffect);
|
|
281
|
-
if (callees == null) return false;
|
|
282
|
-
if (callees.functions.has(enclosingFunction)) return true;
|
|
283
|
-
const id = getFunctionId(enclosingFunction);
|
|
284
|
-
return id != null && id.type === AST_NODE_TYPES.Identifier && callees.names.has(id.name);
|
|
285
|
-
}
|
|
286
|
-
function visitInlineFunction(node, callKind, options) {
|
|
287
|
-
const listener = node.arguments.at(1);
|
|
288
|
-
if (!Check.isFunction(listener)) return;
|
|
289
|
-
if (options.signal != null) return;
|
|
290
|
-
context.report({
|
|
291
|
-
data: { eventMethodKind: callKind },
|
|
292
|
-
messageId: "unexpected-inline-function",
|
|
293
|
-
node: listener
|
|
441
|
+
const { facts, visitor } = createFactCollector();
|
|
442
|
+
return merge(visitor, { "Program:exit"() {
|
|
443
|
+
const resolvedFacts = resolveEventListenerFacts(context, facts);
|
|
444
|
+
for (const violation of inferEventListenerViolations(context, resolvedFacts)) context.report({
|
|
445
|
+
data: violation.data,
|
|
446
|
+
messageId: violation.kind,
|
|
447
|
+
node: violation.node
|
|
294
448
|
});
|
|
295
|
-
}
|
|
296
|
-
return {
|
|
297
|
-
["CallExpression"](node) {
|
|
298
|
-
const fn = Traverse.findParent(node, isUseEffectCallback$5);
|
|
299
|
-
if (fn == null) return;
|
|
300
|
-
const fKind = isUseEffectSetupCallback(fn) ? "setup" : "cleanup";
|
|
301
|
-
const setupFn = fKind === "setup" ? fn : Traverse.findParent(fn, Check.isFunction);
|
|
302
|
-
const effect = setupFn == null ? null : Extract.unwrap(setupFn).parent ?? null;
|
|
303
|
-
const callee = Extract.unwrap(node.callee);
|
|
304
|
-
match(getCallKind$5(node)).with("addEventListener", (callKind) => {
|
|
305
|
-
if (callee.type === AST_NODE_TYPES.MemberExpression && Check.isIdentifier(callee.object) && isInitializedFromReactNative(callee.object.name, context.sourceCode.getScope(node))) return;
|
|
306
|
-
const [type, listener, options] = node.arguments;
|
|
307
|
-
if (type == null || listener == null) return;
|
|
308
|
-
const opts = options == null ? defaultOptions : getOptions(context, options);
|
|
309
|
-
visitInlineFunction(node, callKind, opts);
|
|
310
|
-
aEntries.push({
|
|
311
|
-
...opts,
|
|
312
|
-
type,
|
|
313
|
-
callee,
|
|
314
|
-
effect,
|
|
315
|
-
listener,
|
|
316
|
-
method: "addEventListener",
|
|
317
|
-
node,
|
|
318
|
-
phase: fKind
|
|
319
|
-
});
|
|
320
|
-
}).with("removeEventListener", (callKind) => {
|
|
321
|
-
const [type, listener, options] = node.arguments;
|
|
322
|
-
if (type == null || listener == null) return;
|
|
323
|
-
const opts = options == null ? defaultOptions : getOptions(context, options);
|
|
324
|
-
visitInlineFunction(node, callKind, opts);
|
|
325
|
-
rEntries.push({
|
|
326
|
-
type,
|
|
327
|
-
callee,
|
|
328
|
-
capture: opts.capture,
|
|
329
|
-
effect,
|
|
330
|
-
listener,
|
|
331
|
-
method: "removeEventListener",
|
|
332
|
-
node,
|
|
333
|
-
phase: fKind
|
|
334
|
-
});
|
|
335
|
-
}).with("other", () => {
|
|
336
|
-
if (fKind !== "cleanup" || effect == null) return;
|
|
337
|
-
const callee = resolveCleanupCallee(context, node);
|
|
338
|
-
if (callee == null) return;
|
|
339
|
-
const callees = getOrInsertComputed(cleanupCallees, effect, () => ({
|
|
340
|
-
functions: /* @__PURE__ */ new Set(),
|
|
341
|
-
names: /* @__PURE__ */ new Set()
|
|
342
|
-
}));
|
|
343
|
-
if (callee.kind === "function") callees.functions.add(callee.node);
|
|
344
|
-
else callees.names.add(callee.name);
|
|
345
|
-
}).otherwise(() => null);
|
|
346
|
-
},
|
|
347
|
-
["Program:exit"]() {
|
|
348
|
-
for (const aEntry of aEntries) {
|
|
349
|
-
if (aEntry.signal != null) continue;
|
|
350
|
-
if (rEntries.some((rEntry) => isInverseEntry(aEntry, rEntry))) continue;
|
|
351
|
-
const effect = aEntry.effect;
|
|
352
|
-
const effectMethodKind = effect != null && effect.type === AST_NODE_TYPES.CallExpression ? Extract.getCalleeName(effect) ?? "useEffect" : "useEffect";
|
|
353
|
-
context.report({
|
|
354
|
-
data: { effectMethodKind },
|
|
355
|
-
messageId: "expected-remove-event-listener-in-cleanup",
|
|
356
|
-
node: aEntry.node
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
};
|
|
449
|
+
} });
|
|
361
450
|
}
|
|
362
451
|
// src/rules/no-leaked-fetch/lib.ts
|
|
363
452
|
function resolveToObjectExpression(context, node) {
|
|
@@ -427,7 +516,20 @@ function getFetchController(context, node) {
|
|
|
427
516
|
controller: null,
|
|
428
517
|
isParamSignal: false
|
|
429
518
|
};
|
|
430
|
-
const
|
|
519
|
+
const findSignal = (properties) => {
|
|
520
|
+
for (const property of properties) {
|
|
521
|
+
if (property.type === AST_NODE_TYPES.Property) {
|
|
522
|
+
const key = Extract.unwrap(property.key);
|
|
523
|
+
if ((!property.computed && Check.isIdentifier(key) ? key.name : key.type === AST_NODE_TYPES.Literal && typeof key.value === "string" ? key.value : key.type === AST_NODE_TYPES.TemplateLiteral && key.expressions.length === 0 ? key.quasis[0]?.value.cooked ?? key.quasis[0]?.value.raw ?? null : null) === "signal") return property;
|
|
524
|
+
}
|
|
525
|
+
if (property.type === AST_NODE_TYPES.SpreadElement && property.argument.type === AST_NODE_TYPES.ObjectExpression) {
|
|
526
|
+
const found = findSignal(property.argument.properties);
|
|
527
|
+
if (found != null) return found;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
return null;
|
|
531
|
+
};
|
|
532
|
+
const signalProp = findSignal(options.properties);
|
|
431
533
|
if (signalProp?.type !== AST_NODE_TYPES.Property) return {
|
|
432
534
|
controller: null,
|
|
433
535
|
isParamSignal: false
|
|
@@ -537,8 +639,10 @@ function getCallKind$3(context, node) {
|
|
|
537
639
|
const callee = Extract.unwrap(node.callee);
|
|
538
640
|
if (callee.type !== AST_NODE_TYPES.MemberExpression) return "other";
|
|
539
641
|
const name = Extract.getCalleeName(node);
|
|
540
|
-
if (name
|
|
541
|
-
return "other";
|
|
642
|
+
if (name == null) return "other";
|
|
643
|
+
if (!isMatching(P.union("observe", "unobserve", "disconnect"))(name)) return "other";
|
|
644
|
+
if (!isFromObserver$1(context, callee, "IntersectionObserver")) return "other";
|
|
645
|
+
return name;
|
|
542
646
|
}
|
|
543
647
|
function isUseRefInitialValue$1(node) {
|
|
544
648
|
let child = node;
|
|
@@ -654,7 +758,7 @@ function create$3(context) {
|
|
|
654
758
|
if (dEntries.some((e) => !isInsideObserverCallback(e.node) && isInCleanupPhase(e.node) && isSameObserver(e.observer))) continue;
|
|
655
759
|
const matchedOEntries = oEntries.filter((e) => isSameObserver(e.observer));
|
|
656
760
|
const matchedUEntries = uEntries.filter((e) => isSameObserver(e.observer));
|
|
657
|
-
const isDynamic = or(Check.isCallExpression, Check.
|
|
761
|
+
const isDynamic = or(Check.isCallExpression, Check.isConditionalOrLoop);
|
|
658
762
|
if (matchedOEntries.some((e) => {
|
|
659
763
|
const entryPhaseNode = phaseNode ?? Traverse.findParent(e.node, isUseEffectCallback$3);
|
|
660
764
|
const isPhaseNode = (n) => n === entryPhaseNode;
|
|
@@ -789,8 +893,10 @@ function getCallKind$1(context, node) {
|
|
|
789
893
|
const callee = Extract.unwrap(node.callee);
|
|
790
894
|
if (callee.type !== AST_NODE_TYPES.MemberExpression) return "other";
|
|
791
895
|
const name = Extract.getCalleeName(node);
|
|
792
|
-
if (name
|
|
793
|
-
return "other";
|
|
896
|
+
if (name == null) return "other";
|
|
897
|
+
if (!isMatching(P.union("observe", "unobserve", "disconnect"))(name)) return "other";
|
|
898
|
+
if (!isFromObserver(context, callee, "ResizeObserver")) return "other";
|
|
899
|
+
return name;
|
|
794
900
|
}
|
|
795
901
|
function isUseRefInitialValue(node) {
|
|
796
902
|
let child = node;
|
|
@@ -906,7 +1012,7 @@ function create$1(context) {
|
|
|
906
1012
|
if (dEntries.some((e) => !isInsideObserverCallback(e.node) && isInCleanupPhase(e.node) && isSameObserver(e.observer))) continue;
|
|
907
1013
|
const matchedOEntries = oEntries.filter((e) => isSameObserver(e.observer));
|
|
908
1014
|
const matchedUEntries = uEntries.filter((e) => isSameObserver(e.observer));
|
|
909
|
-
const isDynamic = (node) => node?.type === AST_NODE_TYPES.CallExpression || Check.
|
|
1015
|
+
const isDynamic = (node) => node?.type === AST_NODE_TYPES.CallExpression || Check.isConditionalOrLoop(node);
|
|
910
1016
|
if (matchedOEntries.some((e) => {
|
|
911
1017
|
const entryPhaseNode = phaseNode ?? Traverse.findParent(e.node, isUseEffectCallback$1);
|
|
912
1018
|
const isPhaseNode = (node) => node === entryPhaseNode;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-react-web-api",
|
|
3
|
-
"version": "5.30.0-beta.
|
|
3
|
+
"version": "5.30.0-beta.21",
|
|
4
4
|
"description": "ESLint React's ESLint plugin for interacting with Web APIs",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -37,13 +37,13 @@
|
|
|
37
37
|
"dist"
|
|
38
38
|
],
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@eslint-react/ast": "5.30.0-beta.
|
|
41
|
-
"@eslint-react/core": "5.30.0-beta.
|
|
42
|
-
"@eslint-react/eslint": "5.30.0-beta.
|
|
43
|
-
"@eslint-react/shared": "5.30.0-beta.
|
|
44
|
-
"@eslint-react/var": "5.30.0-beta.
|
|
45
|
-
"@typescript-eslint/types": "^8.72.
|
|
46
|
-
"@typescript-eslint/utils": "^8.72.
|
|
40
|
+
"@eslint-react/ast": "5.30.0-beta.21",
|
|
41
|
+
"@eslint-react/core": "5.30.0-beta.21",
|
|
42
|
+
"@eslint-react/eslint": "5.30.0-beta.21",
|
|
43
|
+
"@eslint-react/shared": "5.30.0-beta.21",
|
|
44
|
+
"@eslint-react/var": "5.30.0-beta.21",
|
|
45
|
+
"@typescript-eslint/types": "^8.72.1",
|
|
46
|
+
"@typescript-eslint/utils": "^8.72.1",
|
|
47
47
|
"ts-pattern": "^5.9.2"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
@@ -55,8 +55,8 @@
|
|
|
55
55
|
"@types/react": "^19.3.0",
|
|
56
56
|
"@types/react-dom": "^19.3.0",
|
|
57
57
|
"dedent": "^1.7.2",
|
|
58
|
-
"eslint": "^10.
|
|
59
|
-
"tsdown": "^0.23.
|
|
58
|
+
"eslint": "^10.14.0",
|
|
59
|
+
"tsdown": "^0.23.4",
|
|
60
60
|
"typescript": "6.0.3"
|
|
61
61
|
},
|
|
62
62
|
"peerDependencies": {
|