eslint-plugin-react-web-api 5.30.0-beta.8 → 5.30.0-rc.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/dist/index.js +291 -187
- package/package.json +8 -8
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, isProvenanceEqual, 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-
|
|
23
|
+
var version = "5.30.0-rc.0";
|
|
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,43 +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
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (
|
|
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);
|
|
154
311
|
}
|
|
155
|
-
|
|
312
|
+
case AST_NODE_TYPES.MemberExpression: return node;
|
|
313
|
+
default: return null;
|
|
156
314
|
}
|
|
157
|
-
case AST_NODE_TYPES.MemberExpression: return node;
|
|
158
|
-
default: return null;
|
|
159
315
|
}
|
|
316
|
+
return visit(node);
|
|
160
317
|
}
|
|
161
|
-
|
|
162
|
-
capture: false,
|
|
163
|
-
signal: null
|
|
164
|
-
};
|
|
165
|
-
function getOptions(context, node) {
|
|
318
|
+
function getListenerOptions({ context, node }) {
|
|
166
319
|
function visit(node) {
|
|
167
320
|
switch (node.type) {
|
|
168
321
|
case AST_NODE_TYPES.Identifier: {
|
|
169
322
|
const initNode = resolve(context, node);
|
|
170
323
|
if (initNode?.type === AST_NODE_TYPES.ObjectExpression) return visit(initNode);
|
|
171
|
-
return
|
|
324
|
+
return defaultListenerOptions;
|
|
172
325
|
}
|
|
173
326
|
case AST_NODE_TYPES.Literal: return {
|
|
174
|
-
...
|
|
327
|
+
...defaultListenerOptions,
|
|
175
328
|
capture: Boolean(node.value)
|
|
176
329
|
};
|
|
177
330
|
case AST_NODE_TYPES.ObjectExpression: {
|
|
178
|
-
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");
|
|
179
345
|
const vCapture = match(pCapture).with(P.nullish, () => false).with({ type: AST_NODE_TYPES.Property }, (prop) => {
|
|
180
346
|
const value = prop.value;
|
|
181
347
|
switch (value.type) {
|
|
@@ -183,62 +349,78 @@ function getOptions(context, node) {
|
|
|
183
349
|
default: return Boolean(getStaticValue(value, context.sourceCode.getScope(node))?.value);
|
|
184
350
|
}
|
|
185
351
|
}).otherwise(() => false);
|
|
186
|
-
const pSignal =
|
|
352
|
+
const pSignal = findOption(node.properties, "signal");
|
|
187
353
|
return {
|
|
188
354
|
capture: vCapture,
|
|
189
|
-
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
|
|
190
359
|
};
|
|
191
360
|
}
|
|
192
|
-
default: return
|
|
361
|
+
default: return defaultListenerOptions;
|
|
193
362
|
}
|
|
194
363
|
}
|
|
195
364
|
return visit(node);
|
|
196
365
|
}
|
|
197
|
-
|
|
198
|
-
const
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
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
|
+
};
|
|
204
377
|
}
|
|
205
|
-
function
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
if (init.type !== AST_NODE_TYPES.ObjectExpression) return null;
|
|
228
|
-
for (const prop of init.properties) {
|
|
229
|
-
if (prop.type !== AST_NODE_TYPES.Property || prop.computed) continue;
|
|
230
|
-
const key = Extract.unwrap(prop.key);
|
|
231
|
-
if ((key.type === AST_NODE_TYPES.Identifier ? key.name : isString(key.value) ? key.value : null) !== property.name) continue;
|
|
232
|
-
const value = Extract.unwrap(prop.value);
|
|
233
|
-
if (Check.isFunction(value)) return {
|
|
234
|
-
kind: "function",
|
|
235
|
-
node: value
|
|
236
|
-
};
|
|
237
|
-
}
|
|
238
|
-
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;
|
|
239
400
|
}
|
|
240
|
-
|
|
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
|
+
});
|
|
241
419
|
}
|
|
420
|
+
return {
|
|
421
|
+
entries,
|
|
422
|
+
cleanupCalleeEntries
|
|
423
|
+
};
|
|
242
424
|
}
|
|
243
425
|
var no_leaked_event_listener_default = createRule({
|
|
244
426
|
meta: {
|
|
@@ -250,116 +432,21 @@ var no_leaked_event_listener_default = createRule({
|
|
|
250
432
|
},
|
|
251
433
|
schema: []
|
|
252
434
|
},
|
|
253
|
-
name:
|
|
435
|
+
name: "no-leaked-event-listener",
|
|
254
436
|
create: create$5
|
|
255
437
|
});
|
|
256
438
|
function create$5(context) {
|
|
257
439
|
if (!context.sourceCode.text.includes("addEventListener")) return {};
|
|
258
440
|
if (!/use\w*Effect/u.test(context.sourceCode.text)) return {};
|
|
259
|
-
const
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
switch (true) {
|
|
267
|
-
case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return Compare.isEqual(a.object, b.object);
|
|
268
|
-
case a.type === AST_NODE_TYPES.MemberExpression: return isGlobalObject(a.object);
|
|
269
|
-
case b.type === AST_NODE_TYPES.MemberExpression: return isGlobalObject(b.object);
|
|
270
|
-
default: return true;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
function isInverseEntry(aEntry, rEntry) {
|
|
274
|
-
const { type: aType, callee: aCallee, capture: aCapture, effect: aEffect, listener: aListener, phase: aPhase } = aEntry;
|
|
275
|
-
const { type: rType, callee: rCallee, capture: rCapture, effect: rEffect, listener: rListener, phase: rPhase } = rEntry;
|
|
276
|
-
if (aPhase !== "setup") return false;
|
|
277
|
-
if (!isSameObject(aCallee, rCallee) || !Compare.isEqual(aListener, rListener) || !isProvenanceEqual(context, aType, rType) || aCapture !== rCapture) return false;
|
|
278
|
-
if (rPhase === "cleanup") return true;
|
|
279
|
-
if (aEffect == null || aEffect !== rEffect) return false;
|
|
280
|
-
const enclosingFunction = Traverse.findParent(rEntry.node, Check.isFunction);
|
|
281
|
-
if (enclosingFunction == null) return false;
|
|
282
|
-
const callees = cleanupCallees.get(aEffect);
|
|
283
|
-
if (callees == null) return false;
|
|
284
|
-
if (callees.functions.has(enclosingFunction)) return true;
|
|
285
|
-
const id = getFunctionId(enclosingFunction);
|
|
286
|
-
return id != null && id.type === AST_NODE_TYPES.Identifier && callees.names.has(id.name);
|
|
287
|
-
}
|
|
288
|
-
function visitInlineFunction(node, callKind, options) {
|
|
289
|
-
const listener = node.arguments.at(1);
|
|
290
|
-
if (!Check.isFunction(listener)) return;
|
|
291
|
-
if (options.signal != null) return;
|
|
292
|
-
context.report({
|
|
293
|
-
data: { eventMethodKind: callKind },
|
|
294
|
-
messageId: "unexpected-inline-function",
|
|
295
|
-
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
|
|
296
448
|
});
|
|
297
|
-
}
|
|
298
|
-
return {
|
|
299
|
-
["CallExpression"](node) {
|
|
300
|
-
const fn = Traverse.findParent(node, isUseEffectCallback$5);
|
|
301
|
-
if (fn == null) return;
|
|
302
|
-
const fKind = isUseEffectSetupCallback(fn) ? "setup" : "cleanup";
|
|
303
|
-
const setupFn = fKind === "setup" ? fn : Traverse.findParent(fn, Check.isFunction);
|
|
304
|
-
const effect = setupFn == null ? null : Extract.unwrap(setupFn).parent ?? null;
|
|
305
|
-
const callee = Extract.unwrap(node.callee);
|
|
306
|
-
match(getCallKind$5(node)).with("addEventListener", (callKind) => {
|
|
307
|
-
if (callee.type === AST_NODE_TYPES.MemberExpression && Check.isIdentifier(callee.object) && isInitializedFromReactNative(callee.object.name, context.sourceCode.getScope(node))) return;
|
|
308
|
-
const [type, listener, options] = node.arguments;
|
|
309
|
-
if (type == null || listener == null) return;
|
|
310
|
-
const opts = options == null ? defaultOptions : getOptions(context, options);
|
|
311
|
-
visitInlineFunction(node, callKind, opts);
|
|
312
|
-
aEntries.push({
|
|
313
|
-
...opts,
|
|
314
|
-
type,
|
|
315
|
-
callee,
|
|
316
|
-
effect,
|
|
317
|
-
listener,
|
|
318
|
-
method: "addEventListener",
|
|
319
|
-
node,
|
|
320
|
-
phase: fKind
|
|
321
|
-
});
|
|
322
|
-
}).with("removeEventListener", (callKind) => {
|
|
323
|
-
const [type, listener, options] = node.arguments;
|
|
324
|
-
if (type == null || listener == null) return;
|
|
325
|
-
const opts = options == null ? defaultOptions : getOptions(context, options);
|
|
326
|
-
visitInlineFunction(node, callKind, opts);
|
|
327
|
-
rEntries.push({
|
|
328
|
-
type,
|
|
329
|
-
callee,
|
|
330
|
-
capture: opts.capture,
|
|
331
|
-
effect,
|
|
332
|
-
listener,
|
|
333
|
-
method: "removeEventListener",
|
|
334
|
-
node,
|
|
335
|
-
phase: fKind
|
|
336
|
-
});
|
|
337
|
-
}).with("other", () => {
|
|
338
|
-
if (fKind !== "cleanup" || effect == null) return;
|
|
339
|
-
const callee = resolveCleanupCallee(context, node);
|
|
340
|
-
if (callee == null) return;
|
|
341
|
-
const callees = getOrInsertComputed(cleanupCallees, effect, () => ({
|
|
342
|
-
functions: /* @__PURE__ */ new Set(),
|
|
343
|
-
names: /* @__PURE__ */ new Set()
|
|
344
|
-
}));
|
|
345
|
-
if (callee.kind === "function") callees.functions.add(callee.node);
|
|
346
|
-
else callees.names.add(callee.name);
|
|
347
|
-
}).otherwise(() => null);
|
|
348
|
-
},
|
|
349
|
-
["Program:exit"]() {
|
|
350
|
-
for (const aEntry of aEntries) {
|
|
351
|
-
if (aEntry.signal != null) continue;
|
|
352
|
-
if (rEntries.some((rEntry) => isInverseEntry(aEntry, rEntry))) continue;
|
|
353
|
-
const effect = aEntry.effect;
|
|
354
|
-
const effectMethodKind = effect != null && effect.type === AST_NODE_TYPES.CallExpression ? Extract.getCalleeName(effect) ?? "useEffect" : "useEffect";
|
|
355
|
-
context.report({
|
|
356
|
-
data: { effectMethodKind },
|
|
357
|
-
messageId: "expected-remove-event-listener-in-cleanup",
|
|
358
|
-
node: aEntry.node
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
};
|
|
449
|
+
} });
|
|
363
450
|
}
|
|
364
451
|
// src/rules/no-leaked-fetch/lib.ts
|
|
365
452
|
function resolveToObjectExpression(context, node) {
|
|
@@ -429,7 +516,20 @@ function getFetchController(context, node) {
|
|
|
429
516
|
controller: null,
|
|
430
517
|
isParamSignal: false
|
|
431
518
|
};
|
|
432
|
-
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);
|
|
433
533
|
if (signalProp?.type !== AST_NODE_TYPES.Property) return {
|
|
434
534
|
controller: null,
|
|
435
535
|
isParamSignal: false
|
|
@@ -539,8 +639,10 @@ function getCallKind$3(context, node) {
|
|
|
539
639
|
const callee = Extract.unwrap(node.callee);
|
|
540
640
|
if (callee.type !== AST_NODE_TYPES.MemberExpression) return "other";
|
|
541
641
|
const name = Extract.getCalleeName(node);
|
|
542
|
-
if (name
|
|
543
|
-
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;
|
|
544
646
|
}
|
|
545
647
|
function isUseRefInitialValue$1(node) {
|
|
546
648
|
let child = node;
|
|
@@ -656,7 +758,7 @@ function create$3(context) {
|
|
|
656
758
|
if (dEntries.some((e) => !isInsideObserverCallback(e.node) && isInCleanupPhase(e.node) && isSameObserver(e.observer))) continue;
|
|
657
759
|
const matchedOEntries = oEntries.filter((e) => isSameObserver(e.observer));
|
|
658
760
|
const matchedUEntries = uEntries.filter((e) => isSameObserver(e.observer));
|
|
659
|
-
const isDynamic = or(Check.isCallExpression, Check.
|
|
761
|
+
const isDynamic = or(Check.isCallExpression, Check.isConditionalOrLoop);
|
|
660
762
|
if (matchedOEntries.some((e) => {
|
|
661
763
|
const entryPhaseNode = phaseNode ?? Traverse.findParent(e.node, isUseEffectCallback$3);
|
|
662
764
|
const isPhaseNode = (n) => n === entryPhaseNode;
|
|
@@ -791,8 +893,10 @@ function getCallKind$1(context, node) {
|
|
|
791
893
|
const callee = Extract.unwrap(node.callee);
|
|
792
894
|
if (callee.type !== AST_NODE_TYPES.MemberExpression) return "other";
|
|
793
895
|
const name = Extract.getCalleeName(node);
|
|
794
|
-
if (name
|
|
795
|
-
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;
|
|
796
900
|
}
|
|
797
901
|
function isUseRefInitialValue(node) {
|
|
798
902
|
let child = node;
|
|
@@ -908,7 +1012,7 @@ function create$1(context) {
|
|
|
908
1012
|
if (dEntries.some((e) => !isInsideObserverCallback(e.node) && isInCleanupPhase(e.node) && isSameObserver(e.observer))) continue;
|
|
909
1013
|
const matchedOEntries = oEntries.filter((e) => isSameObserver(e.observer));
|
|
910
1014
|
const matchedUEntries = uEntries.filter((e) => isSameObserver(e.observer));
|
|
911
|
-
const isDynamic = (node) => node?.type === AST_NODE_TYPES.CallExpression || Check.
|
|
1015
|
+
const isDynamic = (node) => node?.type === AST_NODE_TYPES.CallExpression || Check.isConditionalOrLoop(node);
|
|
912
1016
|
if (matchedOEntries.some((e) => {
|
|
913
1017
|
const entryPhaseNode = phaseNode ?? Traverse.findParent(e.node, isUseEffectCallback$1);
|
|
914
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-
|
|
3
|
+
"version": "5.30.0-rc.0",
|
|
4
4
|
"description": "ESLint React's ESLint plugin for interacting with Web APIs",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -37,11 +37,11 @@
|
|
|
37
37
|
"dist"
|
|
38
38
|
],
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@eslint-react/ast": "5.30.0-
|
|
41
|
-
"@eslint-react/core": "5.30.0-
|
|
42
|
-
"@eslint-react/eslint": "5.30.0-
|
|
43
|
-
"@eslint-react/shared": "5.30.0-
|
|
44
|
-
"@eslint-react/var": "5.30.0-
|
|
40
|
+
"@eslint-react/ast": "5.30.0-rc.0",
|
|
41
|
+
"@eslint-react/core": "5.30.0-rc.0",
|
|
42
|
+
"@eslint-react/eslint": "5.30.0-rc.0",
|
|
43
|
+
"@eslint-react/shared": "5.30.0-rc.0",
|
|
44
|
+
"@eslint-react/var": "5.30.0-rc.0",
|
|
45
45
|
"@typescript-eslint/types": "^8.72.1",
|
|
46
46
|
"@typescript-eslint/utils": "^8.72.1",
|
|
47
47
|
"ts-pattern": "^5.9.2"
|
|
@@ -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.5",
|
|
60
60
|
"typescript": "6.0.3"
|
|
61
61
|
},
|
|
62
62
|
"peerDependencies": {
|