@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
@@ -0,0 +1,296 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) Jupyter Development Team.
4
+ * Distributed under the terms of the Modified BSD License.
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ var desc = Object.getOwnPropertyDescriptor(m, k);
9
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
+ desc = { enumerable: true, get: function() { return m[k]; } };
11
+ }
12
+ Object.defineProperty(o, k2, desc);
13
+ }) : (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ o[k2] = m[k];
16
+ }));
17
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
18
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
19
+ }) : function(o, v) {
20
+ o["default"] = v;
21
+ });
22
+ var __importStar = (this && this.__importStar) || (function () {
23
+ var ownKeys = function(o) {
24
+ ownKeys = Object.getOwnPropertyNames || function (o) {
25
+ var ar = [];
26
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
27
+ return ar;
28
+ };
29
+ return ownKeys(o);
30
+ };
31
+ return function (mod) {
32
+ if (mod && mod.__esModule) return mod;
33
+ var result = {};
34
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
35
+ __setModuleDefault(result, mod);
36
+ return result;
37
+ };
38
+ })();
39
+ const fs = __importStar(require("fs"));
40
+ const path = __importStar(require("path"));
41
+ const utils_1 = require("@typescript-eslint/utils");
42
+ const create_rule_1 = require("../utils/create-rule");
43
+ const plugin_utils_1 = require("../utils/plugin-utils");
44
+ const package_json_1 = require("../utils/package-json");
45
+ const MAX_PACKAGE_LEVELS = 12;
46
+ const packageManifestCache = new Map();
47
+ const packagePathCache = new Map();
48
+ /**
49
+ * Reads manifest metadata used to detect package boundaries.
50
+ */
51
+ function readPackageManifest(packagePath) {
52
+ const packageJson = (0, package_json_1.readPackageJson)(packagePath);
53
+ if (!packageJson) {
54
+ return null;
55
+ }
56
+ const cached = packageManifestCache.get(packagePath);
57
+ if (cached?.mtimeMs === packageJson.mtimeMs) {
58
+ return cached;
59
+ }
60
+ const data = packageJson.data;
61
+ const jupyterlab = data.jupyterlab;
62
+ const name = typeof data.name === 'string' ? data.name : null;
63
+ const isExtension = name !== null &&
64
+ !!jupyterlab &&
65
+ typeof jupyterlab === 'object' &&
66
+ (('extension' in jupyterlab && Boolean(jupyterlab.extension)) ||
67
+ ('mimeExtension' in jupyterlab && Boolean(jupyterlab.mimeExtension)));
68
+ const info = {
69
+ mtimeMs: packageJson.mtimeMs,
70
+ name,
71
+ isExtension
72
+ };
73
+ packageManifestCache.set(packagePath, info);
74
+ return info;
75
+ }
76
+ /**
77
+ * Reads the package name from a manifest that declares an extension entry.
78
+ */
79
+ function readExtensionPackage(packagePath) {
80
+ const manifest = readPackageManifest(packagePath);
81
+ if (!manifest?.isExtension || manifest.name === null) {
82
+ return null;
83
+ }
84
+ const info = {
85
+ mtimeMs: manifest.mtimeMs,
86
+ name: manifest.name
87
+ };
88
+ return info;
89
+ }
90
+ /**
91
+ * Finds the nearest extension package manifest that governs a linted file.
92
+ */
93
+ function getExtensionPackageName(fromFile) {
94
+ const start = path.dirname(path.resolve(fromFile));
95
+ const cachedPath = packagePathCache.get(start);
96
+ if (cachedPath !== undefined) {
97
+ return cachedPath === null
98
+ ? null
99
+ : (readExtensionPackage(cachedPath)?.name ?? null);
100
+ }
101
+ const visited = [];
102
+ let found = null;
103
+ let directory = start;
104
+ for (let level = 0; level < MAX_PACKAGE_LEVELS; level++) {
105
+ visited.push(directory);
106
+ const packagePath = path.join(directory, 'package.json');
107
+ if (fs.existsSync(packagePath)) {
108
+ found = packagePath;
109
+ break;
110
+ }
111
+ if (fs.existsSync(path.join(directory, '.git'))) {
112
+ break;
113
+ }
114
+ const parent = path.dirname(directory);
115
+ if (parent === directory) {
116
+ break;
117
+ }
118
+ directory = parent;
119
+ }
120
+ for (const seen of visited) {
121
+ packagePathCache.set(seen, found);
122
+ }
123
+ return found === null ? null : (readExtensionPackage(found)?.name ?? null);
124
+ }
125
+ const pluginIdConvention = (0, create_rule_1.createRule)({
126
+ name: 'plugin-id-convention',
127
+ meta: {
128
+ type: 'problem',
129
+ docs: {
130
+ description: 'Ensure JupyterLab plugin IDs are prefixed with the extension package name',
131
+ url: 'https://eslint-plugin.readthedocs.io/en/latest/rules/plugin-id-convention/'
132
+ },
133
+ messages: {
134
+ mismatchedPrefix: 'JupyterLab plugin ID "{{ pluginId }}" should start with "{{ packageName }}:" so extension-level configuration (disable, defer, lock) applies to it.',
135
+ idEqualsPackageName: 'JupyterLab plugin ID "{{ pluginId }}" is the package name alone; the convention is "{{ packageName }}:<plugin>".'
136
+ },
137
+ schema: [
138
+ {
139
+ type: 'object',
140
+ properties: {
141
+ reportIdEqualToPackageName: {
142
+ type: 'boolean',
143
+ default: false,
144
+ description: 'Also report a plugin whose ID is exactly the package name. Extension-level configuration matches such an ID in full, so it has no user impact, but it does not follow the `<package>:<plugin>` convention.'
145
+ }
146
+ },
147
+ additionalProperties: false
148
+ }
149
+ ]
150
+ },
151
+ defaultOptions: [{ reportIdEqualToPackageName: false }],
152
+ create(context, [options]) {
153
+ let services = null;
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
+ }
255
+ /**
256
+ * Reports plugin IDs that do not use the owning extension package prefix.
257
+ */
258
+ function reportIfNeeded(node) {
259
+ // The shape and annotation checks are syntactic, so they run before the
260
+ // ID is resolved: resolving may ask the type checker, and most object
261
+ // literals with an `id` are commands, menu items or DOM nodes.
262
+ const hasId = (0, plugin_utils_1.getObjectProperties)(node).has('id');
263
+ if (!hasId ||
264
+ (!(0, plugin_utils_1.looksLikePluginObject)(node, hasId) &&
265
+ !(0, plugin_utils_1.looksLikeMimeExtensionObject)(node, hasId) &&
266
+ !hasPluginType(node))) {
267
+ return;
268
+ }
269
+ const pluginId = (0, plugin_utils_1.getPluginId)(node, context.sourceCode.getScope(node), checker, getTSNode);
270
+ if (pluginId === null) {
271
+ return;
272
+ }
273
+ const packageName = getExtensionPackageName(context.filename);
274
+ if (!packageName || pluginId.startsWith(`${packageName}:`)) {
275
+ return;
276
+ }
277
+ // An ID that is exactly the package name follows no convention, but
278
+ // `disabledExtensions`, `deferredExtensions` and `lockedExtensions`
279
+ // match it in full, so it is reported only when asked for.
280
+ const isPackageName = pluginId === packageName;
281
+ if (isPackageName && !options.reportIdEqualToPackageName) {
282
+ return;
283
+ }
284
+ const idProperty = (0, plugin_utils_1.getObjectProperties)(node).get('id');
285
+ context.report({
286
+ node: idProperty?.value ?? node,
287
+ messageId: isPackageName ? 'idEqualsPackageName' : 'mismatchedPrefix',
288
+ data: { pluginId, packageName }
289
+ });
290
+ }
291
+ return {
292
+ ObjectExpression: reportIfNeeded
293
+ };
294
+ }
295
+ });
296
+ module.exports = pluginIdConvention;
@@ -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;