eslint-plugin-react-web-api 5.30.0-beta.10 → 5.30.0-beta.11
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 +119 -123
- package/package.json +6 -6
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
3
|
import { Check, Compare, Extract, Traverse } from "@eslint-react/ast";
|
|
4
|
-
import { getFunctionId, isUseEffectCleanupCallback, isUseEffectSetupCallback, isUseRefLikeCall } from "@eslint-react/core";
|
|
5
4
|
import "@eslint-react/eslint";
|
|
6
5
|
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 { getFunctionId, isUseEffectCleanupCallback, isUseEffectSetupCallback, isUseRefLikeCall } from "@eslint-react/core";
|
|
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.11";
|
|
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
|
}
|
|
@@ -127,6 +124,56 @@ dual(2, (self, predicate) => {
|
|
|
127
124
|
return input.slice(0, idx);
|
|
128
125
|
});
|
|
129
126
|
// src/rules/no-leaked-event-listener/lib.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 resolveCleanupCallee(context, node) {
|
|
144
|
+
const callee = Extract.unwrap(node.callee);
|
|
145
|
+
if (callee.type === AST_NODE_TYPES.Identifier) {
|
|
146
|
+
const resolved = resolve(context, callee, { at: -1 });
|
|
147
|
+
const fn = resolved == null ? null : Extract.unwrap(resolved);
|
|
148
|
+
return fn != null && Check.isFunction(fn) ? {
|
|
149
|
+
kind: "function",
|
|
150
|
+
node: fn
|
|
151
|
+
} : {
|
|
152
|
+
kind: "name",
|
|
153
|
+
name: callee.name
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
if (callee.type !== AST_NODE_TYPES.MemberExpression || callee.computed) return null;
|
|
157
|
+
const object = Extract.unwrap(callee.object);
|
|
158
|
+
const property = Extract.unwrap(callee.property);
|
|
159
|
+
if (object.type !== AST_NODE_TYPES.Identifier || property.type !== AST_NODE_TYPES.Identifier) return null;
|
|
160
|
+
const init = resolve(context, object, { at: -1 });
|
|
161
|
+
const objectExpression = init == null ? null : Extract.unwrap(init);
|
|
162
|
+
if (objectExpression?.type !== AST_NODE_TYPES.ObjectExpression) return null;
|
|
163
|
+
for (const prop of objectExpression.properties) {
|
|
164
|
+
if (prop.type !== AST_NODE_TYPES.Property || prop.computed || Extract.getPropertyName(prop, "max") !== property.name) continue;
|
|
165
|
+
const value = Extract.unwrap(prop.value);
|
|
166
|
+
if (Check.isFunction(value)) return {
|
|
167
|
+
kind: "function",
|
|
168
|
+
node: value
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
const defaultListenerOptions = {
|
|
174
|
+
capture: false,
|
|
175
|
+
signal: null
|
|
176
|
+
};
|
|
130
177
|
function isSignalPropertyBinding(context, node) {
|
|
131
178
|
const defNode = findVariable(context.sourceCode.getScope(node), node)?.defs.at(-1)?.node;
|
|
132
179
|
if (defNode?.type !== AST_NODE_TYPES.VariableDeclarator || defNode.id.type !== AST_NODE_TYPES.ObjectPattern) return false;
|
|
@@ -158,20 +205,16 @@ function getSignalValueExpression(context, node, seen = /* @__PURE__ */ new Set(
|
|
|
158
205
|
default: return null;
|
|
159
206
|
}
|
|
160
207
|
}
|
|
161
|
-
|
|
162
|
-
capture: false,
|
|
163
|
-
signal: null
|
|
164
|
-
};
|
|
165
|
-
function getOptions(context, node) {
|
|
208
|
+
function getListenerOptions(context, node) {
|
|
166
209
|
function visit(node) {
|
|
167
210
|
switch (node.type) {
|
|
168
211
|
case AST_NODE_TYPES.Identifier: {
|
|
169
212
|
const initNode = resolve(context, node);
|
|
170
213
|
if (initNode?.type === AST_NODE_TYPES.ObjectExpression) return visit(initNode);
|
|
171
|
-
return
|
|
214
|
+
return defaultListenerOptions;
|
|
172
215
|
}
|
|
173
216
|
case AST_NODE_TYPES.Literal: return {
|
|
174
|
-
...
|
|
217
|
+
...defaultListenerOptions,
|
|
175
218
|
capture: Boolean(node.value)
|
|
176
219
|
};
|
|
177
220
|
case AST_NODE_TYPES.ObjectExpression: {
|
|
@@ -189,57 +232,47 @@ function getOptions(context, node) {
|
|
|
189
232
|
signal: pSignal?.type === AST_NODE_TYPES.Property ? getSignalValueExpression(context, pSignal.value) : null
|
|
190
233
|
};
|
|
191
234
|
}
|
|
192
|
-
default: return
|
|
235
|
+
default: return defaultListenerOptions;
|
|
193
236
|
}
|
|
194
237
|
}
|
|
195
238
|
return visit(node);
|
|
196
239
|
}
|
|
197
|
-
|
|
198
|
-
const
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
240
|
+
function getListenerArguments(context, node) {
|
|
241
|
+
const [type, listener, options] = node.arguments;
|
|
242
|
+
if (type == null || listener == null) return null;
|
|
243
|
+
return {
|
|
244
|
+
type,
|
|
245
|
+
listener,
|
|
246
|
+
options: options == null ? defaultListenerOptions : getListenerOptions(context, options)
|
|
247
|
+
};
|
|
204
248
|
}
|
|
205
|
-
function
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
} : {
|
|
215
|
-
kind: "name",
|
|
216
|
-
name: callee.name
|
|
217
|
-
};
|
|
218
|
-
}
|
|
219
|
-
case AST_NODE_TYPES.MemberExpression: {
|
|
220
|
-
if (callee.computed) return null;
|
|
221
|
-
const property = Extract.unwrap(callee.property);
|
|
222
|
-
const object = Extract.unwrap(callee.object);
|
|
223
|
-
if (property.type !== AST_NODE_TYPES.Identifier || object.type !== AST_NODE_TYPES.Identifier) return null;
|
|
224
|
-
const defNode = findVariable(context.sourceCode.getScope(node), object)?.defs.at(-1)?.node;
|
|
225
|
-
if (defNode?.type !== AST_NODE_TYPES.VariableDeclarator || defNode.init == null) return null;
|
|
226
|
-
const init = Extract.unwrap(defNode.init);
|
|
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;
|
|
239
|
-
}
|
|
240
|
-
default: return null;
|
|
249
|
+
function isGlobalObject(node) {
|
|
250
|
+
return Check.isIdentifier(node) && (node.name === "window" || node.name === "globalThis" || node.name === "self");
|
|
251
|
+
}
|
|
252
|
+
function isSameObject(a, b) {
|
|
253
|
+
switch (true) {
|
|
254
|
+
case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return Compare.isEqual(a.object, b.object);
|
|
255
|
+
case a.type === AST_NODE_TYPES.MemberExpression: return isGlobalObject(a.object);
|
|
256
|
+
case b.type === AST_NODE_TYPES.MemberExpression: return isGlobalObject(b.object);
|
|
257
|
+
default: return true;
|
|
241
258
|
}
|
|
242
259
|
}
|
|
260
|
+
function isInverseEntry(context, aEntry, rEntry, cleanupCalleeEntries) {
|
|
261
|
+
const { type: aType, callee: aCallee, capture: aCapture, effect: aEffect, listener: aListener, phase: aPhase } = aEntry;
|
|
262
|
+
const { type: rType, callee: rCallee, capture: rCapture, effect: rEffect, listener: rListener, phase: rPhase } = rEntry;
|
|
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);
|
|
275
|
+
}
|
|
243
276
|
var no_leaked_event_listener_default = createRule({
|
|
244
277
|
meta: {
|
|
245
278
|
type: "problem",
|
|
@@ -250,7 +283,7 @@ var no_leaked_event_listener_default = createRule({
|
|
|
250
283
|
},
|
|
251
284
|
schema: []
|
|
252
285
|
},
|
|
253
|
-
name:
|
|
286
|
+
name: "no-leaked-event-listener",
|
|
254
287
|
create: create$5
|
|
255
288
|
});
|
|
256
289
|
function create$5(context) {
|
|
@@ -258,37 +291,9 @@ function create$5(context) {
|
|
|
258
291
|
if (!/use\w*Effect/u.test(context.sourceCode.text)) return {};
|
|
259
292
|
const aEntries = [];
|
|
260
293
|
const rEntries = [];
|
|
261
|
-
const
|
|
262
|
-
function
|
|
263
|
-
|
|
264
|
-
}
|
|
265
|
-
function isSameObject(a, b) {
|
|
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;
|
|
294
|
+
const cEntries = /* @__PURE__ */ new Map();
|
|
295
|
+
function reportInlineListener(listener, callKind, signal) {
|
|
296
|
+
if (signal != null || !Check.isFunction(listener)) return;
|
|
292
297
|
context.report({
|
|
293
298
|
data: { eventMethodKind: callKind },
|
|
294
299
|
messageId: "unexpected-inline-function",
|
|
@@ -297,59 +302,50 @@ function create$5(context) {
|
|
|
297
302
|
}
|
|
298
303
|
return {
|
|
299
304
|
["CallExpression"](node) {
|
|
300
|
-
const
|
|
301
|
-
if (
|
|
302
|
-
const
|
|
303
|
-
const setupFn = fKind === "setup" ? fn : Traverse.findParent(fn, Check.isFunction);
|
|
304
|
-
const effect = setupFn == null ? null : Extract.unwrap(setupFn).parent ?? null;
|
|
305
|
+
const effectContext = getEffectContext(node);
|
|
306
|
+
if (effectContext == null) return;
|
|
307
|
+
const { effect, phase } = effectContext;
|
|
305
308
|
const callee = Extract.unwrap(node.callee);
|
|
306
309
|
match(getCallKind$5(node)).with("addEventListener", (callKind) => {
|
|
307
310
|
if (callee.type === AST_NODE_TYPES.MemberExpression && Check.isIdentifier(callee.object) && isInitializedFromReactNative(callee.object.name, context.sourceCode.getScope(node))) return;
|
|
308
|
-
const
|
|
309
|
-
if (
|
|
310
|
-
|
|
311
|
-
|
|
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;
|
|
312
315
|
aEntries.push({
|
|
313
|
-
|
|
314
|
-
type,
|
|
316
|
+
type: args.type,
|
|
315
317
|
callee,
|
|
318
|
+
capture: args.options.capture,
|
|
316
319
|
effect,
|
|
317
|
-
listener,
|
|
318
|
-
method: "addEventListener",
|
|
320
|
+
listener: args.listener,
|
|
319
321
|
node,
|
|
320
|
-
phase
|
|
322
|
+
phase
|
|
321
323
|
});
|
|
322
324
|
}).with("removeEventListener", (callKind) => {
|
|
323
|
-
const
|
|
324
|
-
if (
|
|
325
|
-
|
|
326
|
-
visitInlineFunction(node, callKind, opts);
|
|
325
|
+
const args = getListenerArguments(context, node);
|
|
326
|
+
if (args == null) return;
|
|
327
|
+
reportInlineListener(args.listener, callKind, args.options.signal);
|
|
327
328
|
rEntries.push({
|
|
328
|
-
type,
|
|
329
|
+
type: args.type,
|
|
329
330
|
callee,
|
|
330
|
-
capture:
|
|
331
|
+
capture: args.options.capture,
|
|
331
332
|
effect,
|
|
332
|
-
listener,
|
|
333
|
-
method: "removeEventListener",
|
|
333
|
+
listener: args.listener,
|
|
334
334
|
node,
|
|
335
|
-
phase
|
|
335
|
+
phase
|
|
336
336
|
});
|
|
337
337
|
}).with("other", () => {
|
|
338
|
-
if (
|
|
339
|
-
const
|
|
340
|
-
if (
|
|
341
|
-
const
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
}));
|
|
345
|
-
if (callee.kind === "function") callees.functions.add(callee.node);
|
|
346
|
-
else callees.names.add(callee.name);
|
|
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);
|
|
347
344
|
}).otherwise(() => null);
|
|
348
345
|
},
|
|
349
346
|
["Program:exit"]() {
|
|
350
347
|
for (const aEntry of aEntries) {
|
|
351
|
-
if (
|
|
352
|
-
if (rEntries.some((rEntry) => isInverseEntry(aEntry, rEntry))) continue;
|
|
348
|
+
if (rEntries.some((rEntry) => isInverseEntry(context, aEntry, rEntry, cEntries))) continue;
|
|
353
349
|
const effect = aEntry.effect;
|
|
354
350
|
const effectMethodKind = effect != null && effect.type === AST_NODE_TYPES.CallExpression ? Extract.getCalleeName(effect) ?? "useEffect" : "useEffect";
|
|
355
351
|
context.report({
|
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.11",
|
|
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.11",
|
|
41
|
+
"@eslint-react/core": "5.30.0-beta.11",
|
|
42
|
+
"@eslint-react/eslint": "5.30.0-beta.11",
|
|
43
|
+
"@eslint-react/shared": "5.30.0-beta.11",
|
|
44
|
+
"@eslint-react/var": "5.30.0-beta.11",
|
|
45
45
|
"@typescript-eslint/types": "^8.72.1",
|
|
46
46
|
"@typescript-eslint/utils": "^8.72.1",
|
|
47
47
|
"ts-pattern": "^5.9.2"
|