@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,332 @@
|
|
|
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
|
+
// Keyed by the start of the caption the widget puts in `title.caption`, which
|
|
9
|
+
// is what Lumino renders into the `title` attribute of the tab. Five of the six
|
|
10
|
+
// captions are exactly the key; the file browser appends its keyboard shortcut,
|
|
11
|
+
// so its caption reads `File Browser (Ctrl+Shift+F)`.
|
|
12
|
+
const SIDEBAR_TITLE_TO_TAB = new Map([
|
|
13
|
+
['Debugger', { id: 'jp-debugger-sidebar', side: 'right' }],
|
|
14
|
+
['Extension Manager', { id: 'extensionmanager.main-view', side: 'left' }],
|
|
15
|
+
['File Browser', { id: 'filebrowser', side: 'left' }],
|
|
16
|
+
['Property Inspector', { id: 'jp-property-inspector', side: 'right' }],
|
|
17
|
+
[
|
|
18
|
+
'Running Terminals and Kernels',
|
|
19
|
+
{ id: 'jp-running-sessions', side: 'left' }
|
|
20
|
+
],
|
|
21
|
+
['Table of Contents', { id: 'table-of-contents', side: 'left' }]
|
|
22
|
+
]);
|
|
23
|
+
// Options that change only how long Playwright waits, never what the gesture
|
|
24
|
+
// does, so the helper still reproduces it. Every other key changes it:
|
|
25
|
+
// `button` opens a context menu, `clickCount` opens and then closes the tab,
|
|
26
|
+
// `modifiers` makes a different gesture, `force` skips the actionability
|
|
27
|
+
// checks `openTab` relies on, and `position` aims at a point inside the tab.
|
|
28
|
+
const NEUTRAL_OPTION_KEYS = new Set([
|
|
29
|
+
'timeout',
|
|
30
|
+
'noWaitAfter',
|
|
31
|
+
'delay'
|
|
32
|
+
]);
|
|
33
|
+
/**
|
|
34
|
+
* True when the click carries an option the sidebar and activity helpers
|
|
35
|
+
* cannot reproduce, so that only the plain gesture is ever reported. A
|
|
36
|
+
* computed key or a spread could carry any of them, so both count as
|
|
37
|
+
* unsupported.
|
|
38
|
+
*/
|
|
39
|
+
function hasUnsupportedOptions(node) {
|
|
40
|
+
for (const arg of node.arguments) {
|
|
41
|
+
if (arg.type !== 'ObjectExpression') {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
for (const prop of arg.properties) {
|
|
45
|
+
if (prop.type === 'SpreadElement' || prop.computed) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
const key = prop.key.type === 'Identifier'
|
|
49
|
+
? prop.key.name
|
|
50
|
+
: prop.key.type === 'Literal'
|
|
51
|
+
? prop.key.value
|
|
52
|
+
: null;
|
|
53
|
+
if (typeof key !== 'string' || !NEUTRAL_OPTION_KEYS.has(key)) {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
// Galata's own `buildTabSelector` picks a sidebar tab by `data-id`, so a test
|
|
61
|
+
// written against it names the id rather than the caption.
|
|
62
|
+
const SIDEBAR_ID_TO_TAB = new Map([...SIDEBAR_TITLE_TO_TAB].map(([title, tab]) => [tab.id, { title, ...tab }]));
|
|
63
|
+
const DATA_ID_ATTRIBUTE_PATTERN = /\[\s*data-id\s*=\s*(?:"([^"]+)"|'([^']+)')\s*\]/g;
|
|
64
|
+
// The operator is captured so that `[title^="File Browser"]`, which is how the
|
|
65
|
+
// file browser tab has to be selected once its caption carries the shortcut, is
|
|
66
|
+
// read as well as the exact form.
|
|
67
|
+
const TITLE_ATTRIBUTE_PATTERN = /\[\s*title\s*([~^$*|]?=)\s*(?:"([^"]+)"|'([^']+)')\s*\]/g;
|
|
68
|
+
const MAIN_AREA_PATTERN = /(?:^|[\s>])(?:div)?\s*\[\s*role\s*=\s*(?:"main"|'main'|main)\s*\]/;
|
|
69
|
+
// The main area and the down area are Lumino tab bars too, so a widget moved
|
|
70
|
+
// out of the sidebar carries the same title attribute on its new tab. A
|
|
71
|
+
// selector that names one of those areas is not selecting a sidebar tab, and it
|
|
72
|
+
// can name one either by the stack node id or by the class the owning panel adds
|
|
73
|
+
// to its own tab bar: `DockPanel` adds `lm-DockPanel-tabBar` and `TabPanel`,
|
|
74
|
+
// which the down area is, adds `lm-TabPanel-tabBar`. The side bars are bare
|
|
75
|
+
// `TabBar` widgets and carry neither.
|
|
76
|
+
const DOCK_AREA_PATTERN = /#jp-main-dock-panel|#jp-down-stack|\.lm-DockPanel-tabBar|\.lm-TabPanel-tabBar/;
|
|
77
|
+
const TEXT_SELECTOR_PATTERN = /(?:^|>>)\s*text\s*=\s*(?:"([^"]+)"|'([^']+)'|(.+?))\s*(?:$|>>)/;
|
|
78
|
+
const TEXT_IS_PATTERN = /:text-is\(\s*(?:"([^"]+)"|'([^']+)')\s*\)/;
|
|
79
|
+
// A Lumino tab bar class only ever sits on a tab bar, so it proves the target
|
|
80
|
+
// is a tab wherever it is written. `role="tab"` is the generic ARIA role, which
|
|
81
|
+
// the settings editor plugin list carries on every `.jp-PluginList-entry`, so it
|
|
82
|
+
// proves one only once the main area is named as well.
|
|
83
|
+
const LUMINO_TAB_CLASS_PATTERN = /\.lm-TabBar-tab(?:\b|[.:[\s>])|\.lm-TabBar-tabLabel(?:\b|[.:[\s>])/;
|
|
84
|
+
const TAB_ROLE_PATTERN = /\[\s*role\s*=\s*(?:"tab"|'tab'|tab)\s*\]/;
|
|
85
|
+
const FILE_LIKE_ACTIVITY_NAME_PATTERN = /\.[A-Za-z0-9][\w-]*(?:\s*\*)?$/;
|
|
86
|
+
// The sidebar and the down area are Lumino tab bars too, so a tab token alone
|
|
87
|
+
// only proves a main area tab once these are ruled out. `page.activity` reaches
|
|
88
|
+
// only what sits under `[role="main"]`, which neither of them does, and the down
|
|
89
|
+
// area is named by its stack node id or by its `TabPanel` tab bar class.
|
|
90
|
+
const SIDE_TABBAR_PATTERN = /\.jp-SideBar|#jp-left-stack|#jp-right-stack|#jp-down-stack|\.lm-TabPanel-tabBar/;
|
|
91
|
+
// The attribute each `getByX` locator matches on. Playwright compares them by
|
|
92
|
+
// substring, so `*=` is the operator that says the same thing.
|
|
93
|
+
const LOCATOR_METHOD_ATTRIBUTE = new Map([
|
|
94
|
+
['getByTitle', 'title'],
|
|
95
|
+
['getByLabel', 'aria-label'],
|
|
96
|
+
['getByPlaceholder', 'placeholder'],
|
|
97
|
+
['getByAltText', 'alt'],
|
|
98
|
+
['getByTestId', 'data-testid']
|
|
99
|
+
]);
|
|
100
|
+
/**
|
|
101
|
+
* One link of a locator chain, written as the selector that picks the same
|
|
102
|
+
* element, or null when its argument is not static.
|
|
103
|
+
*
|
|
104
|
+
* A `getByRole` link keeps only its role: the accessible name is not an
|
|
105
|
+
* attribute, and folding it into `text=` would let a bare
|
|
106
|
+
* `getByRole('tab', { name })` look like a main area tab when the same call
|
|
107
|
+
* selects a sidebar tab just as well.
|
|
108
|
+
*/
|
|
109
|
+
function foldSelectorPart(part) {
|
|
110
|
+
const text = (0, playwright_selectors_1.extractStaticSelectorText)(part.argNode, {
|
|
111
|
+
allowPartialTemplate: false
|
|
112
|
+
});
|
|
113
|
+
if (text === null) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
if (part.method === 'getByRole') {
|
|
117
|
+
return `[role="${text}"]`;
|
|
118
|
+
}
|
|
119
|
+
if (part.method === 'getByText') {
|
|
120
|
+
return `text=${text}`;
|
|
121
|
+
}
|
|
122
|
+
const attribute = LOCATOR_METHOD_ATTRIBUTE.get(part.method);
|
|
123
|
+
// Anything left is `locator(selector)` or the selector `page.click` takes,
|
|
124
|
+
// both of which are already Playwright selectors.
|
|
125
|
+
return attribute ? `[${attribute}*="${text.trim()}"]` : text;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The accessible name a `getByRole('tab', { name })` tip matches on.
|
|
129
|
+
*
|
|
130
|
+
* Lumino renders a tab as `<li role="tab" title="{caption}">`, and a sidebar
|
|
131
|
+
* widget sets `title.caption` but no `title.label`, so the accessible name
|
|
132
|
+
* falls back to that same caption.
|
|
133
|
+
*/
|
|
134
|
+
function tabRoleNameOf(part) {
|
|
135
|
+
if (part.method !== 'getByRole' || !part.nameArgNode) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
const role = (0, playwright_selectors_1.extractStaticSelectorText)(part.argNode, {
|
|
139
|
+
allowPartialTemplate: false
|
|
140
|
+
});
|
|
141
|
+
if (role !== 'tab') {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
// `getByRole(..., { name })` matches a substring of normalized whitespace,
|
|
145
|
+
// so a caller may pad the name.
|
|
146
|
+
const name = (0, playwright_selectors_1.extractStaticSelectorText)(part.nameArgNode, {
|
|
147
|
+
allowPartialTemplate: false
|
|
148
|
+
});
|
|
149
|
+
return name === null ? null : name.trim();
|
|
150
|
+
}
|
|
151
|
+
function getSelectorSource(match) {
|
|
152
|
+
if (match.interactionMethod !== 'click' ||
|
|
153
|
+
hasUnsupportedOptions(match.callNode)) {
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
const parts = [];
|
|
157
|
+
for (const part of match.selectorParts) {
|
|
158
|
+
const folded = foldSelectorPart(part);
|
|
159
|
+
// A link that cannot be read could carry the very token that rules the tab
|
|
160
|
+
// out, so a chain is read only when every link of it is static.
|
|
161
|
+
if (folded === null) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
parts.push(folded);
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
node: match.callNode,
|
|
168
|
+
selector: parts.join(' >> '),
|
|
169
|
+
tabRoleName: tabRoleNameOf(match.selectorParts[match.selectorParts.length - 1])
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* True when `[title <operator> "<value>"]` names the tab whose caption starts
|
|
174
|
+
* with `title`.
|
|
175
|
+
*
|
|
176
|
+
* Only the three operators that the start of the caption settles are read.
|
|
177
|
+
* `$=`, `~=` and `|=` are matched against its end, which the file browser
|
|
178
|
+
* shortcut leaves unknown, so a selector using one of them is left alone. The
|
|
179
|
+
* value has to fit inside the caption, never the other way round: a title such
|
|
180
|
+
* as `Close Debugger` contains a caption without naming the tab that holds it.
|
|
181
|
+
*/
|
|
182
|
+
function titleValueNamesTab(operator, value, title) {
|
|
183
|
+
switch (operator) {
|
|
184
|
+
case '=':
|
|
185
|
+
return value === title;
|
|
186
|
+
case '^=':
|
|
187
|
+
return title.startsWith(value);
|
|
188
|
+
case '*=':
|
|
189
|
+
return title.includes(value);
|
|
190
|
+
default:
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* The single sidebar tab an attribute value names, or null when it names none
|
|
196
|
+
* or more than one. A short prefix such as `[title^="P"]` fits one caption and
|
|
197
|
+
* is taken; one that fits two is dropped rather than resolved arbitrarily.
|
|
198
|
+
*/
|
|
199
|
+
function findTabByTitleAttribute(operator, value) {
|
|
200
|
+
let found = null;
|
|
201
|
+
for (const [title, tab] of SIDEBAR_TITLE_TO_TAB) {
|
|
202
|
+
if (!titleValueNamesTab(operator, value, title)) {
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
if (found) {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
found = { title, ...tab };
|
|
209
|
+
}
|
|
210
|
+
return found;
|
|
211
|
+
}
|
|
212
|
+
function findSidebarTitle(source) {
|
|
213
|
+
// Every way a selector can name a sidebar tab spells one of these out, and
|
|
214
|
+
// most interactions in a Galata suite spell out neither, so the substring
|
|
215
|
+
// test keeps the pattern scans off the common path.
|
|
216
|
+
if (source.tabRoleName === null &&
|
|
217
|
+
!source.selector.includes('title') &&
|
|
218
|
+
!source.selector.includes('data-id')) {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
if (MAIN_AREA_PATTERN.test(source.selector) ||
|
|
222
|
+
DOCK_AREA_PATTERN.test(source.selector)) {
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
if (source.tabRoleName !== null) {
|
|
226
|
+
const tab = SIDEBAR_TITLE_TO_TAB.get(source.tabRoleName);
|
|
227
|
+
if (tab) {
|
|
228
|
+
return { title: source.tabRoleName, ...tab };
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
for (const match of source.selector.matchAll(TITLE_ATTRIBUTE_PATTERN)) {
|
|
232
|
+
const tab = findTabByTitleAttribute(match[1], match[2] ?? match[3]);
|
|
233
|
+
if (tab) {
|
|
234
|
+
return tab;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
for (const match of source.selector.matchAll(DATA_ID_ATTRIBUTE_PATTERN)) {
|
|
238
|
+
const tab = SIDEBAR_ID_TO_TAB.get(match[1] ?? match[2]);
|
|
239
|
+
if (tab) {
|
|
240
|
+
return tab;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
/** The text a selector matches on, written as `text=` or as `:text-is()`. */
|
|
246
|
+
function activityTextFrom(selector) {
|
|
247
|
+
const text = TEXT_SELECTOR_PATTERN.exec(selector);
|
|
248
|
+
if (text) {
|
|
249
|
+
return (text[1] ?? text[2] ?? text[3]).trim();
|
|
250
|
+
}
|
|
251
|
+
const exact = TEXT_IS_PATTERN.exec(selector);
|
|
252
|
+
return exact ? (exact[1] ?? exact[2]).trim() : null;
|
|
253
|
+
}
|
|
254
|
+
function getActivityTabName(source) {
|
|
255
|
+
const { selector } = source;
|
|
256
|
+
// The name comes from `text=`, from `:text-is()` or from the role name, so
|
|
257
|
+
// without either there is nothing to put in the message. Same reason as the
|
|
258
|
+
// guard in `findSidebarTitle`.
|
|
259
|
+
if (source.tabRoleName === null && !selector.includes('text')) {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
if (SIDE_TABBAR_PATTERN.test(selector)) {
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
const inMainArea = MAIN_AREA_PATTERN.test(selector);
|
|
266
|
+
// A tab token proves the target is a tab wherever it is written. Without one
|
|
267
|
+
// the only evidence is the main area plus a name shaped like a file, which
|
|
268
|
+
// the dock panel node also covers the widget content with, so that path
|
|
269
|
+
// stays the fallback.
|
|
270
|
+
const namesTab = LUMINO_TAB_CLASS_PATTERN.test(selector) ||
|
|
271
|
+
(TAB_ROLE_PATTERN.test(selector) && inMainArea);
|
|
272
|
+
// Galata builds its own tab locator as
|
|
273
|
+
// `page.getByRole('main').getByRole('tab', { name })`, so a chain written
|
|
274
|
+
// that way names the activity outright.
|
|
275
|
+
const tabName = activityTextFrom(selector) ?? (inMainArea ? source.tabRoleName : null);
|
|
276
|
+
if (tabName === null || tabName.length === 0) {
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
const looksLikeADocumentInTheMainArea = inMainArea && FILE_LIKE_ACTIVITY_NAME_PATTERN.test(tabName);
|
|
280
|
+
return namesTab || looksLikeADocumentInTheMainArea ? tabName : null;
|
|
281
|
+
}
|
|
282
|
+
const galataPreferSidebarActivityHelper = (0, create_rule_1.createRule)({
|
|
283
|
+
name: 'galata-prefer-sidebar-activity-helper',
|
|
284
|
+
meta: {
|
|
285
|
+
type: 'suggestion',
|
|
286
|
+
docs: {
|
|
287
|
+
description: 'Prefer Galata sidebar and activity helpers over raw Playwright tab selectors'
|
|
288
|
+
},
|
|
289
|
+
messages: {
|
|
290
|
+
preferSidebarHelper: 'Use page.sidebar.openTab("{{ id }}") to open the "{{ title }}" sidebar tab, or page.sidebar.close("{{ side }}"), its default side, if this click is closing the sidebar.',
|
|
291
|
+
preferActivityHelper: 'Use page.activity.activateTab("{{ tabName }}") instead of clicking a main area tab by text.'
|
|
292
|
+
},
|
|
293
|
+
schema: []
|
|
294
|
+
},
|
|
295
|
+
defaultOptions: [],
|
|
296
|
+
create(context) {
|
|
297
|
+
function reportSelectorSource(source) {
|
|
298
|
+
const sidebar = findSidebarTitle(source);
|
|
299
|
+
if (sidebar) {
|
|
300
|
+
context.report({
|
|
301
|
+
node: source.node,
|
|
302
|
+
messageId: 'preferSidebarHelper',
|
|
303
|
+
data: { title: sidebar.title, id: sidebar.id, side: sidebar.side }
|
|
304
|
+
});
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
const tabName = getActivityTabName(source);
|
|
308
|
+
if (tabName) {
|
|
309
|
+
context.report({
|
|
310
|
+
node: source.node,
|
|
311
|
+
messageId: 'preferActivityHelper',
|
|
312
|
+
data: { tabName }
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return {
|
|
317
|
+
CallExpression(node) {
|
|
318
|
+
// Most call expressions are not interactions at all, and
|
|
319
|
+
// `matchSelectorInteraction` rejects them on the callee alone, so the
|
|
320
|
+
// scope is looked up only once something actually needs it.
|
|
321
|
+
let scope = null;
|
|
322
|
+
const currentScope = () => (scope ?? (scope = context.sourceCode.getScope(node)));
|
|
323
|
+
const match = (0, playwright_selectors_1.matchSelectorInteraction)(node, identifier => (0, playwright_selectors_1.resolveLocatorBinding)(identifier, currentScope()));
|
|
324
|
+
const source = match ? getSelectorSource(match) : null;
|
|
325
|
+
if (source) {
|
|
326
|
+
reportSelectorSource(source);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
module.exports = galataPreferSidebarActivityHelper;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
declare const incorrectTranslatorUsage: import("@typescript-eslint/utils/ts-eslint").RuleModule<"chainedBundleMethod" | "invalidBundleName" | "destructuredBundle", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
2
|
+
name: string;
|
|
3
|
+
};
|
|
4
|
+
export = incorrectTranslatorUsage;
|
|
@@ -0,0 +1,208 @@
|
|
|
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 translation_1 = require("../utils/translation");
|
|
8
|
+
/**
|
|
9
|
+
* Returns the object on which `.load(...)` is called when the node is a call
|
|
10
|
+
* of the form `<expr>.load(...)`, or null otherwise.
|
|
11
|
+
*/
|
|
12
|
+
function getLoadCallObject(node) {
|
|
13
|
+
const expression = (0, translation_1.unwrapExpression)(node);
|
|
14
|
+
if (expression.type === 'CallExpression' &&
|
|
15
|
+
expression.callee.type === 'MemberExpression' &&
|
|
16
|
+
!expression.callee.computed &&
|
|
17
|
+
expression.callee.property.type === 'Identifier' &&
|
|
18
|
+
expression.callee.property.name === 'load') {
|
|
19
|
+
return expression.callee.object;
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Returns true when the expression looks like an ITranslator: an identifier
|
|
25
|
+
* or property whose name contains "translator", e.g. `translator`,
|
|
26
|
+
* `this.translator`, `this._translator`, `props.translator` or
|
|
27
|
+
* `nullTranslator`.
|
|
28
|
+
*/
|
|
29
|
+
function isTranslatorReference(node) {
|
|
30
|
+
const expression = (0, translation_1.unwrapExpression)(node);
|
|
31
|
+
if (expression.type === 'Identifier') {
|
|
32
|
+
return /translator/i.test(expression.name);
|
|
33
|
+
}
|
|
34
|
+
if (expression.type === 'MemberExpression' &&
|
|
35
|
+
!expression.computed &&
|
|
36
|
+
expression.property.type === 'Identifier') {
|
|
37
|
+
return /translator/i.test(expression.property.name);
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns true when the node is a `<translator>.load(...)` call producing a
|
|
43
|
+
* translation bundle.
|
|
44
|
+
*/
|
|
45
|
+
function isTranslatorLoadCall(node) {
|
|
46
|
+
const object = getLoadCallObject(node);
|
|
47
|
+
return object !== null && isTranslatorReference(object);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Renders a readable dotted path for a member/identifier target, e.g.
|
|
51
|
+
* `options.trans` or `this.bundle`, for use in diagnostic messages.
|
|
52
|
+
*/
|
|
53
|
+
function getTargetName(node) {
|
|
54
|
+
const expression = (0, translation_1.unwrapExpression)(node);
|
|
55
|
+
if (expression.type === 'ThisExpression') {
|
|
56
|
+
return 'this';
|
|
57
|
+
}
|
|
58
|
+
if (expression.type === 'Identifier') {
|
|
59
|
+
return expression.name;
|
|
60
|
+
}
|
|
61
|
+
if (expression.type === 'MemberExpression' &&
|
|
62
|
+
!expression.computed &&
|
|
63
|
+
expression.property.type === 'Identifier') {
|
|
64
|
+
return `${getTargetName(expression.object)}.${expression.property.name}`;
|
|
65
|
+
}
|
|
66
|
+
return 'this expression';
|
|
67
|
+
}
|
|
68
|
+
const incorrectTranslatorUsage = (0, create_rule_1.createRule)({
|
|
69
|
+
name: 'incorrect-translator-usage',
|
|
70
|
+
meta: {
|
|
71
|
+
type: 'problem',
|
|
72
|
+
docs: {
|
|
73
|
+
description: 'Require translation bundles returned by translator.load() to be stored under an extractor-recognized name (e.g. trans, this.trans, this._trans, props.trans, this.props.trans)',
|
|
74
|
+
url: 'https://eslint-plugin.readthedocs.io/en/latest/rules/incorrect-translator-usage/'
|
|
75
|
+
},
|
|
76
|
+
messages: {
|
|
77
|
+
chainedBundleMethod: 'Do not call {{ method }}() directly on the result of load(). Store the translation bundle in a variable named trans first, e.g. const trans = translator.load("mydomain"); trans.__("..."). Chained calls are not picked up by the translation string extractor.',
|
|
78
|
+
invalidBundleName: 'Store the translation bundle in a variable named trans (or a trans/_trans property); strings used through "{{ name }}" will not be picked up by the translation string extractor.',
|
|
79
|
+
destructuredBundle: 'Do not destructure the result of translator.load(). Store the translation bundle in a variable named trans, e.g. const trans = translator.load("mydomain").'
|
|
80
|
+
},
|
|
81
|
+
schema: []
|
|
82
|
+
},
|
|
83
|
+
defaultOptions: [],
|
|
84
|
+
create(context) {
|
|
85
|
+
function checkIdentifierTarget(target) {
|
|
86
|
+
if (target.name !== translation_1.BUNDLE_VARIABLE_NAME) {
|
|
87
|
+
context.report({
|
|
88
|
+
node: target,
|
|
89
|
+
messageId: 'invalidBundleName',
|
|
90
|
+
data: { name: target.name }
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
// translator.load('mydomain').__('...')
|
|
96
|
+
CallExpression(node) {
|
|
97
|
+
const callee = node.callee;
|
|
98
|
+
if (callee.type === 'MemberExpression' &&
|
|
99
|
+
!callee.computed &&
|
|
100
|
+
callee.property.type === 'Identifier' &&
|
|
101
|
+
translation_1.BUNDLE_METHODS.has(callee.property.name) &&
|
|
102
|
+
isTranslatorLoadCall(callee.object)) {
|
|
103
|
+
context.report({
|
|
104
|
+
node,
|
|
105
|
+
messageId: 'chainedBundleMethod',
|
|
106
|
+
data: { method: callee.property.name }
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
// const bundle = translator.load('mydomain')
|
|
111
|
+
VariableDeclarator(node) {
|
|
112
|
+
if (!node.init || !isTranslatorLoadCall(node.init)) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (node.id.type === 'Identifier') {
|
|
116
|
+
checkIdentifierTarget(node.id);
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
context.report({ node: node.id, messageId: 'destructuredBundle' });
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
// bundle = translator.load('mydomain')
|
|
123
|
+
// this.bundle = translator.load('mydomain')
|
|
124
|
+
AssignmentExpression(node) {
|
|
125
|
+
if (node.operator !== '=' || !isTranslatorLoadCall(node.right)) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const left = node.left;
|
|
129
|
+
if (left.type === 'Identifier') {
|
|
130
|
+
checkIdentifierTarget(left);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (left.type === 'MemberExpression') {
|
|
134
|
+
if (left.computed || left.property.type !== 'Identifier') {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (!(0, translation_1.isRecognizedBundleMember)(left)) {
|
|
138
|
+
context.report({
|
|
139
|
+
node: left,
|
|
140
|
+
messageId: 'invalidBundleName',
|
|
141
|
+
data: { name: getTargetName(left) }
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (left.type === 'ObjectPattern' || left.type === 'ArrayPattern') {
|
|
147
|
+
context.report({ node: left, messageId: 'destructuredBundle' });
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
// function f(bundle = translator.load('mydomain')) {}
|
|
151
|
+
AssignmentPattern(node) {
|
|
152
|
+
if (!isTranslatorLoadCall(node.right)) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (node.left.type === 'Identifier') {
|
|
156
|
+
checkIdentifierTarget(node.left);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
context.report({ node: node.left, messageId: 'destructuredBundle' });
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
// class A { bundle = translator.load('mydomain'); }
|
|
163
|
+
PropertyDefinition(node) {
|
|
164
|
+
if (!node.value || !isTranslatorLoadCall(node.value)) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (node.key.type === 'PrivateIdentifier') {
|
|
168
|
+
// this.#trans is never recognized by the string extractor
|
|
169
|
+
context.report({
|
|
170
|
+
node: node.key,
|
|
171
|
+
messageId: 'invalidBundleName',
|
|
172
|
+
data: { name: `#${node.key.name}` }
|
|
173
|
+
});
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (node.computed || node.key.type !== 'Identifier') {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (!translation_1.BUNDLE_PROPERTY_NAMES.has(node.key.name)) {
|
|
180
|
+
context.report({
|
|
181
|
+
node: node.key,
|
|
182
|
+
messageId: 'invalidBundleName',
|
|
183
|
+
data: { name: node.key.name }
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
// { bundle: translator.load('mydomain') }
|
|
188
|
+
Property(node) {
|
|
189
|
+
if (node.computed || !isTranslatorLoadCall(node.value)) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
const name = node.key.type === 'Identifier'
|
|
193
|
+
? node.key.name
|
|
194
|
+
: node.key.type === 'Literal' && typeof node.key.value === 'string'
|
|
195
|
+
? node.key.value
|
|
196
|
+
: null;
|
|
197
|
+
if (name !== null && name !== translation_1.BUNDLE_VARIABLE_NAME) {
|
|
198
|
+
context.report({
|
|
199
|
+
node: node.key,
|
|
200
|
+
messageId: 'invalidBundleName',
|
|
201
|
+
data: { name }
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
module.exports = incorrectTranslatorUsage;
|
|
@@ -0,0 +1,92 @@
|
|
|
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 translation_1 = require("../utils/translation");
|
|
8
|
+
/**
|
|
9
|
+
* Returns true when the message is written out at the call site, so the string
|
|
10
|
+
* extractor can read it straight from the source: a quoted string, or a
|
|
11
|
+
* template literal with nothing interpolated into it.
|
|
12
|
+
*
|
|
13
|
+
* The extractor reads the call site and nothing else — it never follows a
|
|
14
|
+
* variable to its definition — so anything else leaves it with no message to
|
|
15
|
+
* extract, however static the value may be at runtime.
|
|
16
|
+
*/
|
|
17
|
+
function isStaticString(node) {
|
|
18
|
+
const expression = (0, translation_1.unwrapExpression)(node);
|
|
19
|
+
if (expression.type === 'Literal') {
|
|
20
|
+
return typeof expression.value === 'string';
|
|
21
|
+
}
|
|
22
|
+
if (expression.type === 'TemplateLiteral') {
|
|
23
|
+
return expression.expressions.length === 0;
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Returns true for a `+` expression. Concatenation is the concern of the
|
|
29
|
+
* `no-translation-concatenation` rule, so this rule leaves it alone rather than
|
|
30
|
+
* reporting the same line twice.
|
|
31
|
+
*/
|
|
32
|
+
function isConcatenation(node) {
|
|
33
|
+
const expression = (0, translation_1.unwrapExpression)(node);
|
|
34
|
+
return expression.type === 'BinaryExpression' && expression.operator === '+';
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Picks the message that best explains why an argument is not extractable.
|
|
38
|
+
*/
|
|
39
|
+
function getMessageId(node) {
|
|
40
|
+
return (0, translation_1.unwrapExpression)(node).type === 'TemplateLiteral'
|
|
41
|
+
? 'noInterpolation'
|
|
42
|
+
: 'noDynamicMessage';
|
|
43
|
+
}
|
|
44
|
+
const noDynamicTranslation = (0, create_rule_1.createRule)({
|
|
45
|
+
name: 'no-dynamic-translation',
|
|
46
|
+
meta: {
|
|
47
|
+
type: 'problem',
|
|
48
|
+
docs: {
|
|
49
|
+
description: 'Require translation messages to be written as literals at the call site',
|
|
50
|
+
url: 'https://eslint-plugin.readthedocs.io/en/latest/rules/no-dynamic-translation/'
|
|
51
|
+
},
|
|
52
|
+
messages: {
|
|
53
|
+
noInterpolation: 'Do not interpolate values into translation strings. Use a placeholder instead, e.g. trans.__("Delete %1", fileName).',
|
|
54
|
+
noDynamicMessage: 'Write the translation string as a literal in the call itself or use a placeholder instead, e.g. trans.__("Kernel %1", status).'
|
|
55
|
+
},
|
|
56
|
+
schema: []
|
|
57
|
+
},
|
|
58
|
+
defaultOptions: [],
|
|
59
|
+
create(context) {
|
|
60
|
+
return {
|
|
61
|
+
CallExpression(node) {
|
|
62
|
+
if (node.callee.type !== 'MemberExpression') {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const callee = node.callee;
|
|
66
|
+
if (callee.computed || callee.property.type !== 'Identifier') {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const method = callee.property.name;
|
|
70
|
+
if (!translation_1.BUNDLE_METHODS.has(method)) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (!(0, translation_1.isTransBundle)(callee.object)) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
for (const index of translation_1.STATIC_ARGUMENT_INDICES[method]) {
|
|
77
|
+
const argument = node.arguments[index];
|
|
78
|
+
if (!argument || isConcatenation(argument)) {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (!isStaticString(argument)) {
|
|
82
|
+
context.report({
|
|
83
|
+
node: argument,
|
|
84
|
+
messageId: getMessageId(argument)
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
module.exports = noDynamicTranslation;
|