@jupyter/eslint-plugin 1.3.0 → 1.5.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/README.md +12 -0
- package/lib/index.d.ts +6 -207
- package/lib/index.js +49 -28
- package/lib/rules/command-described-by.js +18 -27
- package/lib/rules/command-id-convention.d.ts +5 -0
- package/lib/rules/command-id-convention.js +87 -0
- package/lib/rules/no-untranslated-string.d.ts +2 -1
- package/lib/rules/no-untranslated-string.js +11 -3
- package/lib/rules/plugin-activation-args.d.ts +3 -3
- package/lib/rules/plugin-activation-args.js +183 -142
- package/lib/rules/plugin-description.js +12 -9
- package/lib/rules/plugin-id-convention.d.ts +2 -3
- package/lib/rules/plugin-id-convention.js +10 -107
- package/lib/rules/prefer-lazy-imports.d.ts +2 -2
- package/lib/rules/prefer-lazy-imports.js +59 -28
- package/lib/rules/prefer-signal-this-arg.d.ts +3 -3
- package/lib/rules/prefer-signal-this-arg.js +12 -13
- package/lib/rules/require-disposable-ownership.d.ts +1 -2
- package/lib/rules/require-disposable-ownership.js +2 -10
- package/lib/rules/require-disposable-transfer.d.ts +1 -2
- package/lib/rules/require-disposable-transfer.js +7 -12
- package/lib/rules/require-signal-cleanup.d.ts +3 -3
- package/lib/rules/require-signal-cleanup.js +12 -13
- package/lib/rules/require-signal-this-arg.d.ts +1 -2
- package/lib/rules/require-signal-this-arg.js +2 -10
- package/lib/rules/token-format.js +8 -2
- package/lib/utils/ast.d.ts +1 -1
- package/lib/utils/commands.d.ts +17 -1
- package/lib/utils/commands.js +205 -0
- package/lib/utils/disposables.d.ts +2 -13
- package/lib/utils/disposables.js +3 -18
- package/lib/utils/lazy-imports.d.ts +2 -2
- package/lib/utils/lazy-imports.js +10 -1
- package/lib/utils/options.d.ts +9 -0
- package/lib/utils/options.js +22 -0
- package/lib/utils/plugin-utils.d.ts +39 -10
- package/lib/utils/plugin-utils.js +376 -25
- package/lib/utils/signal-lifetime.d.ts +1 -2
- package/lib/utils/signals.d.ts +1 -2
- package/lib/utils/translation.d.ts +1 -1
- package/lib/utils/type-services.d.ts +12 -0
- package/lib/utils/type-services.js +27 -0
- package/package.json +4 -10
|
@@ -36,15 +36,46 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
36
36
|
return result;
|
|
37
37
|
};
|
|
38
38
|
})();
|
|
39
|
-
const utils_1 = require("@typescript-eslint/utils");
|
|
40
39
|
const ts = __importStar(require("typescript"));
|
|
41
40
|
const plugin_utils_1 = require("../utils/plugin-utils");
|
|
41
|
+
const type_services_1 = require("../utils/type-services");
|
|
42
42
|
const create_rule_1 = require("../utils/create-rule");
|
|
43
43
|
const DEFAULT_ALLOWED_FIRST_ARGUMENT_NAMES = ['app', '_app', '_'];
|
|
44
|
+
function unwrapParentheses(node) {
|
|
45
|
+
let current = node;
|
|
46
|
+
while (current.type === 'TSAsExpression' ||
|
|
47
|
+
current.type === 'TSTypeAssertion' ||
|
|
48
|
+
current.type === 'TSSatisfiesExpression') {
|
|
49
|
+
current = current.expression;
|
|
50
|
+
}
|
|
51
|
+
return current;
|
|
52
|
+
}
|
|
53
|
+
function extractFunctionInfo(node, fn) {
|
|
54
|
+
const paramNames = [];
|
|
55
|
+
const paramTypes = [];
|
|
56
|
+
const paramNodes = [];
|
|
57
|
+
for (const param of fn.params) {
|
|
58
|
+
const id = param.type === 'AssignmentPattern' ? param.left : param;
|
|
59
|
+
if (id.type !== 'Identifier') {
|
|
60
|
+
// A destructured parameter has no name to check, and a rest parameter
|
|
61
|
+
// has no fixed position, so leave those functions alone.
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
paramNames.push(id.name);
|
|
65
|
+
paramTypes.push((0, plugin_utils_1.extractParameterType)(id));
|
|
66
|
+
paramNodes.push(id);
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
node,
|
|
70
|
+
paramNames,
|
|
71
|
+
paramTypes,
|
|
72
|
+
paramNodes
|
|
73
|
+
};
|
|
74
|
+
}
|
|
44
75
|
/**
|
|
45
76
|
* Finds the activate function in the plugin object
|
|
46
77
|
*/
|
|
47
|
-
function findActivateFunction(node) {
|
|
78
|
+
function findActivateFunction(node, sourceCode) {
|
|
48
79
|
const activateProp = node.properties.find((prop) => prop.type === 'Property' &&
|
|
49
80
|
((prop.key.type === 'Identifier' && prop.key.name === 'activate') ||
|
|
50
81
|
(prop.key.type === 'Literal' && prop.key.value === 'activate')));
|
|
@@ -52,19 +83,38 @@ function findActivateFunction(node) {
|
|
|
52
83
|
return null;
|
|
53
84
|
}
|
|
54
85
|
const activateValue = activateProp.value;
|
|
55
|
-
// Handle both arrow functions and regular functions
|
|
86
|
+
// Handle both arrow functions and regular functions inline
|
|
56
87
|
if (activateValue.type === 'ArrowFunctionExpression' ||
|
|
57
88
|
activateValue.type === 'FunctionExpression') {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
89
|
+
return extractFunctionInfo(activateProp, activateValue);
|
|
90
|
+
}
|
|
91
|
+
// Handle referenced functions: `activate` or `activate: activatePlugin`
|
|
92
|
+
if (activateValue.type === 'Identifier' && sourceCode) {
|
|
93
|
+
const scope = sourceCode.getScope(activateValue);
|
|
94
|
+
let currentScope = scope;
|
|
95
|
+
let variable = null;
|
|
96
|
+
while (currentScope) {
|
|
97
|
+
const found = currentScope.set.get(activateValue.name);
|
|
98
|
+
if (found) {
|
|
99
|
+
variable = found;
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
currentScope = currentScope.upper;
|
|
103
|
+
}
|
|
104
|
+
if (variable && variable.defs.length > 0) {
|
|
105
|
+
const def = variable.defs[0];
|
|
106
|
+
if (def.type === 'FunctionName' &&
|
|
107
|
+
def.node.type === 'FunctionDeclaration') {
|
|
108
|
+
return extractFunctionInfo(activateProp, def.node);
|
|
109
|
+
}
|
|
110
|
+
if (def.type === 'Variable' && def.node.init) {
|
|
111
|
+
const init = unwrapParentheses(def.node.init);
|
|
112
|
+
if (init.type === 'ArrowFunctionExpression' ||
|
|
113
|
+
init.type === 'FunctionExpression') {
|
|
114
|
+
return extractFunctionInfo(activateProp, init);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
68
118
|
}
|
|
69
119
|
return null;
|
|
70
120
|
}
|
|
@@ -152,17 +202,7 @@ const jupyterPluginActivationArgs = (0, create_rule_1.createRule)({
|
|
|
152
202
|
],
|
|
153
203
|
create(context, [options]) {
|
|
154
204
|
const allowedFirstArgumentNames = options.allowedFirstArgumentNames || DEFAULT_ALLOWED_FIRST_ARGUMENT_NAMES;
|
|
155
|
-
|
|
156
|
-
let services = null;
|
|
157
|
-
let checker = null;
|
|
158
|
-
try {
|
|
159
|
-
services = utils_1.ESLintUtils.getParserServices(context, true);
|
|
160
|
-
checker = services.program ? services.program.getTypeChecker() : null;
|
|
161
|
-
}
|
|
162
|
-
catch {
|
|
163
|
-
// Parser services unavailable (e.g., non-TS file or misconfigured parser)
|
|
164
|
-
services = null;
|
|
165
|
-
}
|
|
205
|
+
const { services, checker, getTSNode } = (0, type_services_1.getTypeServices)(context);
|
|
166
206
|
/**
|
|
167
207
|
* Given the AST node for a token in requires/optional, uses the TypeScript checker to extract T from
|
|
168
208
|
* `Token<T>` and returns the TypeScript type object for T.
|
|
@@ -290,156 +330,157 @@ const jupyterPluginActivationArgs = (0, create_rule_1.createRule)({
|
|
|
290
330
|
return [false, false];
|
|
291
331
|
}
|
|
292
332
|
return {
|
|
293
|
-
|
|
294
|
-
const
|
|
295
|
-
if (!
|
|
333
|
+
ObjectExpression(pluginObj) {
|
|
334
|
+
const hasId = (0, plugin_utils_1.getObjectProperties)(pluginObj).has('id');
|
|
335
|
+
if (!hasId) {
|
|
296
336
|
return;
|
|
297
337
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
338
|
+
const pluginKind = (0, plugin_utils_1.getPluginObjectKind)(pluginObj, checker, getTSNode);
|
|
339
|
+
if (pluginKind !== 'frontend' && pluginKind !== 'service-manager') {
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
const { requires, optional } = extractRequiresOptional(pluginObj);
|
|
343
|
+
const activateInfo = findActivateFunction(pluginObj, context.sourceCode);
|
|
344
|
+
if (!activateInfo) {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
const { paramNames, paramTypes, paramNodes } = activateInfo;
|
|
348
|
+
const expectedCount = 1 + requires.length + optional.length;
|
|
349
|
+
const expectedTokensWithoutApp = [...requires, ...optional];
|
|
350
|
+
if (expectedCount === 1 && paramNames.length === 0) {
|
|
351
|
+
// Special case, not invalid
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
if (pluginKind === 'frontend') {
|
|
355
|
+
// Validation 1a: Check if first argument is one of the allowed names
|
|
356
|
+
if (paramNames.length > 0 &&
|
|
357
|
+
!allowedFirstArgumentNames.includes(paramNames[0])) {
|
|
358
|
+
context.report({
|
|
359
|
+
node: activateInfo.node,
|
|
360
|
+
messageId: 'appNotFirst',
|
|
361
|
+
data: {
|
|
362
|
+
arg: paramNames[0],
|
|
363
|
+
allowedNames: allowedFirstArgumentNames
|
|
364
|
+
.map(name => `"${name}"`)
|
|
365
|
+
.join(', ')
|
|
366
|
+
}
|
|
367
|
+
});
|
|
310
368
|
return;
|
|
311
369
|
}
|
|
312
|
-
if
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
370
|
+
// Validation 1b: Check if first argument type is compatible with JupyterFrontEnd
|
|
371
|
+
if (paramNames.length > 0 && paramTypes.length > 0) {
|
|
372
|
+
const firstParamType = paramTypes[0];
|
|
373
|
+
if (!isCompatibleWithJupyterFrontEnd(firstParamType)) {
|
|
316
374
|
context.report({
|
|
317
375
|
node: activateInfo.node,
|
|
318
|
-
messageId: '
|
|
376
|
+
messageId: 'invalidAppType',
|
|
319
377
|
data: {
|
|
320
378
|
arg: paramNames[0],
|
|
321
|
-
|
|
322
|
-
.map(name => `"${name}"`)
|
|
323
|
-
.join(', ')
|
|
379
|
+
type: firstParamType || 'unknown'
|
|
324
380
|
}
|
|
325
381
|
});
|
|
326
382
|
return;
|
|
327
383
|
}
|
|
328
|
-
// Validation 1b: Check if first argument type is compatible with JupyterFrontEnd
|
|
329
|
-
if (paramNames.length > 0 && paramTypes.length > 0) {
|
|
330
|
-
const firstParamType = paramTypes[0];
|
|
331
|
-
if (!isCompatibleWithJupyterFrontEnd(firstParamType)) {
|
|
332
|
-
context.report({
|
|
333
|
-
node: activateInfo.node,
|
|
334
|
-
messageId: 'invalidAppType',
|
|
335
|
-
data: {
|
|
336
|
-
arg: paramNames[0],
|
|
337
|
-
type: firstParamType || 'unknown'
|
|
338
|
-
}
|
|
339
|
-
});
|
|
340
|
-
return;
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
384
|
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
node: activateInfo.node,
|
|
349
|
-
messageId: 'serviceManagerFirstArgNotNull',
|
|
350
|
-
data: {
|
|
351
|
-
arg: paramTypes[0]
|
|
352
|
-
}
|
|
353
|
-
});
|
|
354
|
-
return;
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
// Validation 2: Check if argument count matches
|
|
358
|
-
if (paramNames.length !== expectedCount) {
|
|
385
|
+
}
|
|
386
|
+
else if (pluginKind === 'service-manager') {
|
|
387
|
+
// Validation 1: First argument must be literal null
|
|
388
|
+
if (paramNames.length === 0 || paramTypes[0] !== null) {
|
|
359
389
|
context.report({
|
|
360
390
|
node: activateInfo.node,
|
|
361
|
-
messageId: '
|
|
391
|
+
messageId: 'serviceManagerFirstArgNotNull',
|
|
362
392
|
data: {
|
|
363
|
-
|
|
364
|
-
tokenCount: String(requires.length + optional.length),
|
|
365
|
-
actual: String(paramNames.length)
|
|
393
|
+
arg: paramTypes[0]
|
|
366
394
|
}
|
|
367
395
|
});
|
|
396
|
+
return;
|
|
368
397
|
}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
398
|
+
}
|
|
399
|
+
// Validation 2: Check if argument count matches
|
|
400
|
+
if (paramNames.length !== expectedCount) {
|
|
401
|
+
context.report({
|
|
402
|
+
node: activateInfo.node,
|
|
403
|
+
messageId: 'wrongArgumentCount',
|
|
404
|
+
data: {
|
|
405
|
+
expected: String(expectedCount),
|
|
406
|
+
tokenCount: String(requires.length + optional.length),
|
|
407
|
+
actual: String(paramNames.length)
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
// Validation 3: Validate remaining parameters against expected token order
|
|
412
|
+
const actualParamTypes = paramTypes.slice(1); // First arg already validated above
|
|
413
|
+
const actualParamNodes = paramNodes.slice(1);
|
|
414
|
+
// Validate that parameter types match expected token types in order
|
|
415
|
+
for (let i = 0; i < actualParamTypes.length; i++) {
|
|
416
|
+
const paramType = actualParamTypes[i];
|
|
417
|
+
const paramNode = actualParamNodes[i];
|
|
418
|
+
const expectedToken = expectedTokensWithoutApp[i];
|
|
419
|
+
if (expectedToken === undefined) {
|
|
420
|
+
// Extra argument
|
|
421
|
+
context.report({
|
|
422
|
+
node: activateInfo.node,
|
|
423
|
+
messageId: 'extraArgument',
|
|
424
|
+
data: { arg: paramNames[i + 1] }
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
else {
|
|
428
|
+
const [matches, tokenUnresolved] = tokenMatchesParam(expectedToken, paramType, paramNode);
|
|
429
|
+
if (tokenUnresolved) {
|
|
379
430
|
context.report({
|
|
380
431
|
node: activateInfo.node,
|
|
381
|
-
messageId: '
|
|
382
|
-
data: {
|
|
432
|
+
messageId: 'unresolvableTokenType',
|
|
433
|
+
data: { token: expectedToken.name }
|
|
383
434
|
});
|
|
384
435
|
}
|
|
385
|
-
else {
|
|
386
|
-
|
|
387
|
-
|
|
436
|
+
else if (!matches) {
|
|
437
|
+
// Distinguish token-order conflict vs. invalid type annotation.
|
|
438
|
+
const matchesAnyOtherToken = expectedTokensWithoutApp.some((otherToken, j) => {
|
|
439
|
+
if (j === i)
|
|
440
|
+
return false;
|
|
441
|
+
const [otherMatches, tokenUnresolved] = tokenMatchesParam(otherToken, paramType, paramNode);
|
|
442
|
+
return otherMatches && !tokenUnresolved;
|
|
443
|
+
});
|
|
444
|
+
if (matchesAnyOtherToken) {
|
|
388
445
|
context.report({
|
|
389
446
|
node: activateInfo.node,
|
|
390
|
-
messageId: '
|
|
391
|
-
data: {
|
|
447
|
+
messageId: 'mismatchedOrder',
|
|
448
|
+
data: { arg: paramNames[i + 1] }
|
|
392
449
|
});
|
|
393
450
|
}
|
|
394
|
-
else
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
451
|
+
else {
|
|
452
|
+
context.report({
|
|
453
|
+
node: activateInfo.node,
|
|
454
|
+
messageId: 'incorrectType',
|
|
455
|
+
data: {
|
|
456
|
+
arg: paramNames[i + 1],
|
|
457
|
+
type: paramType,
|
|
458
|
+
expected: expectedToken.name
|
|
459
|
+
}
|
|
401
460
|
});
|
|
402
|
-
if (matchesAnyOtherToken) {
|
|
403
|
-
context.report({
|
|
404
|
-
node: activateInfo.node,
|
|
405
|
-
messageId: 'mismatchedOrder',
|
|
406
|
-
data: { arg: paramNames[i + 1] }
|
|
407
|
-
});
|
|
408
|
-
}
|
|
409
|
-
else {
|
|
410
|
-
context.report({
|
|
411
|
-
node: activateInfo.node,
|
|
412
|
-
messageId: 'incorrectType',
|
|
413
|
-
data: {
|
|
414
|
-
arg: paramNames[i + 1],
|
|
415
|
-
type: paramType,
|
|
416
|
-
expected: expectedToken.name
|
|
417
|
-
}
|
|
418
|
-
});
|
|
419
|
-
}
|
|
420
461
|
}
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
}
|
|
429
|
-
}
|
|
462
|
+
}
|
|
463
|
+
else if (i >= requires.length && paramNode) {
|
|
464
|
+
// Token matched and is optional — param must be nullable.
|
|
465
|
+
if (!isParamNullable(paramNode)) {
|
|
466
|
+
context.report({
|
|
467
|
+
node: activateInfo.node,
|
|
468
|
+
messageId: 'optionalNotNullable',
|
|
469
|
+
data: { arg: paramNames[i + 1], type: expectedToken.name }
|
|
470
|
+
});
|
|
430
471
|
}
|
|
431
472
|
}
|
|
432
473
|
}
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
}
|
|
442
|
-
}
|
|
474
|
+
}
|
|
475
|
+
// Validation 4: Check for missing arguments (only if counts don't match)
|
|
476
|
+
if (paramNames.length < expectedCount) {
|
|
477
|
+
for (let i = Math.max(paramNames.length - 1, 0); i < expectedTokensWithoutApp.length; i++) {
|
|
478
|
+
const missingToken = expectedTokensWithoutApp[i];
|
|
479
|
+
context.report({
|
|
480
|
+
node: activateInfo.node,
|
|
481
|
+
messageId: 'missingArgument',
|
|
482
|
+
data: { token: missingToken.name }
|
|
483
|
+
});
|
|
443
484
|
}
|
|
444
485
|
}
|
|
445
486
|
}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
const create_rule_1 = require("../utils/create-rule");
|
|
7
7
|
const plugin_utils_1 = require("../utils/plugin-utils");
|
|
8
|
+
const type_services_1 = require("../utils/type-services");
|
|
8
9
|
/**
|
|
9
10
|
* Returns whether the object expression has a description property and whether
|
|
10
11
|
* it is non-empty.
|
|
@@ -53,23 +54,25 @@ const jupyterPluginDescription = (0, create_rule_1.createRule)({
|
|
|
53
54
|
},
|
|
54
55
|
defaultOptions: [],
|
|
55
56
|
create(context) {
|
|
57
|
+
const { checker, getTSNode } = (0, type_services_1.getTypeServices)(context);
|
|
56
58
|
return {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if (!
|
|
59
|
+
ObjectExpression(node) {
|
|
60
|
+
const hasId = (0, plugin_utils_1.getObjectProperties)(node).has('id');
|
|
61
|
+
if (!hasId) {
|
|
60
62
|
return;
|
|
61
63
|
}
|
|
62
|
-
|
|
63
|
-
if (
|
|
64
|
+
const pluginKind = (0, plugin_utils_1.getPluginObjectKind)(node, checker, getTSNode);
|
|
65
|
+
if (pluginKind !== 'frontend' &&
|
|
66
|
+
pluginKind !== 'service-manager' &&
|
|
67
|
+
pluginKind !== 'ambiguous') {
|
|
64
68
|
return;
|
|
65
69
|
}
|
|
66
|
-
const pluginId = (0, plugin_utils_1.getPluginId)(
|
|
70
|
+
const pluginId = (0, plugin_utils_1.getPluginId)(node, context.sourceCode.getScope(node), checker, getTSNode);
|
|
67
71
|
const pluginIdSuffix = pluginId ? ` "${pluginId}"` : '';
|
|
68
|
-
|
|
69
|
-
const descriptionStatus = checkDescriptionProperty(varDecl.init);
|
|
72
|
+
const descriptionStatus = checkDescriptionProperty(node);
|
|
70
73
|
if (descriptionStatus !== 'present') {
|
|
71
74
|
context.report({
|
|
72
|
-
node
|
|
75
|
+
node,
|
|
73
76
|
messageId: descriptionStatus === 'empty'
|
|
74
77
|
? 'emptyDescription'
|
|
75
78
|
: 'missingDescription',
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
declare const pluginIdConvention: ESLintUtils.RuleModule<"mismatchedPrefix" | "idEqualsPackageName", [{
|
|
1
|
+
declare const pluginIdConvention: import("@typescript-eslint/utils/ts-eslint").RuleModule<"mismatchedPrefix" | "idEqualsPackageName", [{
|
|
3
2
|
reportIdEqualToPackageName: boolean;
|
|
4
|
-
}], unknown,
|
|
3
|
+
}], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
5
4
|
name: string;
|
|
6
5
|
};
|
|
7
6
|
export = pluginIdConvention;
|
|
@@ -38,9 +38,9 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
38
38
|
})();
|
|
39
39
|
const fs = __importStar(require("fs"));
|
|
40
40
|
const path = __importStar(require("path"));
|
|
41
|
-
const utils_1 = require("@typescript-eslint/utils");
|
|
42
41
|
const create_rule_1 = require("../utils/create-rule");
|
|
43
42
|
const plugin_utils_1 = require("../utils/plugin-utils");
|
|
43
|
+
const type_services_1 = require("../utils/type-services");
|
|
44
44
|
const package_json_1 = require("../utils/package-json");
|
|
45
45
|
const MAX_PACKAGE_LEVELS = 12;
|
|
46
46
|
const packageManifestCache = new Map();
|
|
@@ -150,108 +150,7 @@ const pluginIdConvention = (0, create_rule_1.createRule)({
|
|
|
150
150
|
},
|
|
151
151
|
defaultOptions: [{ reportIdEqualToPackageName: false }],
|
|
152
152
|
create(context, [options]) {
|
|
153
|
-
|
|
154
|
-
let checker = null;
|
|
155
|
-
try {
|
|
156
|
-
services = utils_1.ESLintUtils.getParserServices(context, true);
|
|
157
|
-
checker = services.program ? services.program.getTypeChecker() : null;
|
|
158
|
-
}
|
|
159
|
-
catch {
|
|
160
|
-
services = null;
|
|
161
|
-
}
|
|
162
|
-
const getTSNode = services
|
|
163
|
-
? (node) => services?.esTreeNodeToTSNodeMap.get(node)
|
|
164
|
-
: null;
|
|
165
|
-
/**
|
|
166
|
-
* Returns true when a type annotation refers to a plugin descriptor or to
|
|
167
|
-
* a MIME renderer extension entry, which JupyterLab registers as a plugin
|
|
168
|
-
* under the entry's `id`.
|
|
169
|
-
*/
|
|
170
|
-
function mentionsPluginType(typeNode) {
|
|
171
|
-
return ((0, plugin_utils_1.typeMentionsJupyterPlugin)(typeNode, checker, getTSNode) ||
|
|
172
|
-
(0, plugin_utils_1.typeMentionsMimeExtension)(typeNode, checker, getTSNode));
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Checks whether an object literal is typed as a plugin descriptor or as
|
|
176
|
-
* a MIME renderer extension entry.
|
|
177
|
-
*/
|
|
178
|
-
function hasPluginType(node) {
|
|
179
|
-
const parent = node.parent;
|
|
180
|
-
if (!parent) {
|
|
181
|
-
return false;
|
|
182
|
-
}
|
|
183
|
-
if (parent.type === 'VariableDeclarator') {
|
|
184
|
-
return ((0, plugin_utils_1.getJupyterPluginKind)(parent, checker, getTSNode) !== null ||
|
|
185
|
-
(parent.id.type === 'Identifier' &&
|
|
186
|
-
mentionsPluginType(parent.id.typeAnnotation?.typeAnnotation)));
|
|
187
|
-
}
|
|
188
|
-
if (parent.type === 'TSAsExpression' ||
|
|
189
|
-
parent.type === 'TSSatisfiesExpression' ||
|
|
190
|
-
parent.type === 'TSTypeAssertion') {
|
|
191
|
-
return (mentionsPluginType(parent.typeAnnotation) ||
|
|
192
|
-
isReturnedFromPluginFactory(node));
|
|
193
|
-
}
|
|
194
|
-
if (isReturnedFromPluginFactory(node)) {
|
|
195
|
-
return true;
|
|
196
|
-
}
|
|
197
|
-
if (parent.type !== 'ArrayExpression') {
|
|
198
|
-
return false;
|
|
199
|
-
}
|
|
200
|
-
const grandparent = parent.parent;
|
|
201
|
-
if (!grandparent) {
|
|
202
|
-
return false;
|
|
203
|
-
}
|
|
204
|
-
if (grandparent.type === 'VariableDeclarator') {
|
|
205
|
-
return (grandparent.id.type === 'Identifier' &&
|
|
206
|
-
mentionsPluginType(grandparent.id.typeAnnotation?.typeAnnotation));
|
|
207
|
-
}
|
|
208
|
-
if (grandparent.type === 'TSAsExpression' ||
|
|
209
|
-
grandparent.type === 'TSSatisfiesExpression' ||
|
|
210
|
-
grandparent.type === 'TSTypeAssertion') {
|
|
211
|
-
return (mentionsPluginType(grandparent.typeAnnotation) ||
|
|
212
|
-
isReturnedFromPluginFactory(parent));
|
|
213
|
-
}
|
|
214
|
-
return isReturnedFromPluginFactory(parent);
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Checks whether an object or array literal is returned by a factory whose
|
|
218
|
-
* return type is a plugin descriptor.
|
|
219
|
-
*/
|
|
220
|
-
function isReturnedFromPluginFactory(node) {
|
|
221
|
-
let expression = node;
|
|
222
|
-
let parent = expression.parent;
|
|
223
|
-
while (parent?.type === 'TSAsExpression' ||
|
|
224
|
-
parent?.type === 'TSSatisfiesExpression' ||
|
|
225
|
-
parent?.type === 'TSTypeAssertion') {
|
|
226
|
-
expression = parent;
|
|
227
|
-
parent = parent.parent;
|
|
228
|
-
}
|
|
229
|
-
if (parent?.type === 'ReturnStatement' &&
|
|
230
|
-
parent.argument === expression) {
|
|
231
|
-
const fn = getEnclosingFunction(parent);
|
|
232
|
-
return mentionsPluginType(fn?.returnType?.typeAnnotation);
|
|
233
|
-
}
|
|
234
|
-
if (parent?.type === 'ArrowFunctionExpression' &&
|
|
235
|
-
parent.body === expression) {
|
|
236
|
-
return mentionsPluginType(parent.returnType?.typeAnnotation);
|
|
237
|
-
}
|
|
238
|
-
return false;
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* Finds the function that owns a return statement.
|
|
242
|
-
*/
|
|
243
|
-
function getEnclosingFunction(node) {
|
|
244
|
-
let current = node.parent;
|
|
245
|
-
while (current) {
|
|
246
|
-
if (current.type === 'FunctionDeclaration' ||
|
|
247
|
-
current.type === 'FunctionExpression' ||
|
|
248
|
-
current.type === 'ArrowFunctionExpression') {
|
|
249
|
-
return current;
|
|
250
|
-
}
|
|
251
|
-
current = current.parent;
|
|
252
|
-
}
|
|
253
|
-
return null;
|
|
254
|
-
}
|
|
153
|
+
const { checker, getTSNode } = (0, type_services_1.getTypeServices)(context);
|
|
255
154
|
/**
|
|
256
155
|
* Reports plugin IDs that do not use the owning extension package prefix.
|
|
257
156
|
*/
|
|
@@ -260,10 +159,14 @@ const pluginIdConvention = (0, create_rule_1.createRule)({
|
|
|
260
159
|
// ID is resolved: resolving may ask the type checker, and most object
|
|
261
160
|
// literals with an `id` are commands, menu items or DOM nodes.
|
|
262
161
|
const hasId = (0, plugin_utils_1.getObjectProperties)(node).has('id');
|
|
263
|
-
if (!hasId
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
162
|
+
if (!hasId) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const pluginKind = (0, plugin_utils_1.getPluginObjectKind)(node, checker, getTSNode, {
|
|
166
|
+
allowUntyped: true,
|
|
167
|
+
matchMime: true
|
|
168
|
+
});
|
|
169
|
+
if (!pluginKind) {
|
|
267
170
|
return;
|
|
268
171
|
}
|
|
269
172
|
const pluginId = (0, plugin_utils_1.getPluginId)(node, context.sourceCode.getScope(node), checker, getTSNode);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TSESLint } from '@typescript-eslint/utils';
|
|
2
2
|
import { LazyImportOptions } from '../utils/lazy-imports';
|
|
3
|
-
declare const jupyterPreferLazyImports:
|
|
3
|
+
declare const jupyterPreferLazyImports: TSESLint.RuleModule<string, [LazyImportOptions], unknown, TSESLint.RuleListener> & {
|
|
4
4
|
name: string;
|
|
5
5
|
};
|
|
6
6
|
export = jupyterPreferLazyImports;
|