@jupyter/eslint-plugin 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +68 -0
- package/lib/index.js +42 -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/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-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-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/playwright-selectors.d.ts +66 -0
- package/lib/utils/playwright-selectors.js +193 -0
- package/lib/utils/signal-lifetime.d.ts +158 -0
- package/lib/utils/signal-lifetime.js +755 -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,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) Jupyter Development Team.
|
|
4
|
+
* Distributed under the terms of the Modified BSD License.
|
|
5
|
+
*/
|
|
6
|
+
const create_rule_1 = require("../utils/create-rule");
|
|
7
|
+
const jupyterNoPageconfigBaseUrl = (0, create_rule_1.createRule)({
|
|
8
|
+
name: 'no-pageconfig-base-url',
|
|
9
|
+
meta: {
|
|
10
|
+
type: 'suggestion',
|
|
11
|
+
docs: {
|
|
12
|
+
description: 'Disallow PageConfig.getBaseUrl() outside of makeSettings()',
|
|
13
|
+
url: 'https://eslint-plugin.readthedocs.io/en/latest/rules/no-pageconfig-base-url/'
|
|
14
|
+
},
|
|
15
|
+
messages: {
|
|
16
|
+
noPageconfigBaseUrl: 'PageConfig.getBaseUrl() should not be called outside of makeSettings(). ' +
|
|
17
|
+
'Store ServerConnection.ISettings and access .baseUrl from it each time.'
|
|
18
|
+
},
|
|
19
|
+
schema: []
|
|
20
|
+
},
|
|
21
|
+
defaultOptions: [],
|
|
22
|
+
create(context) {
|
|
23
|
+
return {
|
|
24
|
+
CallExpression(node) {
|
|
25
|
+
const { callee } = node;
|
|
26
|
+
if (callee.type === 'MemberExpression' &&
|
|
27
|
+
callee.object.type === 'Identifier' &&
|
|
28
|
+
callee.object.name === 'PageConfig' &&
|
|
29
|
+
callee.property.type === 'Identifier' &&
|
|
30
|
+
callee.property.name === 'getBaseUrl') {
|
|
31
|
+
context.report({ node, messageId: 'noPageconfigBaseUrl' });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
module.exports = jupyterNoPageconfigBaseUrl;
|
|
@@ -4,99 +4,55 @@
|
|
|
4
4
|
* Distributed under the terms of the Modified BSD License.
|
|
5
5
|
*/
|
|
6
6
|
const create_rule_1 = require("../utils/create-rule");
|
|
7
|
-
const
|
|
7
|
+
const translation_1 = require("../utils/translation");
|
|
8
8
|
/**
|
|
9
|
-
* Returns true when
|
|
10
|
-
*
|
|
9
|
+
* Returns true when the expression is text the string extractor can read
|
|
10
|
+
* straight from the source: a quoted string, a template literal with nothing
|
|
11
|
+
* interpolated into it, or a `+` tree of those.
|
|
12
|
+
*
|
|
13
|
+
* Concatenating literals is only ever a source-formatting choice — the
|
|
14
|
+
* extractor still sees the whole message — so it stays allowed.
|
|
11
15
|
*/
|
|
12
|
-
function
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
function isStaticString(node) {
|
|
17
|
+
const expression = (0, translation_1.unwrapExpression)(node);
|
|
18
|
+
if (expression.type === 'Literal') {
|
|
19
|
+
return typeof expression.value === 'string';
|
|
15
20
|
}
|
|
16
|
-
if (
|
|
17
|
-
return
|
|
21
|
+
if (expression.type === 'TemplateLiteral') {
|
|
22
|
+
return expression.expressions.length === 0;
|
|
18
23
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Returns the first `+` BinaryExpression found anywhere in the subtree that
|
|
23
|
-
* involves a non-literal operand, or null if none exists.
|
|
24
|
-
*/
|
|
25
|
-
function findConcatenation(node) {
|
|
26
|
-
if (node.type === 'BinaryExpression' && node.operator === '+') {
|
|
27
|
-
return isAllStringLiterals(node) ? null : node;
|
|
28
|
-
}
|
|
29
|
-
const record = node;
|
|
30
|
-
for (const key of Object.keys(record)) {
|
|
31
|
-
if (key === 'type' || key === 'loc' || key === 'range' || key === 'parent') {
|
|
32
|
-
continue;
|
|
33
|
-
}
|
|
34
|
-
const child = record[key];
|
|
35
|
-
if (child && typeof child === 'object') {
|
|
36
|
-
if (Array.isArray(child)) {
|
|
37
|
-
for (const item of child) {
|
|
38
|
-
if (item && typeof item.type === 'string') {
|
|
39
|
-
const found = findConcatenation(item);
|
|
40
|
-
if (found)
|
|
41
|
-
return found;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
else if (typeof child.type === 'string') {
|
|
46
|
-
const found = findConcatenation(child);
|
|
47
|
-
if (found)
|
|
48
|
-
return found;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
24
|
+
if (expression.type === 'BinaryExpression' && expression.operator === '+') {
|
|
25
|
+
return isStaticString(expression.left) && isStaticString(expression.right);
|
|
51
26
|
}
|
|
52
|
-
return
|
|
27
|
+
return false;
|
|
53
28
|
}
|
|
54
29
|
/**
|
|
55
|
-
* Returns
|
|
56
|
-
*
|
|
57
|
-
*
|
|
30
|
+
* Returns the message argument's `+` expression when it concatenates something
|
|
31
|
+
* the extractor cannot read, or null otherwise.
|
|
32
|
+
*
|
|
33
|
+
* Only the argument's own top-level form is inspected. A `+` buried inside a
|
|
34
|
+
* larger expression — `('Delete ' + name).trim()`, `xs.map(x => 'p' + x)` —
|
|
35
|
+
* is not what reaches the message slot, so reporting it here would point at
|
|
36
|
+
* the wrong node and duplicate what `no-dynamic-translation` already says
|
|
37
|
+
* about the argument as a whole.
|
|
58
38
|
*/
|
|
59
|
-
function
|
|
60
|
-
|
|
61
|
-
|
|
39
|
+
function getDynamicConcatenation(node) {
|
|
40
|
+
const expression = (0, translation_1.unwrapExpression)(node);
|
|
41
|
+
if (expression.type !== 'BinaryExpression' || expression.operator !== '+') {
|
|
42
|
+
return null;
|
|
62
43
|
}
|
|
63
|
-
|
|
64
|
-
const prop = node.property;
|
|
65
|
-
if (prop.type !== 'Identifier') {
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
// this.trans / this._trans
|
|
69
|
-
if (node.object.type === 'ThisExpression') {
|
|
70
|
-
return prop.name === 'trans' || prop.name === '_trans';
|
|
71
|
-
}
|
|
72
|
-
// props.trans
|
|
73
|
-
if (prop.name === 'trans' &&
|
|
74
|
-
node.object.type === 'Identifier' &&
|
|
75
|
-
node.object.name === 'props') {
|
|
76
|
-
return true;
|
|
77
|
-
}
|
|
78
|
-
// this.props.trans
|
|
79
|
-
if (prop.name === 'trans' &&
|
|
80
|
-
node.object.type === 'MemberExpression' &&
|
|
81
|
-
!node.object.computed &&
|
|
82
|
-
node.object.object.type === 'ThisExpression' &&
|
|
83
|
-
node.object.property.type === 'Identifier' &&
|
|
84
|
-
node.object.property.name === 'props') {
|
|
85
|
-
return true;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return false;
|
|
44
|
+
return isStaticString(expression) ? null : expression;
|
|
89
45
|
}
|
|
90
46
|
const noTranslationConcatenation = (0, create_rule_1.createRule)({
|
|
91
47
|
name: 'no-translation-concatenation',
|
|
92
48
|
meta: {
|
|
93
49
|
type: 'problem',
|
|
94
50
|
docs: {
|
|
95
|
-
description: 'Forbid
|
|
51
|
+
description: 'Forbid concatenating dynamic values into translation messages',
|
|
96
52
|
url: 'https://eslint-plugin.readthedocs.io/en/latest/rules/no-translation-concatenation/'
|
|
97
53
|
},
|
|
98
54
|
messages: {
|
|
99
|
-
noConcatenation: 'Do not
|
|
55
|
+
noConcatenation: 'Do not concatenate values into translation strings; the extractor only reads the literal parts. Use a placeholder instead, e.g. trans.__("Hello %1", name).'
|
|
100
56
|
},
|
|
101
57
|
schema: []
|
|
102
58
|
},
|
|
@@ -111,19 +67,25 @@ const noTranslationConcatenation = (0, create_rule_1.createRule)({
|
|
|
111
67
|
if (callee.computed || callee.property.type !== 'Identifier') {
|
|
112
68
|
return;
|
|
113
69
|
}
|
|
114
|
-
|
|
70
|
+
const method = callee.property.name;
|
|
71
|
+
if (!translation_1.BUNDLE_METHODS.has(method)) {
|
|
115
72
|
return;
|
|
116
73
|
}
|
|
117
|
-
if (!isTransBundle(callee.object)) {
|
|
74
|
+
if (!(0, translation_1.isTransBundle)(callee.object)) {
|
|
118
75
|
return;
|
|
119
76
|
}
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
77
|
+
for (const index of translation_1.STATIC_ARGUMENT_INDICES[method]) {
|
|
78
|
+
const argument = node.arguments[index];
|
|
79
|
+
if (!argument) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const concatenation = getDynamicConcatenation(argument);
|
|
83
|
+
if (concatenation) {
|
|
84
|
+
context.report({
|
|
85
|
+
node: concatenation,
|
|
86
|
+
messageId: 'noConcatenation'
|
|
87
|
+
});
|
|
88
|
+
}
|
|
127
89
|
}
|
|
128
90
|
}
|
|
129
91
|
};
|
|
@@ -37,7 +37,7 @@ function isSetAttributeCall(node) {
|
|
|
37
37
|
node.callee.property.name === 'setAttribute');
|
|
38
38
|
}
|
|
39
39
|
function isShowDialogCall(node) {
|
|
40
|
-
return
|
|
40
|
+
return node.callee.type === 'Identifier' && node.callee.name === 'showDialog';
|
|
41
41
|
}
|
|
42
42
|
function isDialogConstructor(node) {
|
|
43
43
|
return node.callee.type === 'Identifier' && node.callee.name === 'Dialog';
|
|
@@ -222,7 +222,8 @@ const noUntranslatedString = (0, create_rule_1.createRule)({
|
|
|
222
222
|
return;
|
|
223
223
|
}
|
|
224
224
|
// *.title.label = STRING or *.title.caption = STRING
|
|
225
|
-
if ((left.property.name === 'label' ||
|
|
225
|
+
if ((left.property.name === 'label' ||
|
|
226
|
+
left.property.name === 'caption') &&
|
|
226
227
|
left.object.type === 'MemberExpression' &&
|
|
227
228
|
!left.object.computed &&
|
|
228
229
|
left.object.property.type === 'Identifier' &&
|
|
@@ -253,7 +254,8 @@ const noUntranslatedString = (0, create_rule_1.createRule)({
|
|
|
253
254
|
},
|
|
254
255
|
// Raw text between JSX tags: <span>Untranslated text</span>
|
|
255
256
|
JSXText(node) {
|
|
256
|
-
if (node.value.trim().length > 0 &&
|
|
257
|
+
if (node.value.trim().length > 0 &&
|
|
258
|
+
(enforcePunctuation || hasLetters(node.value))) {
|
|
257
259
|
context.report({
|
|
258
260
|
node,
|
|
259
261
|
messageId: 'untranslatedJsxText'
|
|
@@ -282,7 +284,8 @@ const noUntranslatedString = (0, create_rule_1.createRule)({
|
|
|
282
284
|
}
|
|
283
285
|
if (isRawStringNode(node.expression)) {
|
|
284
286
|
const value = getRawStringValue(node.expression);
|
|
285
|
-
if (value !== null &&
|
|
287
|
+
if (value !== null &&
|
|
288
|
+
(enforcePunctuation ? value.trim().length > 0 : hasLetters(value))) {
|
|
286
289
|
context.report({
|
|
287
290
|
node: node.expression,
|
|
288
291
|
messageId: 'untranslatedJsxText'
|
|
@@ -55,8 +55,7 @@ function findActivateFunction(node) {
|
|
|
55
55
|
// Handle both arrow functions and regular functions
|
|
56
56
|
if (activateValue.type === 'ArrowFunctionExpression' ||
|
|
57
57
|
activateValue.type === 'FunctionExpression') {
|
|
58
|
-
const params = activateValue.params
|
|
59
|
-
.filter((param) => param.type === 'Identifier');
|
|
58
|
+
const params = activateValue.params.filter((param) => param.type === 'Identifier');
|
|
60
59
|
const paramNames = params.map(param => param.name);
|
|
61
60
|
const paramTypes = params.map(param => param.type === 'Identifier' ? (0, plugin_utils_1.extractParameterType)(param) : null);
|
|
62
61
|
const paramNodes = params.map(param => param.type === 'Identifier' ? param : null);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
+
declare const preferSignalThisArg: ESLintUtils.RuleModule<"addThisArg" | "preferThisArg" | "boundCallback" | "unbindAndAddThisArg", [{
|
|
3
|
+
longLivedTypes: string[] | undefined;
|
|
4
|
+
}], unknown, ESLintUtils.RuleListener> & {
|
|
5
|
+
name: string;
|
|
6
|
+
};
|
|
7
|
+
export = preferSignalThisArg;
|
|
@@ -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;
|