@jupyter/eslint-plugin 1.2.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.
- package/lib/index.d.ts +19 -1
- package/lib/index.js +14 -3
- package/lib/rules/galata-prefer-context-menu-helper.d.ts +6 -0
- package/lib/rules/galata-prefer-context-menu-helper.js +394 -0
- package/lib/rules/galata-prefer-menu-helper.js +18 -102
- package/lib/rules/no-untranslated-string.d.ts +2 -1
- package/lib/rules/no-untranslated-string.js +191 -113
- package/lib/rules/plugin-id-convention.d.ts +7 -0
- package/lib/rules/plugin-id-convention.js +296 -0
- package/lib/rules/prefer-lazy-imports.js +204 -36
- package/lib/rules/require-disposable-ownership.js +1 -16
- package/lib/rules/require-disposable-transfer.js +7 -2
- package/lib/utils/ast.d.ts +12 -0
- package/lib/utils/ast.js +49 -0
- package/lib/utils/disposables.d.ts +12 -0
- package/lib/utils/disposables.js +101 -10
- package/lib/utils/lazy-imports.d.ts +26 -3
- package/lib/utils/lazy-imports.js +121 -36
- package/lib/utils/module-size.d.ts +7 -1
- package/lib/utils/module-size.js +10 -1
- package/lib/utils/package-json.d.ts +8 -0
- package/lib/utils/package-json.js +76 -0
- package/lib/utils/playwright-selectors.d.ts +15 -0
- package/lib/utils/playwright-selectors.js +80 -0
- package/lib/utils/plugin-utils.d.ts +35 -5
- package/lib/utils/plugin-utils.js +119 -44
- package/lib/utils/shared-packages.js +6 -16
- package/lib/utils/signals.d.ts +2 -8
- package/lib/utils/signals.js +7 -42
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -12,6 +12,11 @@ declare const plugin: {
|
|
|
12
12
|
'plugin-description': import("@typescript-eslint/utils/ts-eslint").RuleModule<"missingDescription" | "emptyDescription", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
13
13
|
name: string;
|
|
14
14
|
};
|
|
15
|
+
'plugin-id-convention': import("@typescript-eslint/utils/ts-eslint").RuleModule<"mismatchedPrefix" | "idEqualsPackageName", [{
|
|
16
|
+
reportIdEqualToPackageName: boolean;
|
|
17
|
+
}], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
18
|
+
name: string;
|
|
19
|
+
};
|
|
15
20
|
'no-translation-concatenation': import("@typescript-eslint/utils/ts-eslint").RuleModule<"noConcatenation", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
16
21
|
name: string;
|
|
17
22
|
};
|
|
@@ -21,8 +26,9 @@ declare const plugin: {
|
|
|
21
26
|
'token-format': import("@typescript-eslint/utils/ts-eslint").RuleModule<"missingColon" | "invalidTokenSymbol", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
22
27
|
name: string;
|
|
23
28
|
};
|
|
24
|
-
'no-untranslated-string': import("@typescript-eslint/utils/ts-eslint").RuleModule<"untranslatedCommandProp" | "untranslatedSetAttribute" | "untranslatedPropertyAssign" | "
|
|
29
|
+
'no-untranslated-string': import("@typescript-eslint/utils/ts-eslint").RuleModule<"untranslatedCommandProp" | "untranslatedSetAttribute" | "untranslatedPropertyAssign" | "untranslatedDialogOption" | "untranslatedDialogButtonLabel" | "untranslatedJsxText" | "untranslatedJsxAttribute" | "untranslatedProperty", [{
|
|
25
30
|
enforcePunctuation: boolean;
|
|
31
|
+
checkProperties: string[];
|
|
26
32
|
}], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
27
33
|
name: string;
|
|
28
34
|
};
|
|
@@ -49,6 +55,9 @@ declare const plugin: {
|
|
|
49
55
|
}], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
50
56
|
name: string;
|
|
51
57
|
};
|
|
58
|
+
'galata-prefer-context-menu-helper': import("@typescript-eslint/utils/ts-eslint").RuleModule<"preferNotebookOpenNoKernel" | "preferFilebrowserOpenFactory" | "preferFilebrowserOpen", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
59
|
+
name: string;
|
|
60
|
+
};
|
|
52
61
|
'galata-prefer-filebrowser-helper': import("@typescript-eslint/utils/ts-eslint").RuleModule<"preferNotebookOpenByPath" | "preferOpenHomeDirectory" | "preferOpenDirectory" | "preferFilebrowserHelper", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
53
62
|
name: string;
|
|
54
63
|
};
|
|
@@ -81,6 +90,7 @@ declare const plugin: {
|
|
|
81
90
|
'jupyter/plugin-activation-args': string;
|
|
82
91
|
'jupyter/command-described-by': string;
|
|
83
92
|
'jupyter/plugin-description': string;
|
|
93
|
+
'jupyter/plugin-id-convention': string;
|
|
84
94
|
'jupyter/no-translation-concatenation': string;
|
|
85
95
|
'jupyter/no-dynamic-translation': string;
|
|
86
96
|
'jupyter/token-format': string;
|
|
@@ -95,6 +105,7 @@ declare const plugin: {
|
|
|
95
105
|
'jupyter/prefer-lazy-imports': string;
|
|
96
106
|
'jupyter/no-schema-enum'?: undefined;
|
|
97
107
|
'jupyter/require-soft-assertions-before-snapshots'?: undefined;
|
|
108
|
+
'jupyter/galata-prefer-context-menu-helper'?: undefined;
|
|
98
109
|
'jupyter/galata-prefer-filebrowser-helper'?: undefined;
|
|
99
110
|
'jupyter/galata-prefer-menu-helper'?: undefined;
|
|
100
111
|
'jupyter/galata-prefer-notebook-cell-helper'?: undefined;
|
|
@@ -111,6 +122,7 @@ declare const plugin: {
|
|
|
111
122
|
'jupyter/plugin-activation-args'?: undefined;
|
|
112
123
|
'jupyter/command-described-by'?: undefined;
|
|
113
124
|
'jupyter/plugin-description'?: undefined;
|
|
125
|
+
'jupyter/plugin-id-convention'?: undefined;
|
|
114
126
|
'jupyter/no-translation-concatenation'?: undefined;
|
|
115
127
|
'jupyter/no-dynamic-translation'?: undefined;
|
|
116
128
|
'jupyter/token-format'?: undefined;
|
|
@@ -124,6 +136,7 @@ declare const plugin: {
|
|
|
124
136
|
'jupyter/incorrect-translator-usage'?: undefined;
|
|
125
137
|
'jupyter/prefer-lazy-imports'?: undefined;
|
|
126
138
|
'jupyter/require-soft-assertions-before-snapshots'?: undefined;
|
|
139
|
+
'jupyter/galata-prefer-context-menu-helper'?: undefined;
|
|
127
140
|
'jupyter/galata-prefer-filebrowser-helper'?: undefined;
|
|
128
141
|
'jupyter/galata-prefer-menu-helper'?: undefined;
|
|
129
142
|
'jupyter/galata-prefer-notebook-cell-helper'?: undefined;
|
|
@@ -133,11 +146,13 @@ declare const plugin: {
|
|
|
133
146
|
files: string[];
|
|
134
147
|
rules: {
|
|
135
148
|
'jupyter/require-soft-assertions-before-snapshots': string;
|
|
149
|
+
'jupyter/galata-prefer-context-menu-helper': string;
|
|
136
150
|
'jupyter/galata-prefer-filebrowser-helper': string;
|
|
137
151
|
'jupyter/galata-prefer-menu-helper': string;
|
|
138
152
|
'jupyter/galata-prefer-notebook-cell-helper': string;
|
|
139
153
|
'jupyter/galata-prefer-sidebar-activity-helper': string;
|
|
140
154
|
'jupyter/prefer-lazy-imports': string;
|
|
155
|
+
'jupyter/plugin-id-convention': string;
|
|
141
156
|
'jupyter/plugin-activation-args'?: undefined;
|
|
142
157
|
'jupyter/command-described-by'?: undefined;
|
|
143
158
|
'jupyter/plugin-description'?: undefined;
|
|
@@ -161,6 +176,7 @@ declare const plugin: {
|
|
|
161
176
|
'jupyter/plugin-activation-args': string;
|
|
162
177
|
'jupyter/command-described-by': string;
|
|
163
178
|
'jupyter/plugin-description': string;
|
|
179
|
+
'jupyter/plugin-id-convention': string;
|
|
164
180
|
'jupyter/no-translation-concatenation': string;
|
|
165
181
|
'jupyter/no-dynamic-translation': string;
|
|
166
182
|
'jupyter/token-format': string;
|
|
@@ -179,11 +195,13 @@ declare const plugin: {
|
|
|
179
195
|
files: string[];
|
|
180
196
|
rules: {
|
|
181
197
|
'jupyter/require-soft-assertions-before-snapshots': string;
|
|
198
|
+
'jupyter/galata-prefer-context-menu-helper': string;
|
|
182
199
|
'jupyter/galata-prefer-filebrowser-helper': string;
|
|
183
200
|
'jupyter/galata-prefer-menu-helper': string;
|
|
184
201
|
'jupyter/galata-prefer-notebook-cell-helper': string;
|
|
185
202
|
'jupyter/galata-prefer-sidebar-activity-helper': string;
|
|
186
203
|
'jupyter/prefer-lazy-imports': string;
|
|
204
|
+
'jupyter/plugin-id-convention': string;
|
|
187
205
|
};
|
|
188
206
|
}[];
|
|
189
207
|
};
|
package/lib/index.js
CHANGED
|
@@ -43,6 +43,7 @@ const jsoncParser = __importStar(require("jsonc-eslint-parser"));
|
|
|
43
43
|
const plugin_activation_args_1 = __importDefault(require("./rules/plugin-activation-args"));
|
|
44
44
|
const command_described_by_1 = __importDefault(require("./rules/command-described-by"));
|
|
45
45
|
const plugin_description_1 = __importDefault(require("./rules/plugin-description"));
|
|
46
|
+
const plugin_id_convention_1 = __importDefault(require("./rules/plugin-id-convention"));
|
|
46
47
|
const no_translation_concatenation_1 = __importDefault(require("./rules/no-translation-concatenation"));
|
|
47
48
|
const no_dynamic_translation_1 = __importDefault(require("./rules/no-dynamic-translation"));
|
|
48
49
|
const token_format_1 = __importDefault(require("./rules/token-format"));
|
|
@@ -53,6 +54,7 @@ const no_pageconfig_base_url_1 = __importDefault(require("./rules/no-pageconfig-
|
|
|
53
54
|
const require_signal_cleanup_1 = __importDefault(require("./rules/require-signal-cleanup"));
|
|
54
55
|
const require_signal_this_arg_1 = __importDefault(require("./rules/require-signal-this-arg"));
|
|
55
56
|
const prefer_signal_this_arg_1 = __importDefault(require("./rules/prefer-signal-this-arg"));
|
|
57
|
+
const galata_prefer_context_menu_helper_1 = __importDefault(require("./rules/galata-prefer-context-menu-helper"));
|
|
56
58
|
const galata_prefer_filebrowser_helper_1 = __importDefault(require("./rules/galata-prefer-filebrowser-helper"));
|
|
57
59
|
const galata_prefer_menu_helper_1 = __importDefault(require("./rules/galata-prefer-menu-helper"));
|
|
58
60
|
const galata_prefer_notebook_cell_helper_1 = __importDefault(require("./rules/galata-prefer-notebook-cell-helper"));
|
|
@@ -66,6 +68,7 @@ const plugin = {
|
|
|
66
68
|
'plugin-activation-args': plugin_activation_args_1.default,
|
|
67
69
|
'command-described-by': command_described_by_1.default,
|
|
68
70
|
'plugin-description': plugin_description_1.default,
|
|
71
|
+
'plugin-id-convention': plugin_id_convention_1.default,
|
|
69
72
|
'no-translation-concatenation': no_translation_concatenation_1.default,
|
|
70
73
|
'no-dynamic-translation': no_dynamic_translation_1.default,
|
|
71
74
|
'token-format': token_format_1.default,
|
|
@@ -76,6 +79,7 @@ const plugin = {
|
|
|
76
79
|
'require-signal-cleanup': require_signal_cleanup_1.default,
|
|
77
80
|
'require-signal-this-arg': require_signal_this_arg_1.default,
|
|
78
81
|
'prefer-signal-this-arg': prefer_signal_this_arg_1.default,
|
|
82
|
+
'galata-prefer-context-menu-helper': galata_prefer_context_menu_helper_1.default,
|
|
79
83
|
'galata-prefer-filebrowser-helper': galata_prefer_filebrowser_helper_1.default,
|
|
80
84
|
'galata-prefer-menu-helper': galata_prefer_menu_helper_1.default,
|
|
81
85
|
'galata-prefer-notebook-cell-helper': galata_prefer_notebook_cell_helper_1.default,
|
|
@@ -93,6 +97,7 @@ const plugin = {
|
|
|
93
97
|
'jupyter/plugin-activation-args': 'error',
|
|
94
98
|
'jupyter/command-described-by': 'warn',
|
|
95
99
|
'jupyter/plugin-description': 'warn',
|
|
100
|
+
'jupyter/plugin-id-convention': 'warn',
|
|
96
101
|
'jupyter/no-translation-concatenation': 'error',
|
|
97
102
|
'jupyter/no-dynamic-translation': 'warn',
|
|
98
103
|
'jupyter/token-format': 'error',
|
|
@@ -118,12 +123,15 @@ const plugin = {
|
|
|
118
123
|
files: ['**/*.spec.ts', '**/*.spec.js', '**/*.test.ts', '**/*.test.js'],
|
|
119
124
|
rules: {
|
|
120
125
|
'jupyter/require-soft-assertions-before-snapshots': 'warn',
|
|
126
|
+
'jupyter/galata-prefer-context-menu-helper': 'warn',
|
|
121
127
|
'jupyter/galata-prefer-filebrowser-helper': 'warn',
|
|
122
128
|
'jupyter/galata-prefer-menu-helper': 'warn',
|
|
123
129
|
'jupyter/galata-prefer-notebook-cell-helper': 'warn',
|
|
124
130
|
'jupyter/galata-prefer-sidebar-activity-helper': 'warn',
|
|
125
|
-
// Test files declare mock plugins; deferring their imports is
|
|
126
|
-
|
|
131
|
+
// Test files declare mock plugins; deferring their imports is
|
|
132
|
+
// pointless and their IDs are never used to disable an extension.
|
|
133
|
+
'jupyter/prefer-lazy-imports': 'off',
|
|
134
|
+
'jupyter/plugin-id-convention': 'off'
|
|
127
135
|
}
|
|
128
136
|
}
|
|
129
137
|
],
|
|
@@ -132,6 +140,7 @@ const plugin = {
|
|
|
132
140
|
'jupyter/plugin-activation-args': 'error',
|
|
133
141
|
'jupyter/command-described-by': 'warn',
|
|
134
142
|
'jupyter/plugin-description': 'warn',
|
|
143
|
+
'jupyter/plugin-id-convention': 'warn',
|
|
135
144
|
'jupyter/no-translation-concatenation': 'error',
|
|
136
145
|
'jupyter/no-dynamic-translation': 'warn',
|
|
137
146
|
'jupyter/token-format': 'error',
|
|
@@ -156,11 +165,13 @@ const plugin = {
|
|
|
156
165
|
],
|
|
157
166
|
rules: {
|
|
158
167
|
'jupyter/require-soft-assertions-before-snapshots': 'warn',
|
|
168
|
+
'jupyter/galata-prefer-context-menu-helper': 'warn',
|
|
159
169
|
'jupyter/galata-prefer-filebrowser-helper': 'warn',
|
|
160
170
|
'jupyter/galata-prefer-menu-helper': 'warn',
|
|
161
171
|
'jupyter/galata-prefer-notebook-cell-helper': 'warn',
|
|
162
172
|
'jupyter/galata-prefer-sidebar-activity-helper': 'warn',
|
|
163
|
-
'jupyter/prefer-lazy-imports': 'off'
|
|
173
|
+
'jupyter/prefer-lazy-imports': 'off',
|
|
174
|
+
'jupyter/plugin-id-convention': 'off'
|
|
164
175
|
}
|
|
165
176
|
}
|
|
166
177
|
]
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { TSESLint } from '@typescript-eslint/utils';
|
|
2
|
+
type MessageIds = 'preferNotebookOpenNoKernel' | 'preferFilebrowserOpenFactory' | 'preferFilebrowserOpen';
|
|
3
|
+
declare const galataPreferContextMenuHelper: TSESLint.RuleModule<MessageIds, [], unknown, TSESLint.RuleListener> & {
|
|
4
|
+
name: string;
|
|
5
|
+
};
|
|
6
|
+
export = galataPreferContextMenuHelper;
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) Jupyter Development Team.
|
|
4
|
+
* Distributed under the terms of the Modified BSD License.
|
|
5
|
+
*/
|
|
6
|
+
const ast_1 = require("../utils/ast");
|
|
7
|
+
const create_rule_1 = require("../utils/create-rule");
|
|
8
|
+
const playwright_selectors_1 = require("../utils/playwright-selectors");
|
|
9
|
+
// The label of the file browser context menu submenu that lists the document
|
|
10
|
+
// factories a file can be opened with.
|
|
11
|
+
const OPEN_WITH_LABEL_PATTERN = /^open with$/i;
|
|
12
|
+
// The factory `notebook-extension` adds to `Open With` for a notebook opened
|
|
13
|
+
// without starting a kernel.
|
|
14
|
+
const NO_KERNEL_FACTORY_PATTERN = /^notebook \(no kernel\)$/i;
|
|
15
|
+
// Markup proving a selector is scoped inside an open Lumino popup menu — the
|
|
16
|
+
// submenu, once `Open With` has been opened.
|
|
17
|
+
const POPUP_MENU_PATTERN = /\blm-Menu\b|role\s*=\s*["']menuitem(?:checkbox)?["']|role\s*=\s*["']menu["']/;
|
|
18
|
+
// The whole selector is a text query, `text=Markdown Preview`. Once the
|
|
19
|
+
// `Open With` submenu is open a bare label click lands in it, which is how
|
|
20
|
+
// every corpus site spells the factory click.
|
|
21
|
+
const BARE_TEXT_QUERY_PATTERN = /^text=/;
|
|
22
|
+
// A file browser listing item.
|
|
23
|
+
const FILE_BROWSER_ITEM_PATTERN = /\bjp-DirListing-item\b/;
|
|
24
|
+
// A label carried by a `text=` engine query, which `combineStaticSelectorText`
|
|
25
|
+
// also normalizes `getByText(…)` and `getByRole(role, { name })` into.
|
|
26
|
+
const TEXT_QUERY_LABEL_PATTERN = /(?:^|\s|>>\s*)text=\s*(.+)$/;
|
|
27
|
+
// The same label written with one of Playwright's text pseudo-classes:
|
|
28
|
+
// `:has-text("Editor")`, `:text("Editor")`, `:text-is("Editor")`. The quoted
|
|
29
|
+
// form is tried first so that a label containing a `)` survives.
|
|
30
|
+
const QUOTED_PSEUDO_LABEL_PATTERN = /:(?:has-)?text(?:-is)?\(\s*["'](.+?)["']\s*\)/;
|
|
31
|
+
const BARE_PSEUDO_LABEL_PATTERN = /:(?:has-)?text(?:-is)?\(\s*([^"')][^)]*?)\s*\)/;
|
|
32
|
+
// Keys that extend a file browser selection rather than moving it. A context
|
|
33
|
+
// menu opened over several selected files acts on all of them, and no helper
|
|
34
|
+
// opens more than one document, so the rule stays quiet there.
|
|
35
|
+
//
|
|
36
|
+
// Only these spellings count. `Shift+Enter` and `Control+Enter` are notebook
|
|
37
|
+
// run shortcuts and select nothing, so a blanket "any modified key" test would
|
|
38
|
+
// silence genuine findings.
|
|
39
|
+
const SELECTION_KEY_PATTERN = /^(?:Shift|Control|Meta|ControlOrMeta)\+(?:Arrow(?:Up|Down|Left|Right)|Home|End|[aA])$/;
|
|
40
|
+
// Modifiers that turn a click into a selection change: shift extends the
|
|
41
|
+
// range, control/meta toggles one item.
|
|
42
|
+
const SELECTION_MODIFIERS = new Set([
|
|
43
|
+
'Shift',
|
|
44
|
+
'Control',
|
|
45
|
+
'Meta',
|
|
46
|
+
'ControlOrMeta'
|
|
47
|
+
]);
|
|
48
|
+
// Interactions that only observe. A wait between the right-click and the
|
|
49
|
+
// factory click does not change what is on screen, so it must not break the
|
|
50
|
+
// sequence.
|
|
51
|
+
const OBSERVING_METHODS = new Set(['waitForSelector']);
|
|
52
|
+
/**
|
|
53
|
+
* The item label a selector looks for, or null when it carries none.
|
|
54
|
+
*
|
|
55
|
+
* A label is required to have a word character in it, so that the leftover
|
|
56
|
+
* static text of a fully interpolated name — `` `text=${factory}` `` reduces to
|
|
57
|
+
* `text=` — does not read as a label.
|
|
58
|
+
*/
|
|
59
|
+
function extractItemLabel(selectorText) {
|
|
60
|
+
const match = TEXT_QUERY_LABEL_PATTERN.exec(selectorText) ??
|
|
61
|
+
QUOTED_PSEUDO_LABEL_PATTERN.exec(selectorText) ??
|
|
62
|
+
BARE_PSEUDO_LABEL_PATTERN.exec(selectorText);
|
|
63
|
+
if (!match) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
const label = match[1]
|
|
67
|
+
.trim()
|
|
68
|
+
.replace(/^["']|["']$/g, '')
|
|
69
|
+
.trim();
|
|
70
|
+
return /\w/.test(label) ? label : null;
|
|
71
|
+
}
|
|
72
|
+
function isPageKeyboard(node) {
|
|
73
|
+
return (node.type === 'MemberExpression' &&
|
|
74
|
+
!node.computed &&
|
|
75
|
+
node.object.type === 'Identifier' &&
|
|
76
|
+
node.object.name === 'page' &&
|
|
77
|
+
node.property.type === 'Identifier' &&
|
|
78
|
+
node.property.name === 'keyboard');
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Whether a call's receiver is Galata's menu helper, `….menu`.
|
|
82
|
+
*/
|
|
83
|
+
function isMenuHelperReceiver(node) {
|
|
84
|
+
return (node.type === 'MemberExpression' &&
|
|
85
|
+
!node.computed &&
|
|
86
|
+
node.property.type === 'Identifier' &&
|
|
87
|
+
node.property.name === 'menu');
|
|
88
|
+
}
|
|
89
|
+
/** `page.menu.closeAll()`, which dismisses whatever menu is open. */
|
|
90
|
+
function isMenuCloseAll(callee) {
|
|
91
|
+
return (callee.property.type === 'Identifier' &&
|
|
92
|
+
callee.property.name === 'closeAll' &&
|
|
93
|
+
isMenuHelperReceiver(callee.object));
|
|
94
|
+
}
|
|
95
|
+
/** `page.click(sel, { modifiers: ['Shift'] })` and friends. */
|
|
96
|
+
function hasSelectionModifier(node) {
|
|
97
|
+
return node.arguments.some(arg => arg.type === 'ObjectExpression' &&
|
|
98
|
+
arg.properties.some(prop => prop.type === 'Property' &&
|
|
99
|
+
!prop.computed &&
|
|
100
|
+
((prop.key.type === 'Identifier' && prop.key.name === 'modifiers') ||
|
|
101
|
+
(prop.key.type === 'Literal' && prop.key.value === 'modifiers')) &&
|
|
102
|
+
prop.value.type === 'ArrayExpression' &&
|
|
103
|
+
prop.value.elements.some(element => element?.type === 'Literal' &&
|
|
104
|
+
typeof element.value === 'string' &&
|
|
105
|
+
SELECTION_MODIFIERS.has(element.value))));
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Reads one call as a step of the context menu flow, or null when the call
|
|
109
|
+
* changes nothing the rule tracks.
|
|
110
|
+
*/
|
|
111
|
+
function classifyGesture(node, resolveBinding) {
|
|
112
|
+
const gesture = (kind, label = null) => ({
|
|
113
|
+
kind,
|
|
114
|
+
start: node.range[0],
|
|
115
|
+
node,
|
|
116
|
+
label
|
|
117
|
+
});
|
|
118
|
+
const callee = node.callee;
|
|
119
|
+
if (callee.type === 'MemberExpression' &&
|
|
120
|
+
!callee.computed &&
|
|
121
|
+
callee.property.type === 'Identifier') {
|
|
122
|
+
// `page.menu.openContextMenu(selector)` / `openContextMenuLocator(selector)`
|
|
123
|
+
// right-click the target themselves, so they open the context menu exactly
|
|
124
|
+
// as a raw right-click does. A test that uses them and then walks
|
|
125
|
+
// `Open With` by hand is still the sequence this rule replaces.
|
|
126
|
+
if (callee.property.name.startsWith('openContextMenu') &&
|
|
127
|
+
isMenuHelperReceiver(callee.object)) {
|
|
128
|
+
return gesture('contextMenuOpen');
|
|
129
|
+
}
|
|
130
|
+
if (isMenuCloseAll(callee)) {
|
|
131
|
+
return gesture('menuClose');
|
|
132
|
+
}
|
|
133
|
+
if (callee.property.name === 'press' && isPageKeyboard(callee.object)) {
|
|
134
|
+
const keyArg = node.arguments[0];
|
|
135
|
+
const key = keyArg && keyArg.type !== 'SpreadElement'
|
|
136
|
+
? (0, playwright_selectors_1.extractStaticSelectorText)(keyArg, { allowPartialTemplate: false })
|
|
137
|
+
: null;
|
|
138
|
+
if (key === 'Escape') {
|
|
139
|
+
return gesture('menuClose');
|
|
140
|
+
}
|
|
141
|
+
if (key !== null && SELECTION_KEY_PATTERN.test(key)) {
|
|
142
|
+
return gesture('multiSelect');
|
|
143
|
+
}
|
|
144
|
+
return gesture('otherGesture');
|
|
145
|
+
}
|
|
146
|
+
// A right-click is the one gesture that opens the context menu, whatever it
|
|
147
|
+
// targets, so the receiver does not have to resolve to `page` first:
|
|
148
|
+
// `const item = page.locator(a); await item.click({ button: 'right' });`
|
|
149
|
+
// opens it just as `page.click(a, { button: 'right' })` does.
|
|
150
|
+
if (callee.property.name === 'click') {
|
|
151
|
+
if ((0, playwright_selectors_1.isRightClick)(node)) {
|
|
152
|
+
return gesture('contextMenuOpen');
|
|
153
|
+
}
|
|
154
|
+
if (hasSelectionModifier(node)) {
|
|
155
|
+
return gesture('multiSelect');
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const match = (0, playwright_selectors_1.matchSelectorInteraction)(node, resolveBinding);
|
|
160
|
+
if (!match) {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
if (OBSERVING_METHODS.has(match.interactionMethod)) {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
const selectorText = (0, playwright_selectors_1.combineStaticSelectorText)(match);
|
|
167
|
+
if (selectorText === null) {
|
|
168
|
+
// An interaction on a selector the rule cannot read still happened, and it
|
|
169
|
+
// may have dismissed the menu, so it counts as a gesture.
|
|
170
|
+
return gesture('otherGesture');
|
|
171
|
+
}
|
|
172
|
+
const label = extractItemLabel(selectorText);
|
|
173
|
+
// The submenu opens on hover and on click alike: Lumino opens a submenu when
|
|
174
|
+
// the pointer rests on its parent item, and clicking the item opens it too.
|
|
175
|
+
// Both spellings appear in the corpus.
|
|
176
|
+
if (label !== null &&
|
|
177
|
+
OPEN_WITH_LABEL_PATTERN.test(label) &&
|
|
178
|
+
(match.interactionMethod === 'hover' || match.interactionMethod === 'click')) {
|
|
179
|
+
return gesture('openWith');
|
|
180
|
+
}
|
|
181
|
+
// A menu item is activated with a single click. A hover on a factory item is
|
|
182
|
+
// deliberately not one: it opens nothing, and the tests that hover a factory
|
|
183
|
+
// are screenshotting the submenu, which no helper can do.
|
|
184
|
+
if (match.interactionMethod === 'click') {
|
|
185
|
+
if (POPUP_MENU_PATTERN.test(selectorText)) {
|
|
186
|
+
return gesture('menuItemClick', label);
|
|
187
|
+
}
|
|
188
|
+
// A null label here means the whole name was interpolated,
|
|
189
|
+
// `` page.click(`text=${factory}`) ``. The click is still the one that
|
|
190
|
+
// activates an item, so the flow is complete and the message falls back to
|
|
191
|
+
// naming the helpers without an argument.
|
|
192
|
+
if (BARE_TEXT_QUERY_PATTERN.test(selectorText)) {
|
|
193
|
+
return gesture('menuItemClick', label);
|
|
194
|
+
}
|
|
195
|
+
// A plain click on a listing item leaves that one file selected, so a
|
|
196
|
+
// right-click after it acts on a single document again even if an earlier
|
|
197
|
+
// statement had extended the selection.
|
|
198
|
+
if (FILE_BROWSER_ITEM_PATTERN.test(selectorText)) {
|
|
199
|
+
return gesture('singleSelect');
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return gesture('otherGesture');
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Every gesture inside `scope`, in source order.
|
|
206
|
+
*
|
|
207
|
+
* Nested test callbacks and named helpers are not entered: their statements do
|
|
208
|
+
* not run where they are written, so one test's menu state must not reach the
|
|
209
|
+
* next. Gestures inside an `if` or a loop *are*
|
|
210
|
+
* collected, because the flow the rule looks for is a fixed three-step sequence
|
|
211
|
+
* that no corpus site splits across branches.
|
|
212
|
+
*/
|
|
213
|
+
function collectGestures(scope, resolveBinding) {
|
|
214
|
+
const gestures = [];
|
|
215
|
+
(0, ast_1.walkFrom)(scope, node => {
|
|
216
|
+
if (node !== scope && (0, playwright_selectors_1.isTestScopeBoundary)(node)) {
|
|
217
|
+
return 'skip-children';
|
|
218
|
+
}
|
|
219
|
+
if (node.type === 'CallExpression') {
|
|
220
|
+
const gesture = classifyGesture(node, resolveBinding);
|
|
221
|
+
if (gesture) {
|
|
222
|
+
gestures.push(gesture);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
// A gesture can sit inside another call's arguments — `Promise.all([…,
|
|
226
|
+
// page.click(…)])` — so children are visited whether or not this node was
|
|
227
|
+
// itself a gesture.
|
|
228
|
+
return undefined;
|
|
229
|
+
});
|
|
230
|
+
return gestures.sort((left, right) => left.start - right.start);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Whether the call is the whole of its statement, `await page.click(…);`.
|
|
234
|
+
*/
|
|
235
|
+
function isOwnStatement(node) {
|
|
236
|
+
let current = node;
|
|
237
|
+
let parent = current.parent;
|
|
238
|
+
while (parent &&
|
|
239
|
+
(parent.type === 'AwaitExpression' ||
|
|
240
|
+
parent.type === 'TSNonNullExpression' ||
|
|
241
|
+
parent.type === 'TSAsExpression')) {
|
|
242
|
+
current = parent;
|
|
243
|
+
parent = current.parent;
|
|
244
|
+
}
|
|
245
|
+
return parent?.type === 'ExpressionStatement';
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Replays the gestures of one test and returns the factory clicks that complete
|
|
249
|
+
* a raw `Open With` sequence, keyed by the call to report.
|
|
250
|
+
*
|
|
251
|
+
* The flow is three steps: something opens the context menu, `Open With` opens
|
|
252
|
+
* the factory submenu, and a click on a factory opens the document. The factory
|
|
253
|
+
* click has to be the first item click after `Open With` — an item clicked
|
|
254
|
+
* later is in some other menu, because activating an item closes the one it was
|
|
255
|
+
* in.
|
|
256
|
+
*/
|
|
257
|
+
function findOpenWithFlows(scope, resolveBinding) {
|
|
258
|
+
const findings = new Map();
|
|
259
|
+
// The open context menu, and whether more than one file was selected when it
|
|
260
|
+
// was opened.
|
|
261
|
+
let contextMenu = null;
|
|
262
|
+
let multiSelectSeen = false;
|
|
263
|
+
let openWithOpen = false;
|
|
264
|
+
for (const gesture of collectGestures(scope, resolveBinding)) {
|
|
265
|
+
switch (gesture.kind) {
|
|
266
|
+
case 'multiSelect':
|
|
267
|
+
multiSelectSeen = true;
|
|
268
|
+
break;
|
|
269
|
+
case 'singleSelect':
|
|
270
|
+
multiSelectSeen = false;
|
|
271
|
+
// Clicking in the file browser also dismisses whatever menu was open.
|
|
272
|
+
contextMenu = null;
|
|
273
|
+
openWithOpen = false;
|
|
274
|
+
break;
|
|
275
|
+
case 'contextMenuOpen':
|
|
276
|
+
// The selection outlives the menu, so `multiSelectSeen` is not reset
|
|
277
|
+
// here. A right-click on an item that is already part of a multi-file
|
|
278
|
+
// selection keeps the whole selection, and only a plain click narrows
|
|
279
|
+
// it back to one. Which of the two a right-click is cannot be read off
|
|
280
|
+
// the selector: `Shift+ArrowDown` extends the selection to a neighbour
|
|
281
|
+
// the test never names, so the item right-clicked next may well be
|
|
282
|
+
// that neighbour. Staying with the wider selection is the answer that
|
|
283
|
+
// does not invent a report.
|
|
284
|
+
contextMenu = { multiSelect: multiSelectSeen };
|
|
285
|
+
openWithOpen = false;
|
|
286
|
+
break;
|
|
287
|
+
case 'menuClose':
|
|
288
|
+
contextMenu = null;
|
|
289
|
+
openWithOpen = false;
|
|
290
|
+
break;
|
|
291
|
+
case 'openWith':
|
|
292
|
+
// `Open With` only exists in the file browser context menu, so with
|
|
293
|
+
// nothing having opened one the hover is on something else, or on a
|
|
294
|
+
// context menu opened outside this test — either way the rule has no
|
|
295
|
+
// sequence to replace.
|
|
296
|
+
openWithOpen = contextMenu !== null;
|
|
297
|
+
break;
|
|
298
|
+
case 'menuItemClick': {
|
|
299
|
+
if (openWithOpen && contextMenu) {
|
|
300
|
+
const finding = describeFlow(gesture, contextMenu.multiSelect);
|
|
301
|
+
if (finding) {
|
|
302
|
+
findings.set(gesture.node, finding);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
// Activating an item closes the menu it was in.
|
|
306
|
+
contextMenu = null;
|
|
307
|
+
openWithOpen = false;
|
|
308
|
+
break;
|
|
309
|
+
}
|
|
310
|
+
case 'otherGesture':
|
|
311
|
+
// A gesture the rule cannot place may have dismissed the menu, so what
|
|
312
|
+
// is on screen is no longer known.
|
|
313
|
+
contextMenu = null;
|
|
314
|
+
openWithOpen = false;
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return findings;
|
|
319
|
+
}
|
|
320
|
+
function describeFlow(gesture, multiSelect) {
|
|
321
|
+
// Every helper opens exactly one document. A context menu opened over a
|
|
322
|
+
// multi-file selection opens one per file, which is what the test is there
|
|
323
|
+
// to check, so there is nothing to recommend.
|
|
324
|
+
if (multiSelect) {
|
|
325
|
+
return null;
|
|
326
|
+
}
|
|
327
|
+
if (!isOwnStatement(gesture.node)) {
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
const { label } = gesture;
|
|
331
|
+
if (label !== null && NO_KERNEL_FACTORY_PATTERN.test(label)) {
|
|
332
|
+
return { messageId: 'preferNotebookOpenNoKernel', factory: label };
|
|
333
|
+
}
|
|
334
|
+
return label === null
|
|
335
|
+
? { messageId: 'preferFilebrowserOpen', factory: null }
|
|
336
|
+
: { messageId: 'preferFilebrowserOpenFactory', factory: label };
|
|
337
|
+
}
|
|
338
|
+
const galataPreferContextMenuHelper = (0, create_rule_1.createRule)({
|
|
339
|
+
name: 'galata-prefer-context-menu-helper',
|
|
340
|
+
meta: {
|
|
341
|
+
type: 'suggestion',
|
|
342
|
+
docs: {
|
|
343
|
+
description: 'Prefer the Galata filebrowser and notebook open helpers over a raw right-click `Open With` context menu sequence'
|
|
344
|
+
},
|
|
345
|
+
messages: {
|
|
346
|
+
preferNotebookOpenNoKernel: 'Prefer `page.notebook.open(name, { noKernel: true })` over a raw right-click, `Open With`, `Notebook (no kernel)` sequence.',
|
|
347
|
+
preferFilebrowserOpenFactory: "Prefer `page.filebrowser.open(path, '{{factory}}')` over a raw right-click, `Open With`, factory sequence.",
|
|
348
|
+
preferFilebrowserOpen: 'Prefer `page.filebrowser.open(path, factory)` (or `page.notebook.open(name, { noKernel: true })`) over a raw right-click, `Open With`, factory sequence.'
|
|
349
|
+
},
|
|
350
|
+
schema: []
|
|
351
|
+
},
|
|
352
|
+
defaultOptions: [],
|
|
353
|
+
create(context) {
|
|
354
|
+
// One replay per test, keyed by its scope node, so a file that opens
|
|
355
|
+
// several documents this way does not re-read the same statements once per
|
|
356
|
+
// click.
|
|
357
|
+
const scopeCache = new WeakMap();
|
|
358
|
+
const resolveBinding = (identifier) => {
|
|
359
|
+
const scope = context.sourceCode.getScope(identifier);
|
|
360
|
+
return (0, playwright_selectors_1.resolveLocatorBinding)(identifier, scope);
|
|
361
|
+
};
|
|
362
|
+
return {
|
|
363
|
+
CallExpression(node) {
|
|
364
|
+
// The reported call is always a click on a factory item, so everything
|
|
365
|
+
// else can be skipped before any scope is walked.
|
|
366
|
+
const callee = node.callee;
|
|
367
|
+
if (callee.type !== 'MemberExpression' ||
|
|
368
|
+
callee.computed ||
|
|
369
|
+
callee.property.type !== 'Identifier' ||
|
|
370
|
+
callee.property.name !== 'click') {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
const scope = (0, playwright_selectors_1.enclosingTestScope)(node);
|
|
374
|
+
let findings = scopeCache.get(scope);
|
|
375
|
+
if (!findings) {
|
|
376
|
+
findings = findOpenWithFlows(scope, resolveBinding);
|
|
377
|
+
scopeCache.set(scope, findings);
|
|
378
|
+
}
|
|
379
|
+
const finding = findings.get(node);
|
|
380
|
+
if (!finding) {
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
context.report({
|
|
384
|
+
node,
|
|
385
|
+
messageId: finding.messageId,
|
|
386
|
+
...(finding.factory !== null
|
|
387
|
+
? { data: { factory: finding.factory } }
|
|
388
|
+
: {})
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
module.exports = galataPreferContextMenuHelper;
|