@jupyter/eslint-plugin 1.0.1 → 1.2.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 +98 -1
- package/lib/index.js +61 -11
- package/lib/rules/galata-prefer-filebrowser-helper.d.ts +5 -0
- package/lib/rules/galata-prefer-filebrowser-helper.js +105 -0
- package/lib/rules/galata-prefer-menu-helper.d.ts +6 -0
- package/lib/rules/galata-prefer-menu-helper.js +478 -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/incorrect-translator-usage.d.ts +4 -0
- package/lib/rules/incorrect-translator-usage.js +208 -0
- package/lib/rules/no-dynamic-translation.d.ts +4 -0
- package/lib/rules/no-dynamic-translation.js +92 -0
- package/lib/rules/no-pageconfig-base-url.d.ts +4 -0
- package/lib/rules/no-pageconfig-base-url.js +37 -0
- package/lib/rules/no-schema-enum.d.ts +1 -1
- package/lib/rules/no-schema-enum.js +54 -2
- package/lib/rules/no-translation-concatenation.js +46 -84
- package/lib/rules/no-untranslated-string.js +7 -4
- package/lib/rules/plugin-activation-args.js +1 -2
- package/lib/rules/prefer-lazy-imports.d.ts +6 -0
- package/lib/rules/prefer-lazy-imports.js +308 -0
- package/lib/rules/prefer-signal-this-arg.d.ts +7 -0
- package/lib/rules/prefer-signal-this-arg.js +196 -0
- package/lib/rules/require-disposable-ownership.d.ts +5 -0
- package/lib/rules/require-disposable-ownership.js +120 -0
- package/lib/rules/require-disposable-transfer.d.ts +5 -0
- package/lib/rules/require-disposable-transfer.js +223 -0
- package/lib/rules/require-signal-cleanup.d.ts +8 -0
- package/lib/rules/require-signal-cleanup.js +164 -0
- package/lib/rules/require-signal-this-arg.d.ts +5 -0
- package/lib/rules/require-signal-this-arg.js +74 -0
- package/lib/rules/token-format.js +1 -2
- package/lib/utils/disposables.d.ts +64 -0
- package/lib/utils/disposables.js +1723 -0
- package/lib/utils/lazy-imports.d.ts +70 -0
- package/lib/utils/lazy-imports.js +515 -0
- package/lib/utils/module-size.d.ts +12 -0
- package/lib/utils/module-size.js +240 -0
- package/lib/utils/playwright-selectors.d.ts +102 -0
- package/lib/utils/playwright-selectors.js +306 -0
- package/lib/utils/plugin-utils.d.ts +18 -0
- package/lib/utils/plugin-utils.js +127 -19
- package/lib/utils/shared-packages.d.ts +9 -0
- package/lib/utils/shared-packages.js +138 -0
- package/lib/utils/signal-lifetime.d.ts +158 -0
- package/lib/utils/signal-lifetime.js +816 -0
- package/lib/utils/signals.d.ts +143 -0
- package/lib/utils/signals.js +601 -0
- package/lib/utils/translation.d.ts +45 -0
- package/lib/utils/translation.js +122 -0
- package/package.json +9 -2
|
@@ -0,0 +1,196 @@
|
|
|
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 create_rule_1 = require("../utils/create-rule");
|
|
8
|
+
const signals_1 = require("../utils/signals");
|
|
9
|
+
const signal_lifetime_1 = require("../utils/signal-lifetime");
|
|
10
|
+
const preferSignalThisArg = (0, create_rule_1.createRule)({
|
|
11
|
+
name: 'prefer-signal-this-arg',
|
|
12
|
+
meta: {
|
|
13
|
+
type: 'problem',
|
|
14
|
+
hasSuggestions: true,
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'Pass a thisArg when connecting to a Lumino signal whose sender outlives the connecting class',
|
|
17
|
+
url: 'https://eslint-plugin.readthedocs.io/en/latest/rules/prefer-signal-this-arg/'
|
|
18
|
+
},
|
|
19
|
+
messages: {
|
|
20
|
+
preferThisArg: 'This connection to "{{ sender }}" has no receiver, so the receiver-based cleanup this class relies on (Signal.clearData(this)) cannot remove it — and {{ reason }}. Pass "this" as the second argument to connect().',
|
|
21
|
+
boundCallback: 'A ".bind()" callback is a fresh function on every call, so this connection to "{{ sender }}" cannot be removed by any disconnect() — and {{ reason }}. Connect the method directly and pass "this" as the second argument.',
|
|
22
|
+
addThisArg: 'Add "this" as the second argument',
|
|
23
|
+
unbindAndAddThisArg: 'Drop ".bind(this)" and pass "this" to connect()'
|
|
24
|
+
},
|
|
25
|
+
schema: [
|
|
26
|
+
{
|
|
27
|
+
type: 'object',
|
|
28
|
+
properties: {
|
|
29
|
+
longLivedTypes: {
|
|
30
|
+
type: 'array',
|
|
31
|
+
items: {
|
|
32
|
+
type: 'string'
|
|
33
|
+
},
|
|
34
|
+
description: 'Type names treated as application-lifetime services. Replaces the built-in list when provided.'
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
additionalProperties: false
|
|
38
|
+
}
|
|
39
|
+
]
|
|
40
|
+
},
|
|
41
|
+
defaultOptions: [
|
|
42
|
+
{
|
|
43
|
+
longLivedTypes: undefined
|
|
44
|
+
}
|
|
45
|
+
],
|
|
46
|
+
create(context, [options]) {
|
|
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
|
+
}
|
|
57
|
+
const sourceCode = context.sourceCode;
|
|
58
|
+
let program = null;
|
|
59
|
+
let signalLocalNames = new Set(['Signal']);
|
|
60
|
+
const receiverCleanupCache = new Map();
|
|
61
|
+
const ownershipCache = new Map();
|
|
62
|
+
return {
|
|
63
|
+
Program(node) {
|
|
64
|
+
program = node;
|
|
65
|
+
signalLocalNames = (0, signals_1.collectSignalNamespaceLocalNames)(node);
|
|
66
|
+
receiverCleanupCache.clear();
|
|
67
|
+
ownershipCache.clear();
|
|
68
|
+
},
|
|
69
|
+
CallExpression(node) {
|
|
70
|
+
if (!(0, signals_1.isConnectCall)(node) || node.arguments.length !== 1 || !program) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const enclosingClass = (0, signals_1.getEnclosingClass)(node);
|
|
74
|
+
if (!enclosingClass) {
|
|
75
|
+
// No `this` to pass; module scope and plugin activate() functions
|
|
76
|
+
// are app-lifetime connections anyway.
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if ((0, signals_1.getThisBinding)(node) !== 'instance') {
|
|
80
|
+
// `this` here is the class object (`static` member) or whatever the
|
|
81
|
+
// caller binds (nested regular function). Passing it would not
|
|
82
|
+
// register the instance that receiver-based cleanup clears, so `,
|
|
83
|
+
// this` is not the fix.
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const chain = (0, signal_lifetime_1.buildSenderChain)(node.callee.object);
|
|
87
|
+
if ((0, signal_lifetime_1.isSelfTerminatingSignal)(chain)) {
|
|
88
|
+
// `x.disposed.connect(() => ...)` is cleaned up sender-side.
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if ((0, signals_1.resolveUnboundThisMethodConnect)(node)) {
|
|
92
|
+
// A bare method reference that uses `this` is a runtime bug, not
|
|
93
|
+
// just a cleanup concern — reported at error level by
|
|
94
|
+
// require-signal-this-arg instead.
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const callback = node.arguments[0];
|
|
98
|
+
const shape = (0, signal_lifetime_1.classifyCallbackShape)(callback);
|
|
99
|
+
if (shape === 'opaque') {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
if ((0, signal_lifetime_1.hasMatchingDisconnect)(program, callback, sourceCode, 1)) {
|
|
103
|
+
// Lumino matches connections by the exact (signal, slot, thisArg)
|
|
104
|
+
// triple, so a paired one-argument disconnect(callback) is a working
|
|
105
|
+
// teardown that adding `, this` here would silently break.
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
let cleanupIsViable = receiverCleanupCache.get(enclosingClass);
|
|
109
|
+
if (cleanupIsViable === undefined) {
|
|
110
|
+
cleanupIsViable =
|
|
111
|
+
(0, signals_1.classUsesReceiverBasedCleanup)(enclosingClass, signalLocalNames) ||
|
|
112
|
+
(0, signals_1.classExtendsLuminoWidget)(enclosingClass, checker, services);
|
|
113
|
+
receiverCleanupCache.set(enclosingClass, cleanupIsViable);
|
|
114
|
+
}
|
|
115
|
+
if (!cleanupIsViable) {
|
|
116
|
+
// Nothing in this class matches connections by receiver, so adding a
|
|
117
|
+
// thisArg would not make the connection removable. Whatever the
|
|
118
|
+
// lifetimes are, `, this` is not the fix.
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if ((0, signals_1.classifySignalReceiver)(node.callee.object, checker, services) ===
|
|
122
|
+
'not-signal') {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
let facts = ownershipCache.get(enclosingClass);
|
|
126
|
+
if (!facts) {
|
|
127
|
+
facts = (0, signal_lifetime_1.collectClassOwnershipFacts)(enclosingClass, sourceCode, signalLocalNames);
|
|
128
|
+
ownershipCache.set(enclosingClass, facts);
|
|
129
|
+
}
|
|
130
|
+
const analysis = (0, signal_lifetime_1.analyzeSender)(node.callee.object, {
|
|
131
|
+
classNode: enclosingClass,
|
|
132
|
+
facts,
|
|
133
|
+
sourceCode,
|
|
134
|
+
scope: sourceCode.getScope(node),
|
|
135
|
+
checker,
|
|
136
|
+
services,
|
|
137
|
+
longLivedTypes
|
|
138
|
+
});
|
|
139
|
+
// Only report when the sender is positively proven to outlive the
|
|
140
|
+
// receiver. A sender the class owns, or one whose lifetime cannot be
|
|
141
|
+
// established, is silence — a missing thisArg is harmless there.
|
|
142
|
+
let reason;
|
|
143
|
+
if (analysis.lifetime === 'long-lived-service') {
|
|
144
|
+
reason = `"${analysis.typeName}" is an application-lifetime service that will outlive this class`;
|
|
145
|
+
}
|
|
146
|
+
else if (analysis.lifetime === 'long-lived-model') {
|
|
147
|
+
reason = 'a model outlives the views built on it';
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
// A `.bind(this)` can be rewritten into `(method, this)`. A `.bind()`
|
|
153
|
+
// onto anything else cannot — that would move the callback's receiver
|
|
154
|
+
// — so it is reported as an ordinary missing thisArg: appending
|
|
155
|
+
// `, this` is behaviour-preserving there (Lumino invokes the slot with
|
|
156
|
+
// `call(thisArg, ...)`, which an already-bound function ignores) and
|
|
157
|
+
// still makes the connection removable by receiver.
|
|
158
|
+
const bindTarget = shape === 'bound' ? getUnbindReplacement(callback, sourceCode) : null;
|
|
159
|
+
context.report({
|
|
160
|
+
node: callback,
|
|
161
|
+
messageId: bindTarget ? 'boundCallback' : 'preferThisArg',
|
|
162
|
+
data: { sender: analysis.senderText, reason },
|
|
163
|
+
suggest: bindTarget
|
|
164
|
+
? [
|
|
165
|
+
{
|
|
166
|
+
messageId: 'unbindAndAddThisArg',
|
|
167
|
+
fix: fixer => fixer.replaceText(callback, bindTarget)
|
|
168
|
+
}
|
|
169
|
+
]
|
|
170
|
+
: [
|
|
171
|
+
{
|
|
172
|
+
messageId: 'addThisArg',
|
|
173
|
+
fix: fixer => fixer.insertTextAfter(callback, ', this')
|
|
174
|
+
}
|
|
175
|
+
]
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
/**
|
|
182
|
+
* Turns `this._onFoo.bind(this)` into the text `this._onFoo, this`, so the
|
|
183
|
+
* suggestion rewrites `connect(fn.bind(this))` into `connect(fn, this)`.
|
|
184
|
+
* Returns null when `.bind()` is passed anything other than `this` — the
|
|
185
|
+
* receiver would change and the edit would not be behaviour-preserving.
|
|
186
|
+
*/
|
|
187
|
+
function getUnbindReplacement(callback, sourceCode) {
|
|
188
|
+
if (callback.type !== 'CallExpression' ||
|
|
189
|
+
callback.callee.type !== 'MemberExpression' ||
|
|
190
|
+
callback.arguments.length !== 1 ||
|
|
191
|
+
callback.arguments[0].type !== 'ThisExpression') {
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
return `${sourceCode.getText(callback.callee.object)}, this`;
|
|
195
|
+
}
|
|
196
|
+
module.exports = preferSignalThisArg;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
+
declare const requireDisposableOwnership: ESLintUtils.RuleModule<"unmanagedDisposable" | "unmanagedDisposableVariable", [{}], unknown, ESLintUtils.RuleListener> & {
|
|
3
|
+
name: string;
|
|
4
|
+
};
|
|
5
|
+
export = requireDisposableOwnership;
|
|
@@ -0,0 +1,120 @@
|
|
|
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 create_rule_1 = require("../utils/create-rule");
|
|
8
|
+
const disposables_1 = require("../utils/disposables");
|
|
9
|
+
const requireDisposableOwnership = (0, create_rule_1.createRule)({
|
|
10
|
+
name: 'require-disposable-ownership',
|
|
11
|
+
meta: {
|
|
12
|
+
type: 'problem',
|
|
13
|
+
docs: {
|
|
14
|
+
description: 'Require newly created IDisposable objects to be owned, returned, assigned to a field, or disposed',
|
|
15
|
+
url: 'https://eslint-plugin.readthedocs.io/en/latest/rules/require-disposable-ownership/'
|
|
16
|
+
},
|
|
17
|
+
messages: {
|
|
18
|
+
unmanagedDisposable: 'Disposable created here must be owned, returned, assigned to a field, or disposed.',
|
|
19
|
+
unmanagedDisposableVariable: 'Disposable "{{ name }}" must be owned, returned, assigned to a field, or disposed.'
|
|
20
|
+
},
|
|
21
|
+
schema: [
|
|
22
|
+
{
|
|
23
|
+
type: 'object',
|
|
24
|
+
properties: {
|
|
25
|
+
ownershipFunctionNames: {
|
|
26
|
+
type: 'array',
|
|
27
|
+
items: { type: 'string' },
|
|
28
|
+
description: 'Function or method names that take ownership of a disposable argument. Added to the built-in defaults unless extendDefaultOwnershipFunctionNames is false.'
|
|
29
|
+
},
|
|
30
|
+
extendDefaultOwnershipFunctionNames: {
|
|
31
|
+
type: 'boolean',
|
|
32
|
+
default: true,
|
|
33
|
+
description: 'Whether ownershipFunctionNames extends the built-in defaults. Set to false to replace them, or to drop them entirely when no list is given.'
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
additionalProperties: false
|
|
37
|
+
}
|
|
38
|
+
]
|
|
39
|
+
},
|
|
40
|
+
defaultOptions: [{}],
|
|
41
|
+
create(context, [options]) {
|
|
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
|
+
}
|
|
67
|
+
const ownership = {
|
|
68
|
+
sourceCode: context.sourceCode,
|
|
69
|
+
checker,
|
|
70
|
+
services,
|
|
71
|
+
ownershipFunctionNames: (0, disposables_1.resolveNameListOption)(options.ownershipFunctionNames, disposables_1.DEFAULT_OWNERSHIP_FUNCTION_NAMES, options.extendDefaultOwnershipFunctionNames !== false)
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
CallExpression(node) {
|
|
75
|
+
(0, disposables_1.markManagedDisposableUse)(pending, node, ownership);
|
|
76
|
+
},
|
|
77
|
+
ReturnStatement(node) {
|
|
78
|
+
(0, disposables_1.markManagedDisposableUse)(pending, node, ownership);
|
|
79
|
+
},
|
|
80
|
+
AssignmentExpression(node) {
|
|
81
|
+
(0, disposables_1.markManagedDisposableUse)(pending, node, ownership);
|
|
82
|
+
},
|
|
83
|
+
NewExpression(node) {
|
|
84
|
+
(0, disposables_1.markManagedDisposableUse)(pending, node, ownership);
|
|
85
|
+
if ((0, disposables_1.isInJupyterPluginActivate)(node, ownership)) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (!isDisposableCreation(node)) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const assignedVariable = (0, disposables_1.getAssignedVariable)(node, context.sourceCode);
|
|
92
|
+
if (assignedVariable) {
|
|
93
|
+
if ((0, disposables_1.isOuterFunctionScopeVariable)(node, assignedVariable, context.sourceCode) ||
|
|
94
|
+
(0, disposables_1.isExportedVariable)(assignedVariable) ||
|
|
95
|
+
(0, disposables_1.isDisposedByNestedFunction)(assignedVariable, ownership)) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
(0, disposables_1.addPendingDisposable)(pending, assignedVariable, node);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if ((0, disposables_1.isDisposableExpressionManaged)(node, ownership)) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
context.report({ node, messageId: 'unmanagedDisposable' });
|
|
105
|
+
},
|
|
106
|
+
'Program:exit'() {
|
|
107
|
+
for (const [variable, records] of pending) {
|
|
108
|
+
for (const record of records) {
|
|
109
|
+
context.report({
|
|
110
|
+
node: record.node,
|
|
111
|
+
messageId: 'unmanagedDisposableVariable',
|
|
112
|
+
data: { name: variable.name }
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
module.exports = requireDisposableOwnership;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
+
declare const requireDisposableTransfer: ESLintUtils.RuleModule<"unmanagedDisposableVariable" | "unhandledDisposable", [{}], unknown, ESLintUtils.RuleListener> & {
|
|
3
|
+
name: string;
|
|
4
|
+
};
|
|
5
|
+
export = requireDisposableTransfer;
|
|
@@ -0,0 +1,223 @@
|
|
|
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 create_rule_1 = require("../utils/create-rule");
|
|
8
|
+
const disposables_1 = require("../utils/disposables");
|
|
9
|
+
const DEFAULT_IGNORED_RETURN_FUNCTION_NAMES = [
|
|
10
|
+
'_adjacentBar',
|
|
11
|
+
'_currentTabBar',
|
|
12
|
+
'_findWidgetByID',
|
|
13
|
+
'_handleNewSession',
|
|
14
|
+
'add',
|
|
15
|
+
'addTab',
|
|
16
|
+
'addCommand',
|
|
17
|
+
'addCommandToolbarButtonClass',
|
|
18
|
+
'addFileType',
|
|
19
|
+
'addGroup',
|
|
20
|
+
'addItem',
|
|
21
|
+
'addKeyBinding',
|
|
22
|
+
'addToolbarButtonClass',
|
|
23
|
+
'addWidgetExtension',
|
|
24
|
+
'contextForWidget',
|
|
25
|
+
'contextMenuWidget',
|
|
26
|
+
'defaultWidgetFactory',
|
|
27
|
+
'delete',
|
|
28
|
+
'find',
|
|
29
|
+
'findWidget',
|
|
30
|
+
'get',
|
|
31
|
+
'getCurrent',
|
|
32
|
+
'getLanguageServerManager',
|
|
33
|
+
'getLogger',
|
|
34
|
+
'getManager',
|
|
35
|
+
'getModelFactory',
|
|
36
|
+
'getWidgetFactory',
|
|
37
|
+
'initializeState',
|
|
38
|
+
'insertCell',
|
|
39
|
+
'insertItem',
|
|
40
|
+
'insertTab',
|
|
41
|
+
'open',
|
|
42
|
+
'openInspector',
|
|
43
|
+
'openOrReveal',
|
|
44
|
+
'pop',
|
|
45
|
+
'popLastModifiedCell',
|
|
46
|
+
'register',
|
|
47
|
+
'registerItem',
|
|
48
|
+
'registerStatusItem',
|
|
49
|
+
'requestCreateSubshell',
|
|
50
|
+
'requestDebug',
|
|
51
|
+
'requestDeleteSubshell',
|
|
52
|
+
'set',
|
|
53
|
+
'shift',
|
|
54
|
+
'widgetAt',
|
|
55
|
+
'widgetRenderer',
|
|
56
|
+
'transform'
|
|
57
|
+
];
|
|
58
|
+
function isDefaultIgnoredFactoryReturnFunctionName(name) {
|
|
59
|
+
return /^add[A-Za-z0-9_$]*Factory$/.test(name);
|
|
60
|
+
}
|
|
61
|
+
function isLikelyDisposableFactoryCall(node) {
|
|
62
|
+
if ((0, disposables_1.isDisposableSetFactoryCall)(node)) {
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
const name = (0, disposables_1.getCalleeName)(node.callee);
|
|
66
|
+
return name !== null && /^(build|create|make|new)[A-Z0-9_$]/.test(name);
|
|
67
|
+
}
|
|
68
|
+
const requireDisposableTransfer = (0, create_rule_1.createRule)({
|
|
69
|
+
name: 'require-disposable-transfer',
|
|
70
|
+
meta: {
|
|
71
|
+
type: 'problem',
|
|
72
|
+
docs: {
|
|
73
|
+
description: 'Require calls returning IDisposable to be stored, returned, added to a DisposableSet, or disposed',
|
|
74
|
+
url: 'https://eslint-plugin.readthedocs.io/en/latest/rules/require-disposable-transfer/'
|
|
75
|
+
},
|
|
76
|
+
messages: {
|
|
77
|
+
unhandledDisposable: 'IDisposable returned from "{{ name }}" must be stored, returned, added to a DisposableSet, or disposed.',
|
|
78
|
+
unmanagedDisposableVariable: 'IDisposable "{{ name }}" must be returned, added to a DisposableSet, assigned to a field, or disposed.'
|
|
79
|
+
},
|
|
80
|
+
schema: [
|
|
81
|
+
{
|
|
82
|
+
type: 'object',
|
|
83
|
+
properties: {
|
|
84
|
+
ownershipFunctionNames: {
|
|
85
|
+
type: 'array',
|
|
86
|
+
items: { type: 'string' },
|
|
87
|
+
description: 'Function or method names that take ownership of a disposable argument. Added to the built-in defaults unless extendDefaultOwnershipFunctionNames is false.'
|
|
88
|
+
},
|
|
89
|
+
extendDefaultOwnershipFunctionNames: {
|
|
90
|
+
type: 'boolean',
|
|
91
|
+
default: true,
|
|
92
|
+
description: 'Whether ownershipFunctionNames extends the built-in defaults. Set to false to replace them, or to drop them entirely when no list is given.'
|
|
93
|
+
},
|
|
94
|
+
ignoredReturnFunctionNames: {
|
|
95
|
+
type: 'array',
|
|
96
|
+
items: { type: 'string' },
|
|
97
|
+
description: 'Function or method names whose disposable return value is intentionally ignored. Added to the built-in defaults unless extendDefaultIgnoredReturnFunctionNames is false.'
|
|
98
|
+
},
|
|
99
|
+
extendDefaultIgnoredReturnFunctionNames: {
|
|
100
|
+
type: 'boolean',
|
|
101
|
+
default: true,
|
|
102
|
+
description: 'Whether ignoredReturnFunctionNames extends the built-in defaults, including the add*Factory and this._map.set()/delete() exemptions. Set to false to replace them, or to drop them entirely when no list is given.'
|
|
103
|
+
},
|
|
104
|
+
checkAllDisposableReturns: {
|
|
105
|
+
type: 'boolean',
|
|
106
|
+
default: false,
|
|
107
|
+
description: 'Check every call whose return type is disposable, not only factory-named calls (create*, build*, make*, new*).'
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
additionalProperties: false
|
|
111
|
+
}
|
|
112
|
+
]
|
|
113
|
+
},
|
|
114
|
+
defaultOptions: [{}],
|
|
115
|
+
create(context, [options]) {
|
|
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
|
+
}
|
|
126
|
+
function returnsDisposable(node) {
|
|
127
|
+
if ((0, disposables_1.isDisposableSetFactoryCall)(node)) {
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
if (!checker || !services) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
const tsNode = services.esTreeNodeToTSNodeMap.get(node);
|
|
135
|
+
const type = checker.getTypeAtLocation(tsNode);
|
|
136
|
+
return (0, disposables_1.isDisposableType)(type, checker);
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// The two pattern-based exemptions below (`add*Factory`, and mutating a
|
|
143
|
+
// map-like field of `this`) are part of the built-in defaults, so the same
|
|
144
|
+
// flag that governs the default name list governs them too.
|
|
145
|
+
const extendDefaultIgnoredReturnFunctionNames = options.extendDefaultIgnoredReturnFunctionNames !==
|
|
146
|
+
false;
|
|
147
|
+
const ignoredReturnFunctionNames = (0, disposables_1.resolveNameListOption)(options.ignoredReturnFunctionNames, DEFAULT_IGNORED_RETURN_FUNCTION_NAMES, extendDefaultIgnoredReturnFunctionNames);
|
|
148
|
+
function isIgnoredReturn(node) {
|
|
149
|
+
if (extendDefaultIgnoredReturnFunctionNames &&
|
|
150
|
+
(0, disposables_1.isClassFieldCollectionMutationCall)(node, ['delete', 'set'])) {
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
const name = (0, disposables_1.getCalleeName)(node.callee);
|
|
154
|
+
if (!name) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
return (ignoredReturnFunctionNames.has(name) ||
|
|
158
|
+
(extendDefaultIgnoredReturnFunctionNames &&
|
|
159
|
+
isDefaultIgnoredFactoryReturnFunctionName(name)));
|
|
160
|
+
}
|
|
161
|
+
const checkAllDisposableReturns = options.checkAllDisposableReturns === true;
|
|
162
|
+
const ownership = {
|
|
163
|
+
sourceCode: context.sourceCode,
|
|
164
|
+
checker,
|
|
165
|
+
services,
|
|
166
|
+
ownershipFunctionNames: (0, disposables_1.resolveNameListOption)(options.ownershipFunctionNames, disposables_1.DEFAULT_OWNERSHIP_FUNCTION_NAMES, options.extendDefaultOwnershipFunctionNames !== false)
|
|
167
|
+
};
|
|
168
|
+
return {
|
|
169
|
+
CallExpression(node) {
|
|
170
|
+
(0, disposables_1.markManagedDisposableUse)(pending, node, ownership);
|
|
171
|
+
if ((0, disposables_1.isInJupyterPluginActivate)(node, ownership) ||
|
|
172
|
+
isIgnoredReturn(node) ||
|
|
173
|
+
!(0, disposables_1.shouldCheckReturnedDisposable)(node) ||
|
|
174
|
+
(0, disposables_1.isDisposableExpressionManaged)(node, ownership)) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (!checkAllDisposableReturns &&
|
|
178
|
+
!isLikelyDisposableFactoryCall(node)) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (!returnsDisposable(node)) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const assignedVariable = (0, disposables_1.getAssignedVariable)(node, context.sourceCode);
|
|
185
|
+
if (assignedVariable) {
|
|
186
|
+
if ((0, disposables_1.isOuterFunctionScopeVariable)(node, assignedVariable, context.sourceCode) ||
|
|
187
|
+
(0, disposables_1.isExportedVariable)(assignedVariable) ||
|
|
188
|
+
(0, disposables_1.isDisposedByNestedFunction)(assignedVariable, ownership)) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
(0, disposables_1.addPendingDisposable)(pending, assignedVariable, node);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
context.report({
|
|
195
|
+
node,
|
|
196
|
+
messageId: 'unhandledDisposable',
|
|
197
|
+
data: { name: (0, disposables_1.getCalleeName)(node.callee) ?? 'call' }
|
|
198
|
+
});
|
|
199
|
+
},
|
|
200
|
+
ReturnStatement(node) {
|
|
201
|
+
(0, disposables_1.markManagedDisposableUse)(pending, node, ownership);
|
|
202
|
+
},
|
|
203
|
+
AssignmentExpression(node) {
|
|
204
|
+
(0, disposables_1.markManagedDisposableUse)(pending, node, ownership);
|
|
205
|
+
},
|
|
206
|
+
NewExpression(node) {
|
|
207
|
+
(0, disposables_1.markManagedDisposableUse)(pending, node, ownership);
|
|
208
|
+
},
|
|
209
|
+
'Program:exit'() {
|
|
210
|
+
for (const [variable, records] of pending) {
|
|
211
|
+
for (const record of records) {
|
|
212
|
+
context.report({
|
|
213
|
+
node: record.node,
|
|
214
|
+
messageId: 'unmanagedDisposableVariable',
|
|
215
|
+
data: { name: variable.name }
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
module.exports = requireDisposableTransfer;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
+
declare const requireSignalCleanup: ESLintUtils.RuleModule<"serviceOutlivesReceiver", [{
|
|
3
|
+
additionalCleanupMethods: string[];
|
|
4
|
+
longLivedTypes: string[] | undefined;
|
|
5
|
+
}], unknown, ESLintUtils.RuleListener> & {
|
|
6
|
+
name: string;
|
|
7
|
+
};
|
|
8
|
+
export = requireSignalCleanup;
|