@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.
- package/lib/index.d.ts +27 -1
- package/lib/index.js +21 -3
- package/lib/rules/command-id-convention.d.ts +5 -0
- package/lib/rules/command-id-convention.js +87 -0
- 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.js +18 -102
- package/lib/rules/no-untranslated-string.d.ts +2 -1
- package/lib/rules/no-untranslated-string.js +191 -113
- package/lib/rules/plugin-activation-args.d.ts +3 -3
- package/lib/rules/plugin-activation-args.js +173 -142
- package/lib/rules/plugin-description.js +12 -9
- package/lib/rules/plugin-id-convention.d.ts +6 -0
- package/lib/rules/plugin-id-convention.js +199 -0
- package/lib/rules/prefer-lazy-imports.d.ts +2 -2
- package/lib/rules/prefer-lazy-imports.js +257 -58
- package/lib/rules/prefer-signal-this-arg.d.ts +2 -3
- package/lib/rules/prefer-signal-this-arg.js +2 -10
- package/lib/rules/require-disposable-ownership.d.ts +1 -2
- package/lib/rules/require-disposable-ownership.js +3 -26
- package/lib/rules/require-disposable-transfer.d.ts +1 -2
- package/lib/rules/require-disposable-transfer.js +14 -14
- package/lib/rules/require-signal-cleanup.d.ts +2 -3
- package/lib/rules/require-signal-cleanup.js +2 -10
- package/lib/rules/require-signal-this-arg.d.ts +1 -2
- package/lib/rules/require-signal-this-arg.js +2 -10
- package/lib/utils/ast.d.ts +12 -0
- package/lib/utils/ast.js +49 -0
- package/lib/utils/commands.d.ts +17 -0
- package/lib/utils/commands.js +205 -0
- package/lib/utils/disposables.d.ts +12 -0
- package/lib/utils/disposables.js +101 -10
- package/lib/utils/lazy-imports.d.ts +27 -3
- package/lib/utils/lazy-imports.js +130 -36
- package/lib/utils/module-size.d.ts +7 -1
- package/lib/utils/module-size.js +10 -1
- package/lib/utils/package-json.d.ts +8 -0
- package/lib/utils/package-json.js +76 -0
- package/lib/utils/playwright-selectors.d.ts +15 -0
- package/lib/utils/playwright-selectors.js +80 -0
- package/lib/utils/plugin-utils.d.ts +66 -6
- package/lib/utils/plugin-utils.js +485 -59
- package/lib/utils/shared-packages.js +6 -16
- package/lib/utils/signals.d.ts +2 -8
- package/lib/utils/signals.js +7 -42
- package/lib/utils/type-services.d.ts +13 -0
- package/lib/utils/type-services.js +27 -0
- package/package.json +1 -1
|
@@ -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 requireSignalCleanup = (0, create_rule_1.createRule)({
|
|
@@ -51,15 +51,7 @@ const requireSignalCleanup = (0, create_rule_1.createRule)({
|
|
|
51
51
|
create(context, [options]) {
|
|
52
52
|
const additionalCleanupMethods = options.additionalCleanupMethods || [];
|
|
53
53
|
const longLivedTypes = new Set(options.longLivedTypes ?? signal_lifetime_1.DEFAULT_LONG_LIVED_TYPES);
|
|
54
|
-
|
|
55
|
-
let checker = null;
|
|
56
|
-
try {
|
|
57
|
-
services = utils_1.ESLintUtils.getParserServices(context, true);
|
|
58
|
-
checker = services.program ? services.program.getTypeChecker() : null;
|
|
59
|
-
}
|
|
60
|
-
catch {
|
|
61
|
-
services = null;
|
|
62
|
-
}
|
|
54
|
+
const { services, checker } = (0, type_services_1.getTypeServices)(context);
|
|
63
55
|
const sourceCode = context.sourceCode;
|
|
64
56
|
let program = null;
|
|
65
57
|
let signalLocalNames = new Set(['Signal']);
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
declare const requireSignalThisArg: ESLintUtils.RuleModule<"missingThisArg" | "addThisArg", [], unknown, ESLintUtils.RuleListener> & {
|
|
1
|
+
declare const requireSignalThisArg: import("@typescript-eslint/utils/ts-eslint").RuleModule<"missingThisArg" | "addThisArg", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
3
2
|
name: string;
|
|
4
3
|
};
|
|
5
4
|
export = requireSignalThisArg;
|
|
@@ -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 requireSignalThisArg = (0, create_rule_1.createRule)({
|
|
10
10
|
name: 'require-signal-this-arg',
|
|
@@ -23,15 +23,7 @@ const requireSignalThisArg = (0, create_rule_1.createRule)({
|
|
|
23
23
|
},
|
|
24
24
|
defaultOptions: [],
|
|
25
25
|
create(context) {
|
|
26
|
-
|
|
27
|
-
let checker = null;
|
|
28
|
-
try {
|
|
29
|
-
services = utils_1.ESLintUtils.getParserServices(context, true);
|
|
30
|
-
checker = services.program ? services.program.getTypeChecker() : null;
|
|
31
|
-
}
|
|
32
|
-
catch {
|
|
33
|
-
services = null;
|
|
34
|
-
}
|
|
26
|
+
const { services, checker } = (0, type_services_1.getTypeServices)(context);
|
|
35
27
|
return {
|
|
36
28
|
CallExpression(node) {
|
|
37
29
|
if (!(0, signals_1.isConnectCall)(node) || node.arguments.length !== 1) {
|
|
@@ -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
|
+
}
|
package/lib/utils/commands.d.ts
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
import { TSESTree } from '@typescript-eslint/types';
|
|
2
|
+
import { TSESLint } from '@typescript-eslint/utils';
|
|
3
|
+
import * as ts from 'typescript';
|
|
2
4
|
/**
|
|
3
5
|
* Checks if a node represents *.addCommand()
|
|
4
6
|
*/
|
|
5
7
|
export declare function isAddCommandCall(node: TSESTree.CallExpression): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Resolves a command ID expression to the string it holds at lint time.
|
|
10
|
+
*
|
|
11
|
+
* On top of `resolveStaticString`, this reads a member of a namespace or enum
|
|
12
|
+
* declared in the same file without type information, since
|
|
13
|
+
* `namespace CommandIDs { export const open = '...'; }` is how JupyterLab and
|
|
14
|
+
* most extensions declare their command IDs.
|
|
15
|
+
*/
|
|
16
|
+
export declare function resolveCommandId(node: TSESTree.Node, sourceCode: Readonly<TSESLint.SourceCode>, checker?: ts.TypeChecker | null, getTSNode?: ((n: TSESTree.Node) => ts.Node | undefined) | null): string | null;
|
|
17
|
+
/**
|
|
18
|
+
* Checks whether an `addCommand` receiver is a registry the code created
|
|
19
|
+
* itself with `new CommandRegistry()`, such as the throw-away registry behind
|
|
20
|
+
* a context menu, rather than the application's shared `app.commands`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function isPrivateCommandRegistry(receiver: TSESTree.Expression, scope: TSESLint.Scope.Scope): boolean;
|
package/lib/utils/commands.js
CHANGED
|
@@ -5,6 +5,11 @@
|
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.isAddCommandCall = isAddCommandCall;
|
|
8
|
+
exports.resolveCommandId = resolveCommandId;
|
|
9
|
+
exports.isPrivateCommandRegistry = isPrivateCommandRegistry;
|
|
10
|
+
const utils_1 = require("@typescript-eslint/utils");
|
|
11
|
+
const ast_1 = require("./ast");
|
|
12
|
+
const plugin_utils_1 = require("./plugin-utils");
|
|
8
13
|
/**
|
|
9
14
|
* Checks if a node represents *.addCommand()
|
|
10
15
|
*/
|
|
@@ -18,3 +23,203 @@ function isAddCommandCall(node) {
|
|
|
18
23
|
}
|
|
19
24
|
return false;
|
|
20
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Resolves a command ID expression to the string it holds at lint time.
|
|
28
|
+
*
|
|
29
|
+
* On top of `resolveStaticString`, this reads a member of a namespace or enum
|
|
30
|
+
* declared in the same file without type information, since
|
|
31
|
+
* `namespace CommandIDs { export const open = '...'; }` is how JupyterLab and
|
|
32
|
+
* most extensions declare their command IDs.
|
|
33
|
+
*/
|
|
34
|
+
function resolveCommandId(node, sourceCode, checker, getTSNode) {
|
|
35
|
+
const scope = sourceCode.getScope(node);
|
|
36
|
+
const resolved = (0, plugin_utils_1.resolveStaticString)(node, scope, checker, getTSNode);
|
|
37
|
+
if (resolved !== null) {
|
|
38
|
+
return resolved;
|
|
39
|
+
}
|
|
40
|
+
if (node.type !== 'MemberExpression' ||
|
|
41
|
+
node.computed ||
|
|
42
|
+
node.object.type !== 'Identifier' ||
|
|
43
|
+
node.property.type !== 'Identifier') {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
const variable = utils_1.ASTUtils.findVariable(scope, node.object);
|
|
47
|
+
if (!variable) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
const memberName = node.property.name;
|
|
51
|
+
// Namespaces and enums can be declared in several merged blocks.
|
|
52
|
+
for (const def of variable.defs) {
|
|
53
|
+
const initializer = findDeclaredMember(def.node, memberName);
|
|
54
|
+
if (initializer) {
|
|
55
|
+
return (0, plugin_utils_1.resolveStaticString)(initializer, sourceCode.getScope(initializer), checker, getTSNode);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Finds the initializer of an exported `const` in a namespace block, or of a
|
|
62
|
+
* member of an enum.
|
|
63
|
+
*/
|
|
64
|
+
function findDeclaredMember(declaration, memberName) {
|
|
65
|
+
if (declaration.type === 'TSEnumDeclaration') {
|
|
66
|
+
for (const member of declaration.body.members) {
|
|
67
|
+
const key = member.id.type === 'Identifier' ? member.id.name : member.id.value;
|
|
68
|
+
if (key === memberName) {
|
|
69
|
+
return member.initializer ?? null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
if (declaration.type !== 'TSModuleDeclaration' ||
|
|
75
|
+
declaration.body?.type !== 'TSModuleBlock') {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
for (const statement of declaration.body.body) {
|
|
79
|
+
if (statement.type !== 'ExportNamedDeclaration' ||
|
|
80
|
+
statement.declaration?.type !== 'VariableDeclaration' ||
|
|
81
|
+
statement.declaration.kind !== 'const') {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
for (const declarator of statement.declaration.declarations) {
|
|
85
|
+
if (declarator.id.type === 'Identifier' &&
|
|
86
|
+
declarator.id.name === memberName) {
|
|
87
|
+
return declarator.init;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Checks whether an `addCommand` receiver is a registry the code created
|
|
95
|
+
* itself with `new CommandRegistry()`, such as the throw-away registry behind
|
|
96
|
+
* a context menu, rather than the application's shared `app.commands`.
|
|
97
|
+
*/
|
|
98
|
+
function isPrivateCommandRegistry(receiver, scope) {
|
|
99
|
+
const values = getAssignedValues(receiver, scope);
|
|
100
|
+
return (values !== null &&
|
|
101
|
+
values.length > 0 &&
|
|
102
|
+
values.every(value => isNewCommandRegistry(value, scope)));
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Collects every value assigned to a variable or to a `this.<field>`, or
|
|
106
|
+
* returns null when the receiver is neither.
|
|
107
|
+
*/
|
|
108
|
+
function getAssignedValues(receiver, scope) {
|
|
109
|
+
if (receiver.type === 'Identifier') {
|
|
110
|
+
const variable = utils_1.ASTUtils.findVariable(scope, receiver);
|
|
111
|
+
if (!variable) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
const values = [];
|
|
115
|
+
for (const reference of variable.references) {
|
|
116
|
+
if (reference.isWrite()) {
|
|
117
|
+
// A write without a value (`for (x of ...)`, destructuring) is not
|
|
118
|
+
// a known registry.
|
|
119
|
+
if (!reference.writeExpr) {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
values.push(reference.writeExpr);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return values;
|
|
126
|
+
}
|
|
127
|
+
if (receiver.type !== 'MemberExpression' ||
|
|
128
|
+
receiver.computed ||
|
|
129
|
+
receiver.object.type !== 'ThisExpression') {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
const property = receiver.property;
|
|
133
|
+
const classBody = getEnclosingClassBody(receiver);
|
|
134
|
+
if (!classBody) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
const values = [];
|
|
138
|
+
let unknownWrite = false;
|
|
139
|
+
(0, ast_1.walkFrom)(classBody, node => {
|
|
140
|
+
if ((node.type === 'ClassDeclaration' || node.type === 'ClassExpression') &&
|
|
141
|
+
node.body !== classBody) {
|
|
142
|
+
return 'skip-children';
|
|
143
|
+
}
|
|
144
|
+
if (node.type === 'PropertyDefinition' &&
|
|
145
|
+
!node.computed &&
|
|
146
|
+
isSameKey(node.key, property)) {
|
|
147
|
+
if (node.value) {
|
|
148
|
+
values.push(node.value);
|
|
149
|
+
}
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
if (node.type === 'AssignmentExpression' &&
|
|
153
|
+
node.left.type === 'MemberExpression' &&
|
|
154
|
+
node.left.object.type === 'ThisExpression' &&
|
|
155
|
+
!node.left.computed &&
|
|
156
|
+
isSameKey(node.left.property, property)) {
|
|
157
|
+
// In a nested regular function `this` is whatever the caller binds,
|
|
158
|
+
// which may or may not be this instance, so the write is unknown.
|
|
159
|
+
if (node.operator !== '=' || getEnclosingClassBody(node) !== classBody) {
|
|
160
|
+
unknownWrite = true;
|
|
161
|
+
return 'stop';
|
|
162
|
+
}
|
|
163
|
+
values.push(node.right);
|
|
164
|
+
}
|
|
165
|
+
return undefined;
|
|
166
|
+
});
|
|
167
|
+
return unknownWrite ? null : values;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Finds the class body whose instance `this` refers to at a node, looking
|
|
171
|
+
* through arrow functions.
|
|
172
|
+
*/
|
|
173
|
+
function getEnclosingClassBody(node) {
|
|
174
|
+
let current = node.parent;
|
|
175
|
+
while (current) {
|
|
176
|
+
if (current.type === 'FunctionExpression' ||
|
|
177
|
+
current.type === 'FunctionDeclaration') {
|
|
178
|
+
// Only a method keeps the class instance as `this`.
|
|
179
|
+
const owner = current.parent;
|
|
180
|
+
if (owner?.type !== 'MethodDefinition' &&
|
|
181
|
+
owner?.type !== 'PropertyDefinition') {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (current.type === 'ClassBody') {
|
|
186
|
+
return current;
|
|
187
|
+
}
|
|
188
|
+
current = current.parent;
|
|
189
|
+
}
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
function isSameKey(key, property) {
|
|
193
|
+
return key.type === property.type && key.name === property.name;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Checks for `new CommandRegistry()`, `new <ns>.CommandRegistry()` or a
|
|
197
|
+
* renamed import of `CommandRegistry`.
|
|
198
|
+
*/
|
|
199
|
+
function isNewCommandRegistry(node, scope) {
|
|
200
|
+
while (node.type === 'TSAsExpression' ||
|
|
201
|
+
node.type === 'TSSatisfiesExpression' ||
|
|
202
|
+
node.type === 'TSNonNullExpression' ||
|
|
203
|
+
node.type === 'TSTypeAssertion') {
|
|
204
|
+
node = node.expression;
|
|
205
|
+
}
|
|
206
|
+
if (node.type !== 'NewExpression') {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
const callee = node.callee;
|
|
210
|
+
if (callee.type === 'MemberExpression') {
|
|
211
|
+
return (!callee.computed &&
|
|
212
|
+
callee.property.type === 'Identifier' &&
|
|
213
|
+
callee.property.name === 'CommandRegistry');
|
|
214
|
+
}
|
|
215
|
+
if (callee.type !== 'Identifier') {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
if (callee.name === 'CommandRegistry') {
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
const definition = utils_1.ASTUtils.findVariable(scope, callee)?.defs[0];
|
|
222
|
+
return (definition?.node.type === 'ImportSpecifier' &&
|
|
223
|
+
definition.node.imported.type === 'Identifier' &&
|
|
224
|
+
definition.node.imported.name === 'CommandRegistry');
|
|
225
|
+
}
|
|
@@ -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 {};
|
package/lib/utils/disposables.js
CHANGED
|
@@ -52,6 +52,7 @@ exports.isClassFieldCollectionMutationCall = isClassFieldCollectionMutationCall;
|
|
|
52
52
|
exports.isDisposableExpressionManaged = isDisposableExpressionManaged;
|
|
53
53
|
exports.markManagedDisposableUse = markManagedDisposableUse;
|
|
54
54
|
exports.isDisposableType = isDisposableType;
|
|
55
|
+
exports.constructsDisposable = constructsDisposable;
|
|
55
56
|
exports.shouldCheckReturnedDisposable = shouldCheckReturnedDisposable;
|
|
56
57
|
const ts = __importStar(require("typescript"));
|
|
57
58
|
const plugin_utils_1 = require("./plugin-utils");
|
|
@@ -954,6 +955,7 @@ function isArrayExtInsertCall(node) {
|
|
|
954
955
|
*/
|
|
955
956
|
const signatureCache = new WeakMap();
|
|
956
957
|
const disposableTypeCache = new WeakMap();
|
|
958
|
+
const disposableConstructorCache = new WeakMap();
|
|
957
959
|
function isDisposableTypeCached(type, checker) {
|
|
958
960
|
const cached = disposableTypeCache.get(type);
|
|
959
961
|
if (cached !== undefined) {
|
|
@@ -1568,6 +1570,29 @@ function markDisposedSignalCallbackDisposals(pending, node, ownership) {
|
|
|
1568
1570
|
markManagedVariablesWithoutScopeCheck(pending, target, ownership, node);
|
|
1569
1571
|
}
|
|
1570
1572
|
}
|
|
1573
|
+
/**
|
|
1574
|
+
* Whether a call can credit any disposable that is still waiting, which decides
|
|
1575
|
+
* whether it is worth asking the type checker which of its arguments take
|
|
1576
|
+
* ownership.
|
|
1577
|
+
*
|
|
1578
|
+
* Crediting removes an entry from `pending` and does nothing for a variable
|
|
1579
|
+
* that is not in it, so a call naming no waiting disposable changes nothing
|
|
1580
|
+
* whatever the answer is. Both the arguments and the receiver are checked
|
|
1581
|
+
* because the answer also decides whether the receiver checks in
|
|
1582
|
+
* `markManagedDisposableUse` run, and those credit the receiver. Any check
|
|
1583
|
+
* added there has to credit the receiver or an argument for this to hold.
|
|
1584
|
+
*/
|
|
1585
|
+
function namesPendingDisposable(pending, node, ownership) {
|
|
1586
|
+
return getIdentifierVariables(ownership.sourceCode, node).some(variable => pending.has(variable));
|
|
1587
|
+
}
|
|
1588
|
+
function mayCreditPendingDisposable(pending, node, ownership) {
|
|
1589
|
+
if (node.type === 'CallExpression' &&
|
|
1590
|
+
node.callee.type === 'MemberExpression' &&
|
|
1591
|
+
namesPendingDisposable(pending, node.callee.object, ownership)) {
|
|
1592
|
+
return true;
|
|
1593
|
+
}
|
|
1594
|
+
return node.arguments.some(argument => namesPendingDisposable(pending, argument, ownership));
|
|
1595
|
+
}
|
|
1571
1596
|
function markManagedDisposableUse(pending, node, ownership) {
|
|
1572
1597
|
if (node.type === 'ReturnStatement') {
|
|
1573
1598
|
if (isInJupyterPluginActivate(node, ownership)) {
|
|
@@ -1600,21 +1625,26 @@ function markManagedDisposableUse(pending, node, ownership) {
|
|
|
1600
1625
|
markDisposedSignalCallbackDisposals(pending, node, ownership);
|
|
1601
1626
|
markImmediateForEachOwnership(pending, node, ownership);
|
|
1602
1627
|
}
|
|
1628
|
+
// Crediting a disposable only ever removes an entry from `pending`, so with
|
|
1629
|
+
// nothing waiting neither check below can change the outcome. Skipping them
|
|
1630
|
+
// avoids resolving the callee signature, which is the most expensive query
|
|
1631
|
+
// this module makes.
|
|
1603
1632
|
if (pending.size > 0) {
|
|
1604
1633
|
markManagedKnownOptionVariables(pending, node, ownership);
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1634
|
+
for (const argument of node.arguments) {
|
|
1635
|
+
if (argument.type !== 'ObjectExpression') {
|
|
1636
|
+
continue;
|
|
1637
|
+
}
|
|
1638
|
+
for (const property of argument.properties) {
|
|
1639
|
+
if (property.type === 'Property' &&
|
|
1640
|
+
namesPendingDisposable(pending, property.value, ownership) &&
|
|
1641
|
+
isOptionsObjectValueManaged(property.value, ownership)) {
|
|
1642
|
+
markManagedVariables(pending, property.value, ownership);
|
|
1643
|
+
}
|
|
1614
1644
|
}
|
|
1615
1645
|
}
|
|
1616
1646
|
}
|
|
1617
|
-
const ownershipArguments = pending.size > 0
|
|
1647
|
+
const ownershipArguments = pending.size > 0 && mayCreditPendingDisposable(pending, node, ownership)
|
|
1618
1648
|
? getOwnershipArguments(node, ownership)
|
|
1619
1649
|
: [];
|
|
1620
1650
|
if (ownershipArguments.length > 0) {
|
|
@@ -1686,6 +1716,67 @@ function isDisposableType(type, checker, seen = new Set()) {
|
|
|
1686
1716
|
hasDisposableHeritage(type) ||
|
|
1687
1717
|
hasDisposableShape(apparentType, checker));
|
|
1688
1718
|
}
|
|
1719
|
+
/**
|
|
1720
|
+
* A class with no type parameters constructs the same type everywhere, so its
|
|
1721
|
+
* instance type answers for every `new` of it. A generic one does not: `dispose`
|
|
1722
|
+
* can be declared as the type parameter, which is callable in one instantiation
|
|
1723
|
+
* and not in another, so those are asked about one expression at a time.
|
|
1724
|
+
*/
|
|
1725
|
+
function nonGenericClassSymbol(symbol) {
|
|
1726
|
+
if (!symbol || !(symbol.flags & ts.SymbolFlags.Class)) {
|
|
1727
|
+
return undefined;
|
|
1728
|
+
}
|
|
1729
|
+
const declarations = symbol.declarations ?? [];
|
|
1730
|
+
const generic = declarations.some(declaration => (ts.isClassDeclaration(declaration) ||
|
|
1731
|
+
ts.isClassExpression(declaration)) &&
|
|
1732
|
+
(declaration.typeParameters?.length ?? 0) > 0);
|
|
1733
|
+
return generic ? undefined : symbol;
|
|
1734
|
+
}
|
|
1735
|
+
/**
|
|
1736
|
+
* Whether a `new` expression creates a disposable, by the type it constructs
|
|
1737
|
+
* when type information is available and by the known constructor names
|
|
1738
|
+
* otherwise.
|
|
1739
|
+
*
|
|
1740
|
+
* A class is constructed all over a codebase, and for a class with no type
|
|
1741
|
+
* parameters what it constructs is a property of the class rather than of any
|
|
1742
|
+
* one call, so the answer is kept per constructor symbol. Resolving that symbol
|
|
1743
|
+
* is name resolution and reading the instance type off it is a lookup, while
|
|
1744
|
+
* typing the `new` expression resolves a construct signature.
|
|
1745
|
+
*/
|
|
1746
|
+
function constructsDisposable(node, ownership) {
|
|
1747
|
+
const { checker, services } = ownership;
|
|
1748
|
+
if (checker && services) {
|
|
1749
|
+
try {
|
|
1750
|
+
const tsNode = services.esTreeNodeToTSNodeMap.get(node);
|
|
1751
|
+
let symbol = checker.getSymbolAtLocation(tsNode.expression);
|
|
1752
|
+
if (symbol && symbol.flags & ts.SymbolFlags.Alias) {
|
|
1753
|
+
// An imported class resolves to a different alias symbol in every
|
|
1754
|
+
// file; the class it points at is shared by all of them.
|
|
1755
|
+
symbol = checker.getAliasedSymbol(symbol);
|
|
1756
|
+
}
|
|
1757
|
+
const classSymbol = nonGenericClassSymbol(symbol);
|
|
1758
|
+
if (classSymbol) {
|
|
1759
|
+
const cached = disposableConstructorCache.get(classSymbol);
|
|
1760
|
+
if (cached !== undefined) {
|
|
1761
|
+
return cached || isDisposableConstructor(node);
|
|
1762
|
+
}
|
|
1763
|
+
}
|
|
1764
|
+
const result = isDisposableTypeCached(classSymbol
|
|
1765
|
+
? checker.getDeclaredTypeOfSymbol(classSymbol)
|
|
1766
|
+
: checker.getTypeAtLocation(tsNode), checker);
|
|
1767
|
+
if (classSymbol) {
|
|
1768
|
+
disposableConstructorCache.set(classSymbol, result);
|
|
1769
|
+
}
|
|
1770
|
+
if (result) {
|
|
1771
|
+
return true;
|
|
1772
|
+
}
|
|
1773
|
+
}
|
|
1774
|
+
catch {
|
|
1775
|
+
// Fall back to the known Lumino disposable constructors below.
|
|
1776
|
+
}
|
|
1777
|
+
}
|
|
1778
|
+
return isDisposableConstructor(node);
|
|
1779
|
+
}
|
|
1689
1780
|
function shouldCheckReturnedDisposable(node) {
|
|
1690
1781
|
const expression = getOuterFallbackExpression(node);
|
|
1691
1782
|
const parent = expression.parent;
|
|
@@ -2,10 +2,12 @@ import { TSESTree } from '@typescript-eslint/types';
|
|
|
2
2
|
import { TSESLint } from '@typescript-eslint/utils';
|
|
3
3
|
export interface LazyImportOptions {
|
|
4
4
|
allowedPackages: string[];
|
|
5
|
+
deferredPackages: string[];
|
|
5
6
|
ignoreImports: string[];
|
|
6
7
|
minimumSize: number;
|
|
7
8
|
reportInteractionCallbacks: boolean;
|
|
8
9
|
reportModuleLevelUsage: boolean;
|
|
10
|
+
reportReExports: boolean;
|
|
9
11
|
}
|
|
10
12
|
/**
|
|
11
13
|
* An async chunk carries a few hundred bytes of bundler runtime, so around one
|
|
@@ -24,6 +26,15 @@ export declare const DEFAULT_MINIMUM_SIZE = 4096;
|
|
|
24
26
|
* Entries prefixed with `!` are denied even when another pattern allows them.
|
|
25
27
|
*/
|
|
26
28
|
export declare const DEFAULT_ALLOWED_PACKAGES: string[];
|
|
29
|
+
/**
|
|
30
|
+
* Packages which JupyterLab itself loads only with `import()`, so a static
|
|
31
|
+
* import anywhere in an extension puts them back on the startup path.
|
|
32
|
+
*
|
|
33
|
+
* A subpath such as `@codemirror/legacy-modes/mode/python` matches through its
|
|
34
|
+
* owning package. Entries prefixed with `!` are exempt even when another
|
|
35
|
+
* pattern matches them.
|
|
36
|
+
*/
|
|
37
|
+
export declare const DEFAULT_DEFERRED_PACKAGES: string[];
|
|
27
38
|
/**
|
|
28
39
|
* Assets which a bundler turns into a URL or a style side effect rather than
|
|
29
40
|
* into bundled bytes, so deferring the import saves nothing.
|
|
@@ -47,10 +58,23 @@ export declare const ALWAYS_IGNORED_IMPORTS: string[];
|
|
|
47
58
|
*/
|
|
48
59
|
export declare function matchesPatterns(specifier: string, patterns: string[]): boolean;
|
|
49
60
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
61
|
+
* When a node runs, relative to the application starting. `module` is while
|
|
62
|
+
* the module is evaluated. `activation` is inside the `activate` of a plugin
|
|
63
|
+
* which declares `autoStart: true`, which `Application.start` waits for before
|
|
64
|
+
* it attaches the shell. `deferred` is any later point the rule cannot place,
|
|
65
|
+
* such as a command or a method call.
|
|
66
|
+
*/
|
|
67
|
+
export type Reach = 'module' | 'activation' | 'deferred';
|
|
68
|
+
/**
|
|
69
|
+
* Finds when the node runs: while the module is evaluated, during the
|
|
70
|
+
* activation of an autostart plugin, or only later. A node inside a named
|
|
71
|
+
* function is placed by the function's call sites, followed transitively, and
|
|
72
|
+
* the earliest of them wins, so a helper called from both `activate` and a
|
|
73
|
+
* command runs during activation. Each function is explored once per walk,
|
|
74
|
+
* which ends a cycle and bounds the cost by the number of functions in the
|
|
75
|
+
* file, so a chain of helpers is followed however long it is.
|
|
52
76
|
*/
|
|
53
|
-
export declare function
|
|
77
|
+
export declare function getReach(node: TSESTree.Node, sourceCode: TSESLint.SourceCode, seen?: Set<TSESTree.Node>): Reach;
|
|
54
78
|
/**
|
|
55
79
|
* Returns true when the node sits inside a user-interaction handler, so it
|
|
56
80
|
* cannot run until the user acts, however the module itself is loaded. The
|