@jupyter/eslint-plugin 1.1.0 → 1.3.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.
- package/lib/index.d.ts +49 -2
- package/lib/index.js +35 -5
- package/lib/rules/galata-prefer-context-menu-helper.d.ts +6 -0
- package/lib/rules/galata-prefer-context-menu-helper.js +394 -0
- package/lib/rules/galata-prefer-menu-helper.d.ts +6 -0
- package/lib/rules/galata-prefer-menu-helper.js +394 -0
- package/lib/rules/galata-prefer-notebook-cell-helper.d.ts +6 -0
- package/lib/rules/galata-prefer-notebook-cell-helper.js +403 -0
- package/lib/rules/galata-prefer-sidebar-activity-helper.d.ts +6 -0
- package/lib/rules/galata-prefer-sidebar-activity-helper.js +332 -0
- package/lib/rules/no-schema-enum.d.ts +1 -1
- package/lib/rules/no-schema-enum.js +54 -2
- package/lib/rules/no-untranslated-string.d.ts +2 -1
- package/lib/rules/no-untranslated-string.js +191 -113
- package/lib/rules/plugin-id-convention.d.ts +7 -0
- package/lib/rules/plugin-id-convention.js +296 -0
- package/lib/rules/prefer-lazy-imports.d.ts +6 -0
- package/lib/rules/prefer-lazy-imports.js +476 -0
- package/lib/rules/require-disposable-ownership.js +1 -16
- package/lib/rules/require-disposable-transfer.js +7 -2
- package/lib/utils/ast.d.ts +12 -0
- package/lib/utils/ast.js +49 -0
- package/lib/utils/disposables.d.ts +12 -0
- package/lib/utils/disposables.js +101 -10
- package/lib/utils/lazy-imports.d.ts +93 -0
- package/lib/utils/lazy-imports.js +600 -0
- package/lib/utils/module-size.d.ts +18 -0
- package/lib/utils/module-size.js +249 -0
- package/lib/utils/package-json.d.ts +8 -0
- package/lib/utils/package-json.js +76 -0
- package/lib/utils/playwright-selectors.d.ts +56 -5
- package/lib/utils/playwright-selectors.js +205 -12
- package/lib/utils/plugin-utils.d.ts +50 -2
- package/lib/utils/plugin-utils.js +221 -38
- package/lib/utils/shared-packages.d.ts +9 -0
- package/lib/utils/shared-packages.js +128 -0
- package/lib/utils/signal-lifetime.js +66 -5
- package/lib/utils/signals.d.ts +2 -8
- package/lib/utils/signals.js +7 -42
- package/package.json +1 -1
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) Jupyter Development Team.
|
|
4
|
+
* Distributed under the terms of the Modified BSD License.
|
|
5
|
+
*/
|
|
6
|
+
const utils_1 = require("@typescript-eslint/utils");
|
|
7
|
+
const plugin_utils_1 = require("../utils/plugin-utils");
|
|
8
|
+
const lazy_imports_1 = require("../utils/lazy-imports");
|
|
9
|
+
const module_size_1 = require("../utils/module-size");
|
|
10
|
+
const shared_packages_1 = require("../utils/shared-packages");
|
|
11
|
+
const create_rule_1 = require("../utils/create-rule");
|
|
12
|
+
const DEFAULT_OPTIONS = {
|
|
13
|
+
allowedPackages: lazy_imports_1.DEFAULT_ALLOWED_PACKAGES,
|
|
14
|
+
deferredPackages: lazy_imports_1.DEFAULT_DEFERRED_PACKAGES,
|
|
15
|
+
ignoreImports: [],
|
|
16
|
+
minimumSize: lazy_imports_1.DEFAULT_MINIMUM_SIZE,
|
|
17
|
+
reportInteractionCallbacks: false,
|
|
18
|
+
reportModuleLevelUsage: false
|
|
19
|
+
};
|
|
20
|
+
const jupyterPreferLazyImports = (0, create_rule_1.createRule)({
|
|
21
|
+
name: 'prefer-lazy-imports',
|
|
22
|
+
meta: {
|
|
23
|
+
type: 'suggestion',
|
|
24
|
+
docs: {
|
|
25
|
+
description: 'Prefer deferred imports for heavy dependencies of JupyterLab plugins',
|
|
26
|
+
url: 'https://eslint-plugin.readthedocs.io/en/latest/rules/prefer-lazy-imports/'
|
|
27
|
+
},
|
|
28
|
+
messages: {
|
|
29
|
+
preferLazyImport: "'{{ source }}' is imported at the top of a plugin module but only used inside functions, so it loads before the application starts. " +
|
|
30
|
+
'Import it where it is used instead: `{{ snippet }}`',
|
|
31
|
+
eagerModuleLevelUse: "'{{ source }}' is used at module level in a plugin module, so it loads before the application starts. " +
|
|
32
|
+
"Move the usage into a function and import it there with `await import('{{ source }}')`.",
|
|
33
|
+
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. " +
|
|
34
|
+
'Do not `await import(...)` inside `activate()`: that delays the whole application start. ' +
|
|
35
|
+
"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.",
|
|
36
|
+
preferLazyImportInteraction: "'{{ source }}' is only used inside user-interaction handlers, so it is not needed until the user acts. " +
|
|
37
|
+
'Import it where it is used instead: `{{ snippet }}`',
|
|
38
|
+
deferredPackageImport: "'{{ source }}' is in `deferredPackages`, so it must only be loaded with `await import()`, but this static import loads it together with the module. " +
|
|
39
|
+
'Import it where it is used instead: `{{ snippet }}`',
|
|
40
|
+
deferredPackageEagerUse: "'{{ source }}' is in `deferredPackages`, so it must only be loaded with `await import()`, but it is used while this module is evaluated. " +
|
|
41
|
+
"Move the usage into a function and import it there with `await import('{{ source }}')`. " +
|
|
42
|
+
'If this module is itself only loaded with `import()`, disable the rule for this import.',
|
|
43
|
+
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. " +
|
|
44
|
+
'It is also in `deferredPackages`, so it must only be loaded with `await import()`. ' +
|
|
45
|
+
'Do not `await import(...)` inside `activate()`: that delays the whole application start. ' +
|
|
46
|
+
"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.",
|
|
47
|
+
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. " +
|
|
48
|
+
'Remove the import, or make it `import type`.',
|
|
49
|
+
deferredPackageReExport: "'{{ source }}' is in `deferredPackages`, so it must only be loaded with `await import()`, but this re-export loads it together with the module. " +
|
|
50
|
+
"Remove the re-export and use `await import('{{ source }}')` where the package is needed."
|
|
51
|
+
},
|
|
52
|
+
schema: [
|
|
53
|
+
{
|
|
54
|
+
type: 'object',
|
|
55
|
+
properties: {
|
|
56
|
+
allowedPackages: {
|
|
57
|
+
type: 'array',
|
|
58
|
+
items: { type: 'string' },
|
|
59
|
+
default: lazy_imports_1.DEFAULT_ALLOWED_PACKAGES,
|
|
60
|
+
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.'
|
|
61
|
+
},
|
|
62
|
+
deferredPackages: {
|
|
63
|
+
type: 'array',
|
|
64
|
+
items: { type: 'string' },
|
|
65
|
+
default: lazy_imports_1.DEFAULT_DEFERRED_PACKAGES,
|
|
66
|
+
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.'
|
|
67
|
+
},
|
|
68
|
+
ignoreImports: {
|
|
69
|
+
type: 'array',
|
|
70
|
+
items: { type: 'string' },
|
|
71
|
+
default: [],
|
|
72
|
+
description: 'Import specifiers to skip, matched the same way as `allowedPackages`: `*` wildcards, `!` to deny a specifier whatever else in the list matches it, and a bare specifier tested both as written and against its owning package. For example `./tokens` or `*.css`.'
|
|
73
|
+
},
|
|
74
|
+
minimumSize: {
|
|
75
|
+
type: 'number',
|
|
76
|
+
minimum: 0,
|
|
77
|
+
default: lazy_imports_1.DEFAULT_MINIMUM_SIZE,
|
|
78
|
+
description: 'Smallest module worth deferring, in bytes of code once comments and type declarations are removed, counted over the module and everything it statically imports by relative path. Set to 0 to report every module regardless of size.'
|
|
79
|
+
},
|
|
80
|
+
reportInteractionCallbacks: {
|
|
81
|
+
type: 'boolean',
|
|
82
|
+
default: false,
|
|
83
|
+
description: 'Also check modules which do not define a plugin, reporting an import there when every use sits inside a user-interaction handler: a command `execute` implementation, a listener for an interaction event such as `click`, or a JSX handler prop such as `onClick`.'
|
|
84
|
+
},
|
|
85
|
+
reportModuleLevelUsage: {
|
|
86
|
+
type: 'boolean',
|
|
87
|
+
default: false,
|
|
88
|
+
description: 'Also report imports used at module level, excluding tokens referenced in `requires`, `optional` and `provides`.'
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
additionalProperties: false
|
|
92
|
+
}
|
|
93
|
+
]
|
|
94
|
+
},
|
|
95
|
+
defaultOptions: [DEFAULT_OPTIONS],
|
|
96
|
+
create(context, [options]) {
|
|
97
|
+
const { allowedPackages, deferredPackages, ignoreImports, minimumSize, reportInteractionCallbacks, reportModuleLevelUsage } = options;
|
|
98
|
+
let services = null;
|
|
99
|
+
let checker = null;
|
|
100
|
+
try {
|
|
101
|
+
services = utils_1.ESLintUtils.getParserServices(context, true);
|
|
102
|
+
checker = services.program ? services.program.getTypeChecker() : null;
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
// Parser services unavailable (non-TS file or no type information)
|
|
106
|
+
services = null;
|
|
107
|
+
}
|
|
108
|
+
const getTSNode = services
|
|
109
|
+
? (node) => services?.esTreeNodeToTSNodeMap.get(node)
|
|
110
|
+
: null;
|
|
111
|
+
let isPluginModule = false;
|
|
112
|
+
const importDeclarations = [];
|
|
113
|
+
// Sources kept in the startup bundle by a value re-export.
|
|
114
|
+
const reExportedSources = new Set();
|
|
115
|
+
function mentionsPluginType(typeNode) {
|
|
116
|
+
return (0, plugin_utils_1.typeMentionsJupyterPlugin)(typeNode, checker, getTSNode);
|
|
117
|
+
}
|
|
118
|
+
// Packages this extension declares as provided by the application, read
|
|
119
|
+
// from `jupyterlab.sharedPackages` in its own manifest. They extend
|
|
120
|
+
// `allowedPackages` rather than replacing it, and are only looked up once
|
|
121
|
+
// an import turns out to need them.
|
|
122
|
+
let hostProvided = null;
|
|
123
|
+
/**
|
|
124
|
+
* Returns true when the specifier is exempt from the usage check, either
|
|
125
|
+
* because the application loads it eagerly anyway or because it was
|
|
126
|
+
* ignored explicitly.
|
|
127
|
+
*/
|
|
128
|
+
function isExempt(source) {
|
|
129
|
+
if ((0, lazy_imports_1.matchesPatterns)(source, lazy_imports_1.ALWAYS_IGNORED_IMPORTS) ||
|
|
130
|
+
(0, lazy_imports_1.matchesPatterns)(source, allowedPackages) ||
|
|
131
|
+
(0, lazy_imports_1.matchesPatterns)(source, ignoreImports)) {
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
if (hostProvided === null) {
|
|
135
|
+
hostProvided = (0, shared_packages_1.getHostProvidedPackages)(context.filename);
|
|
136
|
+
}
|
|
137
|
+
return (0, lazy_imports_1.matchesPatterns)(source, hostProvided);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Returns true when the package must only be loaded with `import()`. The
|
|
141
|
+
* list wins over `allowedPackages` and the manifest, which describe what
|
|
142
|
+
* the application loads at startup, since this one describes what it
|
|
143
|
+
* keeps out of startup on purpose. `ignoreImports` still wins as the
|
|
144
|
+
* explicit way to skip a specifier, and an asset from such a package is
|
|
145
|
+
* left to the bundler like any other asset.
|
|
146
|
+
*/
|
|
147
|
+
function isDeferredPackage(source) {
|
|
148
|
+
return ((0, lazy_imports_1.matchesPatterns)(source, deferredPackages) &&
|
|
149
|
+
!(0, lazy_imports_1.matchesPatterns)(source, lazy_imports_1.ALWAYS_IGNORED_IMPORTS) &&
|
|
150
|
+
!(0, lazy_imports_1.matchesPatterns)(source, ignoreImports));
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Returns true when the module holds too little code for a separate chunk
|
|
154
|
+
* to pay off. Only relative imports can be measured; a package which is not
|
|
155
|
+
* in the shared runtime is bundled into the extension, so it always counts
|
|
156
|
+
* as worth deferring. An unreadable module counts as worth deferring too,
|
|
157
|
+
* so a missing file never hides a finding.
|
|
158
|
+
*/
|
|
159
|
+
function isTooSmall(source) {
|
|
160
|
+
if (minimumSize <= 0) {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
const resolved = (0, module_size_1.resolveRelativeModule)(source, context.filename);
|
|
164
|
+
if (!resolved) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
const size = (0, module_size_1.getTransitiveCodeSize)(resolved, minimumSize);
|
|
168
|
+
return size !== null && size < minimumSize;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Returns true when an identifier sits inside `typeof X`, which TypeScript
|
|
172
|
+
* erases even though the scope manager records it as a value reference.
|
|
173
|
+
*/
|
|
174
|
+
function isInTypeQuery(node) {
|
|
175
|
+
let current = node;
|
|
176
|
+
while (current && current.type !== 'Program') {
|
|
177
|
+
if (current.type === 'TSTypeQuery') {
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
current = current.parent;
|
|
181
|
+
}
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Collects the value references of every runtime binding of an import
|
|
186
|
+
* declaration. Type-only specifiers and type positions are left out
|
|
187
|
+
* because TypeScript erases them.
|
|
188
|
+
*/
|
|
189
|
+
function getValueReferences(declaration) {
|
|
190
|
+
const references = [];
|
|
191
|
+
for (const specifier of declaration.specifiers) {
|
|
192
|
+
if (specifier.type === 'ImportSpecifier' &&
|
|
193
|
+
specifier.importKind === 'type') {
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
for (const variable of context.sourceCode.getDeclaredVariables(specifier)) {
|
|
197
|
+
for (const reference of variable.references) {
|
|
198
|
+
// `isValueReference` comes from the typescript-eslint scope
|
|
199
|
+
// manager. Under the default parser there are no type references,
|
|
200
|
+
// so every reference is a value reference.
|
|
201
|
+
const isValue = reference.isValueReference ?? true;
|
|
202
|
+
if (isValue && !isInTypeQuery(reference.identifier)) {
|
|
203
|
+
references.push(reference);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return references;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Checks every declaration importing one source together. A source pulled
|
|
212
|
+
* in eagerly by any one of them is already in the startup bundle, so
|
|
213
|
+
* deferring the others would not remove it.
|
|
214
|
+
*/
|
|
215
|
+
function checkSource(source, declarations) {
|
|
216
|
+
if (isExempt(source) || reExportedSources.has(source)) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const references = declarations.flatMap(getValueReferences);
|
|
220
|
+
if (references.length === 0) {
|
|
221
|
+
// Unused, or used only in type positions which TypeScript erases.
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
// Measuring a module compiles it, which costs far more than the checks
|
|
225
|
+
// below, and most candidates are ruled out by them. The measurement
|
|
226
|
+
// therefore happens at the point of reporting rather than here.
|
|
227
|
+
function reportUnlessTooSmall(descriptor) {
|
|
228
|
+
if (isTooSmall(source)) {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
context.report(descriptor);
|
|
232
|
+
}
|
|
233
|
+
if (!isPluginModule) {
|
|
234
|
+
// Outside a plugin module an ordinary function proves nothing, since
|
|
235
|
+
// it may run while the application starts. Only a position which
|
|
236
|
+
// provably waits for the user shows the import can load later.
|
|
237
|
+
if (references.every(({ identifier }) => (0, lazy_imports_1.isInInteractionCallback)(identifier))) {
|
|
238
|
+
reportUnlessTooSmall({
|
|
239
|
+
node: declarations[0],
|
|
240
|
+
messageId: 'preferLazyImportInteraction',
|
|
241
|
+
data: {
|
|
242
|
+
source,
|
|
243
|
+
snippet: (0, lazy_imports_1.buildDeferredImportSnippet)(declarations)
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
let deferrable = 0;
|
|
250
|
+
let eager = 0;
|
|
251
|
+
let activation = 0;
|
|
252
|
+
let tokenList = 0;
|
|
253
|
+
for (const { identifier } of references) {
|
|
254
|
+
if ((0, lazy_imports_1.isInPluginTokenList)(identifier)) {
|
|
255
|
+
// A token in `requires`, `optional` or `provides` is read when the
|
|
256
|
+
// plugin is registered, so it can never be deferred.
|
|
257
|
+
tokenList += 1;
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
switch ((0, lazy_imports_1.getReach)(identifier, context.sourceCode)) {
|
|
261
|
+
case 'module':
|
|
262
|
+
eager += 1;
|
|
263
|
+
break;
|
|
264
|
+
case 'activation':
|
|
265
|
+
activation += 1;
|
|
266
|
+
break;
|
|
267
|
+
default:
|
|
268
|
+
deferrable += 1;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if (tokenList === 0 && eager === 0 && activation > 0) {
|
|
272
|
+
// `Application.start` waits for every autostart plugin before it
|
|
273
|
+
// attaches the shell, so the module is fetched before the application
|
|
274
|
+
// starts whatever this file does, and an `await import()` inside
|
|
275
|
+
// `activate` would hold the start back by one more request. The usual
|
|
276
|
+
// snippet is therefore the wrong advice here, even when other uses
|
|
277
|
+
// sit in callbacks which could defer it.
|
|
278
|
+
reportUnlessTooSmall({
|
|
279
|
+
node: declarations[0],
|
|
280
|
+
messageId: 'usedInAutostartActivate',
|
|
281
|
+
data: { source }
|
|
282
|
+
});
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
if (tokenList === 0 && eager === 0 && deferrable > 0) {
|
|
286
|
+
reportUnlessTooSmall({
|
|
287
|
+
node: declarations[0],
|
|
288
|
+
messageId: 'preferLazyImport',
|
|
289
|
+
data: {
|
|
290
|
+
source,
|
|
291
|
+
snippet: (0, lazy_imports_1.buildDeferredImportSnippet)(declarations)
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
if (reportModuleLevelUsage && eager > 0 && tokenList === 0) {
|
|
297
|
+
reportUnlessTooSmall({
|
|
298
|
+
node: declarations[0],
|
|
299
|
+
messageId: 'eagerModuleLevelUse',
|
|
300
|
+
data: { source }
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Reports a static import of a package which must only be loaded with
|
|
306
|
+
* `import()`. Where the bindings are used makes no difference here: a
|
|
307
|
+
* binding used only inside a function still loads the package together
|
|
308
|
+
* with this module, and whether this module is part of the startup bundle
|
|
309
|
+
* cannot be seen from this file. So the import is reported wherever it
|
|
310
|
+
* appears, and the usage only picks the advice.
|
|
311
|
+
*
|
|
312
|
+
* Only `import type` is exempt, and that never gets here. A value import
|
|
313
|
+
* whose bindings have no runtime use, an inline `type` specifier included,
|
|
314
|
+
* is erased by TypeScript without `verbatimModuleSyntax` but kept with it
|
|
315
|
+
* (`import { type X }` becomes `import {} from '...'`), and JavaScript
|
|
316
|
+
* never erases anything, so it counts as loading the package as well.
|
|
317
|
+
*/
|
|
318
|
+
function checkDeferredPackage(source, declarations) {
|
|
319
|
+
// The snippet is built from the declarations with a runtime use, plus a
|
|
320
|
+
// side-effect import, which has nothing to erase. Each of the others is
|
|
321
|
+
// reported on its own line, because deleting the reported declarations
|
|
322
|
+
// would leave it behind, still loading the package under
|
|
323
|
+
// `verbatimModuleSyntax` or in JavaScript.
|
|
324
|
+
const loading = [];
|
|
325
|
+
const references = [];
|
|
326
|
+
for (const declaration of declarations) {
|
|
327
|
+
const own = getValueReferences(declaration);
|
|
328
|
+
if (own.length > 0 || declaration.specifiers.length === 0) {
|
|
329
|
+
loading.push(declaration);
|
|
330
|
+
references.push(...own);
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
context.report({
|
|
334
|
+
node: declaration,
|
|
335
|
+
messageId: 'deferredPackageNotTypeOnly',
|
|
336
|
+
data: { source }
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
if (loading.length === 0) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
let eager = false;
|
|
344
|
+
let activation = false;
|
|
345
|
+
for (const { identifier } of references) {
|
|
346
|
+
if ((0, lazy_imports_1.isInPluginTokenList)(identifier)) {
|
|
347
|
+
eager = true;
|
|
348
|
+
break;
|
|
349
|
+
}
|
|
350
|
+
const reach = (0, lazy_imports_1.getReach)(identifier, context.sourceCode);
|
|
351
|
+
if (reach === 'module') {
|
|
352
|
+
eager = true;
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
if (reach === 'activation') {
|
|
356
|
+
activation = true;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
if (eager) {
|
|
360
|
+
context.report({
|
|
361
|
+
node: loading[0],
|
|
362
|
+
messageId: 'deferredPackageEagerUse',
|
|
363
|
+
data: { source }
|
|
364
|
+
});
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
if (activation) {
|
|
368
|
+
// The snippet would put the `await import()` into `activate`, which
|
|
369
|
+
// holds the whole start back, so the advice is the one for any import
|
|
370
|
+
// used during the activation of an autostart plugin, with the list
|
|
371
|
+
// named as well.
|
|
372
|
+
context.report({
|
|
373
|
+
node: loading[0],
|
|
374
|
+
messageId: 'deferredPackageAutostartUse',
|
|
375
|
+
data: { source }
|
|
376
|
+
});
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
context.report({
|
|
380
|
+
node: loading[0],
|
|
381
|
+
messageId: 'deferredPackageImport',
|
|
382
|
+
data: { source, snippet: (0, lazy_imports_1.buildDeferredImportSnippet)(loading) }
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* A re-export of a package which must only be loaded with `import()` is a
|
|
387
|
+
* finding in itself, `export { type X } from` included: only `export type`
|
|
388
|
+
* is erased under `verbatimModuleSyntax`, the rest stays as
|
|
389
|
+
* `export {} from '...'` and loads the source. For any other source a
|
|
390
|
+
* value re-export keeps it in the startup bundle, so deferring the
|
|
391
|
+
* matching import would gain nothing and the usage check skips it.
|
|
392
|
+
*/
|
|
393
|
+
function checkReExport(node, source, hasValueSpecifier) {
|
|
394
|
+
if (isDeferredPackage(source)) {
|
|
395
|
+
context.report({
|
|
396
|
+
node,
|
|
397
|
+
messageId: 'deferredPackageReExport',
|
|
398
|
+
data: { source }
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
else if (hasValueSpecifier) {
|
|
402
|
+
reExportedSources.add(source);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return {
|
|
406
|
+
VariableDeclarator(node) {
|
|
407
|
+
if (!isPluginModule &&
|
|
408
|
+
node.id.type === 'Identifier' &&
|
|
409
|
+
mentionsPluginType(node.id.typeAnnotation?.typeAnnotation)) {
|
|
410
|
+
isPluginModule = true;
|
|
411
|
+
}
|
|
412
|
+
},
|
|
413
|
+
'TSAsExpression, TSSatisfiesExpression'(node) {
|
|
414
|
+
if (!isPluginModule && mentionsPluginType(node.typeAnnotation)) {
|
|
415
|
+
isPluginModule = true;
|
|
416
|
+
}
|
|
417
|
+
},
|
|
418
|
+
'FunctionDeclaration, FunctionExpression, ArrowFunctionExpression'(node) {
|
|
419
|
+
if (!isPluginModule &&
|
|
420
|
+
mentionsPluginType(node.returnType?.typeAnnotation)) {
|
|
421
|
+
isPluginModule = true;
|
|
422
|
+
}
|
|
423
|
+
},
|
|
424
|
+
ObjectExpression(node) {
|
|
425
|
+
if (!isPluginModule && (0, plugin_utils_1.looksLikePluginObject)(node)) {
|
|
426
|
+
isPluginModule = true;
|
|
427
|
+
}
|
|
428
|
+
},
|
|
429
|
+
ImportDeclaration(node) {
|
|
430
|
+
if (node.parent.type === 'Program' && node.importKind !== 'type') {
|
|
431
|
+
importDeclarations.push(node);
|
|
432
|
+
}
|
|
433
|
+
},
|
|
434
|
+
ExportNamedDeclaration(node) {
|
|
435
|
+
if (!node.source || node.exportKind === 'type') {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
checkReExport(node, node.source.value, node.specifiers.some(specifier => specifier.exportKind !== 'type'));
|
|
439
|
+
},
|
|
440
|
+
ExportAllDeclaration(node) {
|
|
441
|
+
if (node.exportKind !== 'type') {
|
|
442
|
+
checkReExport(node, node.source.value, true);
|
|
443
|
+
}
|
|
444
|
+
},
|
|
445
|
+
'Program:exit'() {
|
|
446
|
+
const bySource = new Map();
|
|
447
|
+
for (const declaration of importDeclarations) {
|
|
448
|
+
const source = declaration.source.value;
|
|
449
|
+
const group = bySource.get(source);
|
|
450
|
+
if (group) {
|
|
451
|
+
group.push(declaration);
|
|
452
|
+
}
|
|
453
|
+
else {
|
|
454
|
+
bySource.set(source, [declaration]);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
// A package which must stay deferred is checked in every module. The
|
|
458
|
+
// usage check needs a plugin module, or the interaction option.
|
|
459
|
+
const checkUsage = isPluginModule || reportInteractionCallbacks;
|
|
460
|
+
for (const [source, declarations] of bySource) {
|
|
461
|
+
if (isDeferredPackage(source)) {
|
|
462
|
+
checkDeferredPackage(source, declarations);
|
|
463
|
+
}
|
|
464
|
+
else if (checkUsage) {
|
|
465
|
+
// A side-effect import has no binding to move into a function.
|
|
466
|
+
const withBindings = declarations.filter(declaration => declaration.specifiers.length > 0);
|
|
467
|
+
if (withBindings.length > 0) {
|
|
468
|
+
checkSource(source, withBindings);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
});
|
|
476
|
+
module.exports = jupyterPreferLazyImports;
|
|
@@ -49,21 +49,6 @@ const requireDisposableOwnership = (0, create_rule_1.createRule)({
|
|
|
49
49
|
catch {
|
|
50
50
|
services = null;
|
|
51
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
|
-
}
|
|
67
52
|
const ownership = {
|
|
68
53
|
sourceCode: context.sourceCode,
|
|
69
54
|
checker,
|
|
@@ -85,7 +70,7 @@ const requireDisposableOwnership = (0, create_rule_1.createRule)({
|
|
|
85
70
|
if ((0, disposables_1.isInJupyterPluginActivate)(node, ownership)) {
|
|
86
71
|
return;
|
|
87
72
|
}
|
|
88
|
-
if (!
|
|
73
|
+
if (!(0, disposables_1.constructsDisposable)(node, ownership)) {
|
|
89
74
|
return;
|
|
90
75
|
}
|
|
91
76
|
const assignedVariable = (0, disposables_1.getAssignedVariable)(node, context.sourceCode);
|
|
@@ -170,14 +170,19 @@ const requireDisposableTransfer = (0, create_rule_1.createRule)({
|
|
|
170
170
|
(0, disposables_1.markManagedDisposableUse)(pending, node, ownership);
|
|
171
171
|
if ((0, disposables_1.isInJupyterPluginActivate)(node, ownership) ||
|
|
172
172
|
isIgnoredReturn(node) ||
|
|
173
|
-
!(0, disposables_1.shouldCheckReturnedDisposable)(node)
|
|
174
|
-
(0, disposables_1.isDisposableExpressionManaged)(node, ownership)) {
|
|
173
|
+
!(0, disposables_1.shouldCheckReturnedDisposable)(node)) {
|
|
175
174
|
return;
|
|
176
175
|
}
|
|
176
|
+
// The name test reads the call itself, while the two tests after it
|
|
177
|
+
// ask the type checker about the callee, so a call this rule would
|
|
178
|
+
// never report is dropped before any of that work happens.
|
|
177
179
|
if (!checkAllDisposableReturns &&
|
|
178
180
|
!isLikelyDisposableFactoryCall(node)) {
|
|
179
181
|
return;
|
|
180
182
|
}
|
|
183
|
+
if ((0, disposables_1.isDisposableExpressionManaged)(node, ownership)) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
181
186
|
if (!returnsDisposable(node)) {
|
|
182
187
|
return;
|
|
183
188
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/types';
|
|
2
|
+
export type WalkAction = 'skip-children' | 'stop' | undefined;
|
|
3
|
+
export declare function isNode(value: unknown): value is TSESTree.Node;
|
|
4
|
+
/**
|
|
5
|
+
* Iterative AST walk from `root`, visiting every node. The visitor may return
|
|
6
|
+
* 'skip-children' to avoid descending into a node, or 'stop' to abort the
|
|
7
|
+
* whole walk.
|
|
8
|
+
*
|
|
9
|
+
* Children come from the parser's visitor keys, so `parent` is never followed
|
|
10
|
+
* and a node's `loc` and `range` are never read as children.
|
|
11
|
+
*/
|
|
12
|
+
export declare function walkFrom(root: TSESTree.Node, visit: (node: TSESTree.Node) => WalkAction): void;
|
package/lib/utils/ast.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) Jupyter Development Team.
|
|
4
|
+
* Distributed under the terms of the Modified BSD License.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.isNode = isNode;
|
|
8
|
+
exports.walkFrom = walkFrom;
|
|
9
|
+
const visitor_keys_1 = require("@typescript-eslint/visitor-keys");
|
|
10
|
+
function isNode(value) {
|
|
11
|
+
return (typeof value === 'object' &&
|
|
12
|
+
value !== null &&
|
|
13
|
+
typeof value.type === 'string');
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Iterative AST walk from `root`, visiting every node. The visitor may return
|
|
17
|
+
* 'skip-children' to avoid descending into a node, or 'stop' to abort the
|
|
18
|
+
* whole walk.
|
|
19
|
+
*
|
|
20
|
+
* Children come from the parser's visitor keys, so `parent` is never followed
|
|
21
|
+
* and a node's `loc` and `range` are never read as children.
|
|
22
|
+
*/
|
|
23
|
+
function walkFrom(root, visit) {
|
|
24
|
+
const stack = [root];
|
|
25
|
+
while (stack.length > 0) {
|
|
26
|
+
const node = stack.pop();
|
|
27
|
+
const action = visit(node);
|
|
28
|
+
if (action === 'stop') {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (action === 'skip-children') {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
const keys = visitor_keys_1.visitorKeys[node.type] ?? (0, visitor_keys_1.getKeys)(node);
|
|
35
|
+
for (const key of keys) {
|
|
36
|
+
const child = node[key];
|
|
37
|
+
if (Array.isArray(child)) {
|
|
38
|
+
for (const item of child) {
|
|
39
|
+
if (isNode(item)) {
|
|
40
|
+
stack.push(item);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
else if (isNode(child)) {
|
|
45
|
+
stack.push(child);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -60,5 +60,17 @@ export declare function isClassFieldCollectionMutationCall(node: TSESTree.CallEx
|
|
|
60
60
|
export declare function isDisposableExpressionManaged(node: TSESTree.Node, ownership: DisposableOwnershipContext): boolean;
|
|
61
61
|
export declare function markManagedDisposableUse(pending: PendingDisposableMap, node: TSESTree.Node, ownership: DisposableOwnershipContext): void;
|
|
62
62
|
export declare function isDisposableType(type: ts.Type, checker: ts.TypeChecker, seen?: Set<ts.Type>): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Whether a `new` expression creates a disposable, by the type it constructs
|
|
65
|
+
* when type information is available and by the known constructor names
|
|
66
|
+
* otherwise.
|
|
67
|
+
*
|
|
68
|
+
* A class is constructed all over a codebase, and for a class with no type
|
|
69
|
+
* parameters what it constructs is a property of the class rather than of any
|
|
70
|
+
* one call, so the answer is kept per constructor symbol. Resolving that symbol
|
|
71
|
+
* is name resolution and reading the instance type off it is a lookup, while
|
|
72
|
+
* typing the `new` expression resolves a construct signature.
|
|
73
|
+
*/
|
|
74
|
+
export declare function constructsDisposable(node: TSESTree.NewExpression, ownership: DisposableOwnershipContext): boolean;
|
|
63
75
|
export declare function shouldCheckReturnedDisposable(node: TSESTree.CallExpression): boolean;
|
|
64
76
|
export {};
|