@jupyter/eslint-plugin 1.0.1 → 1.1.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.
Files changed (35) hide show
  1. package/lib/index.d.ts +68 -0
  2. package/lib/index.js +42 -11
  3. package/lib/rules/galata-prefer-filebrowser-helper.d.ts +5 -0
  4. package/lib/rules/galata-prefer-filebrowser-helper.js +105 -0
  5. package/lib/rules/incorrect-translator-usage.d.ts +4 -0
  6. package/lib/rules/incorrect-translator-usage.js +208 -0
  7. package/lib/rules/no-dynamic-translation.d.ts +4 -0
  8. package/lib/rules/no-dynamic-translation.js +92 -0
  9. package/lib/rules/no-pageconfig-base-url.d.ts +4 -0
  10. package/lib/rules/no-pageconfig-base-url.js +37 -0
  11. package/lib/rules/no-translation-concatenation.js +46 -84
  12. package/lib/rules/no-untranslated-string.js +7 -4
  13. package/lib/rules/plugin-activation-args.js +1 -2
  14. package/lib/rules/prefer-signal-this-arg.d.ts +7 -0
  15. package/lib/rules/prefer-signal-this-arg.js +196 -0
  16. package/lib/rules/require-disposable-ownership.d.ts +5 -0
  17. package/lib/rules/require-disposable-ownership.js +120 -0
  18. package/lib/rules/require-disposable-transfer.d.ts +5 -0
  19. package/lib/rules/require-disposable-transfer.js +223 -0
  20. package/lib/rules/require-signal-cleanup.d.ts +8 -0
  21. package/lib/rules/require-signal-cleanup.js +164 -0
  22. package/lib/rules/require-signal-this-arg.d.ts +5 -0
  23. package/lib/rules/require-signal-this-arg.js +74 -0
  24. package/lib/rules/token-format.js +1 -2
  25. package/lib/utils/disposables.d.ts +64 -0
  26. package/lib/utils/disposables.js +1723 -0
  27. package/lib/utils/playwright-selectors.d.ts +66 -0
  28. package/lib/utils/playwright-selectors.js +193 -0
  29. package/lib/utils/signal-lifetime.d.ts +158 -0
  30. package/lib/utils/signal-lifetime.js +755 -0
  31. package/lib/utils/signals.d.ts +143 -0
  32. package/lib/utils/signals.js +601 -0
  33. package/lib/utils/translation.d.ts +45 -0
  34. package/lib/utils/translation.js +122 -0
  35. package/package.json +9 -2
@@ -0,0 +1,1723 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) Jupyter Development Team.
4
+ * Distributed under the terms of the Modified BSD License.
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ var desc = Object.getOwnPropertyDescriptor(m, k);
9
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
+ desc = { enumerable: true, get: function() { return m[k]; } };
11
+ }
12
+ Object.defineProperty(o, k2, desc);
13
+ }) : (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ o[k2] = m[k];
16
+ }));
17
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
18
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
19
+ }) : function(o, v) {
20
+ o["default"] = v;
21
+ });
22
+ var __importStar = (this && this.__importStar) || (function () {
23
+ var ownKeys = function(o) {
24
+ ownKeys = Object.getOwnPropertyNames || function (o) {
25
+ var ar = [];
26
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
27
+ return ar;
28
+ };
29
+ return ownKeys(o);
30
+ };
31
+ return function (mod) {
32
+ if (mod && mod.__esModule) return mod;
33
+ var result = {};
34
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
35
+ __setModuleDefault(result, mod);
36
+ return result;
37
+ };
38
+ })();
39
+ Object.defineProperty(exports, "__esModule", { value: true });
40
+ exports.DEFAULT_OWNERSHIP_FUNCTION_NAMES = void 0;
41
+ exports.resolveNameListOption = resolveNameListOption;
42
+ exports.getCalleeName = getCalleeName;
43
+ exports.isDisposableConstructor = isDisposableConstructor;
44
+ exports.isDisposableSetFactoryCall = isDisposableSetFactoryCall;
45
+ exports.addPendingDisposable = addPendingDisposable;
46
+ exports.getAssignedVariable = getAssignedVariable;
47
+ exports.isOuterFunctionScopeVariable = isOuterFunctionScopeVariable;
48
+ exports.isExportedVariable = isExportedVariable;
49
+ exports.isDisposedByNestedFunction = isDisposedByNestedFunction;
50
+ exports.isInJupyterPluginActivate = isInJupyterPluginActivate;
51
+ exports.isClassFieldCollectionMutationCall = isClassFieldCollectionMutationCall;
52
+ exports.isDisposableExpressionManaged = isDisposableExpressionManaged;
53
+ exports.markManagedDisposableUse = markManagedDisposableUse;
54
+ exports.isDisposableType = isDisposableType;
55
+ exports.shouldCheckReturnedDisposable = shouldCheckReturnedDisposable;
56
+ const ts = __importStar(require("typescript"));
57
+ const plugin_utils_1 = require("./plugin-utils");
58
+ const DISPOSABLE_INTERFACE_NAMES = ['IDisposable', 'IObservableDisposable'];
59
+ const DISPOSABLE_CONSTRUCTOR_NAMES = [
60
+ 'DisposableDelegate',
61
+ 'ObservableDisposableDelegate',
62
+ 'DisposableSet',
63
+ 'ObservableDisposableSet'
64
+ ];
65
+ const DISPOSABLE_SET_NAMES = ['DisposableSet', 'ObservableDisposableSet'];
66
+ const FLUENT_DISPOSABLE_METHOD_NAMES = ['initializeState'];
67
+ /**
68
+ * Resolves a configurable name list option.
69
+ *
70
+ * A user-supplied list is *added* to the built-in defaults, so a project only
71
+ * has to name its own helpers rather than restate this plugin's list. Passing
72
+ * the matching `extendDefault...` option as `false` drops the defaults, whether
73
+ * or not a replacement list is given, which is also how to ask for the
74
+ * strictest possible checking.
75
+ */
76
+ function resolveNameListOption(provided, defaults, extendDefaults) {
77
+ const names = extendDefaults ? [...defaults] : [];
78
+ if (provided) {
79
+ names.push(...provided);
80
+ }
81
+ return new Set(names);
82
+ }
83
+ exports.DEFAULT_OWNERSHIP_FUNCTION_NAMES = [
84
+ 'add',
85
+ 'addCell',
86
+ 'addFactory',
87
+ 'addGroup',
88
+ 'addItem',
89
+ 'addMenu',
90
+ 'addModelFactory',
91
+ 'addSibling',
92
+ 'addWidget',
93
+ 'addWidgetFactory',
94
+ 'insertBefore',
95
+ 'insertItem',
96
+ 'insertWidget',
97
+ 'registerStatusItem',
98
+ '_wrappedOutput'
99
+ ];
100
+ function getStaticMemberName(node) {
101
+ if (!node.computed && node.property.type === 'Identifier') {
102
+ return node.property.name;
103
+ }
104
+ if (node.computed &&
105
+ node.property.type === 'Literal' &&
106
+ typeof node.property.value === 'string') {
107
+ return node.property.value;
108
+ }
109
+ return null;
110
+ }
111
+ function getCalleeName(node) {
112
+ if (node.type === 'Identifier') {
113
+ return node.name;
114
+ }
115
+ if (node.type === 'MemberExpression') {
116
+ return getStaticMemberName(node);
117
+ }
118
+ return null;
119
+ }
120
+ function getTransparentChild(node) {
121
+ if (node.type === 'ChainExpression' ||
122
+ node.type === 'TSAsExpression' ||
123
+ node.type === 'TSTypeAssertion' ||
124
+ node.type === 'TSNonNullExpression') {
125
+ return node.expression;
126
+ }
127
+ return null;
128
+ }
129
+ function reducerReturnsAccumulator(callback, accumulatorName) {
130
+ if (callback.type !== 'ArrowFunctionExpression' &&
131
+ callback.type !== 'FunctionExpression') {
132
+ return false;
133
+ }
134
+ if (callback.body.type === 'Identifier' &&
135
+ callback.body.name === accumulatorName) {
136
+ return true;
137
+ }
138
+ return (callback.body.type === 'BlockStatement' &&
139
+ callback.body.body.some(statement => statement.type === 'ReturnStatement' &&
140
+ statement.argument?.type === 'Identifier' &&
141
+ statement.argument.name === accumulatorName));
142
+ }
143
+ function isReduceAccumulatorInitialValue(node, parent) {
144
+ if (parent.type !== 'CallExpression' ||
145
+ parent.callee.type !== 'MemberExpression' ||
146
+ getStaticMemberName(parent.callee) !== 'reduce' ||
147
+ parent.arguments.length < 2 ||
148
+ parent.arguments[1] !== node) {
149
+ return false;
150
+ }
151
+ const [callback] = parent.arguments;
152
+ if (!callback ||
153
+ (callback.type !== 'ArrowFunctionExpression' &&
154
+ callback.type !== 'FunctionExpression')) {
155
+ return false;
156
+ }
157
+ const [accumulator] = callback.params;
158
+ return (accumulator?.type === 'Identifier' &&
159
+ reducerReturnsAccumulator(callback, accumulator.name));
160
+ }
161
+ function getOuterExpression(node) {
162
+ let current = node;
163
+ let parent = current.parent;
164
+ while (parent) {
165
+ const child = getTransparentChild(parent);
166
+ if (child === current) {
167
+ current = parent;
168
+ parent = current.parent;
169
+ continue;
170
+ }
171
+ if (isReduceAccumulatorInitialValue(current, parent)) {
172
+ current = parent;
173
+ parent = current.parent;
174
+ continue;
175
+ }
176
+ if (parent.type === 'MemberExpression' &&
177
+ parent.object === current &&
178
+ parent.parent?.type === 'CallExpression' &&
179
+ parent.parent.callee === parent &&
180
+ FLUENT_DISPOSABLE_METHOD_NAMES.includes(getStaticMemberName(parent) ?? '')) {
181
+ current = parent.parent;
182
+ parent = current.parent;
183
+ continue;
184
+ }
185
+ break;
186
+ }
187
+ return current;
188
+ }
189
+ function isThisMemberExpression(node) {
190
+ const expression = getOuterExpression(node);
191
+ if (expression.type === 'ThisExpression') {
192
+ return true;
193
+ }
194
+ if (expression.type !== 'MemberExpression') {
195
+ return false;
196
+ }
197
+ return isThisMemberExpression(expression.object);
198
+ }
199
+ function getOuterFallbackExpression(node) {
200
+ let current = getOuterExpression(node);
201
+ let parent = current.parent;
202
+ while (parent &&
203
+ ((parent.type === 'LogicalExpression' &&
204
+ (parent.operator === '||' || parent.operator === '??') &&
205
+ (parent.left === current || parent.right === current)) ||
206
+ (parent.type === 'ConditionalExpression' &&
207
+ (parent.consequent === current || parent.alternate === current)))) {
208
+ current = parent;
209
+ parent = current.parent;
210
+ }
211
+ return current;
212
+ }
213
+ function isStaticMemberCall(node, name) {
214
+ return (node.callee.type === 'MemberExpression' &&
215
+ getStaticMemberName(node.callee) === name);
216
+ }
217
+ function isOwnershipFunctionCall(node, ownership) {
218
+ const name = getCalleeName(node.callee);
219
+ return name !== null && ownership.ownershipFunctionNames.has(name);
220
+ }
221
+ function isDisposableConstructor(node) {
222
+ const name = getCalleeName(node.callee);
223
+ return name !== null && DISPOSABLE_CONSTRUCTOR_NAMES.includes(name);
224
+ }
225
+ function isDisposableSetConstructor(node) {
226
+ const name = getCalleeName(node.callee);
227
+ return name !== null && DISPOSABLE_SET_NAMES.includes(name);
228
+ }
229
+ function isDisposableSetFactoryCall(node) {
230
+ if (node.type !== 'CallExpression' ||
231
+ node.callee.type !== 'MemberExpression' ||
232
+ getStaticMemberName(node.callee) !== 'from') {
233
+ return false;
234
+ }
235
+ const name = getCalleeName(node.callee.object);
236
+ return name !== null && DISPOSABLE_SET_NAMES.includes(name);
237
+ }
238
+ function isDirectArrayArgument(element) {
239
+ return element !== null && element.type !== 'SpreadElement';
240
+ }
241
+ function getDisposableSetFactoryArguments(node) {
242
+ if (!isDisposableSetFactoryCall(node)) {
243
+ return [];
244
+ }
245
+ const [items] = node.arguments;
246
+ if (!items || items.type !== 'ArrayExpression') {
247
+ return [];
248
+ }
249
+ return items.elements.filter(isDirectArrayArgument);
250
+ }
251
+ function resolveVariable(sourceCode, identifier) {
252
+ let scope = sourceCode.getScope(identifier);
253
+ while (scope) {
254
+ const variable = scope.set.get(identifier.name);
255
+ if (variable) {
256
+ return variable;
257
+ }
258
+ scope = scope.upper;
259
+ }
260
+ return null;
261
+ }
262
+ function getIdentifierVariable(sourceCode, node) {
263
+ if (!node) {
264
+ return null;
265
+ }
266
+ if (node.type === 'Identifier') {
267
+ return resolveVariable(sourceCode, node);
268
+ }
269
+ const child = getTransparentChild(node);
270
+ if (child) {
271
+ return getIdentifierVariable(sourceCode, child);
272
+ }
273
+ return null;
274
+ }
275
+ function getVariableInitializer(variable) {
276
+ for (const definition of variable.defs) {
277
+ const node = definition.node;
278
+ if (node.type === 'VariableDeclarator') {
279
+ return node.init ?? null;
280
+ }
281
+ }
282
+ return null;
283
+ }
284
+ function getIdentifierVariables(sourceCode, node, seen = new Set()) {
285
+ const variable = getIdentifierVariable(sourceCode, node);
286
+ if (variable) {
287
+ if (seen.has(variable)) {
288
+ return [variable];
289
+ }
290
+ seen.add(variable);
291
+ const initializer = getVariableInitializer(variable);
292
+ if (initializer?.type === 'ArrayExpression' ||
293
+ initializer?.type === 'ObjectExpression') {
294
+ return [
295
+ variable,
296
+ ...getIdentifierVariables(sourceCode, initializer, seen)
297
+ ];
298
+ }
299
+ return [variable];
300
+ }
301
+ if (!node) {
302
+ return [];
303
+ }
304
+ const child = getTransparentChild(node);
305
+ if (child) {
306
+ return getIdentifierVariables(sourceCode, child, seen);
307
+ }
308
+ if (node.type === 'ArrayExpression') {
309
+ return node.elements.flatMap(element => element
310
+ ? getIdentifierVariables(sourceCode, element.type === 'SpreadElement' ? element.argument : element, seen)
311
+ : []);
312
+ }
313
+ if (node.type === 'ObjectExpression') {
314
+ return node.properties.flatMap(property => getIdentifierVariables(sourceCode, property.type === 'Property' ? property.value : property.argument, seen));
315
+ }
316
+ return [];
317
+ }
318
+ function addPendingDisposable(pending, variable, node) {
319
+ const records = pending.get(variable) ?? [];
320
+ records.push({ node });
321
+ pending.set(variable, records);
322
+ }
323
+ function markDisposableManaged(pending, variable, useNode) {
324
+ if (!variable) {
325
+ return;
326
+ }
327
+ const records = pending.get(variable);
328
+ if (!records) {
329
+ return;
330
+ }
331
+ if (useNode && shouldMarkAllBranchRecords(records, useNode)) {
332
+ pending.delete(variable);
333
+ return;
334
+ }
335
+ records.pop();
336
+ if (records.length === 0) {
337
+ pending.delete(variable);
338
+ }
339
+ }
340
+ function hasPendingDisposableSet(pending, variable) {
341
+ return (pending.get(variable)?.some(record => {
342
+ const node = record.node;
343
+ return ((node.type === 'NewExpression' && isDisposableSetConstructor(node)) ||
344
+ isDisposableSetFactoryCall(node));
345
+ }) ?? false);
346
+ }
347
+ function getAssignedVariable(node, sourceCode) {
348
+ const expression = getOuterFallbackExpression(node);
349
+ const parent = expression.parent;
350
+ if (!parent) {
351
+ return null;
352
+ }
353
+ if (parent.type === 'VariableDeclarator' &&
354
+ parent.init === expression &&
355
+ parent.id.type === 'Identifier') {
356
+ const declaredVariables = sourceCode.getDeclaredVariables(parent);
357
+ return declaredVariables[0] ?? null;
358
+ }
359
+ if (parent.type === 'AssignmentExpression' &&
360
+ parent.right === expression &&
361
+ parent.left.type === 'Identifier') {
362
+ return resolveVariable(sourceCode, parent.left);
363
+ }
364
+ return null;
365
+ }
366
+ function isFunctionLike(node) {
367
+ return (node.type === 'ArrowFunctionExpression' ||
368
+ node.type === 'FunctionDeclaration' ||
369
+ node.type === 'FunctionExpression');
370
+ }
371
+ function getEnclosingFunction(node) {
372
+ let parent = node.parent;
373
+ while (parent) {
374
+ if (isFunctionLike(parent)) {
375
+ return parent;
376
+ }
377
+ parent = parent.parent;
378
+ }
379
+ return null;
380
+ }
381
+ function isConditionalOrRepeated(node) {
382
+ return (node.type === 'CatchClause' ||
383
+ node.type === 'ConditionalExpression' ||
384
+ node.type === 'DoWhileStatement' ||
385
+ node.type === 'ForInStatement' ||
386
+ node.type === 'ForOfStatement' ||
387
+ node.type === 'ForStatement' ||
388
+ node.type === 'IfStatement' ||
389
+ node.type === 'SwitchCase' ||
390
+ node.type === 'SwitchStatement' ||
391
+ node.type === 'TryStatement' ||
392
+ node.type === 'WhileStatement');
393
+ }
394
+ function isBeforeNode(first, second) {
395
+ if (first.range && second.range) {
396
+ return first.range[1] <= second.range[0];
397
+ }
398
+ return first.loc.end.line <= second.loc.start.line;
399
+ }
400
+ function getExclusiveBranchAncestor(node) {
401
+ let current = node;
402
+ let parent = current.parent;
403
+ while (parent) {
404
+ if (isFunctionLike(parent)) {
405
+ return null;
406
+ }
407
+ if (parent.type === 'IfStatement' &&
408
+ (parent.consequent === current || parent.alternate === current)) {
409
+ return parent;
410
+ }
411
+ if (parent.type === 'SwitchStatement' &&
412
+ current.type === 'SwitchCase' &&
413
+ parent.cases.includes(current)) {
414
+ return parent;
415
+ }
416
+ if (parent.type === 'ConditionalExpression' &&
417
+ (parent.consequent === current || parent.alternate === current)) {
418
+ return parent;
419
+ }
420
+ current = parent;
421
+ parent = current.parent;
422
+ }
423
+ return null;
424
+ }
425
+ function shouldMarkAllBranchRecords(records, useNode) {
426
+ if (records.length <= 1) {
427
+ return false;
428
+ }
429
+ const branch = getExclusiveBranchAncestor(records[0].node);
430
+ return (branch !== null &&
431
+ isBeforeNode(branch, useNode) &&
432
+ records.every(record => getExclusiveBranchAncestor(record.node) === branch));
433
+ }
434
+ function isUnconditionalUse(node, variable) {
435
+ const scopeBlock = variable.scope.block;
436
+ let parent = node.parent;
437
+ while (parent) {
438
+ if (parent === scopeBlock) {
439
+ return true;
440
+ }
441
+ if (isFunctionLike(parent) || isConditionalOrRepeated(parent)) {
442
+ return false;
443
+ }
444
+ parent = parent.parent;
445
+ }
446
+ return false;
447
+ }
448
+ function isTruthyIdentifierTest(node, variable, sourceCode) {
449
+ const identifier = getIdentifierVariable(sourceCode, node);
450
+ if (identifier === variable) {
451
+ return true;
452
+ }
453
+ if (node.type === 'UnaryExpression' &&
454
+ node.operator === '!' &&
455
+ node.argument.type === 'UnaryExpression' &&
456
+ node.argument.operator === '!') {
457
+ return isTruthyIdentifierTest(node.argument.argument, variable, sourceCode);
458
+ }
459
+ if (node.type === 'LogicalExpression' && node.operator === '&&') {
460
+ return (isTruthyIdentifierTest(node.left, variable, sourceCode) ||
461
+ isTruthyIdentifierTest(node.right, variable, sourceCode));
462
+ }
463
+ return false;
464
+ }
465
+ function isTruthyGuardedUse(node, variable, sourceCode) {
466
+ let current = node;
467
+ let parent = current.parent;
468
+ while (parent) {
469
+ if (isFunctionLike(parent)) {
470
+ return false;
471
+ }
472
+ if (parent.type === 'IfStatement' &&
473
+ parent.consequent === current &&
474
+ isTruthyIdentifierTest(parent.test, variable, sourceCode)) {
475
+ return true;
476
+ }
477
+ current = parent;
478
+ parent = current.parent;
479
+ }
480
+ return false;
481
+ }
482
+ function isManagedUse(node, variable, sourceCode) {
483
+ return (isUnconditionalUse(node, variable) ||
484
+ isTruthyGuardedUse(node, variable, sourceCode));
485
+ }
486
+ function isCatchClauseUse(node, variable) {
487
+ const scopeBlock = variable.scope.block;
488
+ let parent = node.parent;
489
+ while (parent) {
490
+ if (parent === scopeBlock) {
491
+ return false;
492
+ }
493
+ if (parent.type === 'CatchClause') {
494
+ return true;
495
+ }
496
+ parent = parent.parent;
497
+ }
498
+ return false;
499
+ }
500
+ function getFunctionScope(scope) {
501
+ let current = scope;
502
+ while (current) {
503
+ if (current.type === 'function' ||
504
+ current.type === 'module' ||
505
+ current.type === 'global') {
506
+ return current;
507
+ }
508
+ current = current.upper;
509
+ }
510
+ return null;
511
+ }
512
+ function isOuterFunctionScopeVariable(node, variable, sourceCode) {
513
+ return (getFunctionScope(sourceCode.getScope(node)) !==
514
+ getFunctionScope(variable.scope));
515
+ }
516
+ /**
517
+ * Checks whether the `export` keyword sits on a variable's own declaration, as
518
+ * in `export const tracker = ...`, including inside an exported `namespace`.
519
+ */
520
+ function isExportedAtDeclaration(variable) {
521
+ return variable.defs.some(definition => {
522
+ let current = definition.node;
523
+ while (current) {
524
+ if (current.type === 'ExportNamedDeclaration' ||
525
+ current.type === 'ExportDefaultDeclaration') {
526
+ return true;
527
+ }
528
+ // Stop at the declaration boundary: only the declaration itself and its
529
+ // enclosing statement can carry the `export` keyword.
530
+ if (current.type === 'VariableDeclaration' &&
531
+ current.parent?.type !== 'ExportNamedDeclaration') {
532
+ return false;
533
+ }
534
+ current = current.parent;
535
+ }
536
+ return false;
537
+ });
538
+ }
539
+ /**
540
+ * Checks whether a variable is exported by a later statement rather than at its
541
+ * declaration: `export { tracker }`, `export { tracker as t }`, or
542
+ * `export default tracker`. The scope manager records each of these as a
543
+ * reference to the local binding, so they are matched by resolution rather than
544
+ * by name.
545
+ */
546
+ function isExportedByExportStatement(variable) {
547
+ return variable.references.some(reference => {
548
+ const parent = reference.identifier.parent;
549
+ return (parent?.type === 'ExportSpecifier' ||
550
+ parent?.type === 'ExportDefaultDeclaration');
551
+ });
552
+ }
553
+ /**
554
+ * Checks whether a variable is an exported binding, in any of the forms the
555
+ * `export` keyword can take.
556
+ *
557
+ * Ownership of an exported singleton passes to the importers of the module, so
558
+ * this file cannot see - and is not responsible for - its disposal. Such
559
+ * declarations are also created exactly once and live for the lifetime of the
560
+ * program, so there is nothing to leak.
561
+ */
562
+ function isExportedVariable(variable) {
563
+ if (!isExportedAtDeclaration(variable) &&
564
+ !isExportedByExportStatement(variable)) {
565
+ return false;
566
+ }
567
+ // A reassigned export is not a single module-lifetime singleton: every value
568
+ // but the last one is replaced, and only the last one can still be disposed.
569
+ return (variable.references.filter(reference => reference.isWrite()).length <= 1);
570
+ }
571
+ /**
572
+ * Checks whether a node is reached on every run of its enclosing function, that
573
+ * is, whether nothing conditional or repeated sits between it and the function
574
+ * body. This is the same standard `isUnconditionalUse` applies within a scope.
575
+ */
576
+ function isUnconditionalWithinFunction(node) {
577
+ let parent = node.parent;
578
+ while (parent && !isFunctionLike(parent)) {
579
+ if (isConditionalOrRepeated(parent)) {
580
+ return false;
581
+ }
582
+ parent = parent.parent;
583
+ }
584
+ return true;
585
+ }
586
+ /**
587
+ * Checks whether a function expression ends up somewhere that can run it later:
588
+ * passed to a call, returned, stored on a field or in an object literal, or
589
+ * assigned to a variable that itself escapes.
590
+ *
591
+ * A callback that is only declared and never used is not a cleanup path, so
592
+ * `const cleanupLater = () => d.dispose();` on its own leaves `d` unmanaged.
593
+ */
594
+ function isEscapingCallback(fn, sourceCode) {
595
+ const expression = getOuterExpression(fn);
596
+ const parent = expression.parent;
597
+ if (!parent) {
598
+ return false;
599
+ }
600
+ if (parent.type === 'VariableDeclarator' && parent.init === expression) {
601
+ const [variable] = sourceCode.getDeclaredVariables(parent);
602
+ return (variable !== undefined &&
603
+ variable.references.some(reference => !reference.init && isEscapingUse(reference.identifier)));
604
+ }
605
+ return isEscapingUse(expression);
606
+ }
607
+ /**
608
+ * Positions in which a value is handed to something else: an argument or callee
609
+ * of a call, a return value, a field or object literal entry, an array element.
610
+ */
611
+ function isEscapingUse(node) {
612
+ const parent = getOuterExpression(node).parent;
613
+ if (!parent) {
614
+ return false;
615
+ }
616
+ return (parent.type === 'CallExpression' ||
617
+ parent.type === 'NewExpression' ||
618
+ parent.type === 'ReturnStatement' ||
619
+ parent.type === 'PropertyDefinition' ||
620
+ parent.type === 'Property' ||
621
+ parent.type === 'ArrayExpression' ||
622
+ (parent.type === 'ArrowFunctionExpression' && parent.body === node) ||
623
+ (parent.type === 'AssignmentExpression' &&
624
+ parent.left.type === 'MemberExpression'));
625
+ }
626
+ /**
627
+ * Checks whether a reference sits inside a closure that the declaring function
628
+ * unconditionally returns, the factory pattern:
629
+ *
630
+ * ```ts
631
+ * function createToolbarFactory() {
632
+ * const items = new ObservableList(...);
633
+ * return (widget: Widget) => { ...uses items... };
634
+ * }
635
+ * ```
636
+ *
637
+ * The closure is the function's product, so the captured state's lifetime is
638
+ * bound to it and belongs to whoever holds the factory. Walks outwards through
639
+ * enclosing functions, because the reference is often nested deeper than the
640
+ * returned closure itself (in a handler declared inside it).
641
+ *
642
+ * Only an unconditional `return` counts, matching how ownership transfer is
643
+ * treated elsewhere in this file. A closure that merely escapes as a call
644
+ * argument does not count: that is `defer(() => console.log(d))`, which hands
645
+ * off nothing.
646
+ */
647
+ function isCapturedByReturnedClosure(identifier, variable, ownership) {
648
+ const declaringFunction = getFunctionScope(variable.scope)?.block;
649
+ if (!declaringFunction) {
650
+ return false;
651
+ }
652
+ let fn = getEnclosingFunction(identifier);
653
+ while (fn && fn !== declaringFunction) {
654
+ // The closure may be returned directly, or sit in an object or array that
655
+ // is returned, possibly via the variable holding it:
656
+ // `const thisObject = { get: () => dummy.x }; return thisObject;`
657
+ // A closure handed to a wrapper call escapes if the wrapper's result does:
658
+ // `jest.fn(() => dummy.x)` as a property of a returned object.
659
+ let escaped = fn;
660
+ for (;;) {
661
+ const outer = getOuterExpression(escaped);
662
+ const wrapper = outer.parent;
663
+ if (wrapper?.type === 'CallExpression' &&
664
+ wrapper.arguments.includes(outer)) {
665
+ escaped = wrapper;
666
+ continue;
667
+ }
668
+ break;
669
+ }
670
+ const expression = getOuterExpression(getManagedAggregateExpression(escaped) ?? escaped);
671
+ const parent = expression.parent;
672
+ const holder = ownership
673
+ ? getAggregateHolderVariable(expression, ownership)
674
+ : null;
675
+ const returnedViaHolder = holder !== null && isVariableHandedOff(holder, ownership);
676
+ const returnedFromDeclaringFunction = returnedViaHolder ||
677
+ (parent?.type === 'ReturnStatement' &&
678
+ parent.argument === expression &&
679
+ getEnclosingFunction(parent) === declaringFunction &&
680
+ isUnconditionalWithinFunction(parent));
681
+ // `const make = () => () => items.use()` returns implicitly.
682
+ const isImplicitReturnBody = parent?.type === 'ArrowFunctionExpression' &&
683
+ parent.body === expression &&
684
+ parent === declaringFunction;
685
+ if (returnedFromDeclaringFunction || isImplicitReturnBody) {
686
+ return true;
687
+ }
688
+ fn = getEnclosingFunction(fn);
689
+ }
690
+ return false;
691
+ }
692
+ /**
693
+ * Checks whether this exact reference is handed to an ownership call, following
694
+ * only value-preserving positions on the way out: `set.add(item)`,
695
+ * `set.add([item])`, `set.add({ item })`, `set.add(item as IDisposable)`.
696
+ *
697
+ * Anchored on the resolved reference rather than on its name, so a same-named
698
+ * binding elsewhere in the callback cannot be mistaken for this variable. The
699
+ * transfer must also be unconditional, matching how same-scope transfers are
700
+ * treated: a transfer that only happens on some paths is not a transfer.
701
+ */
702
+ function isReferenceHandedToOwnershipCall(identifier, ownership) {
703
+ let current = identifier;
704
+ let parent = current.parent;
705
+ while (parent && !isFunctionLike(parent)) {
706
+ if (parent.type === 'CallExpression') {
707
+ return (isOwnershipArgument(parent, current, ownership) &&
708
+ isUnconditionalWithinFunction(parent));
709
+ }
710
+ const preservesValue = getTransparentChild(parent) === current ||
711
+ (parent.type === 'ArrayExpression' &&
712
+ parent.elements.includes(current)) ||
713
+ (parent.type === 'Property' && parent.value === current) ||
714
+ (parent.type === 'ObjectExpression' &&
715
+ parent.properties.includes(current));
716
+ if (!preservesValue) {
717
+ return false;
718
+ }
719
+ current = parent;
720
+ parent = current.parent;
721
+ }
722
+ return false;
723
+ }
724
+ /**
725
+ * Checks whether a variable's lifetime is taken over by a nested function, in
726
+ * any of three ways:
727
+ *
728
+ * - it is captured by a closure the declaring function returns, so the closure
729
+ * owns it (`createToolbarFactory`);
730
+ * - an escaping callback unconditionally disposes it
731
+ * (`requestAnimationFrame(() => splash.dispose())`, promise chains);
732
+ * - an escaping callback unconditionally hands its ownership to something else.
733
+ *
734
+ * The last two generalise the existing `disposed.connect(...)` special case to
735
+ * any callback. Cleanup that is itself conditional inside the callback does not
736
+ * count, so "cleanup only on some paths" stays reportable, and every branch
737
+ * resolves identifiers to this variable rather than comparing names, so a
738
+ * shadowing binding inside the callback cannot silence the outer one.
739
+ */
740
+ function isDisposedByNestedFunction(variable, ownership) {
741
+ const declaringScope = getFunctionScope(variable.scope);
742
+ for (const reference of variable.references) {
743
+ if (getFunctionScope(reference.from) === declaringScope) {
744
+ continue;
745
+ }
746
+ if (isCapturedByReturnedClosure(reference.identifier, variable, ownership)) {
747
+ return true;
748
+ }
749
+ const fn = getEnclosingFunction(reference.identifier);
750
+ if (!fn ||
751
+ (fn.type !== 'ArrowFunctionExpression' &&
752
+ fn.type !== 'FunctionExpression')) {
753
+ continue;
754
+ }
755
+ // A callback nobody can run is not a cleanup path.
756
+ if (!isEscapingCallback(fn, ownership.sourceCode)) {
757
+ continue;
758
+ }
759
+ if (isReferenceHandedToOwnershipCall(reference.identifier, ownership)) {
760
+ return true;
761
+ }
762
+ const disposesVariable = getUnconditionalDisposeTargets(fn).some(target => getIdentifierVariable(ownership.sourceCode, target) === variable);
763
+ if (disposesVariable) {
764
+ return true;
765
+ }
766
+ }
767
+ return false;
768
+ }
769
+ /**
770
+ * Checks whether an object literal is shaped like a Jupyter plugin descriptor,
771
+ * either by its declared type or by its property names.
772
+ */
773
+ function isPluginDescriptorObject(object, ownership) {
774
+ const declarator = object.parent;
775
+ if (declarator?.type === 'VariableDeclarator' &&
776
+ (0, plugin_utils_1.getJupyterPluginKind)(declarator, ownership.checker, ownership.services
777
+ ? node => ownership.services.esTreeNodeToTSNodeMap.get(node)
778
+ : null) !== null) {
779
+ return true;
780
+ }
781
+ const propertyNames = new Set(object.properties.flatMap(property => property.type === 'Property'
782
+ ? [getPropertyName(property)].filter((name) => name !== null)
783
+ : []));
784
+ return (propertyNames.has('id') &&
785
+ propertyNames.has('activate') &&
786
+ ['autoStart', 'optional', 'provides', 'requires'].some(name => propertyNames.has(name)));
787
+ }
788
+ /**
789
+ * Resolves the variable a function is named by, so its references can be
790
+ * inspected: `function activateCsv() {}` or `const activateCsv = () => {}`.
791
+ */
792
+ function getFunctionNameVariable(fn, sourceCode) {
793
+ if (fn.type === 'FunctionDeclaration' && fn.id) {
794
+ const name = fn.id.name;
795
+ return (sourceCode.getDeclaredVariables(fn).find(entry => entry.name === name) ??
796
+ null);
797
+ }
798
+ const declarator = fn.parent;
799
+ if (declarator?.type === 'VariableDeclarator' &&
800
+ declarator.init === fn &&
801
+ declarator.id.type === 'Identifier') {
802
+ return sourceCode.getDeclaredVariables(declarator)[0] ?? null;
803
+ }
804
+ return null;
805
+ }
806
+ /**
807
+ * Checks whether a function is wired up as the `activate` of a plugin
808
+ * descriptor elsewhere in the file, e.g. `activate: activateCsv`.
809
+ */
810
+ function isReferencedAsPluginActivate(variable, ownership) {
811
+ return variable.references.some(reference => {
812
+ const property = reference.identifier.parent;
813
+ return (property?.type === 'Property' &&
814
+ property.value === reference.identifier &&
815
+ getPropertyName(property) === 'activate' &&
816
+ property.parent?.type === 'ObjectExpression' &&
817
+ isPluginDescriptorObject(property.parent, ownership));
818
+ });
819
+ }
820
+ function isInJupyterPluginActivate(node, ownership) {
821
+ const fn = getEnclosingFunction(node);
822
+ if (!fn) {
823
+ return false;
824
+ }
825
+ // Inline form: `const plugin = { id, autoStart, activate: () => { ... } }`.
826
+ const property = fn.parent;
827
+ if (property?.type === 'Property' &&
828
+ property.value === fn &&
829
+ getPropertyName(property) === 'activate' &&
830
+ property.parent?.type === 'ObjectExpression' &&
831
+ isPluginDescriptorObject(property.parent, ownership)) {
832
+ return true;
833
+ }
834
+ // Named form: `function activate() { ... }`, the convention for plugins whose
835
+ // activation lives in a separate function (often in a `Private` namespace).
836
+ if (fn.type === 'FunctionDeclaration' && fn.id?.name === 'activate') {
837
+ return true;
838
+ }
839
+ // Referenced form: `function activateCsv() { ... }` used as
840
+ // `{ id, autoStart, activate: activateCsv }`.
841
+ const nameVariable = getFunctionNameVariable(fn, ownership.sourceCode);
842
+ return (nameVariable !== null &&
843
+ isReferencedAsPluginActivate(nameVariable, ownership));
844
+ }
845
+ function hasTypeName(type, checker, names) {
846
+ const apparentType = checker.getApparentType(type);
847
+ const symbols = [
848
+ type.aliasSymbol,
849
+ type.getSymbol(),
850
+ apparentType.aliasSymbol,
851
+ apparentType.getSymbol()
852
+ ];
853
+ return symbols.some(symbol => {
854
+ if (!symbol) {
855
+ return false;
856
+ }
857
+ return names.includes(symbol.getName());
858
+ });
859
+ }
860
+ function isDisposableSetType(node, ownership) {
861
+ return hasExpressionTypeName(node, ownership, DISPOSABLE_SET_NAMES);
862
+ }
863
+ function hasExpressionTypeName(node, ownership, names) {
864
+ if (!ownership.checker || !ownership.services) {
865
+ return false;
866
+ }
867
+ try {
868
+ const tsNode = ownership.services.esTreeNodeToTSNodeMap.get(node);
869
+ const type = ownership.checker.getTypeAtLocation(tsNode);
870
+ return hasTypeName(type, ownership.checker, names);
871
+ }
872
+ catch {
873
+ return false;
874
+ }
875
+ }
876
+ function declarationHasHeritageName(declaration, names) {
877
+ if (!(ts.isClassDeclaration(declaration) ||
878
+ ts.isClassExpression(declaration) ||
879
+ ts.isInterfaceDeclaration(declaration))) {
880
+ return false;
881
+ }
882
+ return (declaration.heritageClauses?.some(clause => clause.types.some(type => {
883
+ const text = type.expression.getText();
884
+ return names.some(name => text === name || text.endsWith(`.${name}`));
885
+ })) ?? false);
886
+ }
887
+ function hasHeritageName(type, names, seen = new Set()) {
888
+ if (seen.has(type)) {
889
+ return false;
890
+ }
891
+ seen.add(type);
892
+ if (type.isUnion()) {
893
+ return type.types.some(part => hasHeritageName(part, names, seen));
894
+ }
895
+ if (type.isIntersection()) {
896
+ return type.types.some(part => hasHeritageName(part, names, seen));
897
+ }
898
+ const symbol = type.getSymbol();
899
+ return (symbol?.declarations?.some(declaration => declarationHasHeritageName(declaration, names)) ?? false);
900
+ }
901
+ function hasExpressionTypeOrHeritageName(node, ownership, names) {
902
+ if (!ownership.checker || !ownership.services) {
903
+ return false;
904
+ }
905
+ try {
906
+ const tsNode = ownership.services.esTreeNodeToTSNodeMap.get(node);
907
+ const type = ownership.checker.getTypeAtLocation(tsNode);
908
+ return (hasTypeName(type, ownership.checker, names) ||
909
+ hasHeritageName(type, names));
910
+ }
911
+ catch {
912
+ return false;
913
+ }
914
+ }
915
+ function isAttachedPropertyType(node, ownership) {
916
+ return hasExpressionTypeName(node, ownership, ['AttachedProperty']);
917
+ }
918
+ function isDialogType(node, ownership) {
919
+ return hasExpressionTypeOrHeritageName(node, ownership, ['Dialog']);
920
+ }
921
+ function isOwnershipAddCall(node, ownership) {
922
+ if (!isStaticMemberCall(node, 'add') ||
923
+ node.callee.type !== 'MemberExpression') {
924
+ return false;
925
+ }
926
+ return isDisposableSetType(node.callee.object, ownership);
927
+ }
928
+ function isOwnershipSetCall(node, ownership) {
929
+ if (!isStaticMemberCall(node, 'set') ||
930
+ node.callee.type !== 'MemberExpression') {
931
+ return false;
932
+ }
933
+ return isAttachedPropertyType(node.callee.object, ownership);
934
+ }
935
+ function isClassFieldCollectionMutationCall(node, names) {
936
+ return (node.callee.type === 'MemberExpression' &&
937
+ names.includes(getStaticMemberName(node.callee) ?? '') &&
938
+ isThisMemberExpression(node.callee.object));
939
+ }
940
+ function isArrayExtInsertCall(node) {
941
+ return (isStaticMemberCall(node, 'insert') &&
942
+ node.callee.type === 'MemberExpression' &&
943
+ getCalleeName(node.callee.object) === 'ArrayExt' &&
944
+ node.arguments.length >= 3 &&
945
+ isThisMemberExpression(node.arguments[0]));
946
+ }
947
+ /**
948
+ * Ownership is decided by what the callee declares, not by its name: an API
949
+ * that types a parameter as a disposable is saying it takes something with a
950
+ * lifecycle, while one typing it `any` is not.
951
+ *
952
+ * Signature resolution is the expensive part of this rule, so results are
953
+ * memoised per call node and per type for the lifetime of the file.
954
+ */
955
+ const signatureCache = new WeakMap();
956
+ const disposableTypeCache = new WeakMap();
957
+ function isDisposableTypeCached(type, checker) {
958
+ const cached = disposableTypeCache.get(type);
959
+ if (cached !== undefined) {
960
+ return cached;
961
+ }
962
+ const result = isDisposableType(type, checker);
963
+ disposableTypeCache.set(type, result);
964
+ return result;
965
+ }
966
+ function getResolvedSignatureCached(call, ownership) {
967
+ const cached = signatureCache.get(call);
968
+ if (cached !== undefined) {
969
+ return cached;
970
+ }
971
+ let signature = null;
972
+ const { checker, services } = ownership;
973
+ if (checker && services) {
974
+ try {
975
+ const tsNode = services.esTreeNodeToTSNodeMap.get(call);
976
+ signature = tsNode
977
+ ? (checker.getResolvedSignature(tsNode) ?? null)
978
+ : null;
979
+ }
980
+ catch {
981
+ signature = null;
982
+ }
983
+ }
984
+ signatureCache.set(call, signature);
985
+ return signature;
986
+ }
987
+ function getParameterTypeForArgument(call, argumentIndex, ownership) {
988
+ const { checker } = ownership;
989
+ if (!checker) {
990
+ return null;
991
+ }
992
+ const signature = getResolvedSignatureCached(call, ownership);
993
+ const parameters = signature?.getParameters() ?? [];
994
+ if (parameters.length === 0) {
995
+ return null;
996
+ }
997
+ try {
998
+ const index = Math.min(argumentIndex, parameters.length - 1);
999
+ const parameter = parameters[index];
1000
+ const declaration = parameter.valueDeclaration ?? parameter.declarations?.[0];
1001
+ if (!declaration) {
1002
+ return null;
1003
+ }
1004
+ // An optional parameter or one with a default is typed `T | undefined`,
1005
+ // and `getProperty` on a union only reports properties present in every
1006
+ // constituent. Strip the nullish part so `showDialog(options = {})` still
1007
+ // exposes its option types.
1008
+ const type = checker.getNonNullableType(checker.getTypeOfSymbolAtLocation(parameter, declaration));
1009
+ // A rest parameter is declared as an array; the argument binds to its
1010
+ // element type. `console.log(x)` has `...data: any[]`, so this matters.
1011
+ if (ts.isParameter(declaration) &&
1012
+ declaration.dotDotDotToken !== undefined) {
1013
+ // A tuple rest parameter still names each position, as in jest's
1014
+ // `new (...args: [IOptions, IKernelConnection | null]) => T`, so index
1015
+ // into it rather than collapsing to the union of its elements.
1016
+ if (checker.isTupleType(type)) {
1017
+ const elements = checker.getTypeArguments(type);
1018
+ // The rest parameter is always last, so `index` is its position and
1019
+ // the argument lands at that offset into the tuple. Without this,
1020
+ // `(a: number, ...rest: [D, X])` maps the first rest argument to `X`.
1021
+ const offset = argumentIndex - index;
1022
+ return elements[offset] ?? elements[elements.length - 1] ?? type;
1023
+ }
1024
+ return checker.getIndexTypeOfType(type, ts.IndexKind.Number) ?? type;
1025
+ }
1026
+ return type;
1027
+ }
1028
+ catch {
1029
+ return null;
1030
+ }
1031
+ }
1032
+ /**
1033
+ * Whether this specific argument is one the callee takes ownership of. Lazy by
1034
+ * design: callers ask about the one argument they care about, so a call whose
1035
+ * arguments are all uninteresting never costs a signature resolution.
1036
+ */
1037
+ function isOwnershipArgument(call, argument, ownership) {
1038
+ if (call.callee.type === 'Super') {
1039
+ return true;
1040
+ }
1041
+ // Checked first: a named sink may own something that is not a direct
1042
+ // argument, such as an element of the array in `DisposableSet.from([...])`.
1043
+ if (call.type === 'CallExpression' &&
1044
+ isNamedOwnershipSink(call, argument, ownership)) {
1045
+ return true;
1046
+ }
1047
+ let index = call.arguments.indexOf(argument);
1048
+ let elementOfArrayArgument = false;
1049
+ if (index < 0) {
1050
+ // `new Owner([disposable])`: the element binds to the element type of the
1051
+ // parameter, not to the parameter itself.
1052
+ const array = argument.parent;
1053
+ if (array?.type === 'ArrayExpression' &&
1054
+ array.elements.includes(argument)) {
1055
+ index = call.arguments.indexOf(array);
1056
+ elementOfArrayArgument = index >= 0;
1057
+ }
1058
+ if (!elementOfArrayArgument) {
1059
+ return false;
1060
+ }
1061
+ }
1062
+ let type = getParameterTypeForArgument(call, index, ownership);
1063
+ if (type && elementOfArrayArgument && ownership.checker) {
1064
+ type =
1065
+ ownership.checker.getIndexTypeOfType(type, ts.IndexKind.Number) ?? type;
1066
+ }
1067
+ return (type !== null &&
1068
+ ownership.checker !== null &&
1069
+ isDisposableTypeCached(type, ownership.checker));
1070
+ }
1071
+ /**
1072
+ * The options-object form: the disposable is a property of an object literal
1073
+ * argument, so the question is whether that property is declared disposable.
1074
+ */
1075
+ function isTypedOwnedOptionProperty(call, argumentIndex, propertyName, ownership) {
1076
+ const { checker } = ownership;
1077
+ const type = getParameterTypeForArgument(call, argumentIndex, ownership);
1078
+ if (!type || !checker) {
1079
+ return false;
1080
+ }
1081
+ const property = type.getProperty(propertyName);
1082
+ const declaration = property?.valueDeclaration ?? property?.declarations?.[0] ?? null;
1083
+ if (!property || !declaration) {
1084
+ return false;
1085
+ }
1086
+ try {
1087
+ return isDisposableTypeCached(checker.getTypeOfSymbolAtLocation(property, declaration), checker);
1088
+ }
1089
+ catch {
1090
+ return false;
1091
+ }
1092
+ }
1093
+ /**
1094
+ * The remaining name-based sinks: an escape hatch for APIs whose types are too
1095
+ * loose for the check above (notably test mocks typed as `any`), configured
1096
+ * through `ownershipFunctionNames`, plus the shapes that types cannot express
1097
+ * (a `super(...)` handoff, `DisposableSet.from([...])` unwrapping an array,
1098
+ * `this._items.push(...)` into a field collection).
1099
+ */
1100
+ function isNamedOwnershipSink(node, argument, ownership) {
1101
+ return getOwnershipArgumentsByName(node, ownership).includes(argument);
1102
+ }
1103
+ function getOwnershipArguments(node, ownership) {
1104
+ // Named sinks can name things that are not direct arguments (array elements
1105
+ // of `DisposableSet.from([...])`), so they are unioned in rather than
1106
+ // filtered out of `node.arguments`.
1107
+ const named = getOwnershipArgumentsByName(node, ownership);
1108
+ const typed = node.arguments.filter(argument => !named.includes(argument) &&
1109
+ isOwnershipArgument(node, argument, ownership));
1110
+ return [...named, ...typed];
1111
+ }
1112
+ function getOwnershipArgumentsByName(node, ownership) {
1113
+ if (node.callee.type === 'Super') {
1114
+ return node.arguments;
1115
+ }
1116
+ if (isOwnershipAddCall(node, ownership)) {
1117
+ return node.arguments;
1118
+ }
1119
+ if (isDisposableSetFactoryCall(node)) {
1120
+ return getDisposableSetFactoryArguments(node);
1121
+ }
1122
+ if (isOwnershipFunctionCall(node, ownership)) {
1123
+ return node.arguments;
1124
+ }
1125
+ if (isOwnershipSetCall(node, ownership)) {
1126
+ return node.arguments.slice(1);
1127
+ }
1128
+ if (isClassFieldCollectionMutationCall(node, ['set'])) {
1129
+ return node.arguments.slice(1);
1130
+ }
1131
+ if (isClassFieldCollectionMutationCall(node, ['push', 'unshift'])) {
1132
+ return node.arguments;
1133
+ }
1134
+ if (isArrayExtInsertCall(node)) {
1135
+ return node.arguments.slice(2, 3);
1136
+ }
1137
+ return [];
1138
+ }
1139
+ function isArgumentOfCallOrNew(node) {
1140
+ return node.type === 'CallExpression' || node.type === 'NewExpression';
1141
+ }
1142
+ function getPropertyName(node) {
1143
+ if (!node.computed && node.key.type === 'Identifier') {
1144
+ return node.key.name;
1145
+ }
1146
+ if (node.key.type === 'Literal' && typeof node.key.value === 'string') {
1147
+ return node.key.value;
1148
+ }
1149
+ return null;
1150
+ }
1151
+ function getObjectExpression(sourceCode, node) {
1152
+ if (node.type === 'ObjectExpression') {
1153
+ return node;
1154
+ }
1155
+ const child = getTransparentChild(node);
1156
+ if (child) {
1157
+ return getObjectExpression(sourceCode, child);
1158
+ }
1159
+ if (node.type === 'Identifier') {
1160
+ const variable = resolveVariable(sourceCode, node);
1161
+ const initializer = variable ? getVariableInitializer(variable) : null;
1162
+ if (initializer?.type === 'ObjectExpression') {
1163
+ return initializer;
1164
+ }
1165
+ }
1166
+ return null;
1167
+ }
1168
+ /**
1169
+ * The option names a call or constructor takes ownership of, read from the
1170
+ * declared type of its options parameter rather than from a table of known
1171
+ * JupyterLab classes.
1172
+ */
1173
+ /**
1174
+ * The option names a call or constructor takes ownership of at one argument
1175
+ * position, read from the declared type of that parameter rather than from a
1176
+ * table of known JupyterLab classes.
1177
+ *
1178
+ * Scoped per argument: two parameters may both declare a `payload` property
1179
+ * with only one of them disposable, and crediting the wrong one would suppress
1180
+ * a real finding.
1181
+ */
1182
+ function getManagedOptionNames(node, argumentIndex, ownership) {
1183
+ const { checker } = ownership;
1184
+ if (!checker) {
1185
+ return [];
1186
+ }
1187
+ const type = getParameterTypeForArgument(node, argumentIndex, ownership);
1188
+ if (!type) {
1189
+ return [];
1190
+ }
1191
+ const names = [];
1192
+ for (const property of type.getProperties()) {
1193
+ const declaration = property.valueDeclaration ?? property.declarations?.[0];
1194
+ if (!declaration) {
1195
+ continue;
1196
+ }
1197
+ try {
1198
+ if (isDisposableTypeCached(checker.getTypeOfSymbolAtLocation(property, declaration), checker)) {
1199
+ names.push(property.getName());
1200
+ }
1201
+ }
1202
+ catch {
1203
+ // Unresolvable property type: not an ownership claim.
1204
+ }
1205
+ }
1206
+ return names;
1207
+ }
1208
+ function markManagedKnownOptionVariables(pending, node, ownership) {
1209
+ node.arguments.forEach((argument, index) => {
1210
+ if (argument.type === 'ObjectExpression') {
1211
+ return;
1212
+ }
1213
+ const object = getObjectExpression(ownership.sourceCode, argument);
1214
+ if (!object) {
1215
+ return;
1216
+ }
1217
+ const optionNames = getManagedOptionNames(node, index, ownership);
1218
+ if (optionNames.length === 0) {
1219
+ return;
1220
+ }
1221
+ for (const property of object.properties) {
1222
+ if (property.type === 'Property' &&
1223
+ optionNames.includes(getPropertyName(property) ?? '')) {
1224
+ markManagedVariables(pending, property.value, ownership);
1225
+ }
1226
+ }
1227
+ });
1228
+ }
1229
+ function isOptionsObjectValueManaged(expression, ownership) {
1230
+ const property = expression.parent;
1231
+ if (!property ||
1232
+ property.type !== 'Property' ||
1233
+ property.value !== expression) {
1234
+ return false;
1235
+ }
1236
+ const object = property.parent;
1237
+ if (!object || object.type !== 'ObjectExpression') {
1238
+ return false;
1239
+ }
1240
+ const parent = object.parent;
1241
+ const propertyName = getPropertyName(property);
1242
+ if (parent?.type === 'AssignmentPattern' &&
1243
+ parent.right === object &&
1244
+ propertyName === 'shell') {
1245
+ return true;
1246
+ }
1247
+ if (!parent || !isArgumentOfCallOrNew(parent)) {
1248
+ return false;
1249
+ }
1250
+ if (!parent.arguments.includes(object)) {
1251
+ return false;
1252
+ }
1253
+ if (propertyName !== null) {
1254
+ const argumentIndex = parent.arguments.indexOf(object);
1255
+ if (argumentIndex >= 0 &&
1256
+ isTypedOwnedOptionProperty(parent, argumentIndex, propertyName, ownership)) {
1257
+ return true;
1258
+ }
1259
+ }
1260
+ return (parent.callee.type === 'Super' ||
1261
+ isOwnershipArgument(parent, object, ownership));
1262
+ }
1263
+ function getManagedAggregateExpression(node) {
1264
+ let current = getOuterExpression(node);
1265
+ let parent = current.parent;
1266
+ let foundAggregate = false;
1267
+ while (parent) {
1268
+ if (parent.type === 'Property' && parent.value === current) {
1269
+ current = parent;
1270
+ parent = current.parent;
1271
+ foundAggregate = true;
1272
+ continue;
1273
+ }
1274
+ if (parent.type === 'SpreadElement' && parent.argument === current) {
1275
+ current = parent;
1276
+ parent = current.parent;
1277
+ foundAggregate = true;
1278
+ continue;
1279
+ }
1280
+ if (parent.type === 'ArrayExpression' &&
1281
+ parent.elements.includes(current)) {
1282
+ current = parent;
1283
+ parent = current.parent;
1284
+ foundAggregate = true;
1285
+ continue;
1286
+ }
1287
+ if (parent.type === 'ObjectExpression' &&
1288
+ parent.properties.includes(current)) {
1289
+ current = parent;
1290
+ parent = current.parent;
1291
+ foundAggregate = true;
1292
+ continue;
1293
+ }
1294
+ break;
1295
+ }
1296
+ return foundAggregate ? current : null;
1297
+ }
1298
+ /**
1299
+ * Checks whether a variable is itself handed off: returned, assigned to a
1300
+ * field, or passed to an ownership sink. Used to follow a disposable one hop
1301
+ * further, through the variable that holds the aggregate containing it:
1302
+ *
1303
+ * ```ts
1304
+ * const thisObject = { contents: new ContentsManagerMock() };
1305
+ * return thisObject;
1306
+ * ```
1307
+ */
1308
+ function isVariableHandedOff(variable, ownership) {
1309
+ return variable.references.some(reference => {
1310
+ if (reference.init) {
1311
+ return false;
1312
+ }
1313
+ // Walk out through any aggregate the reference sits in, so a spread such
1314
+ // as `new Notebook({ ...history })` still counts as handing off `history`.
1315
+ const aggregate = getManagedAggregateExpression(reference.identifier) ??
1316
+ reference.identifier;
1317
+ const expression = getOuterFallbackExpression(aggregate);
1318
+ const parent = expression.parent;
1319
+ if (!parent) {
1320
+ return false;
1321
+ }
1322
+ if (parent.type === 'ReturnStatement' && parent.argument === expression) {
1323
+ return true;
1324
+ }
1325
+ if (parent.type === 'ArrowFunctionExpression' &&
1326
+ parent.body === expression) {
1327
+ return true;
1328
+ }
1329
+ if (parent.type === 'AssignmentExpression' &&
1330
+ parent.right === expression &&
1331
+ parent.left.type === 'MemberExpression') {
1332
+ return true;
1333
+ }
1334
+ if (parent.type === 'PropertyDefinition' && parent.value === expression) {
1335
+ return true;
1336
+ }
1337
+ if (parent.type === 'CallExpression' || parent.type === 'NewExpression') {
1338
+ return isOwnershipArgument(parent, expression, ownership);
1339
+ }
1340
+ return false;
1341
+ });
1342
+ }
1343
+ /**
1344
+ * Resolves the variable an aggregate is assigned to, if any:
1345
+ * `const thisObject = { ... }`.
1346
+ */
1347
+ function getAggregateHolderVariable(aggregate, ownership) {
1348
+ const parent = aggregate.parent;
1349
+ if (parent?.type !== 'VariableDeclarator' ||
1350
+ parent.init !== aggregate ||
1351
+ parent.id.type !== 'Identifier') {
1352
+ return null;
1353
+ }
1354
+ return ownership.sourceCode.getDeclaredVariables(parent)[0] ?? null;
1355
+ }
1356
+ function isAggregateExpressionManaged(expression, ownership) {
1357
+ const aggregate = getManagedAggregateExpression(expression);
1358
+ if (!aggregate) {
1359
+ return false;
1360
+ }
1361
+ const outerAggregate = getOuterFallbackExpression(aggregate);
1362
+ const parent = outerAggregate.parent;
1363
+ if (!parent) {
1364
+ return false;
1365
+ }
1366
+ // One hop through the variable holding the aggregate.
1367
+ const holder = getAggregateHolderVariable(outerAggregate, ownership);
1368
+ if (holder && isVariableHandedOff(holder, ownership)) {
1369
+ return true;
1370
+ }
1371
+ if (parent.type === 'ReturnStatement' && parent.argument === outerAggregate) {
1372
+ return true;
1373
+ }
1374
+ if (parent.type === 'ArrowFunctionExpression' &&
1375
+ parent.body === outerAggregate) {
1376
+ return true;
1377
+ }
1378
+ if (parent.type === 'AssignmentExpression' &&
1379
+ parent.right === outerAggregate &&
1380
+ parent.left.type === 'MemberExpression') {
1381
+ return true;
1382
+ }
1383
+ if (parent.type === 'PropertyDefinition' && parent.value === outerAggregate) {
1384
+ return true;
1385
+ }
1386
+ if (isOptionsObjectValueManaged(outerAggregate, ownership)) {
1387
+ return true;
1388
+ }
1389
+ if (parent.type === 'CallExpression') {
1390
+ return isOwnershipArgument(parent, outerAggregate, ownership);
1391
+ }
1392
+ return false;
1393
+ }
1394
+ function isDisposableExpressionManaged(node, ownership) {
1395
+ const expression = getOuterFallbackExpression(node);
1396
+ const parent = expression.parent;
1397
+ if (!parent) {
1398
+ return false;
1399
+ }
1400
+ if (parent.type === 'ReturnStatement' && parent.argument === expression) {
1401
+ return true;
1402
+ }
1403
+ if (parent.type === 'ArrowFunctionExpression' && parent.body === expression) {
1404
+ return true;
1405
+ }
1406
+ if (parent.type === 'AssignmentExpression' &&
1407
+ parent.right === expression &&
1408
+ parent.left.type === 'MemberExpression') {
1409
+ return true;
1410
+ }
1411
+ if (parent.type === 'PropertyDefinition' && parent.value === expression) {
1412
+ return true;
1413
+ }
1414
+ if (parent.type === 'CallExpression' || parent.type === 'NewExpression') {
1415
+ return isOwnershipArgument(parent, expression, ownership);
1416
+ }
1417
+ if (parent.type === 'ArrayExpression' &&
1418
+ (parent.parent?.type === 'CallExpression' ||
1419
+ parent.parent?.type === 'NewExpression')) {
1420
+ return isOwnershipArgument(parent.parent, expression, ownership);
1421
+ }
1422
+ if (isOptionsObjectValueManaged(expression, ownership)) {
1423
+ return true;
1424
+ }
1425
+ if (isAggregateExpressionManaged(expression, ownership)) {
1426
+ return true;
1427
+ }
1428
+ if (parent.type === 'MemberExpression' &&
1429
+ parent.object === expression &&
1430
+ parent.parent?.type === 'CallExpression' &&
1431
+ parent.parent.callee === parent) {
1432
+ if (getStaticMemberName(parent) === 'dispose') {
1433
+ return true;
1434
+ }
1435
+ if (getStaticMemberName(parent) === 'launch' &&
1436
+ isDialogType(expression, ownership)) {
1437
+ return true;
1438
+ }
1439
+ }
1440
+ return false;
1441
+ }
1442
+ function isDialogLaunchCall(node, ownership) {
1443
+ return (isStaticMemberCall(node, 'launch') &&
1444
+ node.callee.type === 'MemberExpression' &&
1445
+ node.callee.object.type !== 'Super' &&
1446
+ isDialogType(node.callee.object, ownership));
1447
+ }
1448
+ function markManagedVariables(pending, node, ownership) {
1449
+ const variables = getIdentifierVariables(ownership.sourceCode, node);
1450
+ for (const variable of variables) {
1451
+ if (isManagedUse(node, variable, ownership.sourceCode)) {
1452
+ markDisposableManaged(pending, variable, node);
1453
+ }
1454
+ }
1455
+ }
1456
+ function markManagedVariablesWithoutScopeCheck(pending, node, ownership, useNode) {
1457
+ const variables = getIdentifierVariables(ownership.sourceCode, node);
1458
+ for (const variable of variables) {
1459
+ markDisposableManaged(pending, variable, useNode);
1460
+ }
1461
+ }
1462
+ function getDisposedSignalCallback(node) {
1463
+ if (!isStaticMemberCall(node, 'connect') ||
1464
+ node.callee.type !== 'MemberExpression' ||
1465
+ node.callee.object.type !== 'MemberExpression' ||
1466
+ getStaticMemberName(node.callee.object) !== 'disposed') {
1467
+ return null;
1468
+ }
1469
+ const [callback] = node.arguments;
1470
+ return callback &&
1471
+ (callback.type === 'ArrowFunctionExpression' ||
1472
+ callback.type === 'FunctionExpression')
1473
+ ? callback
1474
+ : null;
1475
+ }
1476
+ function getDisposeCallTarget(node) {
1477
+ if (node.type === 'CallExpression' &&
1478
+ isStaticMemberCall(node, 'dispose') &&
1479
+ node.callee.type === 'MemberExpression' &&
1480
+ node.callee.object.type !== 'Super') {
1481
+ return node.callee.object;
1482
+ }
1483
+ return null;
1484
+ }
1485
+ function getUnconditionalDisposeTargets(callback) {
1486
+ const body = callback.body;
1487
+ const expressionTarget = getDisposeCallTarget(body);
1488
+ if (expressionTarget) {
1489
+ return [expressionTarget];
1490
+ }
1491
+ if (body.type !== 'BlockStatement') {
1492
+ return [];
1493
+ }
1494
+ return body.body.flatMap(statement => {
1495
+ if (statement.type !== 'ExpressionStatement') {
1496
+ return [];
1497
+ }
1498
+ const target = getDisposeCallTarget(statement.expression);
1499
+ return target ? [target] : [];
1500
+ });
1501
+ }
1502
+ function expressionContainsIdentifierName(node, name) {
1503
+ if (!node) {
1504
+ return false;
1505
+ }
1506
+ if (node.type === 'Identifier' && node.name === name) {
1507
+ return true;
1508
+ }
1509
+ const child = getTransparentChild(node);
1510
+ if (child) {
1511
+ return expressionContainsIdentifierName(child, name);
1512
+ }
1513
+ if (node.type === 'ArrayExpression') {
1514
+ return node.elements.some(element => element && element.type !== 'SpreadElement'
1515
+ ? expressionContainsIdentifierName(element, name)
1516
+ : false);
1517
+ }
1518
+ if (node.type === 'ObjectExpression') {
1519
+ return node.properties.some(property => property.type === 'Property'
1520
+ ? expressionContainsIdentifierName(property.value, name)
1521
+ : false);
1522
+ }
1523
+ return false;
1524
+ }
1525
+ function isOwnershipCallWithIdentifier(node, name, ownership) {
1526
+ return (node.type === 'CallExpression' &&
1527
+ getOwnershipArguments(node, ownership).some(argument => expressionContainsIdentifierName(argument, name)));
1528
+ }
1529
+ function forEachCallbackOwnsParameter(callback, parameterName, ownership) {
1530
+ if (isOwnershipCallWithIdentifier(callback.body, parameterName, ownership)) {
1531
+ return true;
1532
+ }
1533
+ if (callback.body.type !== 'BlockStatement') {
1534
+ return false;
1535
+ }
1536
+ return callback.body.body.some(statement => statement.type === 'ExpressionStatement' &&
1537
+ isOwnershipCallWithIdentifier(statement.expression, parameterName, ownership));
1538
+ }
1539
+ function markImmediateForEachOwnership(pending, node, ownership) {
1540
+ if (!isStaticMemberCall(node, 'forEach') ||
1541
+ node.callee.type !== 'MemberExpression' ||
1542
+ node.callee.object.type !== 'ArrayExpression') {
1543
+ return;
1544
+ }
1545
+ const [callback] = node.arguments;
1546
+ if (!callback ||
1547
+ (callback.type !== 'ArrowFunctionExpression' &&
1548
+ callback.type !== 'FunctionExpression')) {
1549
+ return;
1550
+ }
1551
+ const [parameter] = callback.params;
1552
+ if (parameter?.type !== 'Identifier' ||
1553
+ !forEachCallbackOwnsParameter(callback, parameter.name, ownership)) {
1554
+ return;
1555
+ }
1556
+ for (const element of node.callee.object.elements) {
1557
+ if (element && element.type !== 'SpreadElement') {
1558
+ markManagedVariablesWithoutScopeCheck(pending, element, ownership, node);
1559
+ }
1560
+ }
1561
+ }
1562
+ function markDisposedSignalCallbackDisposals(pending, node, ownership) {
1563
+ const callback = getDisposedSignalCallback(node);
1564
+ if (!callback) {
1565
+ return;
1566
+ }
1567
+ for (const target of getUnconditionalDisposeTargets(callback)) {
1568
+ markManagedVariablesWithoutScopeCheck(pending, target, ownership, node);
1569
+ }
1570
+ }
1571
+ function markManagedDisposableUse(pending, node, ownership) {
1572
+ if (node.type === 'ReturnStatement') {
1573
+ if (isInJupyterPluginActivate(node, ownership)) {
1574
+ markManagedVariablesWithoutScopeCheck(pending, node.argument, ownership, node);
1575
+ return;
1576
+ }
1577
+ const variables = getIdentifierVariables(ownership.sourceCode, node.argument);
1578
+ for (const variable of variables) {
1579
+ if (isManagedUse(node, variable, ownership.sourceCode)) {
1580
+ markDisposableManaged(pending, variable, node);
1581
+ }
1582
+ }
1583
+ return;
1584
+ }
1585
+ if (node.type === 'AssignmentExpression') {
1586
+ if (node.left.type === 'MemberExpression') {
1587
+ const variables = getIdentifierVariables(ownership.sourceCode, node.right);
1588
+ for (const variable of variables) {
1589
+ if (isManagedUse(node, variable, ownership.sourceCode)) {
1590
+ markDisposableManaged(pending, variable, node);
1591
+ }
1592
+ }
1593
+ }
1594
+ return;
1595
+ }
1596
+ if (node.type !== 'CallExpression' && node.type !== 'NewExpression') {
1597
+ return;
1598
+ }
1599
+ if (node.type === 'CallExpression') {
1600
+ markDisposedSignalCallbackDisposals(pending, node, ownership);
1601
+ markImmediateForEachOwnership(pending, node, ownership);
1602
+ }
1603
+ if (pending.size > 0) {
1604
+ markManagedKnownOptionVariables(pending, node, ownership);
1605
+ }
1606
+ for (const argument of node.arguments) {
1607
+ if (argument.type !== 'ObjectExpression') {
1608
+ continue;
1609
+ }
1610
+ for (const property of argument.properties) {
1611
+ if (property.type === 'Property' &&
1612
+ isOptionsObjectValueManaged(property.value, ownership)) {
1613
+ markManagedVariables(pending, property.value, ownership);
1614
+ }
1615
+ }
1616
+ }
1617
+ const ownershipArguments = pending.size > 0
1618
+ ? getOwnershipArguments(node, ownership)
1619
+ : [];
1620
+ if (ownershipArguments.length > 0) {
1621
+ for (const argument of ownershipArguments) {
1622
+ markManagedVariables(pending, argument, ownership);
1623
+ }
1624
+ return;
1625
+ }
1626
+ if (node.type === 'CallExpression' &&
1627
+ isStaticMemberCall(node, 'dispose') &&
1628
+ node.callee.type === 'MemberExpression' &&
1629
+ node.callee.object.type !== 'Super') {
1630
+ const variable = getIdentifierVariable(ownership.sourceCode, node.callee.object);
1631
+ if (variable) {
1632
+ if (isManagedUse(node, variable, ownership.sourceCode) ||
1633
+ (hasPendingDisposableSet(pending, variable) &&
1634
+ !isCatchClauseUse(node, variable))) {
1635
+ markDisposableManaged(pending, variable, node);
1636
+ }
1637
+ }
1638
+ }
1639
+ if (node.type === 'CallExpression' && isDialogLaunchCall(node, ownership)) {
1640
+ const variable = getIdentifierVariable(ownership.sourceCode, node.callee.object);
1641
+ if (variable && isManagedUse(node, variable, ownership.sourceCode)) {
1642
+ markDisposableManaged(pending, variable, node);
1643
+ }
1644
+ }
1645
+ }
1646
+ function hasDisposableTypeName(type, checker) {
1647
+ return (hasTypeName(type, checker, DISPOSABLE_INTERFACE_NAMES) ||
1648
+ hasTypeName(type, checker, DISPOSABLE_CONSTRUCTOR_NAMES));
1649
+ }
1650
+ function declarationHasDisposableHeritage(declaration) {
1651
+ return declarationHasHeritageName(declaration, [
1652
+ ...DISPOSABLE_INTERFACE_NAMES,
1653
+ ...DISPOSABLE_CONSTRUCTOR_NAMES
1654
+ ]);
1655
+ }
1656
+ function hasDisposableHeritage(type) {
1657
+ const symbol = type.getSymbol();
1658
+ return symbol?.declarations?.some(declarationHasDisposableHeritage) ?? false;
1659
+ }
1660
+ function hasDisposableShape(type, checker) {
1661
+ const dispose = type.getProperty('dispose');
1662
+ const isDisposed = type.getProperty('isDisposed');
1663
+ if (!dispose || !isDisposed) {
1664
+ return false;
1665
+ }
1666
+ const declaration = dispose.valueDeclaration ?? dispose.declarations?.[0];
1667
+ if (!declaration) {
1668
+ return false;
1669
+ }
1670
+ const disposeType = checker.getTypeOfSymbolAtLocation(dispose, declaration);
1671
+ return disposeType.getCallSignatures().length > 0;
1672
+ }
1673
+ function isDisposableType(type, checker, seen = new Set()) {
1674
+ if (seen.has(type)) {
1675
+ return false;
1676
+ }
1677
+ seen.add(type);
1678
+ if (type.isUnion()) {
1679
+ return type.types.some(part => isDisposableType(part, checker, seen));
1680
+ }
1681
+ if (type.isIntersection()) {
1682
+ return type.types.some(part => isDisposableType(part, checker, seen));
1683
+ }
1684
+ const apparentType = checker.getApparentType(type);
1685
+ return (hasDisposableTypeName(type, checker) ||
1686
+ hasDisposableHeritage(type) ||
1687
+ hasDisposableShape(apparentType, checker));
1688
+ }
1689
+ function shouldCheckReturnedDisposable(node) {
1690
+ const expression = getOuterFallbackExpression(node);
1691
+ const parent = expression.parent;
1692
+ if (!parent) {
1693
+ return false;
1694
+ }
1695
+ if (parent.type === 'ExpressionStatement' ||
1696
+ parent.type === 'ReturnStatement') {
1697
+ return true;
1698
+ }
1699
+ if (parent.type === 'VariableDeclarator' && parent.init === expression) {
1700
+ return true;
1701
+ }
1702
+ if (parent.type === 'AssignmentExpression' && parent.right === expression) {
1703
+ return true;
1704
+ }
1705
+ if (parent.type === 'PropertyDefinition' && parent.value === expression) {
1706
+ return true;
1707
+ }
1708
+ if (parent.type === 'UnaryExpression' &&
1709
+ parent.operator === 'void' &&
1710
+ parent.argument === expression) {
1711
+ return true;
1712
+ }
1713
+ if (parent.type === 'CallExpression' || parent.type === 'NewExpression') {
1714
+ return parent.arguments.includes(expression);
1715
+ }
1716
+ if (parent.type === 'ArrayExpression' &&
1717
+ (parent.parent?.type === 'CallExpression' ||
1718
+ parent.parent?.type === 'NewExpression') &&
1719
+ parent.parent.arguments.includes(parent)) {
1720
+ return true;
1721
+ }
1722
+ return parent.type === 'MemberExpression' && parent.object === expression;
1723
+ }