eslint-plugin-react-web-api 5.30.0-beta.14 → 5.30.0-beta.15
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 +166 -115
- package/package.json +7 -7
package/dist/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
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
|
-
import "@eslint-react/
|
|
5
|
-
import { isAssignmentTargetEqual, isInitializedFromReactNative, isProvenanceEqual, resolve, resolveEnclosingAssignmentTarget, resolveOrigin } from "@eslint-react/var";
|
|
5
|
+
import { getFunctionId, isUseEffectCleanupCallback, isUseEffectSetupCallback, isUseRefLikeCall } from "@eslint-react/core";
|
|
6
6
|
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
7
7
|
import { P, isMatching, match } from "ts-pattern";
|
|
8
|
-
import {
|
|
8
|
+
import { isAssignmentTargetEqual, isInitializedFromReactNative, isProvenanceEqual, resolve, resolveEnclosingAssignmentTarget, resolveOrigin } from "@eslint-react/var";
|
|
9
9
|
import { findVariable, getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
10
10
|
// \0rolldown/runtime.js
|
|
11
11
|
var __defProp = Object.defineProperty;
|
|
@@ -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.15";
|
|
24
24
|
// src/utils/create-rule.ts
|
|
25
25
|
function getDocsUrl(ruleName) {
|
|
26
26
|
return `https://eslint-react.xyz/docs/rules/web-api-${ruleName}`;
|
|
@@ -123,7 +123,7 @@ dual(2, (self, predicate) => {
|
|
|
123
123
|
while (idx < len && predicate(input[idx], idx)) idx++;
|
|
124
124
|
return input.slice(0, idx);
|
|
125
125
|
});
|
|
126
|
-
// src/rules/no-leaked-event-listener/
|
|
126
|
+
// src/rules/no-leaked-event-listener/helpers.ts
|
|
127
127
|
const isUseEffectCallback$5 = or(isUseEffectSetupCallback, isUseEffectCleanupCallback);
|
|
128
128
|
function getEffectContext(node) {
|
|
129
129
|
const fn = Traverse.findParent(node, isUseEffectCallback$5);
|
|
@@ -140,7 +140,86 @@ function getCallKind$5(node) {
|
|
|
140
140
|
if (name != null && isMatching(P.union("addEventListener", "removeEventListener"))(name)) return name;
|
|
141
141
|
return "other";
|
|
142
142
|
}
|
|
143
|
-
function
|
|
143
|
+
function isGlobalObject(node) {
|
|
144
|
+
return Check.isIdentifier(node) && (node.name === "window" || node.name === "globalThis" || node.name === "self");
|
|
145
|
+
}
|
|
146
|
+
function isSameObject(a, b) {
|
|
147
|
+
switch (true) {
|
|
148
|
+
case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return Compare.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 isInverseEntry({ context, addEntry, remEntry, cleanupCalleeEntries }) {
|
|
175
|
+
const { type: aType, callee: aCallee, capture: aCapture, effect: aEffect, listener: aListener, phase: aPhase } = addEntry;
|
|
176
|
+
const { type: rType, callee: rCallee, capture: rCapture, effect: rEffect, listener: rListener, phase: rPhase } = remEntry;
|
|
177
|
+
if (aPhase !== "setup") return false;
|
|
178
|
+
if (!isSameObject(aCallee, rCallee) || !Compare.isEqual(aListener, rListener) || !isProvenanceEqual(context, aType, rType) || aCapture !== rCapture) return false;
|
|
179
|
+
if (rPhase === "cleanup") return true;
|
|
180
|
+
if (aEffect == null || aEffect !== rEffect) return false;
|
|
181
|
+
const enclosingFunction = remEntry.enclosingFunction;
|
|
182
|
+
if (enclosingFunction == null) return false;
|
|
183
|
+
const callees = cleanupCalleeEntries.get(aEffect);
|
|
184
|
+
if (callees == null) return false;
|
|
185
|
+
const { functions, names } = callees;
|
|
186
|
+
if (functions.has(enclosingFunction)) return true;
|
|
187
|
+
const id = getFunctionId(enclosingFunction);
|
|
188
|
+
return id != null && id.type === AST_NODE_TYPES.Identifier && names.has(id.name);
|
|
189
|
+
}
|
|
190
|
+
function inferEventListenerViolations(context, facts) {
|
|
191
|
+
const violations = [];
|
|
192
|
+
const addEntries = [];
|
|
193
|
+
const removeEntries = [];
|
|
194
|
+
for (const entry of facts.entries) {
|
|
195
|
+
if (entry.signal == null && Check.isFunction(entry.listener)) violations.push({
|
|
196
|
+
kind: "unexpected-inline-function",
|
|
197
|
+
data: { eventMethodKind: entry.kind },
|
|
198
|
+
node: entry.listener
|
|
199
|
+
});
|
|
200
|
+
if (entry.kind === "addEventListener") addEntries.push(entry);
|
|
201
|
+
else removeEntries.push(entry);
|
|
202
|
+
}
|
|
203
|
+
for (const addEntry of addEntries) {
|
|
204
|
+
if (addEntry.signal != null) continue;
|
|
205
|
+
if (removeEntries.some((remEntry) => isInverseEntry({
|
|
206
|
+
context,
|
|
207
|
+
addEntry,
|
|
208
|
+
remEntry,
|
|
209
|
+
cleanupCalleeEntries: facts.cleanupCalleeEntries
|
|
210
|
+
}))) continue;
|
|
211
|
+
const effect = addEntry.effect;
|
|
212
|
+
const effectMethodKind = effect != null && effect.type === AST_NODE_TYPES.CallExpression ? Extract.getCalleeName(effect) ?? "useEffect" : "useEffect";
|
|
213
|
+
violations.push({
|
|
214
|
+
kind: "expected-remove-event-listener-in-cleanup",
|
|
215
|
+
data: { effectMethodKind },
|
|
216
|
+
node: addEntry.node
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
return violations.toSorted((a, b) => a.node.range[0] - b.node.range[0] || a.node.range[1] - b.node.range[1]);
|
|
220
|
+
}
|
|
221
|
+
// src/rules/no-leaked-event-listener/origins.ts
|
|
222
|
+
function resolveCleanupCallee({ context, node }) {
|
|
144
223
|
const callee = Extract.unwrap(node.callee);
|
|
145
224
|
if (callee.type === AST_NODE_TYPES.Identifier) {
|
|
146
225
|
const resolved = resolve(context, callee, { at: -1 });
|
|
@@ -186,26 +265,30 @@ function isSignalPropertyBinding(context, node) {
|
|
|
186
265
|
}
|
|
187
266
|
return false;
|
|
188
267
|
}
|
|
189
|
-
function getSignalValueExpression(context, node
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
if (
|
|
268
|
+
function getSignalValueExpression({ context, node }) {
|
|
269
|
+
const seen = /* @__PURE__ */ new Set();
|
|
270
|
+
function visit(node) {
|
|
271
|
+
if (node == null) return null;
|
|
272
|
+
if (seen.has(node)) return null;
|
|
273
|
+
seen.add(node);
|
|
274
|
+
switch (node.type) {
|
|
275
|
+
case AST_NODE_TYPES.Identifier: {
|
|
276
|
+
const resolved = resolveOrigin(context, node);
|
|
277
|
+
const unwrapped = resolved == null ? null : Extract.unwrap(resolved);
|
|
278
|
+
if (unwrapped != null && Check.isFunction(unwrapped)) return node;
|
|
279
|
+
if (unwrapped != null && unwrapped.type === AST_NODE_TYPES.NewExpression && isSignalPropertyBinding(context, node)) {
|
|
280
|
+
const newCallee = Extract.unwrap(unwrapped.callee);
|
|
281
|
+
if (newCallee.type === AST_NODE_TYPES.Identifier && newCallee.name === "AbortController") return node;
|
|
282
|
+
}
|
|
283
|
+
return visit(unwrapped);
|
|
201
284
|
}
|
|
202
|
-
|
|
285
|
+
case AST_NODE_TYPES.MemberExpression: return node;
|
|
286
|
+
default: return null;
|
|
203
287
|
}
|
|
204
|
-
case AST_NODE_TYPES.MemberExpression: return node;
|
|
205
|
-
default: return null;
|
|
206
288
|
}
|
|
289
|
+
return visit(node);
|
|
207
290
|
}
|
|
208
|
-
function getListenerOptions(context, node) {
|
|
291
|
+
function getListenerOptions({ context, node }) {
|
|
209
292
|
function visit(node) {
|
|
210
293
|
switch (node.type) {
|
|
211
294
|
case AST_NODE_TYPES.Identifier: {
|
|
@@ -229,7 +312,10 @@ function getListenerOptions(context, node) {
|
|
|
229
312
|
const pSignal = Extract.findProperty(node.properties, "signal");
|
|
230
313
|
return {
|
|
231
314
|
capture: vCapture,
|
|
232
|
-
signal: pSignal?.type === AST_NODE_TYPES.Property ? getSignalValueExpression(
|
|
315
|
+
signal: pSignal?.type === AST_NODE_TYPES.Property ? getSignalValueExpression({
|
|
316
|
+
context,
|
|
317
|
+
node: pSignal.value
|
|
318
|
+
}) : null
|
|
233
319
|
};
|
|
234
320
|
}
|
|
235
321
|
default: return defaultListenerOptions;
|
|
@@ -237,41 +323,64 @@ function getListenerOptions(context, node) {
|
|
|
237
323
|
}
|
|
238
324
|
return visit(node);
|
|
239
325
|
}
|
|
240
|
-
function getListenerArguments(context, node) {
|
|
326
|
+
function getListenerArguments({ context, node }) {
|
|
241
327
|
const [type, listener, options] = node.arguments;
|
|
242
328
|
if (type == null || listener == null) return null;
|
|
243
329
|
return {
|
|
244
330
|
type,
|
|
245
331
|
listener,
|
|
246
|
-
options: options == null ? defaultListenerOptions : getListenerOptions(
|
|
332
|
+
options: options == null ? defaultListenerOptions : getListenerOptions({
|
|
333
|
+
context,
|
|
334
|
+
node: options
|
|
335
|
+
})
|
|
247
336
|
};
|
|
248
337
|
}
|
|
249
|
-
function
|
|
250
|
-
return Check.isIdentifier(node) && (node.name
|
|
338
|
+
function isReactNativeMemberExpression(context, node) {
|
|
339
|
+
return node.type === AST_NODE_TYPES.MemberExpression && Check.isIdentifier(node.object) && isInitializedFromReactNative(node.object.name, context.sourceCode.getScope(node));
|
|
251
340
|
}
|
|
252
|
-
function
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
341
|
+
function resolveEventListenerFacts(context, facts) {
|
|
342
|
+
const entries = [];
|
|
343
|
+
const cleanupCalleeEntries = /* @__PURE__ */ new Map();
|
|
344
|
+
for (const fact of facts.calls) {
|
|
345
|
+
const { callee, effect, enclosingFunction, kind, node, phase } = fact;
|
|
346
|
+
if (kind === "other") {
|
|
347
|
+
if (phase !== "cleanup" || effect == null) continue;
|
|
348
|
+
const cleanupCallee = resolveCleanupCallee({
|
|
349
|
+
context,
|
|
350
|
+
node
|
|
351
|
+
});
|
|
352
|
+
if (cleanupCallee == null) continue;
|
|
353
|
+
const { functions, names } = getOrInsertComputed(cleanupCalleeEntries, effect, () => ({
|
|
354
|
+
functions: /* @__PURE__ */ new Set(),
|
|
355
|
+
names: /* @__PURE__ */ new Set()
|
|
356
|
+
}));
|
|
357
|
+
if (cleanupCallee.kind === "function") functions.add(cleanupCallee.node);
|
|
358
|
+
else names.add(cleanupCallee.name);
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
if (kind === "addEventListener" && isReactNativeMemberExpression(context, callee)) continue;
|
|
362
|
+
const args = getListenerArguments({
|
|
363
|
+
context,
|
|
364
|
+
node
|
|
365
|
+
});
|
|
366
|
+
if (args == null) continue;
|
|
367
|
+
entries.push({
|
|
368
|
+
type: args.type,
|
|
369
|
+
callee,
|
|
370
|
+
capture: args.options.capture,
|
|
371
|
+
effect,
|
|
372
|
+
enclosingFunction,
|
|
373
|
+
kind,
|
|
374
|
+
listener: args.listener,
|
|
375
|
+
node,
|
|
376
|
+
phase,
|
|
377
|
+
signal: args.options.signal
|
|
378
|
+
});
|
|
258
379
|
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
if (aPhase !== "setup") return false;
|
|
264
|
-
if (!isSameObject(aCallee, rCallee) || !Compare.isEqual(aListener, rListener) || !isProvenanceEqual(context, aType, rType) || aCapture !== rCapture) return false;
|
|
265
|
-
if (rPhase === "cleanup") return true;
|
|
266
|
-
if (aEffect == null || aEffect !== rEffect) return false;
|
|
267
|
-
const enclosingFunction = Traverse.findParent(rEntry.node, Check.isFunction);
|
|
268
|
-
if (enclosingFunction == null) return false;
|
|
269
|
-
const callees = cleanupCalleeEntries.get(aEffect);
|
|
270
|
-
if (callees == null) return false;
|
|
271
|
-
const [functions, names] = callees;
|
|
272
|
-
if (functions.has(enclosingFunction)) return true;
|
|
273
|
-
const id = getFunctionId(enclosingFunction);
|
|
274
|
-
return id != null && id.type === AST_NODE_TYPES.Identifier && names.has(id.name);
|
|
380
|
+
return {
|
|
381
|
+
entries,
|
|
382
|
+
cleanupCalleeEntries
|
|
383
|
+
};
|
|
275
384
|
}
|
|
276
385
|
var no_leaked_event_listener_default = createRule({
|
|
277
386
|
meta: {
|
|
@@ -289,73 +398,15 @@ var no_leaked_event_listener_default = createRule({
|
|
|
289
398
|
function create$5(context) {
|
|
290
399
|
if (!context.sourceCode.text.includes("addEventListener")) return {};
|
|
291
400
|
if (!/use\w*Effect/u.test(context.sourceCode.text)) return {};
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
messageId: "unexpected-inline-function",
|
|
300
|
-
node: listener
|
|
401
|
+
const { facts, visitor } = createFactCollector();
|
|
402
|
+
return merge(visitor, { "Program:exit"() {
|
|
403
|
+
const resolvedFacts = resolveEventListenerFacts(context, facts);
|
|
404
|
+
for (const violation of inferEventListenerViolations(context, resolvedFacts)) context.report({
|
|
405
|
+
data: violation.data,
|
|
406
|
+
messageId: violation.kind,
|
|
407
|
+
node: violation.node
|
|
301
408
|
});
|
|
302
|
-
}
|
|
303
|
-
return {
|
|
304
|
-
["CallExpression"](node) {
|
|
305
|
-
const effectContext = getEffectContext(node);
|
|
306
|
-
if (effectContext == null) return;
|
|
307
|
-
const { effect, phase } = effectContext;
|
|
308
|
-
const callee = Extract.unwrap(node.callee);
|
|
309
|
-
match(getCallKind$5(node)).with("addEventListener", (callKind) => {
|
|
310
|
-
if (callee.type === AST_NODE_TYPES.MemberExpression && Check.isIdentifier(callee.object) && isInitializedFromReactNative(callee.object.name, context.sourceCode.getScope(node))) return;
|
|
311
|
-
const args = getListenerArguments(context, node);
|
|
312
|
-
if (args == null) return;
|
|
313
|
-
reportInlineListener(args.listener, callKind, args.options.signal);
|
|
314
|
-
if (args.options.signal != null) return;
|
|
315
|
-
aEntries.push({
|
|
316
|
-
type: args.type,
|
|
317
|
-
callee,
|
|
318
|
-
capture: args.options.capture,
|
|
319
|
-
effect,
|
|
320
|
-
listener: args.listener,
|
|
321
|
-
node,
|
|
322
|
-
phase
|
|
323
|
-
});
|
|
324
|
-
}).with("removeEventListener", (callKind) => {
|
|
325
|
-
const args = getListenerArguments(context, node);
|
|
326
|
-
if (args == null) return;
|
|
327
|
-
reportInlineListener(args.listener, callKind, args.options.signal);
|
|
328
|
-
rEntries.push({
|
|
329
|
-
type: args.type,
|
|
330
|
-
callee,
|
|
331
|
-
capture: args.options.capture,
|
|
332
|
-
effect,
|
|
333
|
-
listener: args.listener,
|
|
334
|
-
node,
|
|
335
|
-
phase
|
|
336
|
-
});
|
|
337
|
-
}).with("other", () => {
|
|
338
|
-
if (phase !== "cleanup" || effect == null) return;
|
|
339
|
-
const cleanupCallee = resolveCleanupCallee(context, node);
|
|
340
|
-
if (cleanupCallee == null) return;
|
|
341
|
-
const [functions, names] = getOrInsertComputed(cEntries, effect, () => [/* @__PURE__ */ new Set(), /* @__PURE__ */ new Set()]);
|
|
342
|
-
if (cleanupCallee.kind === "function") functions.add(cleanupCallee.node);
|
|
343
|
-
else names.add(cleanupCallee.name);
|
|
344
|
-
}).otherwise(() => null);
|
|
345
|
-
},
|
|
346
|
-
["Program:exit"]() {
|
|
347
|
-
for (const aEntry of aEntries) {
|
|
348
|
-
if (rEntries.some((rEntry) => isInverseEntry(context, aEntry, rEntry, cEntries))) continue;
|
|
349
|
-
const effect = aEntry.effect;
|
|
350
|
-
const effectMethodKind = effect != null && effect.type === AST_NODE_TYPES.CallExpression ? Extract.getCalleeName(effect) ?? "useEffect" : "useEffect";
|
|
351
|
-
context.report({
|
|
352
|
-
data: { effectMethodKind },
|
|
353
|
-
messageId: "expected-remove-event-listener-in-cleanup",
|
|
354
|
-
node: aEntry.node
|
|
355
|
-
});
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
};
|
|
409
|
+
} });
|
|
359
410
|
}
|
|
360
411
|
// src/rules/no-leaked-fetch/lib.ts
|
|
361
412
|
function resolveToObjectExpression(context, node) {
|
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.15",
|
|
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-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.
|
|
40
|
+
"@eslint-react/ast": "5.30.0-beta.15",
|
|
41
|
+
"@eslint-react/core": "5.30.0-beta.15",
|
|
42
|
+
"@eslint-react/eslint": "5.30.0-beta.15",
|
|
43
|
+
"@eslint-react/shared": "5.30.0-beta.15",
|
|
44
|
+
"@eslint-react/var": "5.30.0-beta.15",
|
|
45
45
|
"@typescript-eslint/types": "^8.72.1",
|
|
46
46
|
"@typescript-eslint/utils": "^8.72.1",
|
|
47
47
|
"ts-pattern": "^5.9.2"
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"@types/react-dom": "^19.3.0",
|
|
57
57
|
"dedent": "^1.7.2",
|
|
58
58
|
"eslint": "^10.13.0",
|
|
59
|
-
"tsdown": "^0.23.
|
|
59
|
+
"tsdown": "^0.23.4",
|
|
60
60
|
"typescript": "6.0.3"
|
|
61
61
|
},
|
|
62
62
|
"peerDependencies": {
|