@jupyter/eslint-plugin 1.1.0 → 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.
- package/lib/index.d.ts +30 -1
- package/lib/index.js +24 -5
- package/lib/rules/galata-prefer-menu-helper.d.ts +6 -0
- package/lib/rules/galata-prefer-menu-helper.js +478 -0
- package/lib/rules/galata-prefer-notebook-cell-helper.d.ts +6 -0
- package/lib/rules/galata-prefer-notebook-cell-helper.js +403 -0
- package/lib/rules/galata-prefer-sidebar-activity-helper.d.ts +6 -0
- package/lib/rules/galata-prefer-sidebar-activity-helper.js +332 -0
- package/lib/rules/no-schema-enum.d.ts +1 -1
- package/lib/rules/no-schema-enum.js +54 -2
- package/lib/rules/prefer-lazy-imports.d.ts +6 -0
- package/lib/rules/prefer-lazy-imports.js +308 -0
- package/lib/utils/lazy-imports.d.ts +70 -0
- package/lib/utils/lazy-imports.js +515 -0
- package/lib/utils/module-size.d.ts +12 -0
- package/lib/utils/module-size.js +240 -0
- package/lib/utils/playwright-selectors.d.ts +41 -5
- package/lib/utils/playwright-selectors.js +125 -12
- package/lib/utils/plugin-utils.d.ts +18 -0
- package/lib/utils/plugin-utils.js +127 -19
- package/lib/utils/shared-packages.d.ts +9 -0
- package/lib/utils/shared-packages.js +138 -0
- package/lib/utils/signal-lifetime.js +66 -5
- package/package.json +1 -1
|
@@ -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
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TSESTree } from '@typescript-eslint/utils';
|
|
1
|
+
import { TSESLint, TSESTree } from '@typescript-eslint/utils';
|
|
2
2
|
/**
|
|
3
3
|
* Playwright locator-producing methods.
|
|
4
4
|
*/
|
|
@@ -17,6 +17,11 @@ export interface SelectorPart {
|
|
|
17
17
|
method: string;
|
|
18
18
|
/** The selector or text argument. */
|
|
19
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;
|
|
20
25
|
}
|
|
21
26
|
export interface SelectorInteractionMatch {
|
|
22
27
|
/** The outer CallExpression performing the interaction (report anchor). */
|
|
@@ -33,6 +38,29 @@ export interface SelectorInteractionMatch {
|
|
|
33
38
|
/** true when the call passes `{ button: 'right' }` (context menu open). */
|
|
34
39
|
isRightClick: boolean;
|
|
35
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;
|
|
36
64
|
/**
|
|
37
65
|
* True when an interaction call passes `{ button: 'right' }` option.
|
|
38
66
|
*/
|
|
@@ -46,21 +74,29 @@ export declare function isRightClick(node: TSESTree.CallExpression): boolean;
|
|
|
46
74
|
* `page.locator('#filebrowser').getByText('notebooks').dblclick()`
|
|
47
75
|
*
|
|
48
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.
|
|
49
81
|
*/
|
|
50
|
-
export declare function matchSelectorInteraction(node: TSESTree.CallExpression): SelectorInteractionMatch | null;
|
|
82
|
+
export declare function matchSelectorInteraction(node: TSESTree.CallExpression, resolveBinding?: LocatorBindingResolver): SelectorInteractionMatch | null;
|
|
51
83
|
/**
|
|
52
84
|
* Extracts a best-effort static string from a selector argument so it can be
|
|
53
85
|
* matched against known patterns. String literals return their value;
|
|
54
86
|
* template literals return their static parts joined by a space, so
|
|
55
87
|
* `` `.jp-DirListing-item span:has-text("${name}")` `` still exposes its
|
|
56
|
-
* static prefix.
|
|
88
|
+
* static prefix. Pass `allowPartialTemplate: false` when interpolated
|
|
89
|
+
* templates should be treated as dynamic. Fully dynamic expressions return
|
|
90
|
+
* null.
|
|
57
91
|
*/
|
|
58
|
-
export declare function extractStaticSelectorText(node: TSESTree.Expression): string | null;
|
|
92
|
+
export declare function extractStaticSelectorText(node: TSESTree.Expression, options?: StaticSelectorTextOptions): string | null;
|
|
59
93
|
/**
|
|
60
94
|
* Joins the statically extractable selector parts of a match into a single
|
|
61
95
|
* pattern-matchable string. Arguments of text-matching locators (`getByText`,
|
|
62
96
|
* `getByTitle`, …) are normalized to the `text=` selector form, so patterns
|
|
63
97
|
* written against `text=` selectors also match the locator-method shape.
|
|
64
|
-
*
|
|
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.
|
|
65
101
|
*/
|
|
66
102
|
export declare function combineStaticSelectorText(match: SelectorInteractionMatch): string | null;
|
|
@@ -5,15 +5,18 @@
|
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.INTERACTION_METHODS = exports.LOCATOR_METHODS = void 0;
|
|
8
|
+
exports.resolveLocatorBinding = resolveLocatorBinding;
|
|
8
9
|
exports.isRightClick = isRightClick;
|
|
9
10
|
exports.matchSelectorInteraction = matchSelectorInteraction;
|
|
10
11
|
exports.extractStaticSelectorText = extractStaticSelectorText;
|
|
11
12
|
exports.combineStaticSelectorText = combineStaticSelectorText;
|
|
13
|
+
const utils_1 = require("@typescript-eslint/utils");
|
|
12
14
|
/**
|
|
13
15
|
* Playwright locator-producing methods.
|
|
14
16
|
*/
|
|
15
17
|
exports.LOCATOR_METHODS = new Set([
|
|
16
18
|
'locator',
|
|
19
|
+
'getByRole',
|
|
17
20
|
'getByText',
|
|
18
21
|
'getByTitle',
|
|
19
22
|
'getByLabel',
|
|
@@ -32,6 +35,8 @@ exports.INTERACTION_METHODS = new Set([
|
|
|
32
35
|
'hover',
|
|
33
36
|
'tap',
|
|
34
37
|
'fill',
|
|
38
|
+
'type',
|
|
39
|
+
'pressSequentially',
|
|
35
40
|
'check',
|
|
36
41
|
'uncheck',
|
|
37
42
|
'selectOption',
|
|
@@ -59,9 +64,50 @@ const TEXT_MATCH_METHODS = new Set([
|
|
|
59
64
|
'getByPlaceholder',
|
|
60
65
|
'getByAltText'
|
|
61
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
|
+
}
|
|
62
95
|
function isPageIdentifier(node) {
|
|
63
96
|
return node.type === 'Identifier' && node.name === 'page';
|
|
64
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
|
+
}
|
|
65
111
|
function firstArgument(node) {
|
|
66
112
|
const arg = node.arguments[0];
|
|
67
113
|
return arg && arg.type !== 'SpreadElement' ? arg : null;
|
|
@@ -78,16 +124,51 @@ function isRightClick(node) {
|
|
|
78
124
|
prop.value.type === 'Literal' &&
|
|
79
125
|
prop.value.value === 'right'));
|
|
80
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
|
+
}
|
|
81
147
|
/**
|
|
82
148
|
* Walks a locator-producing chain rooted at `page`, e.g.
|
|
83
149
|
* `page.locator(a).getByText(b).first()`, and returns the selector arguments
|
|
84
150
|
* in root-to-tip order. Returns null when the chain is not rooted at `page`,
|
|
85
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.
|
|
86
156
|
*/
|
|
87
|
-
function collectLocatorChainSelectors(node) {
|
|
157
|
+
function collectLocatorChainSelectors(node, resolveBinding) {
|
|
88
158
|
const selectors = [];
|
|
89
|
-
let current = node;
|
|
90
|
-
|
|
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
|
+
}
|
|
91
172
|
if (current.type !== 'CallExpression') {
|
|
92
173
|
return null;
|
|
93
174
|
}
|
|
@@ -100,17 +181,25 @@ function collectLocatorChainSelectors(node) {
|
|
|
100
181
|
if (exports.LOCATOR_METHODS.has(callee.property.name)) {
|
|
101
182
|
const arg = firstArgument(current);
|
|
102
183
|
if (arg) {
|
|
103
|
-
selectors.unshift({
|
|
184
|
+
selectors.unshift({
|
|
185
|
+
method: callee.property.name,
|
|
186
|
+
argNode: arg,
|
|
187
|
+
...(callee.property.name === 'getByRole'
|
|
188
|
+
? { nameArgNode: roleNameArgument(current) }
|
|
189
|
+
: {})
|
|
190
|
+
});
|
|
104
191
|
}
|
|
105
192
|
}
|
|
106
193
|
else if (!CHAIN_PASSTHROUGH_METHODS.has(callee.property.name)) {
|
|
107
194
|
return null;
|
|
108
195
|
}
|
|
109
|
-
|
|
196
|
+
const object = unwrapAssertions(callee.object);
|
|
197
|
+
if (isPageIdentifier(object)) {
|
|
110
198
|
return selectors.length > 0 ? selectors : null;
|
|
111
199
|
}
|
|
112
|
-
current =
|
|
200
|
+
current = object;
|
|
113
201
|
}
|
|
202
|
+
return null;
|
|
114
203
|
}
|
|
115
204
|
/**
|
|
116
205
|
* Matches raw Playwright selector interactions on the Galata `page` fixture:
|
|
@@ -121,8 +210,12 @@ function collectLocatorChainSelectors(node) {
|
|
|
121
210
|
* `page.locator('#filebrowser').getByText('notebooks').dblclick()`
|
|
122
211
|
*
|
|
123
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.
|
|
124
217
|
*/
|
|
125
|
-
function matchSelectorInteraction(node) {
|
|
218
|
+
function matchSelectorInteraction(node, resolveBinding) {
|
|
126
219
|
const { callee } = node;
|
|
127
220
|
if (callee.type !== 'MemberExpression' || callee.computed) {
|
|
128
221
|
return null;
|
|
@@ -146,7 +239,7 @@ function matchSelectorInteraction(node) {
|
|
|
146
239
|
: null;
|
|
147
240
|
}
|
|
148
241
|
// page.locator(selector).getByText(...).click(...)
|
|
149
|
-
const selectorParts = collectLocatorChainSelectors(callee.object);
|
|
242
|
+
const selectorParts = collectLocatorChainSelectors(callee.object, resolveBinding);
|
|
150
243
|
return selectorParts
|
|
151
244
|
? {
|
|
152
245
|
callNode: node,
|
|
@@ -162,13 +255,23 @@ function matchSelectorInteraction(node) {
|
|
|
162
255
|
* matched against known patterns. String literals return their value;
|
|
163
256
|
* template literals return their static parts joined by a space, so
|
|
164
257
|
* `` `.jp-DirListing-item span:has-text("${name}")` `` still exposes its
|
|
165
|
-
* static prefix.
|
|
258
|
+
* static prefix. Pass `allowPartialTemplate: false` when interpolated
|
|
259
|
+
* templates should be treated as dynamic. Fully dynamic expressions return
|
|
260
|
+
* null.
|
|
166
261
|
*/
|
|
167
|
-
function extractStaticSelectorText(node) {
|
|
262
|
+
function extractStaticSelectorText(node, options = {}) {
|
|
168
263
|
if (node.type === 'Literal') {
|
|
169
264
|
return typeof node.value === 'string' ? node.value : null;
|
|
170
265
|
}
|
|
266
|
+
const allowPartialTemplate = options.allowPartialTemplate !== false;
|
|
171
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
|
+
}
|
|
172
275
|
return node.quasis.map(quasi => quasi.value.cooked ?? '').join(' ');
|
|
173
276
|
}
|
|
174
277
|
return null;
|
|
@@ -178,15 +281,25 @@ function extractStaticSelectorText(node) {
|
|
|
178
281
|
* pattern-matchable string. Arguments of text-matching locators (`getByText`,
|
|
179
282
|
* `getByTitle`, …) are normalized to the `text=` selector form, so patterns
|
|
180
283
|
* written against `text=` selectors also match the locator-method shape.
|
|
181
|
-
*
|
|
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.
|
|
182
287
|
*/
|
|
183
288
|
function combineStaticSelectorText(match) {
|
|
184
289
|
const parts = [];
|
|
185
|
-
for (const { method, argNode } of match.selectorParts) {
|
|
290
|
+
for (const { method, argNode, nameArgNode } of match.selectorParts) {
|
|
186
291
|
const text = extractStaticSelectorText(argNode);
|
|
187
292
|
if (text === null) {
|
|
188
293
|
continue;
|
|
189
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
|
+
}
|
|
190
303
|
parts.push(TEXT_MATCH_METHODS.has(method) ? `text=${text}` : text);
|
|
191
304
|
}
|
|
192
305
|
return parts.length > 0 ? parts.join(' ') : null;
|
|
@@ -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;
|