@jupyter/eslint-plugin 1.2.0 → 1.4.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 (48) hide show
  1. package/lib/index.d.ts +27 -1
  2. package/lib/index.js +21 -3
  3. package/lib/rules/command-id-convention.d.ts +5 -0
  4. package/lib/rules/command-id-convention.js +87 -0
  5. package/lib/rules/galata-prefer-context-menu-helper.d.ts +6 -0
  6. package/lib/rules/galata-prefer-context-menu-helper.js +394 -0
  7. package/lib/rules/galata-prefer-menu-helper.js +18 -102
  8. package/lib/rules/no-untranslated-string.d.ts +2 -1
  9. package/lib/rules/no-untranslated-string.js +191 -113
  10. package/lib/rules/plugin-activation-args.d.ts +3 -3
  11. package/lib/rules/plugin-activation-args.js +173 -142
  12. package/lib/rules/plugin-description.js +12 -9
  13. package/lib/rules/plugin-id-convention.d.ts +6 -0
  14. package/lib/rules/plugin-id-convention.js +199 -0
  15. package/lib/rules/prefer-lazy-imports.d.ts +2 -2
  16. package/lib/rules/prefer-lazy-imports.js +257 -58
  17. package/lib/rules/prefer-signal-this-arg.d.ts +2 -3
  18. package/lib/rules/prefer-signal-this-arg.js +2 -10
  19. package/lib/rules/require-disposable-ownership.d.ts +1 -2
  20. package/lib/rules/require-disposable-ownership.js +3 -26
  21. package/lib/rules/require-disposable-transfer.d.ts +1 -2
  22. package/lib/rules/require-disposable-transfer.js +14 -14
  23. package/lib/rules/require-signal-cleanup.d.ts +2 -3
  24. package/lib/rules/require-signal-cleanup.js +2 -10
  25. package/lib/rules/require-signal-this-arg.d.ts +1 -2
  26. package/lib/rules/require-signal-this-arg.js +2 -10
  27. package/lib/utils/ast.d.ts +12 -0
  28. package/lib/utils/ast.js +49 -0
  29. package/lib/utils/commands.d.ts +17 -0
  30. package/lib/utils/commands.js +205 -0
  31. package/lib/utils/disposables.d.ts +12 -0
  32. package/lib/utils/disposables.js +101 -10
  33. package/lib/utils/lazy-imports.d.ts +27 -3
  34. package/lib/utils/lazy-imports.js +130 -36
  35. package/lib/utils/module-size.d.ts +7 -1
  36. package/lib/utils/module-size.js +10 -1
  37. package/lib/utils/package-json.d.ts +8 -0
  38. package/lib/utils/package-json.js +76 -0
  39. package/lib/utils/playwright-selectors.d.ts +15 -0
  40. package/lib/utils/playwright-selectors.js +80 -0
  41. package/lib/utils/plugin-utils.d.ts +66 -6
  42. package/lib/utils/plugin-utils.js +485 -59
  43. package/lib/utils/shared-packages.js +6 -16
  44. package/lib/utils/signals.d.ts +2 -8
  45. package/lib/utils/signals.js +7 -42
  46. package/lib/utils/type-services.d.ts +13 -0
  47. package/lib/utils/type-services.js +27 -0
  48. package/package.json +1 -1
@@ -3,7 +3,7 @@
3
3
  * Copyright (c) Jupyter Development Team.
4
4
  * Distributed under the terms of the Modified BSD License.
5
5
  */
6
- const utils_1 = require("@typescript-eslint/utils");
6
+ const type_services_1 = require("../utils/type-services");
7
7
  const plugin_utils_1 = require("../utils/plugin-utils");
8
8
  const lazy_imports_1 = require("../utils/lazy-imports");
9
9
  const module_size_1 = require("../utils/module-size");
@@ -11,10 +11,12 @@ const shared_packages_1 = require("../utils/shared-packages");
11
11
  const create_rule_1 = require("../utils/create-rule");
12
12
  const DEFAULT_OPTIONS = {
13
13
  allowedPackages: lazy_imports_1.DEFAULT_ALLOWED_PACKAGES,
14
+ deferredPackages: lazy_imports_1.DEFAULT_DEFERRED_PACKAGES,
14
15
  ignoreImports: [],
15
16
  minimumSize: lazy_imports_1.DEFAULT_MINIMUM_SIZE,
16
17
  reportInteractionCallbacks: false,
17
- reportModuleLevelUsage: false
18
+ reportModuleLevelUsage: false,
19
+ reportReExports: false
18
20
  };
19
21
  const jupyterPreferLazyImports = (0, create_rule_1.createRule)({
20
22
  name: 'prefer-lazy-imports',
@@ -29,8 +31,26 @@ const jupyterPreferLazyImports = (0, create_rule_1.createRule)({
29
31
  'Import it where it is used instead: `{{ snippet }}`',
30
32
  eagerModuleLevelUse: "'{{ source }}' is used at module level in a plugin module, so it loads before the application starts. " +
31
33
  "Move the usage into a function and import it there with `await import('{{ source }}')`.",
34
+ usedInAutostartActivate: "'{{ source }}' is imported at the top of a plugin module and used in `activate()` of an autostart plugin, so it loads before the application starts either way. " +
35
+ 'Do not `await import(...)` inside `activate()`: that delays the whole application start. ' +
36
+ "Register the extension point synchronously and load '{{ source }}' in the callback that first needs it, or ignore this import if activation needs it at once.",
32
37
  preferLazyImportInteraction: "'{{ source }}' is only used inside user-interaction handlers, so it is not needed until the user acts. " +
33
- 'Import it where it is used instead: `{{ snippet }}`'
38
+ 'Import it where it is used instead: `{{ snippet }}`',
39
+ deferredPackageImport: "'{{ source }}' is in `deferredPackages`, so it must only be loaded with `await import()`, but this static import loads it together with the module. " +
40
+ 'Import it where it is used instead: `{{ snippet }}`',
41
+ deferredPackageEagerUse: "'{{ source }}' is in `deferredPackages`, so it must only be loaded with `await import()`, but it is used while this module is evaluated. " +
42
+ "Move the usage into a function and import it there with `await import('{{ source }}')`. " +
43
+ 'If this module is itself only loaded with `import()`, disable the rule for this import.',
44
+ deferredPackageAutostartUse: "'{{ source }}' is imported at the top of a plugin module and used in `activate()` of an autostart plugin, so it loads before the application starts either way. " +
45
+ 'It is also in `deferredPackages`, so it must only be loaded with `await import()`. ' +
46
+ 'Do not `await import(...)` inside `activate()`: that delays the whole application start. ' +
47
+ "Register the extension point synchronously and load '{{ source }}' in the callback that first needs it, or ignore this import if activation needs it at once.",
48
+ deferredPackageNotTypeOnly: "'{{ source }}' is in `deferredPackages`, and this import has no runtime use, but it is not written as `import type`, so a build with `verbatimModuleSyntax` or plain JavaScript loads the package anyway. " +
49
+ 'Remove the import, or make it `import type`.',
50
+ deferredPackageReExport: "'{{ source }}' is in `deferredPackages`, so it must only be loaded with `await import()`, but this re-export loads it together with the module. " +
51
+ "Remove the re-export and use `await import('{{ source }}')` where the package is needed.",
52
+ reExportKeepsImportEager: "'{{ source }}' is imported only for use inside functions, but this re-export keeps it in the startup bundle. " +
53
+ "Export types with `export type` instead, or remove the re-export and import '{{ source }}' where it is used."
34
54
  },
35
55
  schema: [
36
56
  {
@@ -42,6 +62,12 @@ const jupyterPreferLazyImports = (0, create_rule_1.createRule)({
42
62
  default: lazy_imports_1.DEFAULT_ALLOWED_PACKAGES,
43
63
  description: 'Packages already loaded eagerly by the application, which are therefore free to import at the top of a plugin module. Supports `*` wildcards, and `!` to deny a package whatever else in the list matches it. Replaces the default list.'
44
64
  },
65
+ deferredPackages: {
66
+ type: 'array',
67
+ items: { type: 'string' },
68
+ default: lazy_imports_1.DEFAULT_DEFERRED_PACKAGES,
69
+ description: 'Packages which must only be loaded with `await import()`. A static import of one is reported in every module, however its bindings are used. Matched like `allowedPackages`, takes precedence over it and over the manifest, and replaces the default list.'
70
+ },
45
71
  ignoreImports: {
46
72
  type: 'array',
47
73
  items: { type: 'string' },
@@ -63,6 +89,11 @@ const jupyterPreferLazyImports = (0, create_rule_1.createRule)({
63
89
  type: 'boolean',
64
90
  default: false,
65
91
  description: 'Also report imports used at module level, excluding tokens referenced in `requires`, `optional` and `provides`.'
92
+ },
93
+ reportReExports: {
94
+ type: 'boolean',
95
+ default: false,
96
+ description: 'Also report value re-exports of modules whose imports are only used inside functions, which would otherwise keep the module in the startup bundle.'
66
97
  }
67
98
  },
68
99
  additionalProperties: false
@@ -71,42 +102,48 @@ const jupyterPreferLazyImports = (0, create_rule_1.createRule)({
71
102
  },
72
103
  defaultOptions: [DEFAULT_OPTIONS],
73
104
  create(context, [options]) {
74
- const { allowedPackages, ignoreImports, minimumSize, reportInteractionCallbacks, reportModuleLevelUsage } = options;
75
- let services = null;
76
- let checker = null;
77
- try {
78
- services = utils_1.ESLintUtils.getParserServices(context, true);
79
- checker = services.program ? services.program.getTypeChecker() : null;
80
- }
81
- catch {
82
- // Parser services unavailable (non-TS file or no type information)
83
- services = null;
84
- }
85
- const getTSNode = services
86
- ? (node) => services?.esTreeNodeToTSNodeMap.get(node)
87
- : null;
105
+ const { allowedPackages, deferredPackages, ignoreImports, minimumSize, reportInteractionCallbacks, reportModuleLevelUsage, reportReExports } = options;
106
+ const { checker, getTSNode } = (0, type_services_1.getTypeServices)(context);
88
107
  let isPluginModule = false;
89
108
  const importDeclarations = [];
90
109
  // Sources kept in the startup bundle by a value re-export.
91
- const reExportedSources = new Set();
110
+ const reExportDeclarations = new Map();
92
111
  function mentionsPluginType(typeNode) {
93
112
  return (0, plugin_utils_1.typeMentionsJupyterPlugin)(typeNode, checker, getTSNode);
94
113
  }
95
114
  // Packages this extension declares as provided by the application, read
96
115
  // from `jupyterlab.sharedPackages` in its own manifest. They extend
97
116
  // `allowedPackages` rather than replacing it, and are only looked up once
98
- // the file turns out to need checking.
117
+ // an import turns out to need them.
99
118
  let hostProvided = null;
100
119
  /**
101
- * Returns true when the specifier is exempt from the rule, either because
102
- * the application loads it eagerly anyway or because it was ignored
103
- * explicitly.
120
+ * Returns true when the specifier is exempt from the usage check, either
121
+ * because the application loads it eagerly anyway or because it was
122
+ * ignored explicitly.
104
123
  */
105
124
  function isExempt(source) {
106
- return ((0, lazy_imports_1.matchesPatterns)(source, lazy_imports_1.ALWAYS_IGNORED_IMPORTS) ||
125
+ if ((0, lazy_imports_1.matchesPatterns)(source, lazy_imports_1.ALWAYS_IGNORED_IMPORTS) ||
107
126
  (0, lazy_imports_1.matchesPatterns)(source, allowedPackages) ||
108
- (0, lazy_imports_1.matchesPatterns)(source, hostProvided ?? []) ||
109
- (0, lazy_imports_1.matchesPatterns)(source, ignoreImports));
127
+ (0, lazy_imports_1.matchesPatterns)(source, ignoreImports)) {
128
+ return true;
129
+ }
130
+ if (hostProvided === null) {
131
+ hostProvided = (0, shared_packages_1.getHostProvidedPackages)(context.filename);
132
+ }
133
+ return (0, lazy_imports_1.matchesPatterns)(source, hostProvided);
134
+ }
135
+ /**
136
+ * Returns true when the package must only be loaded with `import()`. The
137
+ * list wins over `allowedPackages` and the manifest, which describe what
138
+ * the application loads at startup, since this one describes what it
139
+ * keeps out of startup on purpose. `ignoreImports` still wins as the
140
+ * explicit way to skip a specifier, and an asset from such a package is
141
+ * left to the bundler like any other asset.
142
+ */
143
+ function isDeferredPackage(source) {
144
+ return ((0, lazy_imports_1.matchesPatterns)(source, deferredPackages) &&
145
+ !(0, lazy_imports_1.matchesPatterns)(source, lazy_imports_1.ALWAYS_IGNORED_IMPORTS) &&
146
+ !(0, lazy_imports_1.matchesPatterns)(source, ignoreImports));
110
147
  }
111
148
  /**
112
149
  * Returns true when the module holds too little code for a separate chunk
@@ -123,7 +160,7 @@ const jupyterPreferLazyImports = (0, create_rule_1.createRule)({
123
160
  if (!resolved) {
124
161
  return false;
125
162
  }
126
- const size = (0, module_size_1.getTransitiveCodeSize)(resolved);
163
+ const size = (0, module_size_1.getTransitiveCodeSize)(resolved, minimumSize);
127
164
  return size !== null && size < minimumSize;
128
165
  }
129
166
  /**
@@ -172,7 +209,8 @@ const jupyterPreferLazyImports = (0, create_rule_1.createRule)({
172
209
  * deferring the others would not remove it.
173
210
  */
174
211
  function checkSource(source, declarations) {
175
- if (isExempt(source) || reExportedSources.has(source)) {
212
+ const reExports = reExportDeclarations.get(source);
213
+ if (isExempt(source) || (!reportReExports && reExports)) {
176
214
  return;
177
215
  }
178
216
  const references = declarations.flatMap(getValueReferences);
@@ -180,43 +218,93 @@ const jupyterPreferLazyImports = (0, create_rule_1.createRule)({
180
218
  // Unused, or used only in type positions which TypeScript erases.
181
219
  return;
182
220
  }
183
- if (isTooSmall(source)) {
184
- return;
221
+ // Measuring a module compiles it, which costs far more than the checks
222
+ // below, and most candidates are ruled out by them. The measurement
223
+ // therefore happens at the point of reporting rather than here.
224
+ function reportUnlessTooSmall(descriptor) {
225
+ if (isTooSmall(source)) {
226
+ return;
227
+ }
228
+ context.report(descriptor);
185
229
  }
186
230
  if (!isPluginModule) {
187
231
  // Outside a plugin module an ordinary function proves nothing, since
188
232
  // it may run while the application starts. Only a position which
189
233
  // provably waits for the user shows the import can load later.
190
234
  if (references.every(({ identifier }) => (0, lazy_imports_1.isInInteractionCallback)(identifier))) {
191
- context.report({
192
- node: declarations[0],
193
- messageId: 'preferLazyImportInteraction',
194
- data: {
195
- source,
196
- snippet: (0, lazy_imports_1.buildDeferredImportSnippet)(declarations)
235
+ if (reExports && reportReExports) {
236
+ for (const reExportNode of reExports) {
237
+ reportUnlessTooSmall({
238
+ node: reExportNode,
239
+ messageId: 'reExportKeepsImportEager',
240
+ data: { source }
241
+ });
197
242
  }
198
- });
243
+ }
244
+ else {
245
+ reportUnlessTooSmall({
246
+ node: declarations[0],
247
+ messageId: 'preferLazyImportInteraction',
248
+ data: {
249
+ source,
250
+ snippet: (0, lazy_imports_1.buildDeferredImportSnippet)(declarations)
251
+ }
252
+ });
253
+ }
199
254
  }
200
255
  return;
201
256
  }
202
257
  let deferrable = 0;
203
258
  let eager = 0;
259
+ let activation = 0;
204
260
  let tokenList = 0;
205
261
  for (const { identifier } of references) {
206
262
  if ((0, lazy_imports_1.isInPluginTokenList)(identifier)) {
207
263
  // A token in `requires`, `optional` or `provides` is read when the
208
264
  // plugin is registered, so it can never be deferred.
209
265
  tokenList += 1;
266
+ continue;
210
267
  }
211
- else if ((0, lazy_imports_1.isEagerlyReached)(identifier, context.sourceCode)) {
212
- eager += 1;
268
+ switch ((0, lazy_imports_1.getReach)(identifier, context.sourceCode)) {
269
+ case 'module':
270
+ eager += 1;
271
+ break;
272
+ case 'activation':
273
+ activation += 1;
274
+ break;
275
+ default:
276
+ deferrable += 1;
213
277
  }
214
- else {
215
- deferrable += 1;
278
+ }
279
+ if (tokenList === 0 && eager === 0 && activation > 0) {
280
+ if (reExports) {
281
+ return;
216
282
  }
283
+ // `Application.start` waits for every autostart plugin before it
284
+ // attaches the shell, so the module is fetched before the application
285
+ // starts whatever this file does, and an `await import()` inside
286
+ // `activate` would hold the start back by one more request. The usual
287
+ // snippet is therefore the wrong advice here, even when other uses
288
+ // sit in callbacks which could defer it.
289
+ reportUnlessTooSmall({
290
+ node: declarations[0],
291
+ messageId: 'usedInAutostartActivate',
292
+ data: { source }
293
+ });
294
+ return;
217
295
  }
218
296
  if (tokenList === 0 && eager === 0 && deferrable > 0) {
219
- context.report({
297
+ if (reExports && reportReExports) {
298
+ for (const reExportNode of reExports) {
299
+ reportUnlessTooSmall({
300
+ node: reExportNode,
301
+ messageId: 'reExportKeepsImportEager',
302
+ data: { source }
303
+ });
304
+ }
305
+ return;
306
+ }
307
+ reportUnlessTooSmall({
220
308
  node: declarations[0],
221
309
  messageId: 'preferLazyImport',
222
310
  data: {
@@ -227,13 +315,124 @@ const jupyterPreferLazyImports = (0, create_rule_1.createRule)({
227
315
  return;
228
316
  }
229
317
  if (reportModuleLevelUsage && eager > 0 && tokenList === 0) {
230
- context.report({
318
+ if (reExports) {
319
+ return;
320
+ }
321
+ reportUnlessTooSmall({
231
322
  node: declarations[0],
232
323
  messageId: 'eagerModuleLevelUse',
233
324
  data: { source }
234
325
  });
235
326
  }
236
327
  }
328
+ /**
329
+ * Reports a static import of a package which must only be loaded with
330
+ * `import()`. Where the bindings are used makes no difference here: a
331
+ * binding used only inside a function still loads the package together
332
+ * with this module, and whether this module is part of the startup bundle
333
+ * cannot be seen from this file. So the import is reported wherever it
334
+ * appears, and the usage only picks the advice.
335
+ *
336
+ * Only `import type` is exempt, and that never gets here. A value import
337
+ * whose bindings have no runtime use, an inline `type` specifier included,
338
+ * is erased by TypeScript without `verbatimModuleSyntax` but kept with it
339
+ * (`import { type X }` becomes `import {} from '...'`), and JavaScript
340
+ * never erases anything, so it counts as loading the package as well.
341
+ */
342
+ function checkDeferredPackage(source, declarations) {
343
+ // The snippet is built from the declarations with a runtime use, plus a
344
+ // side-effect import, which has nothing to erase. Each of the others is
345
+ // reported on its own line, because deleting the reported declarations
346
+ // would leave it behind, still loading the package under
347
+ // `verbatimModuleSyntax` or in JavaScript.
348
+ const loading = [];
349
+ const references = [];
350
+ for (const declaration of declarations) {
351
+ const own = getValueReferences(declaration);
352
+ if (own.length > 0 || declaration.specifiers.length === 0) {
353
+ loading.push(declaration);
354
+ references.push(...own);
355
+ }
356
+ else {
357
+ context.report({
358
+ node: declaration,
359
+ messageId: 'deferredPackageNotTypeOnly',
360
+ data: { source }
361
+ });
362
+ }
363
+ }
364
+ if (loading.length === 0) {
365
+ return;
366
+ }
367
+ let eager = false;
368
+ let activation = false;
369
+ for (const { identifier } of references) {
370
+ if ((0, lazy_imports_1.isInPluginTokenList)(identifier)) {
371
+ eager = true;
372
+ break;
373
+ }
374
+ const reach = (0, lazy_imports_1.getReach)(identifier, context.sourceCode);
375
+ if (reach === 'module') {
376
+ eager = true;
377
+ break;
378
+ }
379
+ if (reach === 'activation') {
380
+ activation = true;
381
+ }
382
+ }
383
+ if (eager) {
384
+ context.report({
385
+ node: loading[0],
386
+ messageId: 'deferredPackageEagerUse',
387
+ data: { source }
388
+ });
389
+ return;
390
+ }
391
+ if (activation) {
392
+ // The snippet would put the `await import()` into `activate`, which
393
+ // holds the whole start back, so the advice is the one for any import
394
+ // used during the activation of an autostart plugin, with the list
395
+ // named as well.
396
+ context.report({
397
+ node: loading[0],
398
+ messageId: 'deferredPackageAutostartUse',
399
+ data: { source }
400
+ });
401
+ return;
402
+ }
403
+ context.report({
404
+ node: loading[0],
405
+ messageId: 'deferredPackageImport',
406
+ data: { source, snippet: (0, lazy_imports_1.buildDeferredImportSnippet)(loading) }
407
+ });
408
+ }
409
+ /**
410
+ * A re-export of a package which must only be loaded with `import()` is a
411
+ * finding in itself, `export { type X } from` included: only `export type`
412
+ * is erased under `verbatimModuleSyntax`, the rest stays as
413
+ * `export {} from '...'` and loads the source. For any other source a
414
+ * value re-export keeps it in the startup bundle, so deferring the
415
+ * matching import would gain nothing and the usage check skips it, unless
416
+ * `reportReExports` is enabled to report the re-export instead.
417
+ */
418
+ function checkReExport(node, source, hasValueSpecifier) {
419
+ if (isDeferredPackage(source)) {
420
+ context.report({
421
+ node,
422
+ messageId: 'deferredPackageReExport',
423
+ data: { source }
424
+ });
425
+ }
426
+ else if (hasValueSpecifier) {
427
+ const list = reExportDeclarations.get(source);
428
+ if (list) {
429
+ list.push(node);
430
+ }
431
+ else {
432
+ reExportDeclarations.set(source, [node]);
433
+ }
434
+ }
435
+ }
237
436
  return {
238
437
  VariableDeclarator(node) {
239
438
  if (!isPluginModule &&
@@ -259,34 +458,22 @@ const jupyterPreferLazyImports = (0, create_rule_1.createRule)({
259
458
  }
260
459
  },
261
460
  ImportDeclaration(node) {
262
- if (node.parent.type === 'Program' &&
263
- node.importKind !== 'type' &&
264
- // A side-effect import has no binding to move into a function.
265
- node.specifiers.length > 0) {
461
+ if (node.parent.type === 'Program' && node.importKind !== 'type') {
266
462
  importDeclarations.push(node);
267
463
  }
268
464
  },
269
465
  ExportNamedDeclaration(node) {
270
- // A value re-export keeps the source in the startup bundle, so nothing
271
- // is gained by deferring the import of it.
272
466
  if (!node.source || node.exportKind === 'type') {
273
467
  return;
274
468
  }
275
- const hasValueSpecifier = node.specifiers.some(specifier => specifier.exportKind !== 'type');
276
- if (hasValueSpecifier) {
277
- reExportedSources.add(node.source.value);
278
- }
469
+ checkReExport(node, node.source.value, node.specifiers.some(specifier => specifier.exportKind !== 'type'));
279
470
  },
280
471
  ExportAllDeclaration(node) {
281
472
  if (node.exportKind !== 'type') {
282
- reExportedSources.add(node.source.value);
473
+ checkReExport(node, node.source.value, true);
283
474
  }
284
475
  },
285
476
  'Program:exit'() {
286
- if (!isPluginModule && !reportInteractionCallbacks) {
287
- return;
288
- }
289
- hostProvided = (0, shared_packages_1.getHostProvidedPackages)(context.filename);
290
477
  const bySource = new Map();
291
478
  for (const declaration of importDeclarations) {
292
479
  const source = declaration.source.value;
@@ -298,8 +485,20 @@ const jupyterPreferLazyImports = (0, create_rule_1.createRule)({
298
485
  bySource.set(source, [declaration]);
299
486
  }
300
487
  }
488
+ // A package which must stay deferred is checked in every module. The
489
+ // usage check needs a plugin module, or the interaction option.
490
+ const checkUsage = isPluginModule || reportInteractionCallbacks;
301
491
  for (const [source, declarations] of bySource) {
302
- checkSource(source, declarations);
492
+ if (isDeferredPackage(source)) {
493
+ checkDeferredPackage(source, declarations);
494
+ }
495
+ else if (checkUsage) {
496
+ // A side-effect import has no binding to move into a function.
497
+ const withBindings = declarations.filter(declaration => declaration.specifiers.length > 0);
498
+ if (withBindings.length > 0) {
499
+ checkSource(source, withBindings);
500
+ }
501
+ }
303
502
  }
304
503
  }
305
504
  };
@@ -1,7 +1,6 @@
1
- import { ESLintUtils } from '@typescript-eslint/utils';
2
- declare const preferSignalThisArg: ESLintUtils.RuleModule<"addThisArg" | "preferThisArg" | "boundCallback" | "unbindAndAddThisArg", [{
1
+ declare const preferSignalThisArg: import("@typescript-eslint/utils/ts-eslint").RuleModule<"addThisArg" | "preferThisArg" | "boundCallback" | "unbindAndAddThisArg", [{
3
2
  longLivedTypes: string[] | undefined;
4
- }], unknown, ESLintUtils.RuleListener> & {
3
+ }], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
5
4
  name: string;
6
5
  };
7
6
  export = preferSignalThisArg;
@@ -3,8 +3,8 @@
3
3
  * Copyright (c) Jupyter Development Team.
4
4
  * Distributed under the terms of the Modified BSD License.
5
5
  */
6
- const utils_1 = require("@typescript-eslint/utils");
7
6
  const create_rule_1 = require("../utils/create-rule");
7
+ const type_services_1 = require("../utils/type-services");
8
8
  const signals_1 = require("../utils/signals");
9
9
  const signal_lifetime_1 = require("../utils/signal-lifetime");
10
10
  const preferSignalThisArg = (0, create_rule_1.createRule)({
@@ -45,15 +45,7 @@ const preferSignalThisArg = (0, create_rule_1.createRule)({
45
45
  ],
46
46
  create(context, [options]) {
47
47
  const longLivedTypes = new Set(options.longLivedTypes ?? signal_lifetime_1.DEFAULT_LONG_LIVED_TYPES);
48
- let services = null;
49
- let checker = null;
50
- try {
51
- services = utils_1.ESLintUtils.getParserServices(context, true);
52
- checker = services.program ? services.program.getTypeChecker() : null;
53
- }
54
- catch {
55
- services = null;
56
- }
48
+ const { services, checker } = (0, type_services_1.getTypeServices)(context);
57
49
  const sourceCode = context.sourceCode;
58
50
  let program = null;
59
51
  let signalLocalNames = new Set(['Signal']);
@@ -1,5 +1,4 @@
1
- import { ESLintUtils } from '@typescript-eslint/utils';
2
- declare const requireDisposableOwnership: ESLintUtils.RuleModule<"unmanagedDisposable" | "unmanagedDisposableVariable", [{}], unknown, ESLintUtils.RuleListener> & {
1
+ declare const requireDisposableOwnership: import("@typescript-eslint/utils/ts-eslint").RuleModule<"unmanagedDisposable" | "unmanagedDisposableVariable", [{}], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
3
2
  name: string;
4
3
  };
5
4
  export = requireDisposableOwnership;
@@ -3,8 +3,8 @@
3
3
  * Copyright (c) Jupyter Development Team.
4
4
  * Distributed under the terms of the Modified BSD License.
5
5
  */
6
- const utils_1 = require("@typescript-eslint/utils");
7
6
  const create_rule_1 = require("../utils/create-rule");
7
+ const type_services_1 = require("../utils/type-services");
8
8
  const disposables_1 = require("../utils/disposables");
9
9
  const requireDisposableOwnership = (0, create_rule_1.createRule)({
10
10
  name: 'require-disposable-ownership',
@@ -40,30 +40,7 @@ const requireDisposableOwnership = (0, create_rule_1.createRule)({
40
40
  defaultOptions: [{}],
41
41
  create(context, [options]) {
42
42
  const pending = new Map();
43
- let services = null;
44
- let checker = null;
45
- try {
46
- services = utils_1.ESLintUtils.getParserServices(context, true);
47
- checker = services.program ? services.program.getTypeChecker() : null;
48
- }
49
- catch {
50
- services = null;
51
- }
52
- function isDisposableCreation(node) {
53
- if (checker && services) {
54
- try {
55
- const tsNode = services.esTreeNodeToTSNodeMap.get(node);
56
- const type = checker.getTypeAtLocation(tsNode);
57
- if ((0, disposables_1.isDisposableType)(type, checker)) {
58
- return true;
59
- }
60
- }
61
- catch {
62
- // Fall back to the known Lumino disposable constructors below.
63
- }
64
- }
65
- return (0, disposables_1.isDisposableConstructor)(node);
66
- }
43
+ const { services, checker } = (0, type_services_1.getTypeServices)(context);
67
44
  const ownership = {
68
45
  sourceCode: context.sourceCode,
69
46
  checker,
@@ -85,7 +62,7 @@ const requireDisposableOwnership = (0, create_rule_1.createRule)({
85
62
  if ((0, disposables_1.isInJupyterPluginActivate)(node, ownership)) {
86
63
  return;
87
64
  }
88
- if (!isDisposableCreation(node)) {
65
+ if (!(0, disposables_1.constructsDisposable)(node, ownership)) {
89
66
  return;
90
67
  }
91
68
  const assignedVariable = (0, disposables_1.getAssignedVariable)(node, context.sourceCode);
@@ -1,5 +1,4 @@
1
- import { ESLintUtils } from '@typescript-eslint/utils';
2
- declare const requireDisposableTransfer: ESLintUtils.RuleModule<"unmanagedDisposableVariable" | "unhandledDisposable", [{}], unknown, ESLintUtils.RuleListener> & {
1
+ declare const requireDisposableTransfer: import("@typescript-eslint/utils/ts-eslint").RuleModule<"unmanagedDisposableVariable" | "unhandledDisposable", [{}], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
3
2
  name: string;
4
3
  };
5
4
  export = requireDisposableTransfer;
@@ -3,8 +3,8 @@
3
3
  * Copyright (c) Jupyter Development Team.
4
4
  * Distributed under the terms of the Modified BSD License.
5
5
  */
6
- const utils_1 = require("@typescript-eslint/utils");
7
6
  const create_rule_1 = require("../utils/create-rule");
7
+ const type_services_1 = require("../utils/type-services");
8
8
  const disposables_1 = require("../utils/disposables");
9
9
  const DEFAULT_IGNORED_RETURN_FUNCTION_NAMES = [
10
10
  '_adjacentBar',
@@ -114,24 +114,19 @@ const requireDisposableTransfer = (0, create_rule_1.createRule)({
114
114
  defaultOptions: [{}],
115
115
  create(context, [options]) {
116
116
  const pending = new Map();
117
- let services = null;
118
- let checker = null;
119
- try {
120
- services = utils_1.ESLintUtils.getParserServices(context, true);
121
- checker = services.program ? services.program.getTypeChecker() : null;
122
- }
123
- catch {
124
- services = null;
125
- }
117
+ const { services, checker, getTSNode } = (0, type_services_1.getTypeServices)(context);
126
118
  function returnsDisposable(node) {
127
119
  if ((0, disposables_1.isDisposableSetFactoryCall)(node)) {
128
120
  return true;
129
121
  }
130
- if (!checker || !services) {
122
+ if (!checker || !getTSNode) {
131
123
  return false;
132
124
  }
133
125
  try {
134
- const tsNode = services.esTreeNodeToTSNodeMap.get(node);
126
+ const tsNode = getTSNode(node);
127
+ if (!tsNode) {
128
+ return false;
129
+ }
135
130
  const type = checker.getTypeAtLocation(tsNode);
136
131
  return (0, disposables_1.isDisposableType)(type, checker);
137
132
  }
@@ -170,14 +165,19 @@ const requireDisposableTransfer = (0, create_rule_1.createRule)({
170
165
  (0, disposables_1.markManagedDisposableUse)(pending, node, ownership);
171
166
  if ((0, disposables_1.isInJupyterPluginActivate)(node, ownership) ||
172
167
  isIgnoredReturn(node) ||
173
- !(0, disposables_1.shouldCheckReturnedDisposable)(node) ||
174
- (0, disposables_1.isDisposableExpressionManaged)(node, ownership)) {
168
+ !(0, disposables_1.shouldCheckReturnedDisposable)(node)) {
175
169
  return;
176
170
  }
171
+ // The name test reads the call itself, while the two tests after it
172
+ // ask the type checker about the callee, so a call this rule would
173
+ // never report is dropped before any of that work happens.
177
174
  if (!checkAllDisposableReturns &&
178
175
  !isLikelyDisposableFactoryCall(node)) {
179
176
  return;
180
177
  }
178
+ if ((0, disposables_1.isDisposableExpressionManaged)(node, ownership)) {
179
+ return;
180
+ }
181
181
  if (!returnsDisposable(node)) {
182
182
  return;
183
183
  }
@@ -1,8 +1,7 @@
1
- import { ESLintUtils } from '@typescript-eslint/utils';
2
- declare const requireSignalCleanup: ESLintUtils.RuleModule<"serviceOutlivesReceiver", [{
1
+ declare const requireSignalCleanup: import("@typescript-eslint/utils/ts-eslint").RuleModule<"serviceOutlivesReceiver", [{
3
2
  additionalCleanupMethods: string[];
4
3
  longLivedTypes: string[] | undefined;
5
- }], unknown, ESLintUtils.RuleListener> & {
4
+ }], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
6
5
  name: string;
7
6
  };
8
7
  export = requireSignalCleanup;