@jupyter/eslint-plugin 0.0.3 → 0.0.4
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 +16 -1
- package/lib/index.js +15 -3
- package/lib/rules/no-translation-concatenation.d.ts +4 -0
- package/lib/rules/no-translation-concatenation.js +132 -0
- package/lib/rules/no-untranslated-string.d.ts +4 -0
- package/lib/rules/no-untranslated-string.js +250 -0
- package/lib/rules/plugin-activation-args.d.ts +1 -1
- package/lib/rules/plugin-activation-args.js +67 -59
- package/lib/rules/plugin-description.js +18 -9
- package/lib/rules/token-format.d.ts +4 -0
- package/lib/rules/token-format.js +63 -0
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
declare const plugin: {
|
|
2
2
|
rules: {
|
|
3
|
-
'plugin-activation-args': import("@typescript-eslint/utils/dist/ts-eslint").RuleModule<"mismatchedOrder" | "missingArgument" | "extraArgument" | "appNotFirst" | "serviceManagerFirstArgNotNull" | "invalidAppType" | "wrongArgumentCount" | "unresolvableTokenType", [{
|
|
3
|
+
'plugin-activation-args': import("@typescript-eslint/utils/dist/ts-eslint").RuleModule<"mismatchedOrder" | "incorrectType" | "missingArgument" | "extraArgument" | "appNotFirst" | "serviceManagerFirstArgNotNull" | "invalidAppType" | "wrongArgumentCount" | "unresolvableTokenType", [{
|
|
4
4
|
allowedFirstArgumentNames: string[];
|
|
5
5
|
}], unknown, import("@typescript-eslint/utils/dist/ts-eslint").RuleListener> & {
|
|
6
6
|
name: string;
|
|
@@ -11,6 +11,15 @@ declare const plugin: {
|
|
|
11
11
|
'plugin-description': import("@typescript-eslint/utils/dist/ts-eslint").RuleModule<"missingDescription" | "emptyDescription", [], unknown, import("@typescript-eslint/utils/dist/ts-eslint").RuleListener> & {
|
|
12
12
|
name: string;
|
|
13
13
|
};
|
|
14
|
+
'no-translation-concatenation': import("@typescript-eslint/utils/dist/ts-eslint").RuleModule<"noConcatenation", [], unknown, import("@typescript-eslint/utils/dist/ts-eslint").RuleListener> & {
|
|
15
|
+
name: string;
|
|
16
|
+
};
|
|
17
|
+
'token-format': import("@typescript-eslint/utils/dist/ts-eslint").RuleModule<"missingColon" | "invalidTokenSymbol", [], unknown, import("@typescript-eslint/utils/dist/ts-eslint").RuleListener> & {
|
|
18
|
+
name: string;
|
|
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> & {
|
|
21
|
+
name: string;
|
|
22
|
+
};
|
|
14
23
|
};
|
|
15
24
|
configs: {
|
|
16
25
|
recommended: {
|
|
@@ -19,6 +28,9 @@ declare const plugin: {
|
|
|
19
28
|
'jupyter/plugin-activation-args': string;
|
|
20
29
|
'jupyter/command-described-by': string;
|
|
21
30
|
'jupyter/plugin-description': string;
|
|
31
|
+
'jupyter/no-translation-concatenation': string;
|
|
32
|
+
'jupyter/token-format': string;
|
|
33
|
+
'jupyter/no-untranslated-string': string;
|
|
22
34
|
};
|
|
23
35
|
};
|
|
24
36
|
'recommended-legacy': {
|
|
@@ -26,6 +38,9 @@ declare const plugin: {
|
|
|
26
38
|
'jupyter/plugin-activation-args': string;
|
|
27
39
|
'jupyter/command-described-by': string;
|
|
28
40
|
'jupyter/plugin-description': string;
|
|
41
|
+
'jupyter/no-translation-concatenation': string;
|
|
42
|
+
'jupyter/token-format': string;
|
|
43
|
+
'jupyter/no-untranslated-string': string;
|
|
29
44
|
};
|
|
30
45
|
};
|
|
31
46
|
};
|
package/lib/index.js
CHANGED
|
@@ -9,11 +9,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
9
9
|
const plugin_activation_args_1 = __importDefault(require("./rules/plugin-activation-args"));
|
|
10
10
|
const command_described_by_1 = __importDefault(require("./rules/command-described-by"));
|
|
11
11
|
const plugin_description_1 = __importDefault(require("./rules/plugin-description"));
|
|
12
|
+
const no_translation_concatenation_1 = __importDefault(require("./rules/no-translation-concatenation"));
|
|
13
|
+
const token_format_1 = __importDefault(require("./rules/token-format"));
|
|
14
|
+
const no_untranslated_string_1 = __importDefault(require("./rules/no-untranslated-string"));
|
|
12
15
|
const plugin = {
|
|
13
16
|
rules: {
|
|
14
17
|
'plugin-activation-args': plugin_activation_args_1.default,
|
|
15
18
|
'command-described-by': command_described_by_1.default,
|
|
16
|
-
'plugin-description': plugin_description_1.default
|
|
19
|
+
'plugin-description': plugin_description_1.default,
|
|
20
|
+
'no-translation-concatenation': no_translation_concatenation_1.default,
|
|
21
|
+
'token-format': token_format_1.default,
|
|
22
|
+
'no-untranslated-string': no_untranslated_string_1.default
|
|
17
23
|
},
|
|
18
24
|
configs: {
|
|
19
25
|
recommended: {
|
|
@@ -21,14 +27,20 @@ const plugin = {
|
|
|
21
27
|
rules: {
|
|
22
28
|
'jupyter/plugin-activation-args': 'error',
|
|
23
29
|
'jupyter/command-described-by': 'warn',
|
|
24
|
-
'jupyter/plugin-description': 'warn'
|
|
30
|
+
'jupyter/plugin-description': 'warn',
|
|
31
|
+
'jupyter/no-translation-concatenation': 'error',
|
|
32
|
+
'jupyter/token-format': 'error',
|
|
33
|
+
'jupyter/no-untranslated-string': 'warn'
|
|
25
34
|
}
|
|
26
35
|
},
|
|
27
36
|
'recommended-legacy': {
|
|
28
37
|
rules: {
|
|
29
38
|
'jupyter/plugin-activation-args': 'error',
|
|
30
39
|
'jupyter/command-described-by': 'warn',
|
|
31
|
-
'jupyter/plugin-description': 'warn'
|
|
40
|
+
'jupyter/plugin-description': 'warn',
|
|
41
|
+
'jupyter/no-translation-concatenation': 'error',
|
|
42
|
+
'jupyter/token-format': 'error',
|
|
43
|
+
'jupyter/no-untranslated-string': 'warn'
|
|
32
44
|
}
|
|
33
45
|
}
|
|
34
46
|
}
|
|
@@ -0,0 +1,132 @@
|
|
|
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 TRANS_METHOD = '__';
|
|
8
|
+
/**
|
|
9
|
+
* Returns true when every leaf of a `+` expression tree is a string literal.
|
|
10
|
+
* Such concatenation is static and translation tools can handle it.
|
|
11
|
+
*/
|
|
12
|
+
function isAllStringLiterals(node) {
|
|
13
|
+
if (node.type === 'Literal') {
|
|
14
|
+
return typeof node.value === 'string';
|
|
15
|
+
}
|
|
16
|
+
if (node.type === 'BinaryExpression' && node.operator === '+') {
|
|
17
|
+
return isAllStringLiterals(node.left) && isAllStringLiterals(node.right);
|
|
18
|
+
}
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Returns the first `+` BinaryExpression found anywhere in the subtree that
|
|
23
|
+
* involves a non-literal operand, or null if none exists.
|
|
24
|
+
*/
|
|
25
|
+
function findConcatenation(node) {
|
|
26
|
+
if (node.type === 'BinaryExpression' && node.operator === '+') {
|
|
27
|
+
return isAllStringLiterals(node) ? null : node;
|
|
28
|
+
}
|
|
29
|
+
const record = node;
|
|
30
|
+
for (const key of Object.keys(record)) {
|
|
31
|
+
if (key === 'type' || key === 'loc' || key === 'range' || key === 'parent') {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
const child = record[key];
|
|
35
|
+
if (child && typeof child === 'object') {
|
|
36
|
+
if (Array.isArray(child)) {
|
|
37
|
+
for (const item of child) {
|
|
38
|
+
if (item && typeof item.type === 'string') {
|
|
39
|
+
const found = findConcatenation(item);
|
|
40
|
+
if (found)
|
|
41
|
+
return found;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else if (typeof child.type === 'string') {
|
|
46
|
+
const found = findConcatenation(child);
|
|
47
|
+
if (found)
|
|
48
|
+
return found;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Returns true if the node is a recognized JupyterLab translation bundle:
|
|
56
|
+
* trans | this.trans | this._trans | props.trans | this.props.trans
|
|
57
|
+
* See jupyterlab.readthedocs.io/en/stable/extension/internationalization.html#rules
|
|
58
|
+
*/
|
|
59
|
+
function isTransBundle(node) {
|
|
60
|
+
if (node.type === 'Identifier') {
|
|
61
|
+
return node.name === 'trans';
|
|
62
|
+
}
|
|
63
|
+
if (node.type === 'MemberExpression' && !node.computed) {
|
|
64
|
+
const prop = node.property;
|
|
65
|
+
if (prop.type !== 'Identifier') {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
// this.trans / this._trans
|
|
69
|
+
if (node.object.type === 'ThisExpression') {
|
|
70
|
+
return prop.name === 'trans' || prop.name === '_trans';
|
|
71
|
+
}
|
|
72
|
+
// props.trans
|
|
73
|
+
if (prop.name === 'trans' &&
|
|
74
|
+
node.object.type === 'Identifier' &&
|
|
75
|
+
node.object.name === 'props') {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
// this.props.trans
|
|
79
|
+
if (prop.name === 'trans' &&
|
|
80
|
+
node.object.type === 'MemberExpression' &&
|
|
81
|
+
!node.object.computed &&
|
|
82
|
+
node.object.object.type === 'ThisExpression' &&
|
|
83
|
+
node.object.property.type === 'Identifier' &&
|
|
84
|
+
node.object.property.name === 'props') {
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
const noTranslationConcatenation = (0, create_rule_1.createRule)({
|
|
91
|
+
name: 'no-translation-concatenation',
|
|
92
|
+
meta: {
|
|
93
|
+
type: 'problem',
|
|
94
|
+
docs: {
|
|
95
|
+
description: 'Forbid string concatenation inside translation wrapper calls',
|
|
96
|
+
url: 'https://eslint-plugin.readthedocs.io/en/latest/rules/no-translation-concatenation/'
|
|
97
|
+
},
|
|
98
|
+
messages: {
|
|
99
|
+
noConcatenation: 'Do not use string concatenation inside translation wrappers. Use a placeholder instead, e.g. trans.__("Hello %1", name).'
|
|
100
|
+
},
|
|
101
|
+
schema: []
|
|
102
|
+
},
|
|
103
|
+
defaultOptions: [],
|
|
104
|
+
create(context) {
|
|
105
|
+
return {
|
|
106
|
+
CallExpression(node) {
|
|
107
|
+
if (node.callee.type !== 'MemberExpression') {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const callee = node.callee;
|
|
111
|
+
if (callee.computed || callee.property.type !== 'Identifier') {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (callee.property.name !== TRANS_METHOD) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (!isTransBundle(callee.object)) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const message = node.arguments[0];
|
|
121
|
+
if (!message) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const concat = findConcatenation(message);
|
|
125
|
+
if (concat) {
|
|
126
|
+
context.report({ node: concat, messageId: 'noConcatenation' });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
module.exports = noTranslationConcatenation;
|
|
@@ -0,0 +1,4 @@
|
|
|
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> & {
|
|
2
|
+
name: string;
|
|
3
|
+
};
|
|
4
|
+
export = noUntranslatedString;
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) Jupyter Development Team.
|
|
4
|
+
* Distributed under the terms of the Modified BSD License.
|
|
5
|
+
*/
|
|
6
|
+
const commands_1 = require("../utils/commands");
|
|
7
|
+
const plugin_utils_1 = require("../utils/plugin-utils");
|
|
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;
|
|
19
|
+
}
|
|
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;
|
|
26
|
+
}
|
|
27
|
+
if (node.type === 'ArrowFunctionExpression' &&
|
|
28
|
+
node.body.type !== 'BlockStatement') {
|
|
29
|
+
return isRawStringNode(node.body);
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
function isSetAttributeCall(node) {
|
|
34
|
+
return (node.callee.type === 'MemberExpression' &&
|
|
35
|
+
!node.callee.computed &&
|
|
36
|
+
node.callee.property.type === 'Identifier' &&
|
|
37
|
+
node.callee.property.name === 'setAttribute');
|
|
38
|
+
}
|
|
39
|
+
function isShowDialogCall(node) {
|
|
40
|
+
return (node.callee.type === 'Identifier' && node.callee.name === 'showDialog');
|
|
41
|
+
}
|
|
42
|
+
function isDialogConstructor(node) {
|
|
43
|
+
return node.callee.type === 'Identifier' && node.callee.name === 'Dialog';
|
|
44
|
+
}
|
|
45
|
+
const DIALOG_BUTTON_BUILDERS = [
|
|
46
|
+
'okButton',
|
|
47
|
+
'cancelButton',
|
|
48
|
+
'warnButton',
|
|
49
|
+
'errorButton'
|
|
50
|
+
];
|
|
51
|
+
function isDialogButtonCall(node) {
|
|
52
|
+
return (node.callee.type === 'MemberExpression' &&
|
|
53
|
+
!node.callee.computed &&
|
|
54
|
+
node.callee.property.type === 'Identifier' &&
|
|
55
|
+
DIALOG_BUTTON_BUILDERS.includes(node.callee.property.name));
|
|
56
|
+
}
|
|
57
|
+
const MONITORED_COMMAND_PROPS = ['label', 'caption', 'usage'];
|
|
58
|
+
const MONITORED_SET_ATTRIBUTE_ATTRS = ['aria-label', 'aria-description', 'title'];
|
|
59
|
+
const MONITORED_ASSIGNMENT_PROPS = ['title', 'ariaLabel'];
|
|
60
|
+
const MONITORED_DIALOG_PROPS = ['title', 'body'];
|
|
61
|
+
const noUntranslatedString = (0, create_rule_1.createRule)({
|
|
62
|
+
name: 'no-untranslated-string',
|
|
63
|
+
meta: {
|
|
64
|
+
type: 'suggestion',
|
|
65
|
+
docs: {
|
|
66
|
+
description: 'Require user-facing string literals to be wrapped in a translation call such as trans.__()',
|
|
67
|
+
url: 'https://eslint-plugin.readthedocs.io/en/latest/rules/no-untranslated-string/'
|
|
68
|
+
},
|
|
69
|
+
messages: {
|
|
70
|
+
untranslatedCommandProp: 'Command property "{{ prop }}" has an untranslated string literal. Wrap it with trans.__().',
|
|
71
|
+
untranslatedSetAttribute: 'setAttribute("{{ attr }}", ...) has an untranslated string literal. Wrap the value with trans.__().',
|
|
72
|
+
untranslatedPropertyAssign: 'Assignment to "{{ prop }}" has an untranslated string literal. Wrap it with trans.__().',
|
|
73
|
+
untranslatedTitleProp: 'Assignment to "title.{{ prop }}" has an untranslated string literal. Wrap it with trans.__().',
|
|
74
|
+
untranslatedDialogOption: 'Dialog/showDialog "{{ prop }}" option has an untranslated string literal. Wrap it with trans.__().',
|
|
75
|
+
untranslatedDialogButtonLabel: 'Dialog button "label" option has an untranslated string literal. Wrap it with trans.__().',
|
|
76
|
+
untranslatedJsxText: 'JSX text content has an untranslated string literal. Wrap it with {trans.__(...)}'
|
|
77
|
+
},
|
|
78
|
+
schema: []
|
|
79
|
+
},
|
|
80
|
+
defaultOptions: [],
|
|
81
|
+
create(context) {
|
|
82
|
+
return {
|
|
83
|
+
CallExpression(node) {
|
|
84
|
+
// Branch A: commands.addCommand(id, { label, caption, usage })
|
|
85
|
+
if ((0, commands_1.isAddCommandCall)(node)) {
|
|
86
|
+
if (node.arguments.length < 2) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const optionsArg = node.arguments[1];
|
|
90
|
+
if (optionsArg.type !== 'ObjectExpression') {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const properties = (0, plugin_utils_1.getObjectProperties)(optionsArg);
|
|
94
|
+
for (const propName of MONITORED_COMMAND_PROPS) {
|
|
95
|
+
const prop = properties.get(propName);
|
|
96
|
+
if (prop && isRawStringNode(prop.value)) {
|
|
97
|
+
context.report({
|
|
98
|
+
node: prop.value,
|
|
99
|
+
messageId: 'untranslatedCommandProp',
|
|
100
|
+
data: { prop: propName }
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
// Branch B: element.setAttribute('aria-label'/'aria-description'/'title', string)
|
|
107
|
+
if (isSetAttributeCall(node)) {
|
|
108
|
+
if (node.arguments.length < 2) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const attrNameArg = node.arguments[0];
|
|
112
|
+
if (attrNameArg.type !== 'Literal' ||
|
|
113
|
+
typeof attrNameArg.value !== 'string' ||
|
|
114
|
+
!MONITORED_SET_ATTRIBUTE_ATTRS.includes(attrNameArg.value)) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const attrValueArg = node.arguments[1];
|
|
118
|
+
if (isRawStringNode(attrValueArg)) {
|
|
119
|
+
context.report({
|
|
120
|
+
node: attrValueArg,
|
|
121
|
+
messageId: 'untranslatedSetAttribute',
|
|
122
|
+
data: { attr: attrNameArg.value }
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
// Branch C: showDialog({ title, body })
|
|
128
|
+
if (isShowDialogCall(node)) {
|
|
129
|
+
if (node.arguments.length < 1) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const optionsArg = node.arguments[0];
|
|
133
|
+
if (optionsArg.type !== 'ObjectExpression') {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const properties = (0, plugin_utils_1.getObjectProperties)(optionsArg);
|
|
137
|
+
for (const propName of MONITORED_DIALOG_PROPS) {
|
|
138
|
+
const prop = properties.get(propName);
|
|
139
|
+
if (prop && isRawStringNode(prop.value)) {
|
|
140
|
+
context.report({
|
|
141
|
+
node: prop.value,
|
|
142
|
+
messageId: 'untranslatedDialogOption',
|
|
143
|
+
data: { prop: propName }
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
// Branch D: Dialog.okButton/cancelButton/warnButton/errorButton({ label })
|
|
150
|
+
if (isDialogButtonCall(node)) {
|
|
151
|
+
if (node.arguments.length < 1) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const optionsArg = node.arguments[0];
|
|
155
|
+
if (optionsArg.type !== 'ObjectExpression') {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const properties = (0, plugin_utils_1.getObjectProperties)(optionsArg);
|
|
159
|
+
const labelProp = properties.get('label');
|
|
160
|
+
if (labelProp && isRawStringNode(labelProp.value)) {
|
|
161
|
+
context.report({
|
|
162
|
+
node: labelProp.value,
|
|
163
|
+
messageId: 'untranslatedDialogButtonLabel'
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
// new Dialog({ title, body })
|
|
169
|
+
NewExpression(node) {
|
|
170
|
+
if (!isDialogConstructor(node)) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (node.arguments.length < 1) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const optionsArg = node.arguments[0];
|
|
177
|
+
if (optionsArg.type !== 'ObjectExpression') {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const properties = (0, plugin_utils_1.getObjectProperties)(optionsArg);
|
|
181
|
+
for (const propName of MONITORED_DIALOG_PROPS) {
|
|
182
|
+
const prop = properties.get(propName);
|
|
183
|
+
if (prop && isRawStringNode(prop.value)) {
|
|
184
|
+
context.report({
|
|
185
|
+
node: prop.value,
|
|
186
|
+
messageId: 'untranslatedDialogOption',
|
|
187
|
+
data: { prop: propName }
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
AssignmentExpression(node) {
|
|
193
|
+
if (node.operator !== '=') {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
const left = node.left;
|
|
197
|
+
if (left.type !== 'MemberExpression' ||
|
|
198
|
+
left.computed ||
|
|
199
|
+
left.property.type !== 'Identifier') {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
// element.title = STRING or element.ariaLabel = STRING
|
|
203
|
+
if (MONITORED_ASSIGNMENT_PROPS.includes(left.property.name) &&
|
|
204
|
+
isRawStringNode(node.right)) {
|
|
205
|
+
context.report({
|
|
206
|
+
node: node.right,
|
|
207
|
+
messageId: 'untranslatedPropertyAssign',
|
|
208
|
+
data: { prop: left.property.name }
|
|
209
|
+
});
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
// *.title.label = STRING or *.title.caption = STRING
|
|
213
|
+
if ((left.property.name === 'label' || left.property.name === 'caption') &&
|
|
214
|
+
left.object.type === 'MemberExpression' &&
|
|
215
|
+
!left.object.computed &&
|
|
216
|
+
left.object.property.type === 'Identifier' &&
|
|
217
|
+
left.object.property.name === 'title' &&
|
|
218
|
+
isRawStringNode(node.right)) {
|
|
219
|
+
context.report({
|
|
220
|
+
node: node.right,
|
|
221
|
+
messageId: 'untranslatedTitleProp',
|
|
222
|
+
data: { prop: left.property.name }
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
// Raw text between JSX tags: <span>Untranslated text</span>
|
|
227
|
+
JSXText(node) {
|
|
228
|
+
if (node.value.trim().length > 0) {
|
|
229
|
+
context.report({
|
|
230
|
+
node,
|
|
231
|
+
messageId: 'untranslatedJsxText'
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
// String literal inside JSX expression: <span>{'raw string'}</span>
|
|
236
|
+
JSXExpressionContainer(node) {
|
|
237
|
+
if (node.expression.type === 'JSXEmptyExpression') {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
if (isRawStringNode(node.expression)) {
|
|
241
|
+
context.report({
|
|
242
|
+
node: node.expression,
|
|
243
|
+
messageId: 'untranslatedJsxText'
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
module.exports = noUntranslatedString;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
-
declare const jupyterPluginActivationArgs: ESLintUtils.RuleModule<"mismatchedOrder" | "missingArgument" | "extraArgument" | "appNotFirst" | "serviceManagerFirstArgNotNull" | "invalidAppType" | "wrongArgumentCount" | "unresolvableTokenType", [{
|
|
2
|
+
declare const jupyterPluginActivationArgs: ESLintUtils.RuleModule<"mismatchedOrder" | "incorrectType" | "missingArgument" | "extraArgument" | "appNotFirst" | "serviceManagerFirstArgNotNull" | "invalidAppType" | "wrongArgumentCount" | "unresolvableTokenType", [{
|
|
3
3
|
allowedFirstArgumentNames: string[];
|
|
4
4
|
}], unknown, ESLintUtils.RuleListener> & {
|
|
5
5
|
name: string;
|
|
@@ -56,13 +56,13 @@ function findActivateFunction(node) {
|
|
|
56
56
|
if (activateValue.type === 'ArrowFunctionExpression' ||
|
|
57
57
|
activateValue.type === 'FunctionExpression') {
|
|
58
58
|
const params = activateValue.params
|
|
59
|
-
.filter((param) => param.type === 'Identifier')
|
|
60
|
-
|
|
61
|
-
const paramTypes =
|
|
62
|
-
const paramNodes =
|
|
59
|
+
.filter((param) => param.type === 'Identifier');
|
|
60
|
+
const paramNames = params.map(param => param.name);
|
|
61
|
+
const paramTypes = params.map(param => param.type === 'Identifier' ? (0, plugin_utils_1.extractParameterType)(param) : null);
|
|
62
|
+
const paramNodes = params.map(param => param.type === 'Identifier' ? param : null);
|
|
63
63
|
return {
|
|
64
64
|
node: activateProp,
|
|
65
|
-
|
|
65
|
+
paramNames,
|
|
66
66
|
paramTypes,
|
|
67
67
|
paramNodes
|
|
68
68
|
};
|
|
@@ -118,13 +118,14 @@ const jupyterPluginActivationArgs = (0, create_rule_1.createRule)({
|
|
|
118
118
|
},
|
|
119
119
|
messages: {
|
|
120
120
|
mismatchedOrder: 'Activation argument "{{ arg }}" does not match the order of requires/optional tokens.',
|
|
121
|
+
incorrectType: 'Activation argument "{{ arg }}" has incorrect type annotation "{{ type }}". Expected type "{{ expected }}".',
|
|
121
122
|
missingArgument: 'Token "{{ token }}" from requires/optional is missing in activation arguments.',
|
|
122
123
|
extraArgument: 'Activation argument "{{ arg }}" is not in requires/optional tokens.',
|
|
123
124
|
appNotFirst: 'First activation argument must be one of [{{ allowedNames }}] (JupyterFrontEnd), got "{{ arg }}".',
|
|
124
125
|
serviceManagerFirstArgNotNull: 'First activation argument for ServiceManagerPlugin must be null, got "{{ arg }}".',
|
|
125
126
|
invalidAppType: 'First activation argument "{{ arg }}" has invalid type "{{ type }}". Expected JupyterFrontEnd, JupyterLab, or Application.',
|
|
126
127
|
wrongArgumentCount: 'Expected {{ expected }} activation arguments (app + {{ tokenCount }} tokens), got {{ actual }}.',
|
|
127
|
-
unresolvableTokenType: 'Token "{{ token }}" type could not be resolved
|
|
128
|
+
unresolvableTokenType: 'Token "{{ token }}" type could not be resolved. The package may be unbuilt or type checking may not be configured.'
|
|
128
129
|
},
|
|
129
130
|
fixable: 'code',
|
|
130
131
|
schema: [
|
|
@@ -235,7 +236,14 @@ const jupyterPluginActivationArgs = (0, create_rule_1.createRule)({
|
|
|
235
236
|
// For most cases this check is sufficient
|
|
236
237
|
if (paramType === token.name)
|
|
237
238
|
return [true, false];
|
|
238
|
-
|
|
239
|
+
// Without a checker we cannot resolve namespace patterns like
|
|
240
|
+
// `IDebugger.ISidebar` ↔ `IDebuggerSidebar`, so we cannot distinguish
|
|
241
|
+
// a genuine mismatch from a valid aliasing convention.
|
|
242
|
+
// Users wanting full type-aware checks should configure
|
|
243
|
+
// `parserOptions.project`.
|
|
244
|
+
if (!checker)
|
|
245
|
+
return [false, true];
|
|
246
|
+
if (paramNode) {
|
|
239
247
|
const resolvedToken = resolveTokenInnerType(token.node);
|
|
240
248
|
const tokenUnresolved = resolvedToken === null ||
|
|
241
249
|
resolvedToken === undefined ||
|
|
@@ -248,10 +256,6 @@ const jupyterPluginActivationArgs = (0, create_rule_1.createRule)({
|
|
|
248
256
|
return [isSameType(resolvedToken, resolvedParam), false];
|
|
249
257
|
}
|
|
250
258
|
}
|
|
251
|
-
// Without a checker we cannot resolve namespace patterns like
|
|
252
|
-
// `IDebugger.ISidebar` ↔ `IDebuggerSidebar`.
|
|
253
|
-
if (!checker && paramType && paramType.includes('.'))
|
|
254
|
-
return [true, false];
|
|
255
259
|
return [false, false];
|
|
256
260
|
}
|
|
257
261
|
return {
|
|
@@ -267,22 +271,22 @@ const jupyterPluginActivationArgs = (0, create_rule_1.createRule)({
|
|
|
267
271
|
if (!activateInfo) {
|
|
268
272
|
return;
|
|
269
273
|
}
|
|
270
|
-
const {
|
|
274
|
+
const { paramNames, paramTypes, paramNodes } = activateInfo;
|
|
271
275
|
const expectedCount = 1 + requires.length + optional.length;
|
|
272
276
|
const expectedTokensWithoutApp = [...requires, ...optional];
|
|
273
|
-
if (expectedCount === 1 &&
|
|
277
|
+
if (expectedCount === 1 && paramNames.length === 0) {
|
|
274
278
|
// Special case, not invalid
|
|
275
279
|
return;
|
|
276
280
|
}
|
|
277
281
|
if (pluginKind === 'frontend') {
|
|
278
282
|
// Validation 1a: Check if first argument is one of the allowed names
|
|
279
|
-
if (
|
|
280
|
-
!allowedFirstArgumentNames.includes(
|
|
283
|
+
if (paramNames.length > 0 &&
|
|
284
|
+
!allowedFirstArgumentNames.includes(paramNames[0])) {
|
|
281
285
|
context.report({
|
|
282
286
|
node: activateInfo.node,
|
|
283
287
|
messageId: 'appNotFirst',
|
|
284
288
|
data: {
|
|
285
|
-
arg:
|
|
289
|
+
arg: paramNames[0],
|
|
286
290
|
allowedNames: allowedFirstArgumentNames
|
|
287
291
|
.map(name => `"${name}"`)
|
|
288
292
|
.join(', ')
|
|
@@ -291,14 +295,14 @@ const jupyterPluginActivationArgs = (0, create_rule_1.createRule)({
|
|
|
291
295
|
return;
|
|
292
296
|
}
|
|
293
297
|
// Validation 1b: Check if first argument type is compatible with JupyterFrontEnd
|
|
294
|
-
if (
|
|
298
|
+
if (paramNames.length > 0 && paramTypes.length > 0) {
|
|
295
299
|
const firstParamType = paramTypes[0];
|
|
296
300
|
if (!isCompatibleWithJupyterFrontEnd(firstParamType)) {
|
|
297
301
|
context.report({
|
|
298
302
|
node: activateInfo.node,
|
|
299
303
|
messageId: 'invalidAppType',
|
|
300
304
|
data: {
|
|
301
|
-
arg:
|
|
305
|
+
arg: paramNames[0],
|
|
302
306
|
type: firstParamType || 'unknown'
|
|
303
307
|
}
|
|
304
308
|
});
|
|
@@ -308,7 +312,7 @@ const jupyterPluginActivationArgs = (0, create_rule_1.createRule)({
|
|
|
308
312
|
}
|
|
309
313
|
else if (pluginKind === 'service-manager') {
|
|
310
314
|
// Validation 1: First argument must be literal null
|
|
311
|
-
if (
|
|
315
|
+
if (paramNames.length === 0 || paramTypes[0] !== null) {
|
|
312
316
|
context.report({
|
|
313
317
|
node: activateInfo.node,
|
|
314
318
|
messageId: 'serviceManagerFirstArgNotNull',
|
|
@@ -320,70 +324,74 @@ const jupyterPluginActivationArgs = (0, create_rule_1.createRule)({
|
|
|
320
324
|
}
|
|
321
325
|
}
|
|
322
326
|
// Validation 2: Check if argument count matches
|
|
323
|
-
if (
|
|
327
|
+
if (paramNames.length !== expectedCount) {
|
|
324
328
|
context.report({
|
|
325
329
|
node: activateInfo.node,
|
|
326
330
|
messageId: 'wrongArgumentCount',
|
|
327
331
|
data: {
|
|
328
332
|
expected: String(expectedCount),
|
|
329
333
|
tokenCount: String(requires.length + optional.length),
|
|
330
|
-
actual: String(
|
|
334
|
+
actual: String(paramNames.length)
|
|
331
335
|
}
|
|
332
336
|
});
|
|
333
337
|
}
|
|
334
|
-
// Validation 3:
|
|
338
|
+
// Validation 3: Validate remaining parameters against expected token order
|
|
335
339
|
const actualParamTypes = paramTypes.slice(1); // First arg already validated above
|
|
336
340
|
const actualParamNodes = paramNodes.slice(1);
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
341
|
+
// Validate that parameter types match expected token types in order
|
|
342
|
+
for (let i = 0; i < actualParamTypes.length; i++) {
|
|
343
|
+
const paramType = actualParamTypes[i];
|
|
344
|
+
const paramNode = actualParamNodes[i];
|
|
345
|
+
const expectedToken = expectedTokensWithoutApp[i];
|
|
346
|
+
if (expectedToken === undefined) {
|
|
347
|
+
// Extra argument
|
|
348
|
+
context.report({
|
|
349
|
+
node: activateInfo.node,
|
|
350
|
+
messageId: 'extraArgument',
|
|
351
|
+
data: { arg: paramNames[i + 1] }
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
const [matches, tokenUnresolved] = tokenMatchesParam(expectedToken, paramType, paramNode);
|
|
356
|
+
if (tokenUnresolved) {
|
|
357
|
+
context.report({
|
|
358
|
+
node: activateInfo.node,
|
|
359
|
+
messageId: 'unresolvableTokenType',
|
|
360
|
+
data: { token: expectedToken.name }
|
|
361
|
+
});
|
|
353
362
|
}
|
|
354
|
-
else {
|
|
355
|
-
|
|
356
|
-
|
|
363
|
+
else if (!matches) {
|
|
364
|
+
// Distinguish token-order conflict vs. invalid type annotation.
|
|
365
|
+
const matchesAnyOtherToken = expectedTokensWithoutApp.some((otherToken, j) => {
|
|
366
|
+
if (j === i)
|
|
367
|
+
return false;
|
|
368
|
+
const [otherMatches, tokenUnresolved] = tokenMatchesParam(otherToken, paramType, paramNode);
|
|
369
|
+
return otherMatches && !tokenUnresolved;
|
|
370
|
+
});
|
|
371
|
+
if (matchesAnyOtherToken) {
|
|
357
372
|
context.report({
|
|
358
373
|
node: activateInfo.node,
|
|
359
|
-
messageId: '
|
|
360
|
-
data: {
|
|
374
|
+
messageId: 'mismatchedOrder',
|
|
375
|
+
data: { arg: paramNames[i + 1] }
|
|
361
376
|
});
|
|
362
377
|
}
|
|
363
|
-
else
|
|
378
|
+
else {
|
|
364
379
|
context.report({
|
|
365
380
|
node: activateInfo.node,
|
|
366
|
-
messageId: '
|
|
367
|
-
data: {
|
|
381
|
+
messageId: 'incorrectType',
|
|
382
|
+
data: {
|
|
383
|
+
arg: paramNames[i + 1],
|
|
384
|
+
type: paramType,
|
|
385
|
+
expected: expectedToken.name
|
|
386
|
+
}
|
|
368
387
|
});
|
|
369
388
|
}
|
|
370
389
|
}
|
|
371
390
|
}
|
|
372
391
|
}
|
|
373
|
-
else {
|
|
374
|
-
// Without type information, we only check for extra arguments
|
|
375
|
-
const actualParams = params.slice(1);
|
|
376
|
-
for (let i = expectedTokensWithoutApp.length; i < actualParams.length; i++) {
|
|
377
|
-
context.report({
|
|
378
|
-
node: activateInfo.node,
|
|
379
|
-
messageId: 'extraArgument',
|
|
380
|
-
data: { arg: actualParams[i] }
|
|
381
|
-
});
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
392
|
// Validation 4: Check for missing arguments (only if counts don't match)
|
|
385
|
-
if (
|
|
386
|
-
for (let i = Math.max(
|
|
393
|
+
if (paramNames.length < expectedCount) {
|
|
394
|
+
for (let i = Math.max(paramNames.length - 1, 0); i < expectedTokensWithoutApp.length; i++) {
|
|
387
395
|
const missingToken = expectedTokensWithoutApp[i];
|
|
388
396
|
context.report({
|
|
389
397
|
node: activateInfo.node,
|
|
@@ -6,9 +6,14 @@
|
|
|
6
6
|
const create_rule_1 = require("../utils/create-rule");
|
|
7
7
|
const plugin_utils_1 = require("../utils/plugin-utils");
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* Returns whether the object expression has a description property and whether
|
|
10
|
+
* it is non-empty.
|
|
11
|
+
*
|
|
12
|
+
* - 'missing' – no description key found
|
|
13
|
+
* - 'empty' – key exists but its literal value is empty / whitespace-only
|
|
14
|
+
* - 'present' – key exists with a non-empty value (or a non-literal value)
|
|
10
15
|
*/
|
|
11
|
-
function
|
|
16
|
+
function checkDescriptionProperty(obj) {
|
|
12
17
|
for (const prop of obj.properties) {
|
|
13
18
|
if (prop.type === 'Property') {
|
|
14
19
|
let keyName = null;
|
|
@@ -20,16 +25,17 @@ function hasDescriptionProperty(obj) {
|
|
|
20
25
|
keyName = prop.key.value;
|
|
21
26
|
}
|
|
22
27
|
if (keyName === 'description') {
|
|
23
|
-
// Check if description value is not empty
|
|
24
28
|
if (prop.value.type === 'Literal') {
|
|
25
29
|
const value = prop.value.value;
|
|
26
|
-
return typeof value === 'string' && value.trim().length > 0
|
|
30
|
+
return typeof value === 'string' && value.trim().length > 0
|
|
31
|
+
? 'present'
|
|
32
|
+
: 'empty';
|
|
27
33
|
}
|
|
28
|
-
return
|
|
34
|
+
return 'present';
|
|
29
35
|
}
|
|
30
36
|
}
|
|
31
37
|
}
|
|
32
|
-
return
|
|
38
|
+
return 'missing';
|
|
33
39
|
}
|
|
34
40
|
const jupyterPluginDescription = (0, create_rule_1.createRule)({
|
|
35
41
|
name: 'plugin-description',
|
|
@@ -59,11 +65,14 @@ const jupyterPluginDescription = (0, create_rule_1.createRule)({
|
|
|
59
65
|
}
|
|
60
66
|
const pluginId = (0, plugin_utils_1.getPluginId)(varDecl.init);
|
|
61
67
|
const pluginIdSuffix = pluginId ? ` "${pluginId}"` : '';
|
|
62
|
-
// Check if description property exists
|
|
63
|
-
|
|
68
|
+
// Check if description property exists and is non-empty
|
|
69
|
+
const descriptionStatus = checkDescriptionProperty(varDecl.init);
|
|
70
|
+
if (descriptionStatus !== 'present') {
|
|
64
71
|
context.report({
|
|
65
72
|
node: varDecl.init,
|
|
66
|
-
messageId: '
|
|
73
|
+
messageId: descriptionStatus === 'empty'
|
|
74
|
+
? 'emptyDescription'
|
|
75
|
+
: 'missingDescription',
|
|
67
76
|
data: { pluginId: pluginIdSuffix }
|
|
68
77
|
});
|
|
69
78
|
}
|
|
@@ -0,0 +1,63 @@
|
|
|
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
|
+
/**
|
|
8
|
+
* Token symbol (part after the first ':') must be a valid JavaScript identifier.
|
|
9
|
+
*/
|
|
10
|
+
const VALID_TOKEN_SYMBOL = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
11
|
+
const tokenFormat = (0, create_rule_1.createRule)({
|
|
12
|
+
name: 'token-format',
|
|
13
|
+
meta: {
|
|
14
|
+
type: 'problem',
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'Ensure JupyterLab Token ids follow the <package>:<TokenSymbol> naming convention',
|
|
17
|
+
url: 'https://eslint-plugin.readthedocs.io/en/latest/rules/token-format/'
|
|
18
|
+
},
|
|
19
|
+
messages: {
|
|
20
|
+
missingColon: 'Token id "{{ tokenId }}" must follow the format "<package>:<TokenSymbol>" (no colon found).',
|
|
21
|
+
invalidTokenSymbol: 'Token id "{{ tokenId }}" has an invalid symbol "{{ symbol }}". ' +
|
|
22
|
+
'The part after ":" must be a valid JavaScript identifier.'
|
|
23
|
+
},
|
|
24
|
+
schema: []
|
|
25
|
+
},
|
|
26
|
+
defaultOptions: [],
|
|
27
|
+
create(context) {
|
|
28
|
+
return {
|
|
29
|
+
NewExpression(node) {
|
|
30
|
+
if (node.callee.type !== 'Identifier' ||
|
|
31
|
+
node.callee.name !== 'Token') {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (node.arguments.length === 0) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const firstArg = node.arguments[0];
|
|
38
|
+
if (firstArg.type !== 'Literal' || typeof firstArg.value !== 'string') {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const tokenId = firstArg.value;
|
|
42
|
+
const colonIndex = tokenId.indexOf(':');
|
|
43
|
+
if (colonIndex === -1) {
|
|
44
|
+
context.report({
|
|
45
|
+
node: firstArg,
|
|
46
|
+
messageId: 'missingColon',
|
|
47
|
+
data: { tokenId }
|
|
48
|
+
});
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const symbol = tokenId.slice(colonIndex + 1);
|
|
52
|
+
if (!VALID_TOKEN_SYMBOL.test(symbol)) {
|
|
53
|
+
context.report({
|
|
54
|
+
node: firstArg,
|
|
55
|
+
messageId: 'invalidTokenSymbol',
|
|
56
|
+
data: { tokenId, symbol }
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
module.exports = tokenFormat;
|
package/package.json
CHANGED