@jupyter/eslint-plugin 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/lib/index.d.ts +27 -1
  2. package/lib/index.js +21 -3
  3. package/lib/rules/command-id-convention.d.ts +5 -0
  4. package/lib/rules/command-id-convention.js +87 -0
  5. package/lib/rules/galata-prefer-context-menu-helper.d.ts +6 -0
  6. package/lib/rules/galata-prefer-context-menu-helper.js +394 -0
  7. package/lib/rules/galata-prefer-menu-helper.js +18 -102
  8. package/lib/rules/no-untranslated-string.d.ts +2 -1
  9. package/lib/rules/no-untranslated-string.js +191 -113
  10. package/lib/rules/plugin-activation-args.d.ts +3 -3
  11. package/lib/rules/plugin-activation-args.js +173 -142
  12. package/lib/rules/plugin-description.js +12 -9
  13. package/lib/rules/plugin-id-convention.d.ts +6 -0
  14. package/lib/rules/plugin-id-convention.js +199 -0
  15. package/lib/rules/prefer-lazy-imports.d.ts +2 -2
  16. package/lib/rules/prefer-lazy-imports.js +257 -58
  17. package/lib/rules/prefer-signal-this-arg.d.ts +2 -3
  18. package/lib/rules/prefer-signal-this-arg.js +2 -10
  19. package/lib/rules/require-disposable-ownership.d.ts +1 -2
  20. package/lib/rules/require-disposable-ownership.js +3 -26
  21. package/lib/rules/require-disposable-transfer.d.ts +1 -2
  22. package/lib/rules/require-disposable-transfer.js +14 -14
  23. package/lib/rules/require-signal-cleanup.d.ts +2 -3
  24. package/lib/rules/require-signal-cleanup.js +2 -10
  25. package/lib/rules/require-signal-this-arg.d.ts +1 -2
  26. package/lib/rules/require-signal-this-arg.js +2 -10
  27. package/lib/utils/ast.d.ts +12 -0
  28. package/lib/utils/ast.js +49 -0
  29. package/lib/utils/commands.d.ts +17 -0
  30. package/lib/utils/commands.js +205 -0
  31. package/lib/utils/disposables.d.ts +12 -0
  32. package/lib/utils/disposables.js +101 -10
  33. package/lib/utils/lazy-imports.d.ts +27 -3
  34. package/lib/utils/lazy-imports.js +130 -36
  35. package/lib/utils/module-size.d.ts +7 -1
  36. package/lib/utils/module-size.js +10 -1
  37. package/lib/utils/package-json.d.ts +8 -0
  38. package/lib/utils/package-json.js +76 -0
  39. package/lib/utils/playwright-selectors.d.ts +15 -0
  40. package/lib/utils/playwright-selectors.js +80 -0
  41. package/lib/utils/plugin-utils.d.ts +66 -6
  42. package/lib/utils/plugin-utils.js +485 -59
  43. package/lib/utils/shared-packages.js +6 -16
  44. package/lib/utils/signals.d.ts +2 -8
  45. package/lib/utils/signals.js +7 -42
  46. package/lib/utils/type-services.d.ts +13 -0
  47. package/lib/utils/type-services.js +27 -0
  48. package/package.json +1 -1
@@ -5,30 +5,64 @@
5
5
  */
6
6
  const commands_1 = require("../utils/commands");
7
7
  const plugin_utils_1 = require("../utils/plugin-utils");
8
+ const translation_1 = require("../utils/translation");
8
9
  const create_rule_1 = require("../utils/create-rule");
9
10
  function hasLetters(str) {
10
11
  return /\p{L}/u.test(str);
11
12
  }
12
- function getRawStringValue(node) {
13
- if (node.type === 'Literal' && typeof node.value === 'string') {
14
- return node.value;
15
- }
16
- if (node.type === 'TemplateLiteral' && node.expressions.length === 0) {
17
- return node.quasis.map(q => q.value.cooked ?? '').join('');
18
- }
19
- if (node.type === 'ArrowFunctionExpression' &&
20
- node.body.type !== 'BlockStatement') {
21
- return getRawStringValue(node.body);
13
+ /**
14
+ * Returns every raw string a value expression can evaluate to, empty when it
15
+ * can evaluate to none.
16
+ *
17
+ * A conditional has more than one result, and each one reaches the user on its
18
+ * own: `visible ? 'Hide' : 'Show'` displays either branch, `label ?? 'Untitled'`
19
+ * displays either operand. Collecting them all means a half-translated
20
+ * conditional reports only the branch that is still raw.
21
+ */
22
+ function getRawStrings(node) {
23
+ const inner = (0, translation_1.unwrapExpression)(node);
24
+ switch (inner.type) {
25
+ case 'Literal':
26
+ return typeof inner.value === 'string'
27
+ ? [{ node: inner, value: inner.value }]
28
+ : [];
29
+ case 'TemplateLiteral':
30
+ return inner.expressions.length === 0
31
+ ? [
32
+ {
33
+ node: inner,
34
+ value: inner.quasis.map(q => q.value.cooked ?? '').join('')
35
+ }
36
+ ]
37
+ : [];
38
+ case 'ArrowFunctionExpression':
39
+ return inner.body.type === 'BlockStatement'
40
+ ? []
41
+ : getRawStrings(inner.body);
42
+ case 'ConditionalExpression':
43
+ return [
44
+ ...getRawStrings(inner.consequent),
45
+ ...getRawStrings(inner.alternate)
46
+ ];
47
+ case 'LogicalExpression':
48
+ // `a && 'text'` evaluates to the left operand only when that operand is
49
+ // falsy, and the only falsy string is blank, which this rule never
50
+ // reports. `a || 'text'` and `a ?? 'text'` evaluate to either operand.
51
+ return [
52
+ ...(inner.operator === '&&' ? [] : getRawStrings(inner.left)),
53
+ ...getRawStrings(inner.right)
54
+ ];
55
+ default:
56
+ return [];
22
57
  }
23
- return null;
24
58
  }
25
59
  /**
26
- * Returns true if the node is a non-empty raw string literal that should be
27
- * wrapped in a translation call.
60
+ * Folds the hyphenated and camelCase spellings of a name together so that a
61
+ * single configured entry covers both, e.g. the `aria-label` attribute and the
62
+ * `ariaLabel` DOM property.
28
63
  */
29
- function isRawStringNode(node) {
30
- const rawValue = getRawStringValue(node);
31
- return rawValue !== null && rawValue.trim().length > 0;
64
+ function normalizeName(name) {
65
+ return name.replace(/-/g, '').toLowerCase();
32
66
  }
33
67
  function isSetAttributeCall(node) {
34
68
  return (node.callee.type === 'MemberExpression' &&
@@ -54,12 +88,43 @@ function isDialogButtonCall(node) {
54
88
  node.callee.property.type === 'Identifier' &&
55
89
  DIALOG_BUTTON_BUILDERS.includes(node.callee.property.name));
56
90
  }
91
+ // Properties checked only on the call they belong to, because the name alone
92
+ // does not imply user-facing text anywhere else.
57
93
  const MONITORED_COMMAND_PROPS = ['label', 'caption', 'usage'];
58
- const MONITORED_A11Y_ATTRS = ['aria-label', 'aria-description', 'title'];
59
- const MONITORED_SET_ATTRIBUTE_ATTRS = MONITORED_A11Y_ATTRS;
60
- const MONITORED_ASSIGNMENT_PROPS = ['title', 'ariaLabel'];
61
94
  const MONITORED_DIALOG_PROPS = ['title', 'body'];
62
- const MONITORED_JSX_ATTRS = MONITORED_A11Y_ATTRS;
95
+ /**
96
+ * Names checked in every generic position: object literal properties, JSX
97
+ * attributes, `setAttribute()` attribute names and assignment targets.
98
+ */
99
+ const DEFAULT_CHECK_PROPERTIES = [
100
+ 'alt',
101
+ 'aria-description',
102
+ 'aria-label',
103
+ 'caption',
104
+ 'category',
105
+ 'label',
106
+ 'placeholder',
107
+ 'title',
108
+ 'tooltip',
109
+ 'textContent',
110
+ 'innerText'
111
+ ];
112
+ /**
113
+ * Returns the static name of a non-computed object property key, or null when
114
+ * the key is computed or not a plain identifier/string.
115
+ */
116
+ function getPropertyKeyName(node) {
117
+ if (node.computed) {
118
+ return null;
119
+ }
120
+ if (node.key.type === 'Identifier') {
121
+ return node.key.name;
122
+ }
123
+ if (node.key.type === 'Literal' && typeof node.key.value === 'string') {
124
+ return node.key.value;
125
+ }
126
+ return null;
127
+ }
63
128
  const noUntranslatedString = (0, create_rule_1.createRule)({
64
129
  name: 'no-untranslated-string',
65
130
  meta: {
@@ -72,25 +137,85 @@ const noUntranslatedString = (0, create_rule_1.createRule)({
72
137
  untranslatedCommandProp: 'Command property "{{ prop }}" has an untranslated string literal. Wrap it with trans.__().',
73
138
  untranslatedSetAttribute: 'setAttribute("{{ attr }}", ...) has an untranslated string literal. Wrap the value with trans.__().',
74
139
  untranslatedPropertyAssign: 'Assignment to "{{ prop }}" has an untranslated string literal. Wrap it with trans.__().',
75
- untranslatedTitleProp: 'Assignment to "title.{{ prop }}" has an untranslated string literal. Wrap it with trans.__().',
76
140
  untranslatedDialogOption: 'Dialog/showDialog "{{ prop }}" option has an untranslated string literal. Wrap it with trans.__().',
77
141
  untranslatedDialogButtonLabel: 'Dialog button "label" option has an untranslated string literal. Wrap it with trans.__().',
78
- untranslatedJsxText: 'JSX text content has an untranslated string literal. Wrap it with {trans.__(...)}'
142
+ untranslatedJsxText: 'JSX text content has an untranslated string literal. Wrap it with {trans.__(...)}',
143
+ untranslatedJsxAttribute: 'JSX attribute "{{ prop }}" has an untranslated string literal. Wrap it with {trans.__(...)}',
144
+ untranslatedProperty: 'Property "{{ prop }}" has an untranslated string literal. Wrap it with trans.__().'
79
145
  },
80
146
  schema: [
81
147
  {
82
148
  type: 'object',
83
149
  properties: {
84
- enforcePunctuation: { type: 'boolean' }
150
+ enforcePunctuation: { type: 'boolean' },
151
+ checkProperties: {
152
+ type: 'array',
153
+ items: { type: 'string' },
154
+ uniqueItems: true
155
+ }
85
156
  },
86
157
  additionalProperties: false
87
158
  }
88
159
  ]
89
160
  },
90
- defaultOptions: [{ enforcePunctuation: false }],
161
+ defaultOptions: [
162
+ {
163
+ enforcePunctuation: false,
164
+ checkProperties: DEFAULT_CHECK_PROPERTIES
165
+ }
166
+ ],
91
167
  create(context) {
92
- const enforcePunctuation = context.options[0]
93
- ?.enforcePunctuation ?? false;
168
+ const options = context.options[0];
169
+ const enforcePunctuation = options?.enforcePunctuation ?? false;
170
+ const checkProperties = new Set((options?.checkProperties ?? DEFAULT_CHECK_PROPERTIES).map(normalizeName));
171
+ function isMonitoredName(name) {
172
+ return checkProperties.has(normalizeName(name));
173
+ }
174
+ /**
175
+ * Returns true if the text should be wrapped in a translation call.
176
+ *
177
+ * Blank strings are never reported. A string with no letters is read as a
178
+ * bare number when it has digits ('1970', '100%'), which this rule never
179
+ * reports, and as punctuation otherwise ('/', '-'), which it reports only
180
+ * when `enforcePunctuation` is on.
181
+ */
182
+ function isReportableText(value) {
183
+ if (value.trim().length === 0) {
184
+ return false;
185
+ }
186
+ // No letters: a bare number is never reported, punctuation only on request.
187
+ return hasLetters(value) || (enforcePunctuation && !/\p{N}/u.test(value));
188
+ }
189
+ // Literals already reported by a more specific branch (e.g. addCommand or
190
+ // a Dialog button builder), so the generic checks do not duplicate them.
191
+ // Enclosing calls are visited before the properties they contain, so the
192
+ // specific branch always runs first.
193
+ const reportedNodes = new Set();
194
+ function report(node, messageId, data) {
195
+ reportedNodes.add(node);
196
+ context.report({ node, messageId, data });
197
+ }
198
+ /**
199
+ * Reports every string literal behind `value` that has not been reported
200
+ * already.
201
+ */
202
+ function reportRawString(value, messageId, data) {
203
+ for (const raw of getRawStrings(value)) {
204
+ if (reportedNodes.has(raw.node) || !isReportableText(raw.value)) {
205
+ continue;
206
+ }
207
+ report(raw.node, messageId, data);
208
+ }
209
+ }
210
+ /**
211
+ * Reports a monitored JSX attribute whose value is a raw string.
212
+ */
213
+ function checkJsxAttributeValue(attrName, value) {
214
+ if (!attrName || !isMonitoredName(attrName)) {
215
+ return;
216
+ }
217
+ reportRawString(value, 'untranslatedJsxAttribute', { prop: attrName });
218
+ }
94
219
  return {
95
220
  CallExpression(node) {
96
221
  // Branch A: commands.addCommand(id, { label, caption, usage })
@@ -105,17 +230,15 @@ const noUntranslatedString = (0, create_rule_1.createRule)({
105
230
  const properties = (0, plugin_utils_1.getObjectProperties)(optionsArg);
106
231
  for (const propName of MONITORED_COMMAND_PROPS) {
107
232
  const prop = properties.get(propName);
108
- if (prop && isRawStringNode(prop.value)) {
109
- context.report({
110
- node: prop.value,
111
- messageId: 'untranslatedCommandProp',
112
- data: { prop: propName }
233
+ if (prop) {
234
+ reportRawString(prop.value, 'untranslatedCommandProp', {
235
+ prop: propName
113
236
  });
114
237
  }
115
238
  }
116
239
  return;
117
240
  }
118
- // Branch B: element.setAttribute('aria-label'/'aria-description'/'title', string)
241
+ // Branch B: element.setAttribute('aria-label', string)
119
242
  if (isSetAttributeCall(node)) {
120
243
  if (node.arguments.length < 2) {
121
244
  return;
@@ -123,17 +246,12 @@ const noUntranslatedString = (0, create_rule_1.createRule)({
123
246
  const attrNameArg = node.arguments[0];
124
247
  if (attrNameArg.type !== 'Literal' ||
125
248
  typeof attrNameArg.value !== 'string' ||
126
- !MONITORED_SET_ATTRIBUTE_ATTRS.includes(attrNameArg.value)) {
249
+ !isMonitoredName(attrNameArg.value)) {
127
250
  return;
128
251
  }
129
- const attrValueArg = node.arguments[1];
130
- if (isRawStringNode(attrValueArg)) {
131
- context.report({
132
- node: attrValueArg,
133
- messageId: 'untranslatedSetAttribute',
134
- data: { attr: attrNameArg.value }
135
- });
136
- }
252
+ reportRawString(node.arguments[1], 'untranslatedSetAttribute', {
253
+ attr: attrNameArg.value
254
+ });
137
255
  return;
138
256
  }
139
257
  // Branch C: showDialog({ title, body })
@@ -148,11 +266,9 @@ const noUntranslatedString = (0, create_rule_1.createRule)({
148
266
  const properties = (0, plugin_utils_1.getObjectProperties)(optionsArg);
149
267
  for (const propName of MONITORED_DIALOG_PROPS) {
150
268
  const prop = properties.get(propName);
151
- if (prop && isRawStringNode(prop.value)) {
152
- context.report({
153
- node: prop.value,
154
- messageId: 'untranslatedDialogOption',
155
- data: { prop: propName }
269
+ if (prop) {
270
+ reportRawString(prop.value, 'untranslatedDialogOption', {
271
+ prop: propName
156
272
  });
157
273
  }
158
274
  }
@@ -169,11 +285,8 @@ const noUntranslatedString = (0, create_rule_1.createRule)({
169
285
  }
170
286
  const properties = (0, plugin_utils_1.getObjectProperties)(optionsArg);
171
287
  const labelProp = properties.get('label');
172
- if (labelProp && isRawStringNode(labelProp.value)) {
173
- context.report({
174
- node: labelProp.value,
175
- messageId: 'untranslatedDialogButtonLabel'
176
- });
288
+ if (labelProp) {
289
+ reportRawString(labelProp.value, 'untranslatedDialogButtonLabel');
177
290
  }
178
291
  }
179
292
  },
@@ -192,15 +305,16 @@ const noUntranslatedString = (0, create_rule_1.createRule)({
192
305
  const properties = (0, plugin_utils_1.getObjectProperties)(optionsArg);
193
306
  for (const propName of MONITORED_DIALOG_PROPS) {
194
307
  const prop = properties.get(propName);
195
- if (prop && isRawStringNode(prop.value)) {
196
- context.report({
197
- node: prop.value,
198
- messageId: 'untranslatedDialogOption',
199
- data: { prop: propName }
308
+ if (prop) {
309
+ reportRawString(prop.value, 'untranslatedDialogOption', {
310
+ prop: propName
200
311
  });
201
312
  }
202
313
  }
203
314
  },
315
+ // Any monitored assignment target: element.title = STRING,
316
+ // widget.label = STRING, this.label = STRING, node.textContent = STRING,
317
+ // and Lumino widget titles such as this.title.label = STRING
204
318
  AssignmentExpression(node) {
205
319
  if (node.operator !== '=') {
206
320
  return;
@@ -208,58 +322,39 @@ const noUntranslatedString = (0, create_rule_1.createRule)({
208
322
  const left = node.left;
209
323
  if (left.type !== 'MemberExpression' ||
210
324
  left.computed ||
211
- left.property.type !== 'Identifier') {
325
+ left.property.type !== 'Identifier' ||
326
+ !isMonitoredName(left.property.name)) {
212
327
  return;
213
328
  }
214
- // element.title = STRING or element.ariaLabel = STRING
215
- if (MONITORED_ASSIGNMENT_PROPS.includes(left.property.name) &&
216
- isRawStringNode(node.right)) {
217
- context.report({
218
- node: node.right,
219
- messageId: 'untranslatedPropertyAssign',
220
- data: { prop: left.property.name }
221
- });
329
+ reportRawString(node.right, 'untranslatedPropertyAssign', {
330
+ prop: left.property.name
331
+ });
332
+ },
333
+ // Any monitored object property: { label: 'raw string' }.
334
+ // Runs after the call-specific branches above, which mark the literals
335
+ // they already reported so a property is never reported twice.
336
+ Property(node) {
337
+ if (node.shorthand) {
222
338
  return;
223
339
  }
224
- // *.title.label = STRING or *.title.caption = STRING
225
- if ((left.property.name === 'label' ||
226
- left.property.name === 'caption') &&
227
- left.object.type === 'MemberExpression' &&
228
- !left.object.computed &&
229
- left.object.property.type === 'Identifier' &&
230
- left.object.property.name === 'title' &&
231
- isRawStringNode(node.right)) {
232
- context.report({
233
- node: node.right,
234
- messageId: 'untranslatedTitleProp',
235
- data: { prop: left.property.name }
236
- });
340
+ const keyName = getPropertyKeyName(node);
341
+ if (!keyName || !isMonitoredName(keyName)) {
342
+ return;
237
343
  }
344
+ reportRawString(node.value, 'untranslatedProperty', { prop: keyName });
238
345
  },
239
- // Accessibility attribute with a plain string: <span aria-label="text" />
346
+ // Monitored attribute with a plain string: <span aria-label="text" />
240
347
  JSXAttribute(node) {
241
348
  if (!node.value || node.value.type === 'JSXExpressionContainer') {
242
349
  return;
243
350
  }
244
351
  const attrName = node.name.type === 'JSXIdentifier' ? node.name.name : null;
245
- if (!attrName || !MONITORED_JSX_ATTRS.includes(attrName)) {
246
- return;
247
- }
248
- if (isRawStringNode(node.value)) {
249
- context.report({
250
- node: node.value,
251
- messageId: 'untranslatedJsxText'
252
- });
253
- }
352
+ checkJsxAttributeValue(attrName, node.value);
254
353
  },
255
354
  // Raw text between JSX tags: <span>Untranslated text</span>
256
355
  JSXText(node) {
257
- if (node.value.trim().length > 0 &&
258
- (enforcePunctuation || hasLetters(node.value))) {
259
- context.report({
260
- node,
261
- messageId: 'untranslatedJsxText'
262
- });
356
+ if (isReportableText(node.value)) {
357
+ report(node, 'untranslatedJsxText');
263
358
  }
264
359
  },
265
360
  // String literal inside JSX expression: <span>{'raw string'}</span>
@@ -271,27 +366,10 @@ const noUntranslatedString = (0, create_rule_1.createRule)({
271
366
  const attrName = node.parent.name.type === 'JSXIdentifier'
272
367
  ? node.parent.name.name
273
368
  : null;
274
- if (!attrName || !MONITORED_JSX_ATTRS.includes(attrName)) {
275
- return;
276
- }
277
- if (isRawStringNode(node.expression)) {
278
- context.report({
279
- node: node.expression,
280
- messageId: 'untranslatedJsxText'
281
- });
282
- }
369
+ checkJsxAttributeValue(attrName, node.expression);
283
370
  return;
284
371
  }
285
- if (isRawStringNode(node.expression)) {
286
- const value = getRawStringValue(node.expression);
287
- if (value !== null &&
288
- (enforcePunctuation ? value.trim().length > 0 : hasLetters(value))) {
289
- context.report({
290
- node: node.expression,
291
- messageId: 'untranslatedJsxText'
292
- });
293
- }
294
- }
372
+ reportRawString(node.expression, 'untranslatedJsxText');
295
373
  }
296
374
  };
297
375
  }
@@ -1,7 +1,7 @@
1
- import { ESLintUtils } from '@typescript-eslint/utils';
2
- declare const jupyterPluginActivationArgs: ESLintUtils.RuleModule<"mismatchedOrder" | "incorrectType" | "missingArgument" | "extraArgument" | "appNotFirst" | "serviceManagerFirstArgNotNull" | "invalidAppType" | "wrongArgumentCount" | "unresolvableTokenType" | "optionalNotNullable", [{
1
+ import { TSESLint } from '@typescript-eslint/utils';
2
+ declare const jupyterPluginActivationArgs: TSESLint.RuleModule<"mismatchedOrder" | "incorrectType" | "missingArgument" | "extraArgument" | "appNotFirst" | "serviceManagerFirstArgNotNull" | "invalidAppType" | "wrongArgumentCount" | "unresolvableTokenType" | "optionalNotNullable", [{
3
3
  allowedFirstArgumentNames: string[];
4
- }], unknown, ESLintUtils.RuleListener> & {
4
+ }], unknown, TSESLint.RuleListener> & {
5
5
  name: string;
6
6
  };
7
7
  export = jupyterPluginActivationArgs;