eslint-plugin-react-web-api 5.30.0-beta.1 → 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.
Files changed (2) hide show
  1. package/dist/index.js +124 -126
  2. 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
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
- import { isAssignmentTargetEqual, isInitializedFromReactNative, isValueEqual, resolve, resolveEnclosingAssignmentTarget, resolveOrigin } from "@eslint-react/var";
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.1";
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;
@@ -139,8 +186,10 @@ function isSignalPropertyBinding(context, node) {
139
186
  }
140
187
  return false;
141
188
  }
142
- function getSignalValueExpression(context, node) {
189
+ function getSignalValueExpression(context, node, seen = /* @__PURE__ */ new Set()) {
143
190
  if (node == null) return null;
191
+ if (seen.has(node)) return null;
192
+ seen.add(node);
144
193
  switch (node.type) {
145
194
  case AST_NODE_TYPES.Identifier: {
146
195
  const resolved = resolveOrigin(context, node);
@@ -150,26 +199,22 @@ function getSignalValueExpression(context, node) {
150
199
  const newCallee = Extract.unwrap(unwrapped.callee);
151
200
  if (newCallee.type === AST_NODE_TYPES.Identifier && newCallee.name === "AbortController") return node;
152
201
  }
153
- return getSignalValueExpression(context, unwrapped);
202
+ return getSignalValueExpression(context, unwrapped, seen);
154
203
  }
155
204
  case AST_NODE_TYPES.MemberExpression: return node;
156
205
  default: return null;
157
206
  }
158
207
  }
159
- const defaultOptions = {
160
- capture: false,
161
- signal: null
162
- };
163
- function getOptions(context, node) {
208
+ function getListenerOptions(context, node) {
164
209
  function visit(node) {
165
210
  switch (node.type) {
166
211
  case AST_NODE_TYPES.Identifier: {
167
212
  const initNode = resolve(context, node);
168
213
  if (initNode?.type === AST_NODE_TYPES.ObjectExpression) return visit(initNode);
169
- return defaultOptions;
214
+ return defaultListenerOptions;
170
215
  }
171
216
  case AST_NODE_TYPES.Literal: return {
172
- ...defaultOptions,
217
+ ...defaultListenerOptions,
173
218
  capture: Boolean(node.value)
174
219
  };
175
220
  case AST_NODE_TYPES.ObjectExpression: {
@@ -187,57 +232,47 @@ function getOptions(context, node) {
187
232
  signal: pSignal?.type === AST_NODE_TYPES.Property ? getSignalValueExpression(context, pSignal.value) : null
188
233
  };
189
234
  }
190
- default: return defaultOptions;
235
+ default: return defaultListenerOptions;
191
236
  }
192
237
  }
193
238
  return visit(node);
194
239
  }
195
- // src/rules/no-leaked-event-listener/no-leaked-event-listener.ts
196
- const RULE_NAME$5 = "no-leaked-event-listener";
197
- const isUseEffectCallback$5 = or(isUseEffectSetupCallback, isUseEffectCleanupCallback);
198
- function getCallKind$5(node) {
199
- const name = Extract.getCalleeName(node);
200
- if (name != null && isMatching(P.union("addEventListener", "removeEventListener"))(name)) return name;
201
- return "other";
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
+ };
202
248
  }
203
- function resolveCleanupCallee(context, node) {
204
- const callee = Extract.unwrap(node.callee);
205
- switch (callee.type) {
206
- case AST_NODE_TYPES.Identifier: {
207
- const defNode = findVariable(context.sourceCode.getScope(node), callee)?.defs.at(-1)?.node;
208
- const fn = defNode == null ? null : defNode.type === AST_NODE_TYPES.FunctionDeclaration ? defNode : defNode.type === AST_NODE_TYPES.VariableDeclarator && defNode.init != null ? Extract.unwrap(defNode.init) : null;
209
- return fn != null && Check.isFunction(fn) ? {
210
- kind: "function",
211
- node: fn
212
- } : {
213
- kind: "name",
214
- name: callee.name
215
- };
216
- }
217
- case AST_NODE_TYPES.MemberExpression: {
218
- if (callee.computed) return null;
219
- const property = Extract.unwrap(callee.property);
220
- const object = Extract.unwrap(callee.object);
221
- if (property.type !== AST_NODE_TYPES.Identifier || object.type !== AST_NODE_TYPES.Identifier) return null;
222
- const defNode = findVariable(context.sourceCode.getScope(node), object)?.defs.at(-1)?.node;
223
- if (defNode?.type !== AST_NODE_TYPES.VariableDeclarator || defNode.init == null) return null;
224
- const init = Extract.unwrap(defNode.init);
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;
237
- }
238
- 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;
239
258
  }
240
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
+ }
241
276
  var no_leaked_event_listener_default = createRule({
242
277
  meta: {
243
278
  type: "problem",
@@ -248,7 +283,7 @@ var no_leaked_event_listener_default = createRule({
248
283
  },
249
284
  schema: []
250
285
  },
251
- name: RULE_NAME$5,
286
+ name: "no-leaked-event-listener",
252
287
  create: create$5
253
288
  });
254
289
  function create$5(context) {
@@ -256,37 +291,9 @@ function create$5(context) {
256
291
  if (!/use\w*Effect/u.test(context.sourceCode.text)) return {};
257
292
  const aEntries = [];
258
293
  const rEntries = [];
259
- const cleanupCallees = /* @__PURE__ */ new Map();
260
- function isGlobalObject(node) {
261
- return Check.isIdentifier(node) && (node.name === "window" || node.name === "globalThis" || node.name === "self");
262
- }
263
- function isSameObject(a, b) {
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;
294
+ const cEntries = /* @__PURE__ */ new Map();
295
+ function reportInlineListener(listener, callKind, signal) {
296
+ if (signal != null || !Check.isFunction(listener)) return;
290
297
  context.report({
291
298
  data: { eventMethodKind: callKind },
292
299
  messageId: "unexpected-inline-function",
@@ -295,59 +302,50 @@ function create$5(context) {
295
302
  }
296
303
  return {
297
304
  ["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;
305
+ const effectContext = getEffectContext(node);
306
+ if (effectContext == null) return;
307
+ const { effect, phase } = effectContext;
303
308
  const callee = Extract.unwrap(node.callee);
304
309
  match(getCallKind$5(node)).with("addEventListener", (callKind) => {
305
310
  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);
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;
310
315
  aEntries.push({
311
- ...opts,
312
- type,
316
+ type: args.type,
313
317
  callee,
318
+ capture: args.options.capture,
314
319
  effect,
315
- listener,
316
- method: "addEventListener",
320
+ listener: args.listener,
317
321
  node,
318
- phase: fKind
322
+ phase
319
323
  });
320
324
  }).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
+ const args = getListenerArguments(context, node);
326
+ if (args == null) return;
327
+ reportInlineListener(args.listener, callKind, args.options.signal);
325
328
  rEntries.push({
326
- type,
329
+ type: args.type,
327
330
  callee,
328
- capture: opts.capture,
331
+ capture: args.options.capture,
329
332
  effect,
330
- listener,
331
- method: "removeEventListener",
333
+ listener: args.listener,
332
334
  node,
333
- phase: fKind
335
+ phase
334
336
  });
335
337
  }).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);
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);
345
344
  }).otherwise(() => null);
346
345
  },
347
346
  ["Program:exit"]() {
348
347
  for (const aEntry of aEntries) {
349
- if (aEntry.signal != null) continue;
350
- if (rEntries.some((rEntry) => isInverseEntry(aEntry, rEntry))) continue;
348
+ if (rEntries.some((rEntry) => isInverseEntry(context, aEntry, rEntry, cEntries))) continue;
351
349
  const effect = aEntry.effect;
352
350
  const effectMethodKind = effect != null && effect.type === AST_NODE_TYPES.CallExpression ? Extract.getCalleeName(effect) ?? "useEffect" : "useEffect";
353
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.1",
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,13 +37,13 @@
37
37
  "dist"
38
38
  ],
39
39
  "dependencies": {
40
- "@eslint-react/ast": "5.30.0-beta.1",
41
- "@eslint-react/core": "5.30.0-beta.1",
42
- "@eslint-react/eslint": "5.30.0-beta.1",
43
- "@eslint-react/shared": "5.30.0-beta.1",
44
- "@eslint-react/var": "5.30.0-beta.1",
45
- "@typescript-eslint/types": "^8.72.0",
46
- "@typescript-eslint/utils": "^8.72.0",
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
+ "@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": {