@jupyter/eslint-plugin 1.1.0 → 1.3.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 (40) hide show
  1. package/lib/index.d.ts +49 -2
  2. package/lib/index.js +35 -5
  3. package/lib/rules/galata-prefer-context-menu-helper.d.ts +6 -0
  4. package/lib/rules/galata-prefer-context-menu-helper.js +394 -0
  5. package/lib/rules/galata-prefer-menu-helper.d.ts +6 -0
  6. package/lib/rules/galata-prefer-menu-helper.js +394 -0
  7. package/lib/rules/galata-prefer-notebook-cell-helper.d.ts +6 -0
  8. package/lib/rules/galata-prefer-notebook-cell-helper.js +403 -0
  9. package/lib/rules/galata-prefer-sidebar-activity-helper.d.ts +6 -0
  10. package/lib/rules/galata-prefer-sidebar-activity-helper.js +332 -0
  11. package/lib/rules/no-schema-enum.d.ts +1 -1
  12. package/lib/rules/no-schema-enum.js +54 -2
  13. package/lib/rules/no-untranslated-string.d.ts +2 -1
  14. package/lib/rules/no-untranslated-string.js +191 -113
  15. package/lib/rules/plugin-id-convention.d.ts +7 -0
  16. package/lib/rules/plugin-id-convention.js +296 -0
  17. package/lib/rules/prefer-lazy-imports.d.ts +6 -0
  18. package/lib/rules/prefer-lazy-imports.js +476 -0
  19. package/lib/rules/require-disposable-ownership.js +1 -16
  20. package/lib/rules/require-disposable-transfer.js +7 -2
  21. package/lib/utils/ast.d.ts +12 -0
  22. package/lib/utils/ast.js +49 -0
  23. package/lib/utils/disposables.d.ts +12 -0
  24. package/lib/utils/disposables.js +101 -10
  25. package/lib/utils/lazy-imports.d.ts +93 -0
  26. package/lib/utils/lazy-imports.js +600 -0
  27. package/lib/utils/module-size.d.ts +18 -0
  28. package/lib/utils/module-size.js +249 -0
  29. package/lib/utils/package-json.d.ts +8 -0
  30. package/lib/utils/package-json.js +76 -0
  31. package/lib/utils/playwright-selectors.d.ts +56 -5
  32. package/lib/utils/playwright-selectors.js +205 -12
  33. package/lib/utils/plugin-utils.d.ts +50 -2
  34. package/lib/utils/plugin-utils.js +221 -38
  35. package/lib/utils/shared-packages.d.ts +9 -0
  36. package/lib/utils/shared-packages.js +128 -0
  37. package/lib/utils/signal-lifetime.js +66 -5
  38. package/lib/utils/signals.d.ts +2 -8
  39. package/lib/utils/signals.js +7 -42
  40. package/package.json +1 -1
@@ -52,6 +52,7 @@ exports.isClassFieldCollectionMutationCall = isClassFieldCollectionMutationCall;
52
52
  exports.isDisposableExpressionManaged = isDisposableExpressionManaged;
53
53
  exports.markManagedDisposableUse = markManagedDisposableUse;
54
54
  exports.isDisposableType = isDisposableType;
55
+ exports.constructsDisposable = constructsDisposable;
55
56
  exports.shouldCheckReturnedDisposable = shouldCheckReturnedDisposable;
56
57
  const ts = __importStar(require("typescript"));
57
58
  const plugin_utils_1 = require("./plugin-utils");
@@ -954,6 +955,7 @@ function isArrayExtInsertCall(node) {
954
955
  */
955
956
  const signatureCache = new WeakMap();
956
957
  const disposableTypeCache = new WeakMap();
958
+ const disposableConstructorCache = new WeakMap();
957
959
  function isDisposableTypeCached(type, checker) {
958
960
  const cached = disposableTypeCache.get(type);
959
961
  if (cached !== undefined) {
@@ -1568,6 +1570,29 @@ function markDisposedSignalCallbackDisposals(pending, node, ownership) {
1568
1570
  markManagedVariablesWithoutScopeCheck(pending, target, ownership, node);
1569
1571
  }
1570
1572
  }
1573
+ /**
1574
+ * Whether a call can credit any disposable that is still waiting, which decides
1575
+ * whether it is worth asking the type checker which of its arguments take
1576
+ * ownership.
1577
+ *
1578
+ * Crediting removes an entry from `pending` and does nothing for a variable
1579
+ * that is not in it, so a call naming no waiting disposable changes nothing
1580
+ * whatever the answer is. Both the arguments and the receiver are checked
1581
+ * because the answer also decides whether the receiver checks in
1582
+ * `markManagedDisposableUse` run, and those credit the receiver. Any check
1583
+ * added there has to credit the receiver or an argument for this to hold.
1584
+ */
1585
+ function namesPendingDisposable(pending, node, ownership) {
1586
+ return getIdentifierVariables(ownership.sourceCode, node).some(variable => pending.has(variable));
1587
+ }
1588
+ function mayCreditPendingDisposable(pending, node, ownership) {
1589
+ if (node.type === 'CallExpression' &&
1590
+ node.callee.type === 'MemberExpression' &&
1591
+ namesPendingDisposable(pending, node.callee.object, ownership)) {
1592
+ return true;
1593
+ }
1594
+ return node.arguments.some(argument => namesPendingDisposable(pending, argument, ownership));
1595
+ }
1571
1596
  function markManagedDisposableUse(pending, node, ownership) {
1572
1597
  if (node.type === 'ReturnStatement') {
1573
1598
  if (isInJupyterPluginActivate(node, ownership)) {
@@ -1600,21 +1625,26 @@ function markManagedDisposableUse(pending, node, ownership) {
1600
1625
  markDisposedSignalCallbackDisposals(pending, node, ownership);
1601
1626
  markImmediateForEachOwnership(pending, node, ownership);
1602
1627
  }
1628
+ // Crediting a disposable only ever removes an entry from `pending`, so with
1629
+ // nothing waiting neither check below can change the outcome. Skipping them
1630
+ // avoids resolving the callee signature, which is the most expensive query
1631
+ // this module makes.
1603
1632
  if (pending.size > 0) {
1604
1633
  markManagedKnownOptionVariables(pending, node, ownership);
1605
- }
1606
- for (const argument of node.arguments) {
1607
- if (argument.type !== 'ObjectExpression') {
1608
- continue;
1609
- }
1610
- for (const property of argument.properties) {
1611
- if (property.type === 'Property' &&
1612
- isOptionsObjectValueManaged(property.value, ownership)) {
1613
- markManagedVariables(pending, property.value, ownership);
1634
+ for (const argument of node.arguments) {
1635
+ if (argument.type !== 'ObjectExpression') {
1636
+ continue;
1637
+ }
1638
+ for (const property of argument.properties) {
1639
+ if (property.type === 'Property' &&
1640
+ namesPendingDisposable(pending, property.value, ownership) &&
1641
+ isOptionsObjectValueManaged(property.value, ownership)) {
1642
+ markManagedVariables(pending, property.value, ownership);
1643
+ }
1614
1644
  }
1615
1645
  }
1616
1646
  }
1617
- const ownershipArguments = pending.size > 0
1647
+ const ownershipArguments = pending.size > 0 && mayCreditPendingDisposable(pending, node, ownership)
1618
1648
  ? getOwnershipArguments(node, ownership)
1619
1649
  : [];
1620
1650
  if (ownershipArguments.length > 0) {
@@ -1686,6 +1716,67 @@ function isDisposableType(type, checker, seen = new Set()) {
1686
1716
  hasDisposableHeritage(type) ||
1687
1717
  hasDisposableShape(apparentType, checker));
1688
1718
  }
1719
+ /**
1720
+ * A class with no type parameters constructs the same type everywhere, so its
1721
+ * instance type answers for every `new` of it. A generic one does not: `dispose`
1722
+ * can be declared as the type parameter, which is callable in one instantiation
1723
+ * and not in another, so those are asked about one expression at a time.
1724
+ */
1725
+ function nonGenericClassSymbol(symbol) {
1726
+ if (!symbol || !(symbol.flags & ts.SymbolFlags.Class)) {
1727
+ return undefined;
1728
+ }
1729
+ const declarations = symbol.declarations ?? [];
1730
+ const generic = declarations.some(declaration => (ts.isClassDeclaration(declaration) ||
1731
+ ts.isClassExpression(declaration)) &&
1732
+ (declaration.typeParameters?.length ?? 0) > 0);
1733
+ return generic ? undefined : symbol;
1734
+ }
1735
+ /**
1736
+ * Whether a `new` expression creates a disposable, by the type it constructs
1737
+ * when type information is available and by the known constructor names
1738
+ * otherwise.
1739
+ *
1740
+ * A class is constructed all over a codebase, and for a class with no type
1741
+ * parameters what it constructs is a property of the class rather than of any
1742
+ * one call, so the answer is kept per constructor symbol. Resolving that symbol
1743
+ * is name resolution and reading the instance type off it is a lookup, while
1744
+ * typing the `new` expression resolves a construct signature.
1745
+ */
1746
+ function constructsDisposable(node, ownership) {
1747
+ const { checker, services } = ownership;
1748
+ if (checker && services) {
1749
+ try {
1750
+ const tsNode = services.esTreeNodeToTSNodeMap.get(node);
1751
+ let symbol = checker.getSymbolAtLocation(tsNode.expression);
1752
+ if (symbol && symbol.flags & ts.SymbolFlags.Alias) {
1753
+ // An imported class resolves to a different alias symbol in every
1754
+ // file; the class it points at is shared by all of them.
1755
+ symbol = checker.getAliasedSymbol(symbol);
1756
+ }
1757
+ const classSymbol = nonGenericClassSymbol(symbol);
1758
+ if (classSymbol) {
1759
+ const cached = disposableConstructorCache.get(classSymbol);
1760
+ if (cached !== undefined) {
1761
+ return cached || isDisposableConstructor(node);
1762
+ }
1763
+ }
1764
+ const result = isDisposableTypeCached(classSymbol
1765
+ ? checker.getDeclaredTypeOfSymbol(classSymbol)
1766
+ : checker.getTypeAtLocation(tsNode), checker);
1767
+ if (classSymbol) {
1768
+ disposableConstructorCache.set(classSymbol, result);
1769
+ }
1770
+ if (result) {
1771
+ return true;
1772
+ }
1773
+ }
1774
+ catch {
1775
+ // Fall back to the known Lumino disposable constructors below.
1776
+ }
1777
+ }
1778
+ return isDisposableConstructor(node);
1779
+ }
1689
1780
  function shouldCheckReturnedDisposable(node) {
1690
1781
  const expression = getOuterFallbackExpression(node);
1691
1782
  const parent = expression.parent;
@@ -0,0 +1,93 @@
1
+ import { TSESTree } from '@typescript-eslint/types';
2
+ import { TSESLint } from '@typescript-eslint/utils';
3
+ export interface LazyImportOptions {
4
+ allowedPackages: string[];
5
+ deferredPackages: string[];
6
+ ignoreImports: string[];
7
+ minimumSize: number;
8
+ reportInteractionCallbacks: boolean;
9
+ reportModuleLevelUsage: boolean;
10
+ }
11
+ /**
12
+ * An async chunk carries a few hundred bytes of bundler runtime, so around one
13
+ * kilobyte of code the saving cancels out. The default sits well above that
14
+ * break-even point: across the Jupyter extensions this rule was measured on,
15
+ * four kilobytes reports half as many imports as one kilobyte while still
16
+ * covering 95% of the code which could be deferred.
17
+ */
18
+ export declare const DEFAULT_MINIMUM_SIZE = 4096;
19
+ /**
20
+ * Packages shared through Module Federation, which the application therefore
21
+ * loads whether or not a plugin module imports them at the top.
22
+ *
23
+ * This is the singleton list from `jupyterlab/staging/package.json`, minus
24
+ * `@lumino/datagrid` which core itself defers (see `packages/csvviewer`).
25
+ * Entries prefixed with `!` are denied even when another pattern allows them.
26
+ */
27
+ export declare const DEFAULT_ALLOWED_PACKAGES: string[];
28
+ /**
29
+ * Packages which JupyterLab itself loads only with `import()`, so a static
30
+ * import anywhere in an extension puts them back on the startup path.
31
+ *
32
+ * A subpath such as `@codemirror/legacy-modes/mode/python` matches through its
33
+ * owning package. Entries prefixed with `!` are exempt even when another
34
+ * pattern matches them.
35
+ */
36
+ export declare const DEFAULT_DEFERRED_PACKAGES: string[];
37
+ /**
38
+ * Assets which a bundler turns into a URL or a style side effect rather than
39
+ * into bundled bytes, so deferring the import saves nothing.
40
+ *
41
+ * Images and fonts are `asset/resource` in the builder configuration, so they
42
+ * are always emitted as separate files and the browser fetches them only when
43
+ * they are used. A stylesheet goes through `style-loader`, which applies it
44
+ * when it is imported, so deferring it would change when the styles take
45
+ * effect rather than only what is downloaded.
46
+ *
47
+ * Assets which are inlined into the bundle as text are deliberately absent:
48
+ * `.svg` imported from JavaScript, `.raw.css`, `.md`, `.txt` and `.json` all
49
+ * add their full size to the startup chunk, so they are measured like any
50
+ * other module.
51
+ */
52
+ export declare const ALWAYS_IGNORED_IMPORTS: string[];
53
+ /**
54
+ * Matches an import specifier against a list of glob patterns. The specifier is
55
+ * tested both as written and as its owning package name. A matching `!` pattern
56
+ * denies the specifier regardless of any other match.
57
+ */
58
+ export declare function matchesPatterns(specifier: string, patterns: string[]): boolean;
59
+ /**
60
+ * When a node runs, relative to the application starting. `module` is while
61
+ * the module is evaluated. `activation` is inside the `activate` of a plugin
62
+ * which declares `autoStart: true`, which `Application.start` waits for before
63
+ * it attaches the shell. `deferred` is any later point the rule cannot place,
64
+ * such as a command or a method call.
65
+ */
66
+ export type Reach = 'module' | 'activation' | 'deferred';
67
+ /**
68
+ * Finds when the node runs: while the module is evaluated, during the
69
+ * activation of an autostart plugin, or only later. A node inside a named
70
+ * function is placed by the function's call sites, followed transitively, and
71
+ * the earliest of them wins, so a helper called from both `activate` and a
72
+ * command runs during activation. Each function is explored once per walk,
73
+ * which ends a cycle and bounds the cost by the number of functions in the
74
+ * file, so a chain of helpers is followed however long it is.
75
+ */
76
+ export declare function getReach(node: TSESTree.Node, sourceCode: TSESLint.SourceCode, seen?: Set<TSESTree.Node>): Reach;
77
+ /**
78
+ * Returns true when the node sits inside a user-interaction handler, so it
79
+ * cannot run until the user acts, however the module itself is loaded. The
80
+ * closure only exists once the enclosing handler runs, so anything nested
81
+ * deeper inside it waits for the user as well.
82
+ */
83
+ export declare function isInInteractionCallback(node: TSESTree.Node): boolean;
84
+ /**
85
+ * Returns true when the node sits in a plugin's `requires`, `optional` or
86
+ * `provides` entry, where a token has to be referenced at module load.
87
+ */
88
+ export declare function isInPluginTokenList(node: TSESTree.Node): boolean;
89
+ /**
90
+ * Builds the deferred import snippet suggested in the report message, merging
91
+ * every declaration which imports the same source.
92
+ */
93
+ export declare function buildDeferredImportSnippet(declarations: TSESTree.ImportDeclaration[]): string;