@jupyter/eslint-plugin 0.0.4 → 0.0.5

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 CHANGED
@@ -17,7 +17,9 @@ declare const plugin: {
17
17
  'token-format': import("@typescript-eslint/utils/dist/ts-eslint").RuleModule<"missingColon" | "invalidTokenSymbol", [], unknown, import("@typescript-eslint/utils/dist/ts-eslint").RuleListener> & {
18
18
  name: string;
19
19
  };
20
- 'no-untranslated-string': import("@typescript-eslint/utils/dist/ts-eslint").RuleModule<"untranslatedCommandProp" | "untranslatedSetAttribute" | "untranslatedPropertyAssign" | "untranslatedTitleProp" | "untranslatedDialogOption" | "untranslatedDialogButtonLabel" | "untranslatedJsxText", [], unknown, import("@typescript-eslint/utils/dist/ts-eslint").RuleListener> & {
20
+ 'no-untranslated-string': import("@typescript-eslint/utils/dist/ts-eslint").RuleModule<"untranslatedCommandProp" | "untranslatedSetAttribute" | "untranslatedPropertyAssign" | "untranslatedTitleProp" | "untranslatedDialogOption" | "untranslatedDialogButtonLabel" | "untranslatedJsxText", [{
21
+ enforcePunctuation: boolean;
22
+ }], unknown, import("@typescript-eslint/utils/dist/ts-eslint").RuleListener> & {
21
23
  name: string;
22
24
  };
23
25
  };
@@ -1,4 +1,6 @@
1
- declare const noUntranslatedString: import("@typescript-eslint/utils/dist/ts-eslint").RuleModule<"untranslatedCommandProp" | "untranslatedSetAttribute" | "untranslatedPropertyAssign" | "untranslatedTitleProp" | "untranslatedDialogOption" | "untranslatedDialogButtonLabel" | "untranslatedJsxText", [], unknown, import("@typescript-eslint/utils/dist/ts-eslint").RuleListener> & {
1
+ declare const noUntranslatedString: import("@typescript-eslint/utils/dist/ts-eslint").RuleModule<"untranslatedCommandProp" | "untranslatedSetAttribute" | "untranslatedPropertyAssign" | "untranslatedTitleProp" | "untranslatedDialogOption" | "untranslatedDialogButtonLabel" | "untranslatedJsxText", [{
2
+ enforcePunctuation: boolean;
3
+ }], unknown, import("@typescript-eslint/utils/dist/ts-eslint").RuleListener> & {
2
4
  name: string;
3
5
  };
4
6
  export = noUntranslatedString;
@@ -6,29 +6,29 @@
6
6
  const commands_1 = require("../utils/commands");
7
7
  const plugin_utils_1 = require("../utils/plugin-utils");
8
8
  const create_rule_1 = require("../utils/create-rule");
9
- /**
10
- * Returns true if the node is a non-empty raw string literal that should be
11
- * wrapped in a translation call. Handles:
12
- * - String Literal: 'string' or "string"
13
- * - TemplateLiteral with no expressions: `string`
14
- * - Concise ArrowFunctionExpression whose body is one of the above
15
- */
16
- function isRawStringNode(node) {
17
- if (node.type === 'Literal') {
18
- return typeof node.value === 'string' && node.value.length > 0;
9
+ function hasLetters(str) {
10
+ return /\p{L}/u.test(str);
11
+ }
12
+ function getRawStringValue(node) {
13
+ if (node.type === 'Literal' && typeof node.value === 'string') {
14
+ return node.value;
19
15
  }
20
- if (node.type === 'TemplateLiteral') {
21
- if (node.expressions.length > 0) {
22
- return false;
23
- }
24
- const cooked = node.quasis.map(q => q.value.cooked ?? '').join('');
25
- return cooked.length > 0;
16
+ if (node.type === 'TemplateLiteral' && node.expressions.length === 0) {
17
+ return node.quasis.map(q => q.value.cooked ?? '').join('');
26
18
  }
27
19
  if (node.type === 'ArrowFunctionExpression' &&
28
20
  node.body.type !== 'BlockStatement') {
29
- return isRawStringNode(node.body);
21
+ return getRawStringValue(node.body);
30
22
  }
31
- return false;
23
+ return null;
24
+ }
25
+ /**
26
+ * Returns true if the node is a non-empty raw string literal that should be
27
+ * wrapped in a translation call.
28
+ */
29
+ function isRawStringNode(node) {
30
+ const rawValue = getRawStringValue(node);
31
+ return rawValue !== null && rawValue.trim().length > 0;
32
32
  }
33
33
  function isSetAttributeCall(node) {
34
34
  return (node.callee.type === 'MemberExpression' &&
@@ -55,9 +55,11 @@ function isDialogButtonCall(node) {
55
55
  DIALOG_BUTTON_BUILDERS.includes(node.callee.property.name));
56
56
  }
57
57
  const MONITORED_COMMAND_PROPS = ['label', 'caption', 'usage'];
58
- const MONITORED_SET_ATTRIBUTE_ATTRS = ['aria-label', 'aria-description', 'title'];
58
+ const MONITORED_A11Y_ATTRS = ['aria-label', 'aria-description', 'title'];
59
+ const MONITORED_SET_ATTRIBUTE_ATTRS = MONITORED_A11Y_ATTRS;
59
60
  const MONITORED_ASSIGNMENT_PROPS = ['title', 'ariaLabel'];
60
61
  const MONITORED_DIALOG_PROPS = ['title', 'body'];
62
+ const MONITORED_JSX_ATTRS = MONITORED_A11Y_ATTRS;
61
63
  const noUntranslatedString = (0, create_rule_1.createRule)({
62
64
  name: 'no-untranslated-string',
63
65
  meta: {
@@ -75,10 +77,20 @@ const noUntranslatedString = (0, create_rule_1.createRule)({
75
77
  untranslatedDialogButtonLabel: 'Dialog button "label" option has an untranslated string literal. Wrap it with trans.__().',
76
78
  untranslatedJsxText: 'JSX text content has an untranslated string literal. Wrap it with {trans.__(...)}'
77
79
  },
78
- schema: []
80
+ schema: [
81
+ {
82
+ type: 'object',
83
+ properties: {
84
+ enforcePunctuation: { type: 'boolean' }
85
+ },
86
+ additionalProperties: false
87
+ }
88
+ ]
79
89
  },
80
- defaultOptions: [],
90
+ defaultOptions: [{ enforcePunctuation: false }],
81
91
  create(context) {
92
+ const enforcePunctuation = context.options[0]
93
+ ?.enforcePunctuation ?? false;
82
94
  return {
83
95
  CallExpression(node) {
84
96
  // Branch A: commands.addCommand(id, { label, caption, usage })
@@ -223,9 +235,25 @@ const noUntranslatedString = (0, create_rule_1.createRule)({
223
235
  });
224
236
  }
225
237
  },
238
+ // Accessibility attribute with a plain string: <span aria-label="text" />
239
+ JSXAttribute(node) {
240
+ if (!node.value || node.value.type === 'JSXExpressionContainer') {
241
+ return;
242
+ }
243
+ const attrName = node.name.type === 'JSXIdentifier' ? node.name.name : null;
244
+ if (!attrName || !MONITORED_JSX_ATTRS.includes(attrName)) {
245
+ return;
246
+ }
247
+ if (isRawStringNode(node.value)) {
248
+ context.report({
249
+ node: node.value,
250
+ messageId: 'untranslatedJsxText'
251
+ });
252
+ }
253
+ },
226
254
  // Raw text between JSX tags: <span>Untranslated text</span>
227
255
  JSXText(node) {
228
- if (node.value.trim().length > 0) {
256
+ if (node.value.trim().length > 0 && (enforcePunctuation || hasLetters(node.value))) {
229
257
  context.report({
230
258
  node,
231
259
  messageId: 'untranslatedJsxText'
@@ -237,11 +265,29 @@ const noUntranslatedString = (0, create_rule_1.createRule)({
237
265
  if (node.expression.type === 'JSXEmptyExpression') {
238
266
  return;
239
267
  }
268
+ if (node.parent.type === 'JSXAttribute') {
269
+ const attrName = node.parent.name.type === 'JSXIdentifier'
270
+ ? node.parent.name.name
271
+ : null;
272
+ if (!attrName || !MONITORED_JSX_ATTRS.includes(attrName)) {
273
+ return;
274
+ }
275
+ if (isRawStringNode(node.expression)) {
276
+ context.report({
277
+ node: node.expression,
278
+ messageId: 'untranslatedJsxText'
279
+ });
280
+ }
281
+ return;
282
+ }
240
283
  if (isRawStringNode(node.expression)) {
241
- context.report({
242
- node: node.expression,
243
- messageId: 'untranslatedJsxText'
244
- });
284
+ const value = getRawStringValue(node.expression);
285
+ if (value !== null && (enforcePunctuation ? value.trim().length > 0 : hasLetters(value))) {
286
+ context.report({
287
+ node: node.expression,
288
+ messageId: 'untranslatedJsxText'
289
+ });
290
+ }
245
291
  }
246
292
  }
247
293
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jupyter/eslint-plugin",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "ESLint plugin for Jupyter core and extensions with early error catching and best practices enforcement",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",