@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.
Files changed (53) hide show
  1. package/lib/index.d.ts +98 -1
  2. package/lib/index.js +61 -11
  3. package/lib/rules/galata-prefer-filebrowser-helper.d.ts +5 -0
  4. package/lib/rules/galata-prefer-filebrowser-helper.js +105 -0
  5. package/lib/rules/galata-prefer-menu-helper.d.ts +6 -0
  6. package/lib/rules/galata-prefer-menu-helper.js +478 -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/incorrect-translator-usage.d.ts +4 -0
  12. package/lib/rules/incorrect-translator-usage.js +208 -0
  13. package/lib/rules/no-dynamic-translation.d.ts +4 -0
  14. package/lib/rules/no-dynamic-translation.js +92 -0
  15. package/lib/rules/no-pageconfig-base-url.d.ts +4 -0
  16. package/lib/rules/no-pageconfig-base-url.js +37 -0
  17. package/lib/rules/no-schema-enum.d.ts +1 -1
  18. package/lib/rules/no-schema-enum.js +54 -2
  19. package/lib/rules/no-translation-concatenation.js +46 -84
  20. package/lib/rules/no-untranslated-string.js +7 -4
  21. package/lib/rules/plugin-activation-args.js +1 -2
  22. package/lib/rules/prefer-lazy-imports.d.ts +6 -0
  23. package/lib/rules/prefer-lazy-imports.js +308 -0
  24. package/lib/rules/prefer-signal-this-arg.d.ts +7 -0
  25. package/lib/rules/prefer-signal-this-arg.js +196 -0
  26. package/lib/rules/require-disposable-ownership.d.ts +5 -0
  27. package/lib/rules/require-disposable-ownership.js +120 -0
  28. package/lib/rules/require-disposable-transfer.d.ts +5 -0
  29. package/lib/rules/require-disposable-transfer.js +223 -0
  30. package/lib/rules/require-signal-cleanup.d.ts +8 -0
  31. package/lib/rules/require-signal-cleanup.js +164 -0
  32. package/lib/rules/require-signal-this-arg.d.ts +5 -0
  33. package/lib/rules/require-signal-this-arg.js +74 -0
  34. package/lib/rules/token-format.js +1 -2
  35. package/lib/utils/disposables.d.ts +64 -0
  36. package/lib/utils/disposables.js +1723 -0
  37. package/lib/utils/lazy-imports.d.ts +70 -0
  38. package/lib/utils/lazy-imports.js +515 -0
  39. package/lib/utils/module-size.d.ts +12 -0
  40. package/lib/utils/module-size.js +240 -0
  41. package/lib/utils/playwright-selectors.d.ts +102 -0
  42. package/lib/utils/playwright-selectors.js +306 -0
  43. package/lib/utils/plugin-utils.d.ts +18 -0
  44. package/lib/utils/plugin-utils.js +127 -19
  45. package/lib/utils/shared-packages.d.ts +9 -0
  46. package/lib/utils/shared-packages.js +138 -0
  47. package/lib/utils/signal-lifetime.d.ts +158 -0
  48. package/lib/utils/signal-lifetime.js +816 -0
  49. package/lib/utils/signals.d.ts +143 -0
  50. package/lib/utils/signals.js +601 -0
  51. package/lib/utils/translation.d.ts +45 -0
  52. package/lib/utils/translation.js +122 -0
  53. package/package.json +9 -2
@@ -0,0 +1,240 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
40
+ exports.resolveRelativeModule = resolveRelativeModule;
41
+ exports.getTransitiveCodeSize = getTransitiveCodeSize;
42
+ const fs = __importStar(require("fs"));
43
+ const path = __importStar(require("path"));
44
+ const ts = __importStar(require("typescript"));
45
+ const lazy_imports_1 = require("./lazy-imports");
46
+ const CODE_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs'];
47
+ /** Suffixes an ESM specifier may carry which the source file does not. */
48
+ const OUTPUT_TO_SOURCE = {
49
+ '.js': ['.ts', '.tsx', '.js', '.jsx'],
50
+ '.mjs': ['.mts', '.mjs'],
51
+ '.cjs': ['.cts', '.cjs']
52
+ };
53
+ /** Stops a pathological import graph from dominating lint time. */
54
+ const MAX_FILES = 300;
55
+ const MAX_DEPTH = 12;
56
+ const fileCache = new Map();
57
+ /**
58
+ * Resolves a relative import specifier to a file, trying the usual extensions
59
+ * and an `index` file inside a directory. A specifier written for ESM output,
60
+ * such as `./widget.js`, also resolves to the source it was compiled from.
61
+ * Returns null for a bare package specifier, or when nothing matches.
62
+ */
63
+ function resolveRelativeModule(specifier, fromFile) {
64
+ if (!specifier.startsWith('.')) {
65
+ return null;
66
+ }
67
+ const base = path.resolve(path.dirname(fromFile), specifier);
68
+ const outputExtension = path.extname(base);
69
+ const sourceExtensions = OUTPUT_TO_SOURCE[outputExtension];
70
+ if (sourceExtensions) {
71
+ const withoutExtension = base.slice(0, -outputExtension.length);
72
+ for (const extension of sourceExtensions) {
73
+ const candidate = withoutExtension + extension;
74
+ if (isFile(candidate)) {
75
+ return candidate;
76
+ }
77
+ }
78
+ }
79
+ const candidates = [
80
+ ...CODE_EXTENSIONS.map(extension => base + extension),
81
+ ...CODE_EXTENSIONS.map(extension => path.join(base, `index${extension}`))
82
+ ];
83
+ for (const candidate of candidates) {
84
+ if (isFile(candidate)) {
85
+ return candidate;
86
+ }
87
+ }
88
+ return isFile(base) ? base : null;
89
+ }
90
+ function isFile(candidate) {
91
+ try {
92
+ return fs.statSync(candidate).isFile();
93
+ }
94
+ catch {
95
+ return false;
96
+ }
97
+ }
98
+ /**
99
+ * Compiles a module and returns the bytes it contributes to a bundle. Comments
100
+ * and everything TypeScript erases are gone from the result, so a file of
101
+ * interface declarations measures near zero.
102
+ */
103
+ function compiledSize(filePath, source) {
104
+ const isTsx = filePath.endsWith('.tsx') || filePath.endsWith('.jsx');
105
+ let emitted;
106
+ try {
107
+ emitted = ts.transpileModule(source, {
108
+ fileName: filePath,
109
+ reportDiagnostics: false,
110
+ compilerOptions: {
111
+ target: ts.ScriptTarget.ES2020,
112
+ module: ts.ModuleKind.ESNext,
113
+ removeComments: true,
114
+ isolatedModules: true,
115
+ jsx: isTsx ? ts.JsxEmit.Preserve : undefined
116
+ }
117
+ }).outputText;
118
+ }
119
+ catch {
120
+ emitted = source;
121
+ }
122
+ return Buffer.byteLength(emitted.replace(/\s+/g, ' ').trim(), 'utf8');
123
+ }
124
+ /**
125
+ * Collects the relative modules a file imports at run time. Type-only imports
126
+ * are skipped because TypeScript erases them, and so are dynamic imports,
127
+ * which are deferred already.
128
+ */
129
+ function collectDependencies(filePath, source) {
130
+ const dependencies = [];
131
+ let sourceFile;
132
+ try {
133
+ sourceFile = ts.createSourceFile(filePath, source, ts.ScriptTarget.ES2020, false);
134
+ }
135
+ catch {
136
+ return dependencies;
137
+ }
138
+ for (const statement of sourceFile.statements) {
139
+ let specifier;
140
+ if (ts.isImportDeclaration(statement)) {
141
+ if (statement.importClause?.isTypeOnly) {
142
+ continue;
143
+ }
144
+ specifier = statement.moduleSpecifier;
145
+ }
146
+ else if (ts.isExportDeclaration(statement)) {
147
+ if (statement.isTypeOnly) {
148
+ continue;
149
+ }
150
+ specifier = statement.moduleSpecifier;
151
+ }
152
+ if (!specifier || !ts.isStringLiteral(specifier)) {
153
+ continue;
154
+ }
155
+ if ((0, lazy_imports_1.matchesPatterns)(specifier.text, lazy_imports_1.ALWAYS_IGNORED_IMPORTS)) {
156
+ // An asset the bundler turns into a URL adds nothing to the bundle.
157
+ continue;
158
+ }
159
+ const resolved = resolveRelativeModule(specifier.text, filePath);
160
+ if (resolved) {
161
+ dependencies.push(resolved);
162
+ }
163
+ }
164
+ return dependencies;
165
+ }
166
+ /**
167
+ * Reads a file and records the size of its code and the relative modules it
168
+ * imports. Cached until the file changes on disk.
169
+ */
170
+ function readFileInfo(filePath) {
171
+ let stats;
172
+ try {
173
+ stats = fs.statSync(filePath);
174
+ }
175
+ catch {
176
+ return null;
177
+ }
178
+ const mtimeMs = stats.mtimeMs;
179
+ const cached = fileCache.get(filePath);
180
+ if (cached && cached.mtimeMs === mtimeMs) {
181
+ return cached;
182
+ }
183
+ // An asset inlined into the bundle, such as an SVG or a raw stylesheet,
184
+ // contributes its bytes as they are and imports nothing.
185
+ if (!CODE_EXTENSIONS.includes(path.extname(filePath))) {
186
+ const info = { mtimeMs, size: stats.size, dependencies: [] };
187
+ fileCache.set(filePath, info);
188
+ return info;
189
+ }
190
+ let source;
191
+ try {
192
+ source = fs.readFileSync(filePath, 'utf8');
193
+ }
194
+ catch {
195
+ fileCache.set(filePath, null);
196
+ return null;
197
+ }
198
+ const info = {
199
+ mtimeMs,
200
+ size: compiledSize(filePath, source),
201
+ dependencies: collectDependencies(filePath, source)
202
+ };
203
+ fileCache.set(filePath, info);
204
+ return info;
205
+ }
206
+ /**
207
+ * Sums the code size of a module and of every relative module it statically
208
+ * imports. Returns null when the file cannot be read.
209
+ */
210
+ function getTransitiveCodeSize(entry) {
211
+ const root = readFileInfo(entry);
212
+ if (!root) {
213
+ return null;
214
+ }
215
+ const seen = new Set([entry]);
216
+ const queue = [
217
+ { file: entry, depth: 0 }
218
+ ];
219
+ let total = 0;
220
+ while (queue.length > 0) {
221
+ const { file, depth } = queue.shift();
222
+ const info = readFileInfo(file);
223
+ if (!info) {
224
+ // The size is unknown rather than small, and a module which cannot be
225
+ // measured is reported rather than filtered out.
226
+ return null;
227
+ }
228
+ total += info.size;
229
+ if (depth >= MAX_DEPTH || seen.size >= MAX_FILES) {
230
+ continue;
231
+ }
232
+ for (const dependency of info.dependencies) {
233
+ if (!seen.has(dependency)) {
234
+ seen.add(dependency);
235
+ queue.push({ file: dependency, depth: depth + 1 });
236
+ }
237
+ }
238
+ }
239
+ return total;
240
+ }
@@ -0,0 +1,102 @@
1
+ import { TSESLint, TSESTree } from '@typescript-eslint/utils';
2
+ /**
3
+ * Playwright locator-producing methods.
4
+ */
5
+ export declare const LOCATOR_METHODS: ReadonlySet<string>;
6
+ /**
7
+ * Playwright interaction methods. Called directly on `page` they take a
8
+ * selector as first argument (`page.click(sel)`); called on a locator they
9
+ * act on the already-selected element (`page.locator(sel).click()`).
10
+ */
11
+ export declare const INTERACTION_METHODS: ReadonlySet<string>;
12
+ export interface SelectorPart {
13
+ /**
14
+ * The method that received the argument: a locator method for chains, or
15
+ * the interaction method itself for direct calls (`page.click(sel)`).
16
+ */
17
+ method: string;
18
+ /** The selector or text argument. */
19
+ argNode: TSESTree.Expression;
20
+ /**
21
+ * For `getByRole(role, { name })`, the accessible-name option. `argNode`
22
+ * holds the role. Undefined for every other locator method.
23
+ */
24
+ nameArgNode?: TSESTree.Expression;
25
+ }
26
+ export interface SelectorInteractionMatch {
27
+ /** The outer CallExpression performing the interaction (report anchor). */
28
+ callNode: TSESTree.CallExpression;
29
+ /**
30
+ * The selector arguments, root-to-tip: one for a direct call, one per
31
+ * locator call in a chain (`page.locator(a).getByText(b).click()` → [a, b]).
32
+ */
33
+ selectorParts: SelectorPart[];
34
+ /** true for `page.locator(sel).click()`, false for `page.click(sel)`. */
35
+ viaLocatorChain: boolean;
36
+ /** Name of the interaction method, e.g. 'click', 'dblclick', 'fill'. */
37
+ interactionMethod: string;
38
+ /** true when the call passes `{ button: 'right' }` (context menu open). */
39
+ isRightClick: boolean;
40
+ }
41
+ export interface StaticSelectorTextOptions {
42
+ /** Whether to combine static pieces of interpolated template selectors. */
43
+ allowPartialTemplate?: boolean;
44
+ }
45
+ /**
46
+ * Resolves an identifier holding a locator to the expression it was assigned,
47
+ * or returns null when the binding must not be followed. Passed in by a rule
48
+ * that has a scope to look the name up in.
49
+ */
50
+ export type LocatorBindingResolver = (node: TSESTree.Identifier) => TSESTree.Node | null;
51
+ /**
52
+ * The expression a locator-holding identifier was assigned, or null when the
53
+ * binding must not be followed. A locator held in a variable then reaches the
54
+ * same `page` root as the inline chain.
55
+ *
56
+ * Only a `const` declared with an initializer is followed. `const` is what
57
+ * makes the assignment the single write, so the expression read here is the
58
+ * one the gesture acts on; a `let` could hold a different locator by then.
59
+ *
60
+ * This is the resolver a rule normally passes as the `resolveBinding`
61
+ * argument of {@link matchSelectorInteraction}.
62
+ */
63
+ export declare function resolveLocatorBinding(node: TSESTree.Identifier, scope: TSESLint.Scope.Scope): TSESTree.Node | null;
64
+ /**
65
+ * True when an interaction call passes `{ button: 'right' }` option.
66
+ */
67
+ export declare function isRightClick(node: TSESTree.CallExpression): boolean;
68
+ /**
69
+ * Matches raw Playwright selector interactions on the Galata `page` fixture:
70
+ *
71
+ * - `page.<interaction>(selector, ...)` e.g. `page.dblclick('text=a.ipynb')`
72
+ * - `page.<locatorMethod>(selector)[...].<interaction>(...)` e.g.
73
+ * `page.locator('.jp-DirListing-item').click()` or chained locators like
74
+ * `page.locator('#filebrowser').getByText('notebooks').dblclick()`
75
+ *
76
+ * Returns null for any other shape
77
+ *
78
+ * `resolveBinding` is optional. Without it a chain has to be written inline
79
+ * from `page`; with it a locator held in a variable is followed to its
80
+ * assignment first.
81
+ */
82
+ export declare function matchSelectorInteraction(node: TSESTree.CallExpression, resolveBinding?: LocatorBindingResolver): SelectorInteractionMatch | null;
83
+ /**
84
+ * Extracts a best-effort static string from a selector argument so it can be
85
+ * matched against known patterns. String literals return their value;
86
+ * template literals return their static parts joined by a space, so
87
+ * `` `.jp-DirListing-item span:has-text("${name}")` `` still exposes its
88
+ * static prefix. Pass `allowPartialTemplate: false` when interpolated
89
+ * templates should be treated as dynamic. Fully dynamic expressions return
90
+ * null.
91
+ */
92
+ export declare function extractStaticSelectorText(node: TSESTree.Expression, options?: StaticSelectorTextOptions): string | null;
93
+ /**
94
+ * Joins the statically extractable selector parts of a match into a single
95
+ * pattern-matchable string. Arguments of text-matching locators (`getByText`,
96
+ * `getByTitle`, …) are normalized to the `text=` selector form, so patterns
97
+ * written against `text=` selectors also match the locator-method shape.
98
+ * `getByRole(role, { name })` is normalized to the CSS-attribute form
99
+ * `[role="<role>"] text=<name>` for the same reason. Returns null when no part
100
+ * is static.
101
+ */
102
+ export declare function combineStaticSelectorText(match: SelectorInteractionMatch): string | null;
@@ -0,0 +1,306 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) Jupyter Development Team.
4
+ * Distributed under the terms of the Modified BSD License.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.INTERACTION_METHODS = exports.LOCATOR_METHODS = void 0;
8
+ exports.resolveLocatorBinding = resolveLocatorBinding;
9
+ exports.isRightClick = isRightClick;
10
+ exports.matchSelectorInteraction = matchSelectorInteraction;
11
+ exports.extractStaticSelectorText = extractStaticSelectorText;
12
+ exports.combineStaticSelectorText = combineStaticSelectorText;
13
+ const utils_1 = require("@typescript-eslint/utils");
14
+ /**
15
+ * Playwright locator-producing methods.
16
+ */
17
+ exports.LOCATOR_METHODS = new Set([
18
+ 'locator',
19
+ 'getByRole',
20
+ 'getByText',
21
+ 'getByTitle',
22
+ 'getByLabel',
23
+ 'getByTestId',
24
+ 'getByPlaceholder',
25
+ 'getByAltText'
26
+ ]);
27
+ /**
28
+ * Playwright interaction methods. Called directly on `page` they take a
29
+ * selector as first argument (`page.click(sel)`); called on a locator they
30
+ * act on the already-selected element (`page.locator(sel).click()`).
31
+ */
32
+ exports.INTERACTION_METHODS = new Set([
33
+ 'click',
34
+ 'dblclick',
35
+ 'hover',
36
+ 'tap',
37
+ 'fill',
38
+ 'type',
39
+ 'pressSequentially',
40
+ 'check',
41
+ 'uncheck',
42
+ 'selectOption',
43
+ 'focus',
44
+ 'waitForSelector',
45
+ 'press'
46
+ ]);
47
+ /**
48
+ * Locator methods that pass the locator through without adding a selector,
49
+ * e.g. `page.locator(sel).first().click()`.
50
+ */
51
+ const CHAIN_PASSTHROUGH_METHODS = new Set([
52
+ 'first',
53
+ 'last',
54
+ 'nth'
55
+ ]);
56
+ /**
57
+ * Locator methods whose argument is matched against element text rather
58
+ * than being a CSS selector.
59
+ */
60
+ const TEXT_MATCH_METHODS = new Set([
61
+ 'getByText',
62
+ 'getByTitle',
63
+ 'getByLabel',
64
+ 'getByPlaceholder',
65
+ 'getByAltText'
66
+ ]);
67
+ /**
68
+ * The expression a locator-holding identifier was assigned, or null when the
69
+ * binding must not be followed. A locator held in a variable then reaches the
70
+ * same `page` root as the inline chain.
71
+ *
72
+ * Only a `const` declared with an initializer is followed. `const` is what
73
+ * makes the assignment the single write, so the expression read here is the
74
+ * one the gesture acts on; a `let` could hold a different locator by then.
75
+ *
76
+ * This is the resolver a rule normally passes as the `resolveBinding`
77
+ * argument of {@link matchSelectorInteraction}.
78
+ */
79
+ function resolveLocatorBinding(node, scope) {
80
+ const variable = utils_1.ASTUtils.findVariable(scope, node);
81
+ if (!variable || variable.defs.length !== 1) {
82
+ return null;
83
+ }
84
+ const declarator = variable.defs[0].node;
85
+ if (declarator.type !== 'VariableDeclarator' || !declarator.init) {
86
+ return null;
87
+ }
88
+ const declaration = declarator.parent;
89
+ if (declaration?.type !== 'VariableDeclaration' ||
90
+ declaration.kind !== 'const') {
91
+ return null;
92
+ }
93
+ return declarator.init;
94
+ }
95
+ function isPageIdentifier(node) {
96
+ return node.type === 'Identifier' && node.name === 'page';
97
+ }
98
+ /** Strips the `!` and `as T` wrappers a locator expression may carry. */
99
+ function unwrapAssertions(node) {
100
+ let current = node;
101
+ while (current.type === 'TSNonNullExpression' ||
102
+ current.type === 'TSAsExpression' ||
103
+ current.type === 'AwaitExpression') {
104
+ current =
105
+ current.type === 'AwaitExpression'
106
+ ? current.argument
107
+ : current.expression;
108
+ }
109
+ return current;
110
+ }
111
+ function firstArgument(node) {
112
+ const arg = node.arguments[0];
113
+ return arg && arg.type !== 'SpreadElement' ? arg : null;
114
+ }
115
+ /**
116
+ * True when an interaction call passes `{ button: 'right' }` option.
117
+ */
118
+ function isRightClick(node) {
119
+ return node.arguments.some(arg => arg.type === 'ObjectExpression' &&
120
+ arg.properties.some(prop => prop.type === 'Property' &&
121
+ !prop.computed &&
122
+ ((prop.key.type === 'Identifier' && prop.key.name === 'button') ||
123
+ (prop.key.type === 'Literal' && prop.key.value === 'button')) &&
124
+ prop.value.type === 'Literal' &&
125
+ prop.value.value === 'right'));
126
+ }
127
+ /**
128
+ * Reads the static `name` option of a `getByRole(role, { name })` call.
129
+ */
130
+ function roleNameArgument(node) {
131
+ const options = node.arguments[1];
132
+ if (!options || options.type !== 'ObjectExpression') {
133
+ return undefined;
134
+ }
135
+ for (const prop of options.properties) {
136
+ if (prop.type === 'Property' &&
137
+ !prop.computed &&
138
+ ((prop.key.type === 'Identifier' && prop.key.name === 'name') ||
139
+ (prop.key.type === 'Literal' && prop.key.value === 'name')) &&
140
+ prop.value.type !== 'AssignmentPattern' &&
141
+ prop.value.type !== 'TSEmptyBodyFunctionExpression') {
142
+ return prop.value;
143
+ }
144
+ }
145
+ return undefined;
146
+ }
147
+ /**
148
+ * Walks a locator-producing chain rooted at `page`, e.g.
149
+ * `page.locator(a).getByText(b).first()`, and returns the selector arguments
150
+ * in root-to-tip order. Returns null when the chain is not rooted at `page`,
151
+ * contains a non-locator call, or carries no selector argument at all.
152
+ *
153
+ * With a `resolveBinding` callback the walk also steps through an identifier
154
+ * holding a locator, so that `const cell = page.locator(a); cell.click();`
155
+ * reaches the same `page` root as the inline form.
156
+ */
157
+ function collectLocatorChainSelectors(node, resolveBinding) {
158
+ const selectors = [];
159
+ let current = unwrapAssertions(node);
160
+ // A binding may itself be assigned from another binding, so the walk can
161
+ // alternate between resolving a name and stepping down a call. The bound
162
+ // stops a cycle such as `const a = b, b = a` from looping.
163
+ for (let steps = 0; steps < 32; steps++) {
164
+ if (current.type === 'Identifier') {
165
+ const resolved = resolveBinding ? resolveBinding(current) : null;
166
+ if (!resolved) {
167
+ return null;
168
+ }
169
+ current = unwrapAssertions(resolved);
170
+ continue;
171
+ }
172
+ if (current.type !== 'CallExpression') {
173
+ return null;
174
+ }
175
+ const callee = current.callee;
176
+ if (callee.type !== 'MemberExpression' ||
177
+ callee.computed ||
178
+ callee.property.type !== 'Identifier') {
179
+ return null;
180
+ }
181
+ if (exports.LOCATOR_METHODS.has(callee.property.name)) {
182
+ const arg = firstArgument(current);
183
+ if (arg) {
184
+ selectors.unshift({
185
+ method: callee.property.name,
186
+ argNode: arg,
187
+ ...(callee.property.name === 'getByRole'
188
+ ? { nameArgNode: roleNameArgument(current) }
189
+ : {})
190
+ });
191
+ }
192
+ }
193
+ else if (!CHAIN_PASSTHROUGH_METHODS.has(callee.property.name)) {
194
+ return null;
195
+ }
196
+ const object = unwrapAssertions(callee.object);
197
+ if (isPageIdentifier(object)) {
198
+ return selectors.length > 0 ? selectors : null;
199
+ }
200
+ current = object;
201
+ }
202
+ return null;
203
+ }
204
+ /**
205
+ * Matches raw Playwright selector interactions on the Galata `page` fixture:
206
+ *
207
+ * - `page.<interaction>(selector, ...)` e.g. `page.dblclick('text=a.ipynb')`
208
+ * - `page.<locatorMethod>(selector)[...].<interaction>(...)` e.g.
209
+ * `page.locator('.jp-DirListing-item').click()` or chained locators like
210
+ * `page.locator('#filebrowser').getByText('notebooks').dblclick()`
211
+ *
212
+ * Returns null for any other shape
213
+ *
214
+ * `resolveBinding` is optional. Without it a chain has to be written inline
215
+ * from `page`; with it a locator held in a variable is followed to its
216
+ * assignment first.
217
+ */
218
+ function matchSelectorInteraction(node, resolveBinding) {
219
+ const { callee } = node;
220
+ if (callee.type !== 'MemberExpression' || callee.computed) {
221
+ return null;
222
+ }
223
+ const property = callee.property;
224
+ if (property.type !== 'Identifier' ||
225
+ !exports.INTERACTION_METHODS.has(property.name)) {
226
+ return null;
227
+ }
228
+ // page.click(selector, ...)
229
+ if (isPageIdentifier(callee.object)) {
230
+ const selectorArgNode = firstArgument(node);
231
+ return selectorArgNode
232
+ ? {
233
+ callNode: node,
234
+ selectorParts: [{ method: property.name, argNode: selectorArgNode }],
235
+ viaLocatorChain: false,
236
+ interactionMethod: property.name,
237
+ isRightClick: isRightClick(node)
238
+ }
239
+ : null;
240
+ }
241
+ // page.locator(selector).getByText(...).click(...)
242
+ const selectorParts = collectLocatorChainSelectors(callee.object, resolveBinding);
243
+ return selectorParts
244
+ ? {
245
+ callNode: node,
246
+ selectorParts,
247
+ viaLocatorChain: true,
248
+ interactionMethod: property.name,
249
+ isRightClick: isRightClick(node)
250
+ }
251
+ : null;
252
+ }
253
+ /**
254
+ * Extracts a best-effort static string from a selector argument so it can be
255
+ * matched against known patterns. String literals return their value;
256
+ * template literals return their static parts joined by a space, so
257
+ * `` `.jp-DirListing-item span:has-text("${name}")` `` still exposes its
258
+ * static prefix. Pass `allowPartialTemplate: false` when interpolated
259
+ * templates should be treated as dynamic. Fully dynamic expressions return
260
+ * null.
261
+ */
262
+ function extractStaticSelectorText(node, options = {}) {
263
+ if (node.type === 'Literal') {
264
+ return typeof node.value === 'string' ? node.value : null;
265
+ }
266
+ const allowPartialTemplate = options.allowPartialTemplate !== false;
267
+ if (node.type === 'TemplateLiteral') {
268
+ if (!allowPartialTemplate) {
269
+ return node.expressions.length > 0
270
+ ? null
271
+ : node.quasis
272
+ .map(quasi => quasi.value.cooked ?? quasi.value.raw)
273
+ .join('');
274
+ }
275
+ return node.quasis.map(quasi => quasi.value.cooked ?? '').join(' ');
276
+ }
277
+ return null;
278
+ }
279
+ /**
280
+ * Joins the statically extractable selector parts of a match into a single
281
+ * pattern-matchable string. Arguments of text-matching locators (`getByText`,
282
+ * `getByTitle`, …) are normalized to the `text=` selector form, so patterns
283
+ * written against `text=` selectors also match the locator-method shape.
284
+ * `getByRole(role, { name })` is normalized to the CSS-attribute form
285
+ * `[role="<role>"] text=<name>` for the same reason. Returns null when no part
286
+ * is static.
287
+ */
288
+ function combineStaticSelectorText(match) {
289
+ const parts = [];
290
+ for (const { method, argNode, nameArgNode } of match.selectorParts) {
291
+ const text = extractStaticSelectorText(argNode);
292
+ if (text === null) {
293
+ continue;
294
+ }
295
+ if (method === 'getByRole') {
296
+ // The role is emitted even without a name, but a name is never emitted
297
+ // without its role: a bare `text=File` from a dynamic role would look
298
+ // like an unscoped label and could be mistaken for a menu bar item.
299
+ const name = nameArgNode ? extractStaticSelectorText(nameArgNode) : null;
300
+ parts.push(name === null ? `[role="${text}"]` : `[role="${text}"] text=${name}`);
301
+ continue;
302
+ }
303
+ parts.push(TEXT_MATCH_METHODS.has(method) ? `text=${text}` : text);
304
+ }
305
+ return parts.length > 0 ? parts.join(' ') : null;
306
+ }
@@ -25,3 +25,21 @@ export interface TokenEntry {
25
25
  export declare function extractArrayTokens(arrayExpr: TSESTree.ArrayExpression): TokenEntry[];
26
26
  export declare function isNullableAnnotation(param: TSESTree.Identifier): boolean;
27
27
  export declare function extractParameterType(param: TSESTree.Identifier): string | null;
28
+ /**
29
+ * Returns true when a type annotation mentions a JupyterLab plugin type at any
30
+ * depth, so arrays, unions and wrappers such as `Promise<...>` are recognised.
31
+ * Resolves import aliases through the TypeScript checker when it is available.
32
+ */
33
+ export declare function typeMentionsJupyterPlugin(typeNode: TSESTree.TypeNode | undefined | null, checker?: ts.TypeChecker | null, getTSNode?: ((n: TSESTree.Node) => ts.Node | undefined) | null, depth?: number): boolean;
34
+ /**
35
+ * Returns true when a property holds something callable: a function written in
36
+ * place, or a name referring to one declared elsewhere.
37
+ */
38
+ export declare function isCallableProperty(property: TSESTree.Property | undefined): boolean;
39
+ /**
40
+ * Returns true when an object literal has the shape of a JupyterLab plugin:
41
+ * a string `id`, an `activate` function, and at least one of the properties
42
+ * which only plugins carry. Used for plugin objects written without a type
43
+ * annotation.
44
+ */
45
+ export declare function looksLikePluginObject(node: TSESTree.ObjectExpression): boolean;