@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.
- package/lib/index.d.ts +98 -1
- package/lib/index.js +61 -11
- package/lib/rules/galata-prefer-filebrowser-helper.d.ts +5 -0
- package/lib/rules/galata-prefer-filebrowser-helper.js +105 -0
- 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/incorrect-translator-usage.d.ts +4 -0
- package/lib/rules/incorrect-translator-usage.js +208 -0
- package/lib/rules/no-dynamic-translation.d.ts +4 -0
- package/lib/rules/no-dynamic-translation.js +92 -0
- package/lib/rules/no-pageconfig-base-url.d.ts +4 -0
- package/lib/rules/no-pageconfig-base-url.js +37 -0
- package/lib/rules/no-schema-enum.d.ts +1 -1
- package/lib/rules/no-schema-enum.js +54 -2
- package/lib/rules/no-translation-concatenation.js +46 -84
- package/lib/rules/no-untranslated-string.js +7 -4
- package/lib/rules/plugin-activation-args.js +1 -2
- package/lib/rules/prefer-lazy-imports.d.ts +6 -0
- package/lib/rules/prefer-lazy-imports.js +308 -0
- package/lib/rules/prefer-signal-this-arg.d.ts +7 -0
- package/lib/rules/prefer-signal-this-arg.js +196 -0
- package/lib/rules/require-disposable-ownership.d.ts +5 -0
- package/lib/rules/require-disposable-ownership.js +120 -0
- package/lib/rules/require-disposable-transfer.d.ts +5 -0
- package/lib/rules/require-disposable-transfer.js +223 -0
- package/lib/rules/require-signal-cleanup.d.ts +8 -0
- package/lib/rules/require-signal-cleanup.js +164 -0
- package/lib/rules/require-signal-this-arg.d.ts +5 -0
- package/lib/rules/require-signal-this-arg.js +74 -0
- package/lib/rules/token-format.js +1 -2
- package/lib/utils/disposables.d.ts +64 -0
- package/lib/utils/disposables.js +1723 -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 +102 -0
- package/lib/utils/playwright-selectors.js +306 -0
- 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.d.ts +158 -0
- package/lib/utils/signal-lifetime.js +816 -0
- package/lib/utils/signals.d.ts +143 -0
- package/lib/utils/signals.js +601 -0
- package/lib/utils/translation.d.ts +45 -0
- package/lib/utils/translation.js +122 -0
- package/package.json +9 -2
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) Jupyter Development Team.
|
|
4
|
+
* Distributed under the terms of the Modified BSD License.
|
|
5
|
+
*/
|
|
6
|
+
const create_rule_1 = require("../utils/create-rule");
|
|
7
|
+
const playwright_selectors_1 = require("../utils/playwright-selectors");
|
|
8
|
+
// JupyterLab's main menu bar
|
|
9
|
+
const TOP_LEVEL_MENU_LABELS = 'File|Edit|View|Run|Kernel|Tabs|Settings|Help';
|
|
10
|
+
// The entire selector is a bare text query for a top-level menu label:
|
|
11
|
+
// `text=File`, `text="Settings"`.
|
|
12
|
+
const BARE_MENU_BAR_LABEL_PATTERN = new RegExp(`^text=["']?(?:${TOP_LEVEL_MENU_LABELS})["']?$`);
|
|
13
|
+
// Playwright's text pseudo-classes, which take the label in parentheses:
|
|
14
|
+
// `:has-text("File")`, `:text("File")`, `:text-is("File")`. Galata's own
|
|
15
|
+
// `getMenuBarItemLocator` builds `li:has(div.lm-MenuBar-itemLabel:text-is(…))`,
|
|
16
|
+
// so a test copying that shape has to be read here too.
|
|
17
|
+
const TEXT_PSEUDO_CLASS = ':?(?:has-)?text(?:-is)?\\(';
|
|
18
|
+
// A top-level menu label inside a larger selector. Only trusted when the same
|
|
19
|
+
// selector also carries menu markup (see MENU_MARKUP_PATTERN), e.g.
|
|
20
|
+
// `li[role="menuitem"]:has-text("File")`.
|
|
21
|
+
//
|
|
22
|
+
// The left boundary of the `text=` form accepts a plain space as well as `>>`
|
|
23
|
+
// because a locator chain such as
|
|
24
|
+
// `page.locator('.lm-MenuBar-item').getByText('File')` is joined into
|
|
25
|
+
// `.lm-MenuBar-item text=File`. The right boundary deliberately does not:
|
|
26
|
+
// allowing a space there would make `text=File Browser` match the label `File`.
|
|
27
|
+
// The pseudo-class form needs no boundary, because its closing parenthesis is
|
|
28
|
+
// one.
|
|
29
|
+
const SCOPED_MENU_BAR_LABEL_PATTERN = new RegExp(`(?:^|>>\\s*|\\s)text=["']?(?:${TOP_LEVEL_MENU_LABELS})["']?\\s*(?:$|>>)` +
|
|
30
|
+
`|${TEXT_PSEUDO_CLASS}["']?(?:${TOP_LEVEL_MENU_LABELS})["']?\\)`);
|
|
31
|
+
// Any `#jp-mainmenu-…` id, top-level menu or submenu.
|
|
32
|
+
//
|
|
33
|
+
// Every one of these names a popup, never a menu bar item. `MenuFactory` sets
|
|
34
|
+
// the id with `menu.id = item.id`, `Widget.id` writes it to `this.node.id`,
|
|
35
|
+
// and a `Menu`'s node is the `div` it adds `lm-Menu` to. Lumino's
|
|
36
|
+
// `MenuBar.Renderer.renderItem` builds the menu bar `li` from a class name, a
|
|
37
|
+
// dataset and ARIA attributes, and gives it no id at all. So `#jp-mainmenu-tabs`
|
|
38
|
+
// is the Tabs popup and `#jp-mainmenu-file-new` is the File > New popup; they
|
|
39
|
+
// differ by nesting depth only.
|
|
40
|
+
//
|
|
41
|
+
// Only JupyterLab's main menu carries these ids, so they say which menu is open
|
|
42
|
+
// on their own, without the lookback in `findMenuOrigin`.
|
|
43
|
+
const MAIN_MENU_ID_PATTERN = /#jp-mainmenu-/;
|
|
44
|
+
// Markers proving the selector is scoped inside an open popup menu. Note that
|
|
45
|
+
// `\blm-Menu\b` cannot match inside `lm-MenuBar` (there is no word boundary
|
|
46
|
+
// between `u` and `B`) but does match `lm-Menu-item`, `lm-Menu-content`, …
|
|
47
|
+
const POPUP_CONTAINER_PATTERN = /\blm-Menu\b|role\s*=\s*["']menu["']|#jp-mainmenu-/;
|
|
48
|
+
// The menu bar itself. Unlike a popup it is always on screen, so a selector
|
|
49
|
+
// carrying this needs nothing before it to say which menu it is on.
|
|
50
|
+
const MENU_BAR_CONTAINER_PATTERN = /lm-MenuBar\b/;
|
|
51
|
+
// Menu markup that does not resolve menu bar vs popup on its own: Lumino gives
|
|
52
|
+
// `role="menuitem"` to both menu bar items and popup items, and stamps
|
|
53
|
+
// `data-type="submenu"` on any item that opens a submenu.
|
|
54
|
+
//
|
|
55
|
+
// A toggleable item gets `role="menuitemcheckbox"` instead, which the View and
|
|
56
|
+
// Settings menus are full of, so both roles count. A submenu item gets no role
|
|
57
|
+
// at all and is reached through `data-type` instead.
|
|
58
|
+
const MENU_MARKUP_PATTERN = /role\s*=\s*["']menuitem(?:checkbox)?["']|lm-MenuBar\b|data-type\s*=\s*["']?submenu/;
|
|
59
|
+
// Any item label in the selector, whatever spelling it uses. Its presence is
|
|
60
|
+
// what makes a menu path suggestible, so `preferClickMenuItem` can be reported
|
|
61
|
+
// instead of the generic message.
|
|
62
|
+
const TEXT_SELECTOR_PATTERN = new RegExp(`text=|${TEXT_PSEUDO_CLASS}`);
|
|
63
|
+
// Menu items are activated with a single click, so a click is the gesture the
|
|
64
|
+
// rule reads. `dblclick`, `tap`, `press` and `fill` are left alone: a menu-ish
|
|
65
|
+
// selector combined with one of them means the test is doing something else.
|
|
66
|
+
//
|
|
67
|
+
// `hover` is the exception, and it is left alone anyway. `openLocator` opens a
|
|
68
|
+
// submenu by hovering its parent item, so a hover on a menu item does have a
|
|
69
|
+
// helper form, and reporting one would be defensible. It is skipped because a
|
|
70
|
+
// hover is also how a test positions the pointer for a screenshot, which is
|
|
71
|
+
// what the single corpus site doing it wants.
|
|
72
|
+
const MENU_INTERACTION_METHOD = 'click';
|
|
73
|
+
function readMenuEvidence(selectorText) {
|
|
74
|
+
const hasPopupContainer = POPUP_CONTAINER_PATTERN.test(selectorText);
|
|
75
|
+
const hasMenuMarkup = MENU_MARKUP_PATTERN.test(selectorText);
|
|
76
|
+
// The menu bar's own class names the target outright, whatever label it
|
|
77
|
+
// carries, so a third-party menu such as `Git` is covered by it even though
|
|
78
|
+
// the label list is not. Failing that, a top-level label is trusted unscoped
|
|
79
|
+
// (`text=File` and nothing else) or next to menu markup
|
|
80
|
+
// (`li[role="menuitem"]:has-text("File")`). Any other scope means the label
|
|
81
|
+
// is some other piece of UI text. No id appears here: every `#jp-mainmenu-…`
|
|
82
|
+
// names a popup, so it is a container instead.
|
|
83
|
+
const hasTopLevelMarker = MENU_BAR_CONTAINER_PATTERN.test(selectorText) ||
|
|
84
|
+
BARE_MENU_BAR_LABEL_PATTERN.test(selectorText) ||
|
|
85
|
+
(hasMenuMarkup && SCOPED_MENU_BAR_LABEL_PATTERN.test(selectorText));
|
|
86
|
+
return { hasPopupContainer, hasMenuMarkup, hasTopLevelMarker };
|
|
87
|
+
}
|
|
88
|
+
// `MenuHelper` methods that leave the main menu open. `open` and `getMenuItem`
|
|
89
|
+
// are deprecated in favour of the `…Locator` forms, and both spellings appear
|
|
90
|
+
// in current suites, so both are recognized.
|
|
91
|
+
const MENU_OPENING_HELPERS = new Set([
|
|
92
|
+
'open',
|
|
93
|
+
'openLocator',
|
|
94
|
+
'clickMenuItem'
|
|
95
|
+
]);
|
|
96
|
+
function menuOriginOf(node) {
|
|
97
|
+
const callee = node.callee;
|
|
98
|
+
if (callee.type === 'MemberExpression' &&
|
|
99
|
+
callee.property.type === 'Identifier') {
|
|
100
|
+
// `page.menu.openContextMenu(selector)` / `openContextMenuLocator(selector)`
|
|
101
|
+
if (callee.property.name.startsWith('openContextMenu')) {
|
|
102
|
+
return 'context';
|
|
103
|
+
}
|
|
104
|
+
// The helper form of a menu bar click. `openLocator` clicks the first path
|
|
105
|
+
// part and hovers the rest; `open` delegates to it; `clickMenuItem` calls
|
|
106
|
+
// it and then clicks the item. `getMenuItemLocator` is not here: it walks
|
|
107
|
+
// the same path but clicks and hovers nothing, so it opens no menu.
|
|
108
|
+
if (MENU_OPENING_HELPERS.has(callee.property.name) &&
|
|
109
|
+
callee.object.type === 'MemberExpression' &&
|
|
110
|
+
callee.object.property.type === 'Identifier' &&
|
|
111
|
+
callee.object.property.name === 'menu') {
|
|
112
|
+
return 'menubar';
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// A right-click is the one gesture that opens the context menu. Which element
|
|
116
|
+
// it targets does not matter here, so the chain does not have to resolve to
|
|
117
|
+
// `page` first: `const item = page.locator(a); item.click({ button:
|
|
118
|
+
// 'right' })` and `page.activity.getTabLocator(b).click({ button: 'right' })`
|
|
119
|
+
// open the context menu just as `page.click(a, { button: 'right' })` does.
|
|
120
|
+
if (callee.type === 'MemberExpression' &&
|
|
121
|
+
callee.property.type === 'Identifier' &&
|
|
122
|
+
callee.property.name === MENU_INTERACTION_METHOD &&
|
|
123
|
+
(0, playwright_selectors_1.isRightClick)(node)) {
|
|
124
|
+
return 'context';
|
|
125
|
+
}
|
|
126
|
+
const match = (0, playwright_selectors_1.matchSelectorInteraction)(node);
|
|
127
|
+
if (!match) {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
if (match.interactionMethod !== MENU_INTERACTION_METHOD) {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
const selectorText = (0, playwright_selectors_1.combineStaticSelectorText)(match);
|
|
134
|
+
if (selectorText === null) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
const evidence = readMenuEvidence(selectorText);
|
|
138
|
+
// Clicking a menu bar item — the same shape the rule reports as
|
|
139
|
+
// `preferMenuOpen` — is what opens the main menu.
|
|
140
|
+
return evidence.hasTopLevelMarker && !evidence.hasPopupContainer
|
|
141
|
+
? 'menubar'
|
|
142
|
+
: null;
|
|
143
|
+
}
|
|
144
|
+
function isNode(value) {
|
|
145
|
+
return (typeof value === 'object' &&
|
|
146
|
+
value !== null &&
|
|
147
|
+
typeof value.type === 'string');
|
|
148
|
+
}
|
|
149
|
+
// Calls whose callback is stored and run later. Statements next to such a call
|
|
150
|
+
// say nothing about the state its body starts in.
|
|
151
|
+
const DEFERRED_CALLBACK_CALLEES = new Set([
|
|
152
|
+
'test',
|
|
153
|
+
'it',
|
|
154
|
+
'describe',
|
|
155
|
+
'suite',
|
|
156
|
+
'beforeAll',
|
|
157
|
+
'beforeEach',
|
|
158
|
+
'afterAll',
|
|
159
|
+
'afterEach'
|
|
160
|
+
]);
|
|
161
|
+
function rootCalleeName(node) {
|
|
162
|
+
let current = node;
|
|
163
|
+
while (current.type === 'MemberExpression') {
|
|
164
|
+
current = current.object;
|
|
165
|
+
}
|
|
166
|
+
return current.type === 'Identifier' ? current.name : null;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Whether the lookback must stop before leaving `node`, and whether the scan of
|
|
170
|
+
* preceding statements must stop before entering it.
|
|
171
|
+
*
|
|
172
|
+
* A callback written inline runs where it stands, so the statements above it
|
|
173
|
+
* did run first and the walk continues through it: `perf.measure(async () => {
|
|
174
|
+
* … })` keeps the menu its caller opened. A named helper and a test callback do
|
|
175
|
+
* not. `async function openMenu(page) { … }` can be called from anywhere, and
|
|
176
|
+
* `test('b', …)` does not run after the body of `test('a', …)`, so a
|
|
177
|
+
* right-click in one test must not silence the next one.
|
|
178
|
+
*/
|
|
179
|
+
function isLookbackBoundary(node) {
|
|
180
|
+
if (node.type === 'FunctionDeclaration') {
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
if (node.type !== 'FunctionExpression' &&
|
|
184
|
+
node.type !== 'ArrowFunctionExpression') {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
const parent = node.parent;
|
|
188
|
+
if (parent?.type === 'VariableDeclarator' || parent?.type === 'Property') {
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
return (parent?.type === 'CallExpression' &&
|
|
192
|
+
parent.arguments.includes(node) &&
|
|
193
|
+
DEFERRED_CALLBACK_CALLEES.has(rootCalleeName(parent.callee) ?? ''));
|
|
194
|
+
}
|
|
195
|
+
function collectMenuOrigins(node, found) {
|
|
196
|
+
if (isLookbackBoundary(node)) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (node.type === 'CallExpression') {
|
|
200
|
+
const origin = menuOriginOf(node);
|
|
201
|
+
if (origin) {
|
|
202
|
+
found.push({ origin, start: node.range[0] });
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
for (const [key, value] of Object.entries(node)) {
|
|
206
|
+
// `parent` is a back-reference; following it would not terminate.
|
|
207
|
+
if (key === 'parent') {
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
if (Array.isArray(value)) {
|
|
211
|
+
for (const item of value) {
|
|
212
|
+
if (isNode(item)) {
|
|
213
|
+
collectMenuOrigins(item, found);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
else if (isNode(value)) {
|
|
218
|
+
collectMenuOrigins(value, found);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* The last menu-opening gesture among the first `index` statements of `block`.
|
|
224
|
+
*
|
|
225
|
+
* Each statement is read once per block and the running answer is kept, because
|
|
226
|
+
* a file that walks a menu many times asks about the same statements once per
|
|
227
|
+
* gesture; reading them again each time costs the square of their number. The
|
|
228
|
+
* scan grows only as far as it has been asked to, so a block with one gesture
|
|
229
|
+
* near the top never reads the rest of it.
|
|
230
|
+
*/
|
|
231
|
+
function originBeforeStatement(block, body, index, cache) {
|
|
232
|
+
let scan = cache.get(block);
|
|
233
|
+
if (!scan) {
|
|
234
|
+
scan = { prefix: [null], scanned: 0, latest: null };
|
|
235
|
+
cache.set(block, scan);
|
|
236
|
+
}
|
|
237
|
+
while (scan.scanned < index) {
|
|
238
|
+
const found = [];
|
|
239
|
+
collectMenuOrigins(body[scan.scanned], found);
|
|
240
|
+
for (const hit of found) {
|
|
241
|
+
if (!scan.latest || hit.start > scan.latest.start) {
|
|
242
|
+
scan.latest = hit;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
scan.prefix.push(scan.latest?.origin ?? null);
|
|
246
|
+
scan.scanned++;
|
|
247
|
+
}
|
|
248
|
+
return scan.prefix[index];
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* The kind of menu that was last opened before `node` runs, or `null` when
|
|
252
|
+
* nothing in the enclosing scopes says.
|
|
253
|
+
*
|
|
254
|
+
* Statements preceding `node` are read innermost block first, then outward, and
|
|
255
|
+
* the last menu-opening gesture in source order wins — clicking `File` after a
|
|
256
|
+
* right-click replaces the context menu with the main menu. An inner block's
|
|
257
|
+
* preceding statements all come after an outer block's, so the innermost block
|
|
258
|
+
* with an answer holds the latest one and the walk can stop there. The walk
|
|
259
|
+
* also stops at the enclosing test callback or named helper, so one test's menu
|
|
260
|
+
* state never reaches the next (see `isLookbackBoundary`).
|
|
261
|
+
*/
|
|
262
|
+
function findMenuOrigin(node, cache) {
|
|
263
|
+
let current = node;
|
|
264
|
+
let parent = current.parent;
|
|
265
|
+
while (parent) {
|
|
266
|
+
let body = null;
|
|
267
|
+
if (parent.type === 'BlockStatement' || parent.type === 'Program') {
|
|
268
|
+
body = parent.body;
|
|
269
|
+
}
|
|
270
|
+
else if (parent.type === 'SwitchCase') {
|
|
271
|
+
body = parent.consequent;
|
|
272
|
+
}
|
|
273
|
+
const index = body ? body.indexOf(current) : -1;
|
|
274
|
+
if (body && index > 0) {
|
|
275
|
+
const origin = originBeforeStatement(parent, body, index, cache);
|
|
276
|
+
if (origin) {
|
|
277
|
+
return origin;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (isLookbackBoundary(parent)) {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
current = parent;
|
|
284
|
+
parent = parent.parent;
|
|
285
|
+
}
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
// Any mention of menu markup at all, used to confirm that a bare top-level
|
|
289
|
+
// label really is the menu bar. Wider than the patterns above because it is
|
|
290
|
+
// matched against every string in the test, not against a gesture's selector.
|
|
291
|
+
const ANY_MENU_MARKUP_PATTERN = /\blm-Menu|#jp-mainmenu-|role\s*=\s*["']?menuitem|role\s*=\s*["']?menu["']?\s*\]|data-type\s*=\s*["']?submenu/;
|
|
292
|
+
// The word itself, matched only against a test title.
|
|
293
|
+
const MENU_WORD_PATTERN = /menus?\b/i;
|
|
294
|
+
/**
|
|
295
|
+
* Whether the test around `node` is about a menu.
|
|
296
|
+
*
|
|
297
|
+
* `page.click('text=File')` carries no DOM evidence: the eight built-in labels
|
|
298
|
+
* are ordinary words, and a dialog button reading `Run` looks the same. A test
|
|
299
|
+
* that walks the menu bar names the menu somewhere, in its title or in a
|
|
300
|
+
* selector that reaches the popup it opened, so that mention is what separates
|
|
301
|
+
* the two.
|
|
302
|
+
*
|
|
303
|
+
* The title takes the bare word, because a person wrote it to describe the
|
|
304
|
+
* test. Every other string has to carry real markup: `menu` inside a selector
|
|
305
|
+
* or a file name says nothing.
|
|
306
|
+
*/
|
|
307
|
+
function testMentionsMenuMarkup(node) {
|
|
308
|
+
let scope = node;
|
|
309
|
+
while (scope.parent && !isLookbackBoundary(scope)) {
|
|
310
|
+
scope = scope.parent;
|
|
311
|
+
}
|
|
312
|
+
// The title of a `test(…)` sits next to its callback rather than inside it.
|
|
313
|
+
// It is a sentence a person wrote about what the test does, so the bare word
|
|
314
|
+
// is enough there, where in a selector it would not be.
|
|
315
|
+
const enclosingCall = scope.parent;
|
|
316
|
+
if (enclosingCall?.type === 'CallExpression') {
|
|
317
|
+
const title = enclosingCall.arguments[0];
|
|
318
|
+
if (title?.type === 'Literal' &&
|
|
319
|
+
typeof title.value === 'string' &&
|
|
320
|
+
MENU_WORD_PATTERN.test(title.value)) {
|
|
321
|
+
return true;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
let found = false;
|
|
325
|
+
const visit = (current) => {
|
|
326
|
+
if (found) {
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
if ((current.type === 'Literal' && typeof current.value === 'string'
|
|
330
|
+
? ANY_MENU_MARKUP_PATTERN.test(current.value)
|
|
331
|
+
: false) ||
|
|
332
|
+
(current.type === 'TemplateElement' &&
|
|
333
|
+
ANY_MENU_MARKUP_PATTERN.test(current.value.cooked ?? ''))) {
|
|
334
|
+
found = true;
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
for (const [key, value] of Object.entries(current)) {
|
|
338
|
+
if (key === 'parent') {
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
if (Array.isArray(value)) {
|
|
342
|
+
for (const item of value) {
|
|
343
|
+
if (isNode(item)) {
|
|
344
|
+
visit(item);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
else if (isNode(value)) {
|
|
349
|
+
visit(value);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
visit(scope);
|
|
354
|
+
return found;
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Whether a click on popup menu markup is walking the main menu.
|
|
358
|
+
*
|
|
359
|
+
* Lumino gives every menu the same markup: `.lm-Menu` for the widget node,
|
|
360
|
+
* `role="menu"` for its content, `.lm-Menu-item` and `role="menuitem"` for its
|
|
361
|
+
* items. The context menu shares it, and so does every dropdown opened from a
|
|
362
|
+
* toolbar button. JupyterLab builds six of those on its own (the console
|
|
363
|
+
* prompt menu, the debugger pause-on-exceptions menu, two file editor menus,
|
|
364
|
+
* the terminal theme menu, the table of contents toolbar menu), and extensions
|
|
365
|
+
* add more, none of which `page.menu` drives. So popup markup alone proves
|
|
366
|
+
* nothing and the rule needs the main menu to have been opened first.
|
|
367
|
+
*
|
|
368
|
+
* A `#jp-mainmenu-…` id settles it from the selector. Otherwise the last
|
|
369
|
+
* menu-opening gesture before this one has to be a menu bar click, either raw
|
|
370
|
+
* or through `page.menu.open`.
|
|
371
|
+
*/
|
|
372
|
+
function isMainMenuTraversal(node, selectorText, cache) {
|
|
373
|
+
// No context menu and no dropdown carries a `#jp-mainmenu-…` id, and none is
|
|
374
|
+
// inside the menu bar, so either settles it regardless of what came before.
|
|
375
|
+
if (MAIN_MENU_ID_PATTERN.test(selectorText) ||
|
|
376
|
+
MENU_BAR_CONTAINER_PATTERN.test(selectorText)) {
|
|
377
|
+
return true;
|
|
378
|
+
}
|
|
379
|
+
return findMenuOrigin(node, cache) === 'menubar';
|
|
380
|
+
}
|
|
381
|
+
const galataPreferMenuHelper = (0, create_rule_1.createRule)({
|
|
382
|
+
name: 'galata-prefer-menu-helper',
|
|
383
|
+
meta: {
|
|
384
|
+
type: 'suggestion',
|
|
385
|
+
docs: {
|
|
386
|
+
description: 'Prefer the Galata menu helper over raw Playwright selectors for JupyterLab main menu traversal'
|
|
387
|
+
},
|
|
388
|
+
messages: {
|
|
389
|
+
preferMenuOpen: 'Prefer `page.menu.openLocator(path)` (or `page.menu.clickMenuItem(path)`) over clicking the main menu bar directly.',
|
|
390
|
+
preferClickMenuItem: "Prefer `page.menu.clickMenuItem(path)` (e.g. `'File>New>Terminal'`) over raw selectors to click a menu item.",
|
|
391
|
+
preferMenuHelper: 'Prefer the Galata `page.menu` helper (e.g. `page.menu.openLocator(path)`, `page.menu.isOpen(path)`) over raw main menu selectors.'
|
|
392
|
+
},
|
|
393
|
+
schema: []
|
|
394
|
+
},
|
|
395
|
+
defaultOptions: [],
|
|
396
|
+
create(context) {
|
|
397
|
+
// Keyed by block node, so it is per file without being reset by hand.
|
|
398
|
+
const prefixCache = new WeakMap();
|
|
399
|
+
return {
|
|
400
|
+
CallExpression(node) {
|
|
401
|
+
// Most call expressions are not interactions at all, and
|
|
402
|
+
// `matchSelectorInteraction` rejects them on the callee alone, so the
|
|
403
|
+
// scope is looked up only once something actually needs it.
|
|
404
|
+
let scope = null;
|
|
405
|
+
const currentScope = () => (scope ?? (scope = context.sourceCode.getScope(node)));
|
|
406
|
+
const match = (0, playwright_selectors_1.matchSelectorInteraction)(node, identifier => (0, playwright_selectors_1.resolveLocatorBinding)(identifier, currentScope()));
|
|
407
|
+
if (!match) {
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
if (match.interactionMethod !== MENU_INTERACTION_METHOD) {
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
// Right-clicks open the context menu, not the main menu.
|
|
414
|
+
if (match.isRightClick) {
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
const selectorText = (0, playwright_selectors_1.combineStaticSelectorText)(match);
|
|
418
|
+
if (selectorText === null) {
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
const { hasPopupContainer, hasMenuMarkup, hasTopLevelMarker } = readMenuEvidence(selectorText);
|
|
422
|
+
if (!hasTopLevelMarker && !hasPopupContainer && !hasMenuMarkup) {
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
// `getByRole('menuitem', { name })` carries no scope at all: a menu bar
|
|
426
|
+
// item, a main menu item and a right-click context menu item are all
|
|
427
|
+
// `role="menuitem"` with an accessible name. An exact top-level label
|
|
428
|
+
// is unambiguous on that evidence alone, and a popup container in the
|
|
429
|
+
// same chain settles it too. Failing both, the menu bar click that
|
|
430
|
+
// opened the menu answers it, which is the
|
|
431
|
+
// `getByRole('menuitem', { name: 'File' })` then
|
|
432
|
+
// `getByRole('menuitem', { name: 'Open from Path' })` idiom of the
|
|
433
|
+
// Notebook and JupyterLite suites. With none of the three the target
|
|
434
|
+
// could be a context menu item, which the planned context menu rule
|
|
435
|
+
// covers.
|
|
436
|
+
const viaGetByRole = match.selectorParts.some(part => part.method === 'getByRole');
|
|
437
|
+
if (viaGetByRole &&
|
|
438
|
+
!hasTopLevelMarker &&
|
|
439
|
+
!hasPopupContainer &&
|
|
440
|
+
findMenuOrigin(node, prefixCache) !== 'menubar') {
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
// A popup container proves the target sits inside an already open menu,
|
|
444
|
+
// so it wins over a top-level label appearing in the same selector.
|
|
445
|
+
if (hasTopLevelMarker && !hasPopupContainer) {
|
|
446
|
+
// A label on its own is just a word. `File`, `Run` and `Help` name
|
|
447
|
+
// dialog buttons and file names too, so a selector carrying nothing
|
|
448
|
+
// but the label needs the test to mention menu markup somewhere.
|
|
449
|
+
if (!hasMenuMarkup &&
|
|
450
|
+
!MAIN_MENU_ID_PATTERN.test(selectorText) &&
|
|
451
|
+
!testMentionsMenuMarkup(node)) {
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
context.report({
|
|
455
|
+
node: match.callNode,
|
|
456
|
+
messageId: 'preferMenuOpen'
|
|
457
|
+
});
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
// Everything left is a click inside some open popup menu. Which one it
|
|
461
|
+
// is has to come from what opened it; a context menu belongs to the
|
|
462
|
+
// planned context menu rule, and a toolbar dropdown to no rule at all.
|
|
463
|
+
if (!isMainMenuTraversal(node, selectorText, prefixCache)) {
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
// Without an item label there is no path to suggest, so fall back to
|
|
467
|
+
// the generic helper message.
|
|
468
|
+
context.report({
|
|
469
|
+
node: match.callNode,
|
|
470
|
+
messageId: TEXT_SELECTOR_PATTERN.test(selectorText)
|
|
471
|
+
? 'preferClickMenuItem'
|
|
472
|
+
: 'preferMenuHelper'
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
module.exports = galataPreferMenuHelper;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { TSESLint } from '@typescript-eslint/utils';
|
|
2
|
+
type MessageIds = 'preferSetCell' | 'preferEnterCellEditingMode' | 'preferSelectCells' | 'preferRunCell';
|
|
3
|
+
declare const galataPreferNotebookCellHelper: TSESLint.RuleModule<MessageIds, [], unknown, TSESLint.RuleListener> & {
|
|
4
|
+
name: string;
|
|
5
|
+
};
|
|
6
|
+
export = galataPreferNotebookCellHelper;
|