@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,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;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
declare const noSchemaEnum: import("@typescript-eslint/utils/ts-eslint").RuleModule<"forbidEnum", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
1
|
+
declare const noSchemaEnum: import("@typescript-eslint/utils/ts-eslint").RuleModule<"forbidEnum" | "requireStringType", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
2
2
|
name: string;
|
|
3
3
|
};
|
|
4
4
|
export = noSchemaEnum;
|
|
@@ -7,16 +7,52 @@ const create_rule_1 = require("../utils/create-rule");
|
|
|
7
7
|
function isSchemaJsonFile(filename) {
|
|
8
8
|
return /[/\\]schema[/\\][^/\\]*\.json$/.test(filename);
|
|
9
9
|
}
|
|
10
|
+
function getProperty(node, name) {
|
|
11
|
+
return node.properties.find(property => {
|
|
12
|
+
if (property.key.type === 'JSONLiteral') {
|
|
13
|
+
return property.key.value === name;
|
|
14
|
+
}
|
|
15
|
+
return property.key.name === name;
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
function hasOnlyStringConstChoices(node) {
|
|
19
|
+
return (node.elements.length > 0 &&
|
|
20
|
+
node.elements.every(element => {
|
|
21
|
+
if (element.type !== 'JSONObjectExpression') {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
const constProperty = getProperty(element, 'const');
|
|
25
|
+
return (constProperty !== undefined &&
|
|
26
|
+
constProperty.value.type === 'JSONLiteral' &&
|
|
27
|
+
typeof constProperty.value.value === 'string');
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
function getStringTypeInsertion(sourceCode, node) {
|
|
31
|
+
const beforeNode = sourceCode.text.slice(0, node.range[0]);
|
|
32
|
+
const lastNewline = beforeNode.lastIndexOf('\n');
|
|
33
|
+
const lineStart = beforeNode.slice(lastNewline + 1);
|
|
34
|
+
if (lastNewline !== -1 && /^[ \t]*$/.test(lineStart)) {
|
|
35
|
+
const lineBreak = sourceCode.text.includes('\r\n') ? '\r\n' : '\n';
|
|
36
|
+
return `"type": "string",${lineBreak}${lineStart}`;
|
|
37
|
+
}
|
|
38
|
+
// The property shares its line with an earlier token, so the new property has
|
|
39
|
+
// to go on that line too. Repeat the spacing that already separates the
|
|
40
|
+
// properties, which keeps a Prettier-formatted single-line object formatted.
|
|
41
|
+
const spacing = /[ \t]*$/.exec(beforeNode)?.[0] ?? '';
|
|
42
|
+
return spacing ? `"type": "string",${spacing}` : '"type":"string",';
|
|
43
|
+
}
|
|
10
44
|
const noSchemaEnum = (0, create_rule_1.createRule)({
|
|
11
45
|
name: 'no-schema-enum',
|
|
12
46
|
meta: {
|
|
13
47
|
type: 'suggestion',
|
|
14
48
|
docs: {
|
|
15
|
-
description: 'Disallow `enum` in settings JSON schema files; use `oneOf` with `const` and `
|
|
49
|
+
description: 'Disallow `enum` in settings JSON schema files; use `oneOf` with `const`, `title`, and `type` instead'
|
|
16
50
|
},
|
|
17
51
|
messages: {
|
|
18
|
-
forbidEnum: 'Avoid `enum` in settings schema. Use `oneOf` with `const` and `title` per entry to support user-facing labels and translations.'
|
|
52
|
+
forbidEnum: 'Avoid `enum` in settings schema. Use `oneOf` with `const` and `title` per entry to support user-facing labels and translations.',
|
|
53
|
+
requireStringType: 'Add `type: "string"` next to `oneOf`; React JSON Schema Form may not render string choices without it.'
|
|
19
54
|
},
|
|
55
|
+
fixable: 'code',
|
|
20
56
|
schema: []
|
|
21
57
|
},
|
|
22
58
|
defaultOptions: [],
|
|
@@ -30,6 +66,22 @@ const noSchemaEnum = (0, create_rule_1.createRule)({
|
|
|
30
66
|
if (keyValue === 'enum' && node.value.type === 'JSONArrayExpression') {
|
|
31
67
|
context.report({ loc: node.loc, messageId: 'forbidEnum' });
|
|
32
68
|
}
|
|
69
|
+
if (keyValue !== 'oneOf' || node.value.type !== 'JSONArrayExpression') {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const schemaNode = node.parent?.type === 'JSONObjectExpression'
|
|
73
|
+
? node.parent
|
|
74
|
+
: undefined;
|
|
75
|
+
if (!schemaNode ||
|
|
76
|
+
getProperty(schemaNode, 'type') ||
|
|
77
|
+
!hasOnlyStringConstChoices(node.value)) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
context.report({
|
|
81
|
+
loc: node.loc,
|
|
82
|
+
messageId: 'requireStringType',
|
|
83
|
+
fix: fixer => fixer.insertTextBeforeRange([node.range[0], node.range[0]], getStringTypeInsertion(context.sourceCode, node))
|
|
84
|
+
});
|
|
33
85
|
}
|
|
34
86
|
};
|
|
35
87
|
}
|
|
@@ -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,6 @@
|
|
|
1
|
+
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
+
import { LazyImportOptions } from '../utils/lazy-imports';
|
|
3
|
+
declare const jupyterPreferLazyImports: ESLintUtils.RuleModule<string, [LazyImportOptions], unknown, ESLintUtils.RuleListener> & {
|
|
4
|
+
name: string;
|
|
5
|
+
};
|
|
6
|
+
export = jupyterPreferLazyImports;
|
|
@@ -0,0 +1,308 @@
|
|
|
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
|
+
ignoreImports: [],
|
|
15
|
+
minimumSize: lazy_imports_1.DEFAULT_MINIMUM_SIZE,
|
|
16
|
+
reportInteractionCallbacks: false,
|
|
17
|
+
reportModuleLevelUsage: false
|
|
18
|
+
};
|
|
19
|
+
const jupyterPreferLazyImports = (0, create_rule_1.createRule)({
|
|
20
|
+
name: 'prefer-lazy-imports',
|
|
21
|
+
meta: {
|
|
22
|
+
type: 'suggestion',
|
|
23
|
+
docs: {
|
|
24
|
+
description: 'Prefer deferred imports for heavy dependencies of JupyterLab plugins',
|
|
25
|
+
url: 'https://eslint-plugin.readthedocs.io/en/latest/rules/prefer-lazy-imports/'
|
|
26
|
+
},
|
|
27
|
+
messages: {
|
|
28
|
+
preferLazyImport: "'{{ source }}' is imported at the top of a plugin module but only used inside functions, so it loads before the application starts. " +
|
|
29
|
+
'Import it where it is used instead: `{{ snippet }}`',
|
|
30
|
+
eagerModuleLevelUse: "'{{ source }}' is used at module level in a plugin module, so it loads before the application starts. " +
|
|
31
|
+
"Move the usage into a function and import it there with `await import('{{ source }}')`.",
|
|
32
|
+
preferLazyImportInteraction: "'{{ source }}' is only used inside user-interaction handlers, so it is not needed until the user acts. " +
|
|
33
|
+
'Import it where it is used instead: `{{ snippet }}`'
|
|
34
|
+
},
|
|
35
|
+
schema: [
|
|
36
|
+
{
|
|
37
|
+
type: 'object',
|
|
38
|
+
properties: {
|
|
39
|
+
allowedPackages: {
|
|
40
|
+
type: 'array',
|
|
41
|
+
items: { type: 'string' },
|
|
42
|
+
default: lazy_imports_1.DEFAULT_ALLOWED_PACKAGES,
|
|
43
|
+
description: 'Packages already loaded eagerly by the application, which are therefore free to import at the top of a plugin module. Supports `*` wildcards, and `!` to deny a package whatever else in the list matches it. Replaces the default list.'
|
|
44
|
+
},
|
|
45
|
+
ignoreImports: {
|
|
46
|
+
type: 'array',
|
|
47
|
+
items: { type: 'string' },
|
|
48
|
+
default: [],
|
|
49
|
+
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`.'
|
|
50
|
+
},
|
|
51
|
+
minimumSize: {
|
|
52
|
+
type: 'number',
|
|
53
|
+
minimum: 0,
|
|
54
|
+
default: lazy_imports_1.DEFAULT_MINIMUM_SIZE,
|
|
55
|
+
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.'
|
|
56
|
+
},
|
|
57
|
+
reportInteractionCallbacks: {
|
|
58
|
+
type: 'boolean',
|
|
59
|
+
default: false,
|
|
60
|
+
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`.'
|
|
61
|
+
},
|
|
62
|
+
reportModuleLevelUsage: {
|
|
63
|
+
type: 'boolean',
|
|
64
|
+
default: false,
|
|
65
|
+
description: 'Also report imports used at module level, excluding tokens referenced in `requires`, `optional` and `provides`.'
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
additionalProperties: false
|
|
69
|
+
}
|
|
70
|
+
]
|
|
71
|
+
},
|
|
72
|
+
defaultOptions: [DEFAULT_OPTIONS],
|
|
73
|
+
create(context, [options]) {
|
|
74
|
+
const { allowedPackages, ignoreImports, minimumSize, reportInteractionCallbacks, reportModuleLevelUsage } = options;
|
|
75
|
+
let services = null;
|
|
76
|
+
let checker = null;
|
|
77
|
+
try {
|
|
78
|
+
services = utils_1.ESLintUtils.getParserServices(context, true);
|
|
79
|
+
checker = services.program ? services.program.getTypeChecker() : null;
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
// Parser services unavailable (non-TS file or no type information)
|
|
83
|
+
services = null;
|
|
84
|
+
}
|
|
85
|
+
const getTSNode = services
|
|
86
|
+
? (node) => services?.esTreeNodeToTSNodeMap.get(node)
|
|
87
|
+
: null;
|
|
88
|
+
let isPluginModule = false;
|
|
89
|
+
const importDeclarations = [];
|
|
90
|
+
// Sources kept in the startup bundle by a value re-export.
|
|
91
|
+
const reExportedSources = new Set();
|
|
92
|
+
function mentionsPluginType(typeNode) {
|
|
93
|
+
return (0, plugin_utils_1.typeMentionsJupyterPlugin)(typeNode, checker, getTSNode);
|
|
94
|
+
}
|
|
95
|
+
// Packages this extension declares as provided by the application, read
|
|
96
|
+
// from `jupyterlab.sharedPackages` in its own manifest. They extend
|
|
97
|
+
// `allowedPackages` rather than replacing it, and are only looked up once
|
|
98
|
+
// the file turns out to need checking.
|
|
99
|
+
let hostProvided = null;
|
|
100
|
+
/**
|
|
101
|
+
* Returns true when the specifier is exempt from the rule, either because
|
|
102
|
+
* the application loads it eagerly anyway or because it was ignored
|
|
103
|
+
* explicitly.
|
|
104
|
+
*/
|
|
105
|
+
function isExempt(source) {
|
|
106
|
+
return ((0, lazy_imports_1.matchesPatterns)(source, lazy_imports_1.ALWAYS_IGNORED_IMPORTS) ||
|
|
107
|
+
(0, lazy_imports_1.matchesPatterns)(source, allowedPackages) ||
|
|
108
|
+
(0, lazy_imports_1.matchesPatterns)(source, hostProvided ?? []) ||
|
|
109
|
+
(0, lazy_imports_1.matchesPatterns)(source, ignoreImports));
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Returns true when the module holds too little code for a separate chunk
|
|
113
|
+
* to pay off. Only relative imports can be measured; a package which is not
|
|
114
|
+
* in the shared runtime is bundled into the extension, so it always counts
|
|
115
|
+
* as worth deferring. An unreadable module counts as worth deferring too,
|
|
116
|
+
* so a missing file never hides a finding.
|
|
117
|
+
*/
|
|
118
|
+
function isTooSmall(source) {
|
|
119
|
+
if (minimumSize <= 0) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
const resolved = (0, module_size_1.resolveRelativeModule)(source, context.filename);
|
|
123
|
+
if (!resolved) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
const size = (0, module_size_1.getTransitiveCodeSize)(resolved);
|
|
127
|
+
return size !== null && size < minimumSize;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Returns true when an identifier sits inside `typeof X`, which TypeScript
|
|
131
|
+
* erases even though the scope manager records it as a value reference.
|
|
132
|
+
*/
|
|
133
|
+
function isInTypeQuery(node) {
|
|
134
|
+
let current = node;
|
|
135
|
+
while (current && current.type !== 'Program') {
|
|
136
|
+
if (current.type === 'TSTypeQuery') {
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
current = current.parent;
|
|
140
|
+
}
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Collects the value references of every runtime binding of an import
|
|
145
|
+
* declaration. Type-only specifiers and type positions are left out
|
|
146
|
+
* because TypeScript erases them.
|
|
147
|
+
*/
|
|
148
|
+
function getValueReferences(declaration) {
|
|
149
|
+
const references = [];
|
|
150
|
+
for (const specifier of declaration.specifiers) {
|
|
151
|
+
if (specifier.type === 'ImportSpecifier' &&
|
|
152
|
+
specifier.importKind === 'type') {
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
for (const variable of context.sourceCode.getDeclaredVariables(specifier)) {
|
|
156
|
+
for (const reference of variable.references) {
|
|
157
|
+
// `isValueReference` comes from the typescript-eslint scope
|
|
158
|
+
// manager. Under the default parser there are no type references,
|
|
159
|
+
// so every reference is a value reference.
|
|
160
|
+
const isValue = reference.isValueReference ?? true;
|
|
161
|
+
if (isValue && !isInTypeQuery(reference.identifier)) {
|
|
162
|
+
references.push(reference);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return references;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Checks every declaration importing one source together. A source pulled
|
|
171
|
+
* in eagerly by any one of them is already in the startup bundle, so
|
|
172
|
+
* deferring the others would not remove it.
|
|
173
|
+
*/
|
|
174
|
+
function checkSource(source, declarations) {
|
|
175
|
+
if (isExempt(source) || reExportedSources.has(source)) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const references = declarations.flatMap(getValueReferences);
|
|
179
|
+
if (references.length === 0) {
|
|
180
|
+
// Unused, or used only in type positions which TypeScript erases.
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (isTooSmall(source)) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (!isPluginModule) {
|
|
187
|
+
// Outside a plugin module an ordinary function proves nothing, since
|
|
188
|
+
// it may run while the application starts. Only a position which
|
|
189
|
+
// provably waits for the user shows the import can load later.
|
|
190
|
+
if (references.every(({ identifier }) => (0, lazy_imports_1.isInInteractionCallback)(identifier))) {
|
|
191
|
+
context.report({
|
|
192
|
+
node: declarations[0],
|
|
193
|
+
messageId: 'preferLazyImportInteraction',
|
|
194
|
+
data: {
|
|
195
|
+
source,
|
|
196
|
+
snippet: (0, lazy_imports_1.buildDeferredImportSnippet)(declarations)
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
let deferrable = 0;
|
|
203
|
+
let eager = 0;
|
|
204
|
+
let tokenList = 0;
|
|
205
|
+
for (const { identifier } of references) {
|
|
206
|
+
if ((0, lazy_imports_1.isInPluginTokenList)(identifier)) {
|
|
207
|
+
// A token in `requires`, `optional` or `provides` is read when the
|
|
208
|
+
// plugin is registered, so it can never be deferred.
|
|
209
|
+
tokenList += 1;
|
|
210
|
+
}
|
|
211
|
+
else if ((0, lazy_imports_1.isEagerlyReached)(identifier, context.sourceCode)) {
|
|
212
|
+
eager += 1;
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
deferrable += 1;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if (tokenList === 0 && eager === 0 && deferrable > 0) {
|
|
219
|
+
context.report({
|
|
220
|
+
node: declarations[0],
|
|
221
|
+
messageId: 'preferLazyImport',
|
|
222
|
+
data: {
|
|
223
|
+
source,
|
|
224
|
+
snippet: (0, lazy_imports_1.buildDeferredImportSnippet)(declarations)
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (reportModuleLevelUsage && eager > 0 && tokenList === 0) {
|
|
230
|
+
context.report({
|
|
231
|
+
node: declarations[0],
|
|
232
|
+
messageId: 'eagerModuleLevelUse',
|
|
233
|
+
data: { source }
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return {
|
|
238
|
+
VariableDeclarator(node) {
|
|
239
|
+
if (!isPluginModule &&
|
|
240
|
+
node.id.type === 'Identifier' &&
|
|
241
|
+
mentionsPluginType(node.id.typeAnnotation?.typeAnnotation)) {
|
|
242
|
+
isPluginModule = true;
|
|
243
|
+
}
|
|
244
|
+
},
|
|
245
|
+
'TSAsExpression, TSSatisfiesExpression'(node) {
|
|
246
|
+
if (!isPluginModule && mentionsPluginType(node.typeAnnotation)) {
|
|
247
|
+
isPluginModule = true;
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
'FunctionDeclaration, FunctionExpression, ArrowFunctionExpression'(node) {
|
|
251
|
+
if (!isPluginModule &&
|
|
252
|
+
mentionsPluginType(node.returnType?.typeAnnotation)) {
|
|
253
|
+
isPluginModule = true;
|
|
254
|
+
}
|
|
255
|
+
},
|
|
256
|
+
ObjectExpression(node) {
|
|
257
|
+
if (!isPluginModule && (0, plugin_utils_1.looksLikePluginObject)(node)) {
|
|
258
|
+
isPluginModule = true;
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
ImportDeclaration(node) {
|
|
262
|
+
if (node.parent.type === 'Program' &&
|
|
263
|
+
node.importKind !== 'type' &&
|
|
264
|
+
// A side-effect import has no binding to move into a function.
|
|
265
|
+
node.specifiers.length > 0) {
|
|
266
|
+
importDeclarations.push(node);
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
ExportNamedDeclaration(node) {
|
|
270
|
+
// A value re-export keeps the source in the startup bundle, so nothing
|
|
271
|
+
// is gained by deferring the import of it.
|
|
272
|
+
if (!node.source || node.exportKind === 'type') {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
const hasValueSpecifier = node.specifiers.some(specifier => specifier.exportKind !== 'type');
|
|
276
|
+
if (hasValueSpecifier) {
|
|
277
|
+
reExportedSources.add(node.source.value);
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
ExportAllDeclaration(node) {
|
|
281
|
+
if (node.exportKind !== 'type') {
|
|
282
|
+
reExportedSources.add(node.source.value);
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
'Program:exit'() {
|
|
286
|
+
if (!isPluginModule && !reportInteractionCallbacks) {
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
hostProvided = (0, shared_packages_1.getHostProvidedPackages)(context.filename);
|
|
290
|
+
const bySource = new Map();
|
|
291
|
+
for (const declaration of importDeclarations) {
|
|
292
|
+
const source = declaration.source.value;
|
|
293
|
+
const group = bySource.get(source);
|
|
294
|
+
if (group) {
|
|
295
|
+
group.push(declaration);
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
bySource.set(source, [declaration]);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
for (const [source, declarations] of bySource) {
|
|
302
|
+
checkSource(source, declarations);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
module.exports = jupyterPreferLazyImports;
|
|
@@ -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;
|