@jupyter/eslint-plugin 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/lib/index.d.ts +27 -1
  2. package/lib/index.js +21 -3
  3. package/lib/rules/command-id-convention.d.ts +5 -0
  4. package/lib/rules/command-id-convention.js +87 -0
  5. package/lib/rules/galata-prefer-context-menu-helper.d.ts +6 -0
  6. package/lib/rules/galata-prefer-context-menu-helper.js +394 -0
  7. package/lib/rules/galata-prefer-menu-helper.js +18 -102
  8. package/lib/rules/no-untranslated-string.d.ts +2 -1
  9. package/lib/rules/no-untranslated-string.js +191 -113
  10. package/lib/rules/plugin-activation-args.d.ts +3 -3
  11. package/lib/rules/plugin-activation-args.js +173 -142
  12. package/lib/rules/plugin-description.js +12 -9
  13. package/lib/rules/plugin-id-convention.d.ts +6 -0
  14. package/lib/rules/plugin-id-convention.js +199 -0
  15. package/lib/rules/prefer-lazy-imports.d.ts +2 -2
  16. package/lib/rules/prefer-lazy-imports.js +257 -58
  17. package/lib/rules/prefer-signal-this-arg.d.ts +2 -3
  18. package/lib/rules/prefer-signal-this-arg.js +2 -10
  19. package/lib/rules/require-disposable-ownership.d.ts +1 -2
  20. package/lib/rules/require-disposable-ownership.js +3 -26
  21. package/lib/rules/require-disposable-transfer.d.ts +1 -2
  22. package/lib/rules/require-disposable-transfer.js +14 -14
  23. package/lib/rules/require-signal-cleanup.d.ts +2 -3
  24. package/lib/rules/require-signal-cleanup.js +2 -10
  25. package/lib/rules/require-signal-this-arg.d.ts +1 -2
  26. package/lib/rules/require-signal-this-arg.js +2 -10
  27. package/lib/utils/ast.d.ts +12 -0
  28. package/lib/utils/ast.js +49 -0
  29. package/lib/utils/commands.d.ts +17 -0
  30. package/lib/utils/commands.js +205 -0
  31. package/lib/utils/disposables.d.ts +12 -0
  32. package/lib/utils/disposables.js +101 -10
  33. package/lib/utils/lazy-imports.d.ts +27 -3
  34. package/lib/utils/lazy-imports.js +130 -36
  35. package/lib/utils/module-size.d.ts +7 -1
  36. package/lib/utils/module-size.js +10 -1
  37. package/lib/utils/package-json.d.ts +8 -0
  38. package/lib/utils/package-json.js +76 -0
  39. package/lib/utils/playwright-selectors.d.ts +15 -0
  40. package/lib/utils/playwright-selectors.js +80 -0
  41. package/lib/utils/plugin-utils.d.ts +66 -6
  42. package/lib/utils/plugin-utils.js +485 -59
  43. package/lib/utils/shared-packages.js +6 -16
  44. package/lib/utils/signals.d.ts +2 -8
  45. package/lib/utils/signals.js +7 -42
  46. package/lib/utils/type-services.d.ts +13 -0
  47. package/lib/utils/type-services.js +27 -0
  48. package/package.json +1 -1
@@ -40,12 +40,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
40
40
  exports.getJupyterPluginKind = getJupyterPluginKind;
41
41
  exports.getObjectProperties = getObjectProperties;
42
42
  exports.getPluginId = getPluginId;
43
+ exports.resolveStaticString = resolveStaticString;
43
44
  exports.extractArrayTokens = extractArrayTokens;
44
45
  exports.isNullableAnnotation = isNullableAnnotation;
45
46
  exports.extractParameterType = extractParameterType;
47
+ exports.isPluginTypeName = isPluginTypeName;
48
+ exports.getPluginKindFromTypeName = getPluginKindFromTypeName;
49
+ exports.getPluginKindFromTsType = getPluginKindFromTsType;
46
50
  exports.typeMentionsJupyterPlugin = typeMentionsJupyterPlugin;
47
51
  exports.isCallableProperty = isCallableProperty;
48
52
  exports.looksLikePluginObject = looksLikePluginObject;
53
+ exports.looksLikeMimeExtensionObject = looksLikeMimeExtensionObject;
54
+ exports.getEnclosingFunction = getEnclosingFunction;
55
+ exports.getPluginKindFromType = getPluginKindFromType;
56
+ exports.getPluginObjectKind = getPluginObjectKind;
57
+ const utils_1 = require("@typescript-eslint/utils");
49
58
  const ts = __importStar(require("typescript"));
50
59
  /**
51
60
  * Gets plugin kind from a variable declaration type annotation.
@@ -61,22 +70,18 @@ function getJupyterPluginKind(node, checker, getTSNode) {
61
70
  if (typeNode.type !== 'TSTypeReference') {
62
71
  return null;
63
72
  }
64
- // Fast path: direct string match (no alias).
73
+ // Fast path: direct string match (no alias), including namespaced like Private.JupyterFrontEndPlugin.
65
74
  const pluginTypeName = extractTypeName(typeNode.typeName);
66
- if (pluginTypeName === 'JupyterFrontEndPlugin') {
67
- return 'frontend';
68
- }
69
- if (pluginTypeName === 'ServiceManagerPlugin') {
70
- return 'service-manager';
75
+ const fastKind = getPluginKindFromTypeName(pluginTypeName);
76
+ if (fastKind) {
77
+ return fastKind;
71
78
  }
72
79
  // Slow path: resolve import aliases via the TS checker.
73
80
  if (checker && getTSNode && typeNode.typeName.type === 'Identifier') {
74
81
  const resolvedName = resolveTypeAlias(typeNode.typeName, checker, getTSNode);
75
- if (resolvedName === 'JupyterFrontEndPlugin') {
76
- return 'frontend';
77
- }
78
- if (resolvedName === 'ServiceManagerPlugin') {
79
- return 'service-manager';
82
+ const resolvedKind = getPluginKindFromTypeName(resolvedName);
83
+ if (resolvedKind) {
84
+ return resolvedKind;
80
85
  }
81
86
  }
82
87
  return null;
@@ -104,28 +109,46 @@ function getObjectProperties(obj) {
104
109
  return props;
105
110
  }
106
111
  /**
107
- * Gets the plugin ID from an object expression
112
+ * Gets the plugin ID from an object expression. Without a scope only a
113
+ * literal, a template literal without substitutions or a `+` chain of literals
114
+ * resolves. With a scope, a `const` string and a member of a `const` object
115
+ * resolve too, and with a checker so does anything with a string literal
116
+ * type, such as a `const` imported from another module.
108
117
  */
109
- function getPluginId(obj) {
110
- for (const prop of obj.properties) {
111
- if (prop.type === 'Property') {
112
- let keyName = null;
113
- if (prop.key.type === 'Identifier') {
114
- keyName = prop.key.name;
115
- }
116
- else if (prop.key.type === 'Literal' &&
117
- typeof prop.key.value === 'string') {
118
- keyName = prop.key.value;
119
- }
120
- if (keyName === 'id' && prop.value.type === 'Literal') {
121
- const value = prop.value.value;
122
- if (typeof value === 'string') {
123
- return value;
124
- }
125
- }
118
+ function getPluginId(obj, scope, checker, getTSNode) {
119
+ const idProperty = getObjectProperties(obj).get('id');
120
+ return idProperty
121
+ ? resolveStaticString(idProperty.value, scope, checker, getTSNode)
122
+ : null;
123
+ }
124
+ /**
125
+ * Resolves an expression to the string it holds at lint time.
126
+ * `getStaticValue` folds the plain JavaScript part: a literal, a template
127
+ * literal, a `+` chain, a variable that is never reassigned and a member of an
128
+ * object literal that is never mutated, with TypeScript casts stripped. The
129
+ * checker then answers for the TypeScript part: a `const` imported from
130
+ * another module, a namespace member, an enum member or a `static readonly`
131
+ * class property.
132
+ */
133
+ function resolveStaticString(node, scope, checker, getTSNode) {
134
+ const folded = utils_1.ASTUtils.getStaticValue(node, scope ?? undefined);
135
+ if (typeof folded?.value === 'string') {
136
+ return folded.value;
137
+ }
138
+ if (!checker || !getTSNode) {
139
+ return null;
140
+ }
141
+ try {
142
+ const tsNode = getTSNode(node);
143
+ if (!tsNode) {
144
+ return null;
126
145
  }
146
+ const type = checker.getTypeAtLocation(tsNode);
147
+ return type.isStringLiteral() ? type.value : null;
148
+ }
149
+ catch {
150
+ return null;
127
151
  }
128
- return null;
129
152
  }
130
153
  /**
131
154
  * Extracts token names and nodes from an array, including member expressions like JupyterFrontEnd.IPaths
@@ -193,58 +216,205 @@ function extractTypeName(typeName) {
193
216
  }
194
217
  return null;
195
218
  }
196
- const PLUGIN_TYPE_NAMES = ['JupyterFrontEndPlugin', 'ServiceManagerPlugin'];
197
219
  /**
198
220
  * Returns true when a type name refers to a JupyterLab plugin type. Only the
199
221
  * last segment is compared, so a namespaced spelling such as
200
- * `Private.JupyterFrontEndPlugin` matches too.
222
+ * `Private.JupyterFrontEndPlugin` or `app.JupyterFrontEndPlugin` matches too.
201
223
  */
202
224
  function isPluginTypeName(name) {
225
+ return getPluginKindFromTypeName(name) !== null;
226
+ }
227
+ /**
228
+ * Resolves the plugin kind ('frontend' or 'service-manager') from a type name.
229
+ * Compares the last segment so qualified names like `app.JupyterFrontEndPlugin` match.
230
+ */
231
+ function getPluginKindFromTypeName(name) {
203
232
  if (!name) {
204
- return false;
233
+ return null;
205
234
  }
206
235
  const lastSegment = name.split('.').pop() ?? name;
207
- return PLUGIN_TYPE_NAMES.includes(lastSegment);
236
+ if (lastSegment === 'JupyterFrontEndPlugin') {
237
+ return 'frontend';
238
+ }
239
+ if (lastSegment === 'ServiceManagerPlugin') {
240
+ return 'service-manager';
241
+ }
242
+ return null;
208
243
  }
209
244
  /**
210
- * Returns true when a type annotation mentions a JupyterLab plugin type at any
211
- * depth, so arrays, unions and wrappers such as `Promise<...>` are recognised.
212
- * Resolves import aliases through the TypeScript checker when it is available.
245
+ * Generic types whose first type argument is unwrapped when resolving a plugin
246
+ * kind. Arbitrary wrappers such as `Wrapper<T>` or `Omit<T, 'description'>`
247
+ * are deliberately left out, since they need not describe a plugin object.
248
+ */
249
+ const ALLOWED_GENERIC_WRAPPERS = new Set([
250
+ 'Array',
251
+ 'ReadonlyArray',
252
+ 'Partial',
253
+ 'Promise',
254
+ 'Readonly',
255
+ 'Required'
256
+ ]);
257
+ /**
258
+ * The application types which make a Lumino `IPlugin<T, U>` a JupyterLab
259
+ * frontend plugin. A bare Lumino `Application` is left out, so plain Lumino
260
+ * plugins are not held to JupyterLab's conventions.
213
261
  */
214
- function typeMentionsJupyterPlugin(typeNode, checker, getTSNode, depth = 0) {
262
+ const FRONTEND_APPLICATION_TYPES = new Set(['JupyterFrontEnd', 'JupyterLab']);
263
+ /**
264
+ * Collapses the kinds found in the members of a union, intersection or tuple:
265
+ * the single kind when they agree, 'ambiguous' when they differ.
266
+ */
267
+ function mergeKinds(kinds) {
268
+ const found = new Set();
269
+ for (const kind of kinds) {
270
+ if (kind) {
271
+ found.add(kind);
272
+ }
273
+ }
274
+ if (found.size > 1) {
275
+ return 'ambiguous';
276
+ }
277
+ return found.size === 1 ? Array.from(found)[0] : null;
278
+ }
279
+ /**
280
+ * Resolves the plugin kind ('frontend' or 'service-manager') from a TypeScript Type.
281
+ */
282
+ function getPluginKindFromTsType(type, checker, depth = 0) {
283
+ if (depth > 6) {
284
+ return null;
285
+ }
286
+ if (type.isUnion() || type.isIntersection()) {
287
+ return mergeKinds(type.types.map(t => getPluginKindFromTsType(t, checker, depth + 1)));
288
+ }
289
+ // Mapped wrappers such as `Partial<T>` and `Readonly<T>` only keep their
290
+ // argument as an alias type argument.
291
+ const aliasName = type.aliasSymbol?.getName();
292
+ const aliasArg = type.aliasTypeArguments?.[0];
293
+ if (aliasName && aliasArg && ALLOWED_GENERIC_WRAPPERS.has(aliasName)) {
294
+ return getPluginKindFromTsType(aliasArg, checker, depth + 1);
295
+ }
296
+ let symbol = type.aliasSymbol ?? type.getSymbol();
297
+ if (symbol && symbol.flags & ts.SymbolFlags.Alias) {
298
+ try {
299
+ symbol = checker.getAliasedSymbol(symbol);
300
+ }
301
+ catch {
302
+ // ignore
303
+ }
304
+ }
305
+ const name = symbol?.getName();
306
+ if (name) {
307
+ const kind = getPluginKindFromTypeName(name);
308
+ if (kind) {
309
+ return kind;
310
+ }
311
+ }
312
+ const targetSymbol = type.getSymbol();
313
+ const targetName = targetSymbol?.getName();
314
+ if (targetName && targetName !== name) {
315
+ const kind = getPluginKindFromTypeName(targetName);
316
+ if (kind) {
317
+ return kind;
318
+ }
319
+ }
320
+ // Handle IPlugin<T, ...> interface from @lumino/application
321
+ if (name === 'IPlugin' || targetName === 'IPlugin') {
322
+ try {
323
+ const typeRef = type;
324
+ const typeArgs = checker.getTypeArguments(typeRef);
325
+ if (typeArgs && typeArgs.length > 0) {
326
+ const firstArg = typeArgs[0];
327
+ const firstArgSymbol = firstArg.aliasSymbol ?? firstArg.getSymbol();
328
+ const firstArgName = firstArgSymbol?.getName();
329
+ if (firstArgName && FRONTEND_APPLICATION_TYPES.has(firstArgName)) {
330
+ return 'frontend';
331
+ }
332
+ if (firstArg.flags & ts.TypeFlags.Null) {
333
+ return 'service-manager';
334
+ }
335
+ }
336
+ }
337
+ catch {
338
+ // ignore
339
+ }
340
+ }
341
+ return null;
342
+ }
343
+ /**
344
+ * Returns true when `matches` accepts a type reference found at any depth of
345
+ * a type annotation, so arrays, unions and wrappers such as `Promise<...>` are
346
+ * recognised.
347
+ */
348
+ function typeMentions(typeNode, matches, depth = 0) {
215
349
  if (!typeNode || depth > 6) {
216
350
  return false;
217
351
  }
218
352
  switch (typeNode.type) {
219
- case 'TSTypeReference': {
220
- if (isPluginTypeName(extractTypeName(typeNode.typeName))) {
221
- return true;
222
- }
223
- if (checker &&
224
- getTSNode &&
225
- typeNode.typeName.type === 'Identifier' &&
226
- isPluginTypeName(resolveTypeAlias(typeNode.typeName, checker, getTSNode))) {
353
+ case 'TSTypeReference':
354
+ if (matches(typeNode)) {
227
355
  return true;
228
356
  }
229
- return (typeNode.typeArguments?.params ?? []).some(param => typeMentionsJupyterPlugin(param, checker, getTSNode, depth + 1));
230
- }
357
+ return (typeNode.typeArguments?.params ?? []).some(param => typeMentions(param, matches, depth + 1));
231
358
  case 'TSArrayType':
232
- return typeMentionsJupyterPlugin(typeNode.elementType, checker, getTSNode, depth + 1);
359
+ return typeMentions(typeNode.elementType, matches, depth + 1);
233
360
  case 'TSUnionType':
234
361
  case 'TSIntersectionType':
235
- return typeNode.types.some(type => typeMentionsJupyterPlugin(type, checker, getTSNode, depth + 1));
362
+ return typeNode.types.some(type => typeMentions(type, matches, depth + 1));
236
363
  case 'TSTupleType':
237
- return typeNode.elementTypes.some(type => typeMentionsJupyterPlugin(type, checker, getTSNode, depth + 1));
364
+ return typeNode.elementTypes.some(type => typeMentions(type, matches, depth + 1));
238
365
  case 'TSTypeOperator':
239
366
  case 'TSRestType':
240
367
  case 'TSOptionalType':
241
- return typeMentionsJupyterPlugin(typeNode.typeAnnotation ?? null, checker, getTSNode, depth + 1);
368
+ return typeMentions(typeNode.typeAnnotation ?? null, matches, depth + 1);
242
369
  case 'TSNamedTupleMember':
243
- return typeMentionsJupyterPlugin(typeNode.elementType, checker, getTSNode, depth + 1);
370
+ return typeMentions(typeNode.elementType, matches, depth + 1);
244
371
  default:
245
372
  return false;
246
373
  }
247
374
  }
375
+ /**
376
+ * Returns true when a type annotation mentions a JupyterLab plugin type at any
377
+ * depth, so arrays, unions and wrappers such as `Promise<...>` are recognised.
378
+ * Resolves import aliases through the TypeScript checker when it is available.
379
+ */
380
+ function typeMentionsJupyterPlugin(typeNode, checker, getTSNode) {
381
+ return typeMentions(typeNode, reference => {
382
+ if (isPluginTypeName(extractTypeName(reference.typeName))) {
383
+ return true;
384
+ }
385
+ return (!!checker &&
386
+ !!getTSNode &&
387
+ reference.typeName.type === 'Identifier' &&
388
+ isPluginTypeName(resolveTypeAlias(reference.typeName, checker, getTSNode)));
389
+ });
390
+ }
391
+ /** The namespace and interface which type a MIME renderer extension entry. */
392
+ const MIME_EXTENSION_NAMESPACE = 'IRenderMime';
393
+ const MIME_EXTENSION_TYPE_NAME = 'IExtension';
394
+ /**
395
+ * Returns true when a type reference is `IRenderMime.IExtension`. Only the
396
+ * qualified spelling counts, because a bare `IExtension` is too common a name
397
+ * to mean anything on its own. The namespace may sit behind a module import
398
+ * (`Interfaces.IRenderMime.IExtension`), and a renamed import such as
399
+ * `import { IRenderMime as RM }` is resolved through the TypeScript checker
400
+ * when it is available.
401
+ */
402
+ function isMimeExtensionReference(reference, checker, getTSNode) {
403
+ const typeName = reference.typeName;
404
+ if (typeName.type !== 'TSQualifiedName' ||
405
+ typeName.right.name !== MIME_EXTENSION_TYPE_NAME) {
406
+ return false;
407
+ }
408
+ const namespace = extractTypeName(typeName.left)?.split('.').pop();
409
+ if (namespace === MIME_EXTENSION_NAMESPACE) {
410
+ return true;
411
+ }
412
+ return (!!checker &&
413
+ !!getTSNode &&
414
+ typeName.left.type === 'Identifier' &&
415
+ resolveTypeAlias(typeName.left, checker, getTSNode) ===
416
+ MIME_EXTENSION_NAMESPACE);
417
+ }
248
418
  /**
249
419
  * Resolves an identifier through the TypeScript checker to the name it aliases,
250
420
  * e.g. `import { JupyterFrontEndPlugin as JFEP }` gives back the original name.
@@ -297,14 +467,12 @@ function isCallableProperty(property) {
297
467
  * Returns true when an object literal has the shape of a JupyterLab plugin:
298
468
  * a string `id`, an `activate` function, and at least one of the properties
299
469
  * which only plugins carry. Used for plugin objects written without a type
300
- * annotation.
470
+ * annotation. A caller that resolves the ID itself passes whether one is
471
+ * present, so this check needs no type information.
301
472
  */
302
- function looksLikePluginObject(node) {
473
+ function looksLikePluginObject(node, hasId = getPluginId(node) !== null) {
303
474
  const properties = getObjectProperties(node);
304
- const id = properties.get('id');
305
- if (!id ||
306
- id.value.type !== 'Literal' ||
307
- typeof id.value.value !== 'string') {
475
+ if (!hasId || !properties.has('id')) {
308
476
  return false;
309
477
  }
310
478
  if (!isCallableProperty(properties.get('activate'))) {
@@ -312,3 +480,261 @@ function looksLikePluginObject(node) {
312
480
  }
313
481
  return PLUGIN_SHAPE_PROPERTIES.some(name => properties.has(name));
314
482
  }
483
+ /**
484
+ * Returns true when an object literal has the shape of a MIME renderer
485
+ * extension entry: a string `id` and a `rendererFactory`. Used for entries
486
+ * written without a type annotation. A caller that resolves the ID itself
487
+ * passes whether one is present.
488
+ */
489
+ function looksLikeMimeExtensionObject(node, hasId = getPluginId(node) !== null) {
490
+ const properties = getObjectProperties(node);
491
+ return hasId && properties.has('id') && properties.has('rendererFactory');
492
+ }
493
+ /**
494
+ * Finds the function that owns a return statement.
495
+ */
496
+ function getEnclosingFunction(node) {
497
+ let current = node.parent;
498
+ while (current) {
499
+ if (current.type === 'FunctionDeclaration' ||
500
+ current.type === 'FunctionExpression' ||
501
+ current.type === 'ArrowFunctionExpression') {
502
+ return current;
503
+ }
504
+ current = current.parent;
505
+ }
506
+ return null;
507
+ }
508
+ /**
509
+ * Resolves the plugin kind from a TypeScript type annotation node, unwrapping
510
+ * arrays, tuples, type operators (readonly), unions, and allowed generic containers.
511
+ * Returns 'ambiguous' if types are mixed (e.g. union or tuple containing multiple distinct plugin kinds).
512
+ */
513
+ function getPluginKindFromType(typeNode, checker, getTSNode, options, depth = 0) {
514
+ if (!typeNode || depth > 6) {
515
+ return null;
516
+ }
517
+ if (typeNode.type === 'TSTypeReference') {
518
+ const name = extractTypeName(typeNode.typeName);
519
+ const directKind = getPluginKindFromTypeName(name);
520
+ if (directKind) {
521
+ return directKind;
522
+ }
523
+ if (options?.matchMime &&
524
+ isMimeExtensionReference(typeNode, checker, getTSNode)) {
525
+ return 'mime-renderer';
526
+ }
527
+ if (checker && getTSNode && typeNode.typeName.type === 'Identifier') {
528
+ const resolved = resolveTypeAlias(typeNode.typeName, checker, getTSNode);
529
+ const aliasKind = getPluginKindFromTypeName(resolved);
530
+ if (aliasKind) {
531
+ return aliasKind;
532
+ }
533
+ }
534
+ // Handle IPlugin<T, ...> in AST
535
+ const baseName = name ? name.split('.').pop() ?? name : null;
536
+ if (baseName === 'IPlugin') {
537
+ const firstParam = typeNode.typeArguments?.params?.[0];
538
+ if (firstParam) {
539
+ if (firstParam.type === 'TSTypeReference') {
540
+ const firstParamName = extractTypeName(firstParam.typeName);
541
+ const firstBase = firstParamName
542
+ ? firstParamName.split('.').pop() ?? firstParamName
543
+ : null;
544
+ if (firstBase && FRONTEND_APPLICATION_TYPES.has(firstBase)) {
545
+ return 'frontend';
546
+ }
547
+ }
548
+ else if (firstParam.type === 'TSNullKeyword') {
549
+ return 'service-manager';
550
+ }
551
+ }
552
+ }
553
+ // Only unwrap generic containers in the allowed set; arbitrary wrappers
554
+ // such as Wrapper<T> or Omit<T, 'description'> are not plugins.
555
+ if (baseName &&
556
+ ALLOWED_GENERIC_WRAPPERS.has(baseName) &&
557
+ typeNode.typeArguments?.params &&
558
+ typeNode.typeArguments.params.length > 0) {
559
+ return getPluginKindFromType(typeNode.typeArguments.params[0], checker, getTSNode, options, depth + 1);
560
+ }
561
+ return null;
562
+ }
563
+ if (typeNode.type === 'TSArrayType') {
564
+ return getPluginKindFromType(typeNode.elementType, checker, getTSNode, options, depth + 1);
565
+ }
566
+ if (typeNode.type === 'TSTupleType') {
567
+ return mergeKinds(typeNode.elementTypes.map(member => getPluginKindFromType(unwrapTupleMember(member), checker, getTSNode, options, depth + 1)));
568
+ }
569
+ if (typeNode.type === 'TSTypeOperator' && typeNode.operator === 'readonly') {
570
+ return getPluginKindFromType(typeNode.typeAnnotation, checker, getTSNode, options, depth + 1);
571
+ }
572
+ if (typeNode.type === 'TSUnionType' ||
573
+ typeNode.type === 'TSIntersectionType') {
574
+ return mergeKinds(typeNode.types.map(t => getPluginKindFromType(t, checker, getTSNode, options, depth + 1)));
575
+ }
576
+ return null;
577
+ }
578
+ function getEnclosingFunctionReturnType(expr) {
579
+ const parent = expr.parent;
580
+ if (parent?.type === 'ReturnStatement' && parent.argument === expr) {
581
+ const fn = getEnclosingFunction(parent);
582
+ return fn?.returnType?.typeAnnotation ?? null;
583
+ }
584
+ if (parent?.type === 'ArrowFunctionExpression' && parent.body === expr) {
585
+ return parent.returnType?.typeAnnotation ?? null;
586
+ }
587
+ return null;
588
+ }
589
+ function getKindFromEnclosingFunctionReturn(expr, checker, getTSNode, options) {
590
+ const returnType = getEnclosingFunctionReturnType(expr);
591
+ if (returnType) {
592
+ return getPluginKindFromType(returnType, checker, getTSNode, options);
593
+ }
594
+ return null;
595
+ }
596
+ function getTupleElementKind(typeNode, elementIndex, checker, getTSNode, options) {
597
+ if (!typeNode || elementIndex < 0) {
598
+ return null;
599
+ }
600
+ if (typeNode.type === 'TSTypeOperator' && typeNode.operator === 'readonly') {
601
+ return getTupleElementKind(typeNode.typeAnnotation, elementIndex, checker, getTSNode, options);
602
+ }
603
+ if (typeNode.type === 'TSTupleType') {
604
+ const members = typeNode.elementTypes;
605
+ if (elementIndex < members.length) {
606
+ return getPluginKindFromType(unwrapTupleMember(members[elementIndex]), checker, getTSNode, options);
607
+ }
608
+ // Past the fixed members, elements take the type of a trailing rest member.
609
+ const last = members[members.length - 1];
610
+ const lastType = last?.type === 'TSNamedTupleMember' ? last.elementType : last;
611
+ if (lastType?.type === 'TSRestType') {
612
+ return getPluginKindFromType(lastType.typeAnnotation, checker, getTSNode, options);
613
+ }
614
+ }
615
+ return null;
616
+ }
617
+ /**
618
+ * Returns the element type of a tuple member, looking through labels,
619
+ * optional markers and rest spreads.
620
+ */
621
+ function unwrapTupleMember(member) {
622
+ let elementType = member.type === 'TSNamedTupleMember' ? member.elementType : member;
623
+ if (elementType.type === 'TSOptionalType') {
624
+ elementType = elementType.typeAnnotation;
625
+ }
626
+ if (elementType.type === 'TSRestType') {
627
+ elementType = elementType.typeAnnotation;
628
+ }
629
+ return elementType;
630
+ }
631
+ /**
632
+ * Returns the plugin kind ('frontend', 'service-manager', or 'mime-renderer')
633
+ * for an object literal expression by inspecting its type annotation, type cast,
634
+ * enclosing array/tuple, factory function return type, or shape (when allowUntyped is true).
635
+ */
636
+ function getPluginObjectKind(node, checker, getTSNode, options) {
637
+ // 0. If checker is available, resolve contextual type per element.
638
+ if (checker && getTSNode) {
639
+ const tsNode = getTSNode(node);
640
+ if (tsNode) {
641
+ try {
642
+ const contextualType = checker.getContextualType(tsNode);
643
+ if (contextualType) {
644
+ const kind = getPluginKindFromTsType(contextualType, checker);
645
+ if (kind) {
646
+ return kind;
647
+ }
648
+ }
649
+ }
650
+ catch {
651
+ // ignore
652
+ }
653
+ }
654
+ }
655
+ // 1. Unwrap any type assertions directly wrapping the object expression.
656
+ let current = node;
657
+ while (current.parent &&
658
+ (current.parent.type === 'TSAsExpression' ||
659
+ current.parent.type === 'TSSatisfiesExpression' ||
660
+ current.parent.type === 'TSTypeAssertion')) {
661
+ const castNode = current.parent;
662
+ const kind = getPluginKindFromType(castNode.typeAnnotation, checker, getTSNode, options);
663
+ if (kind) {
664
+ return kind;
665
+ }
666
+ current = castNode;
667
+ }
668
+ // 2. Check if current is directly assigned to a typed variable declarator.
669
+ if (current.parent?.type === 'VariableDeclarator') {
670
+ const varDecl = current.parent;
671
+ if (varDecl.id.type === 'Identifier' && varDecl.id.typeAnnotation) {
672
+ const kind = getPluginKindFromType(varDecl.id.typeAnnotation.typeAnnotation, checker, getTSNode, options);
673
+ if (kind) {
674
+ return kind;
675
+ }
676
+ }
677
+ }
678
+ // 3. Check if current is returned from a function with an explicit return type.
679
+ const returnedKind = getKindFromEnclosingFunctionReturn(current, checker, getTSNode, options);
680
+ if (returnedKind) {
681
+ return returnedKind;
682
+ }
683
+ // 4. Check if current is an element of an ArrayExpression.
684
+ if (current.parent?.type === 'ArrayExpression') {
685
+ const arrayElements = current.parent.elements;
686
+ const elementIndex = arrayElements.indexOf(current);
687
+ let arrayNode = current.parent;
688
+ while (arrayNode.parent &&
689
+ (arrayNode.parent.type === 'TSAsExpression' ||
690
+ arrayNode.parent.type === 'TSSatisfiesExpression' ||
691
+ arrayNode.parent.type === 'TSTypeAssertion')) {
692
+ const castNode = arrayNode.parent;
693
+ const tupleKind = getTupleElementKind(castNode.typeAnnotation, elementIndex, checker, getTSNode, options);
694
+ if (tupleKind) {
695
+ return tupleKind;
696
+ }
697
+ const kind = getPluginKindFromType(castNode.typeAnnotation, checker, getTSNode, options);
698
+ if (kind) {
699
+ return kind;
700
+ }
701
+ arrayNode = castNode;
702
+ }
703
+ if (arrayNode.parent?.type === 'VariableDeclarator') {
704
+ const varDecl = arrayNode.parent;
705
+ if (varDecl.id.type === 'Identifier' && varDecl.id.typeAnnotation) {
706
+ const typeAnnotation = varDecl.id.typeAnnotation.typeAnnotation;
707
+ const tupleKind = getTupleElementKind(typeAnnotation, elementIndex, checker, getTSNode, options);
708
+ if (tupleKind) {
709
+ return tupleKind;
710
+ }
711
+ const kind = getPluginKindFromType(typeAnnotation, checker, getTSNode, options);
712
+ if (kind) {
713
+ return kind;
714
+ }
715
+ }
716
+ }
717
+ const enclosingFnType = getEnclosingFunctionReturnType(arrayNode);
718
+ if (enclosingFnType) {
719
+ const tupleKind = getTupleElementKind(enclosingFnType, elementIndex, checker, getTSNode, options);
720
+ if (tupleKind) {
721
+ return tupleKind;
722
+ }
723
+ }
724
+ const returnedArrayKind = getKindFromEnclosingFunctionReturn(arrayNode, checker, getTSNode, options);
725
+ if (returnedArrayKind) {
726
+ return returnedArrayKind;
727
+ }
728
+ }
729
+ // 5. If untyped plugin detection is enabled, check shape.
730
+ if (options?.allowUntyped) {
731
+ const hasId = getObjectProperties(node).has('id');
732
+ if (looksLikePluginObject(node, hasId)) {
733
+ return 'frontend';
734
+ }
735
+ if (options.matchMime && looksLikeMimeExtensionObject(node, hasId)) {
736
+ return 'mime-renderer';
737
+ }
738
+ }
739
+ return null;
740
+ }
@@ -40,6 +40,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
40
40
  exports.getHostProvidedPackages = getHostProvidedPackages;
41
41
  const fs = __importStar(require("fs"));
42
42
  const path = __importStar(require("path"));
43
+ const package_json_1 = require("./package-json");
43
44
  /** How far to look upwards for the labextension manifest. */
44
45
  const MAX_LEVELS = 12;
45
46
  const manifestCache = new Map();
@@ -55,26 +56,15 @@ const manifestPathCache = new Map();
55
56
  * which removes the package from the shared scope altogether.
56
57
  */
57
58
  function readManifest(manifestPath) {
58
- let mtimeMs;
59
- try {
60
- mtimeMs = fs.statSync(manifestPath).mtimeMs;
61
- }
62
- catch {
59
+ const manifest = (0, package_json_1.readPackageJson)(manifestPath);
60
+ if (!manifest) {
63
61
  return null;
64
62
  }
65
63
  const cached = manifestCache.get(manifestPath);
66
- if (cached && cached.mtimeMs === mtimeMs) {
64
+ if (cached && cached.mtimeMs === manifest.mtimeMs) {
67
65
  return cached;
68
66
  }
69
- let data;
70
- try {
71
- data = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
72
- }
73
- catch {
74
- manifestCache.set(manifestPath, null);
75
- return null;
76
- }
77
- const jupyterlab = data.jupyterlab;
67
+ const jupyterlab = manifest.data.jupyterlab;
78
68
  if (!jupyterlab || typeof jupyterlab !== 'object') {
79
69
  // Not a labextension manifest, so keep looking further up.
80
70
  manifestCache.set(manifestPath, null);
@@ -89,7 +79,7 @@ function readManifest(manifestPath) {
89
79
  hostProvided.push(name);
90
80
  }
91
81
  }
92
- const info = { mtimeMs, hostProvided };
82
+ const info = { mtimeMs: manifest.mtimeMs, hostProvided };
93
83
  manifestCache.set(manifestPath, info);
94
84
  return info;
95
85
  }