@jupyter/eslint-plugin 1.0.1 → 1.1.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 (35) hide show
  1. package/lib/index.d.ts +68 -0
  2. package/lib/index.js +42 -11
  3. package/lib/rules/galata-prefer-filebrowser-helper.d.ts +5 -0
  4. package/lib/rules/galata-prefer-filebrowser-helper.js +105 -0
  5. package/lib/rules/incorrect-translator-usage.d.ts +4 -0
  6. package/lib/rules/incorrect-translator-usage.js +208 -0
  7. package/lib/rules/no-dynamic-translation.d.ts +4 -0
  8. package/lib/rules/no-dynamic-translation.js +92 -0
  9. package/lib/rules/no-pageconfig-base-url.d.ts +4 -0
  10. package/lib/rules/no-pageconfig-base-url.js +37 -0
  11. package/lib/rules/no-translation-concatenation.js +46 -84
  12. package/lib/rules/no-untranslated-string.js +7 -4
  13. package/lib/rules/plugin-activation-args.js +1 -2
  14. package/lib/rules/prefer-signal-this-arg.d.ts +7 -0
  15. package/lib/rules/prefer-signal-this-arg.js +196 -0
  16. package/lib/rules/require-disposable-ownership.d.ts +5 -0
  17. package/lib/rules/require-disposable-ownership.js +120 -0
  18. package/lib/rules/require-disposable-transfer.d.ts +5 -0
  19. package/lib/rules/require-disposable-transfer.js +223 -0
  20. package/lib/rules/require-signal-cleanup.d.ts +8 -0
  21. package/lib/rules/require-signal-cleanup.js +164 -0
  22. package/lib/rules/require-signal-this-arg.d.ts +5 -0
  23. package/lib/rules/require-signal-this-arg.js +74 -0
  24. package/lib/rules/token-format.js +1 -2
  25. package/lib/utils/disposables.d.ts +64 -0
  26. package/lib/utils/disposables.js +1723 -0
  27. package/lib/utils/playwright-selectors.d.ts +66 -0
  28. package/lib/utils/playwright-selectors.js +193 -0
  29. package/lib/utils/signal-lifetime.d.ts +158 -0
  30. package/lib/utils/signal-lifetime.js +755 -0
  31. package/lib/utils/signals.d.ts +143 -0
  32. package/lib/utils/signals.js +601 -0
  33. package/lib/utils/translation.d.ts +45 -0
  34. package/lib/utils/translation.js +122 -0
  35. package/package.json +9 -2
package/lib/index.d.ts CHANGED
@@ -15,6 +15,9 @@ declare const plugin: {
15
15
  'no-translation-concatenation': import("@typescript-eslint/utils/ts-eslint").RuleModule<"noConcatenation", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
16
16
  name: string;
17
17
  };
18
+ 'no-dynamic-translation': import("@typescript-eslint/utils/ts-eslint").RuleModule<"noInterpolation" | "noDynamicMessage", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
19
+ name: string;
20
+ };
18
21
  'token-format': import("@typescript-eslint/utils/ts-eslint").RuleModule<"missingColon" | "invalidTokenSymbol", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
19
22
  name: string;
20
23
  };
@@ -29,6 +32,35 @@ declare const plugin: {
29
32
  'require-soft-assertions-before-snapshots': import("@typescript-eslint/utils/ts-eslint").RuleModule<"requireSoftAssertion", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
30
33
  name: string;
31
34
  };
35
+ 'no-pageconfig-base-url': import("@typescript-eslint/utils/ts-eslint").RuleModule<"noPageconfigBaseUrl", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
36
+ name: string;
37
+ };
38
+ 'require-signal-cleanup': import("@typescript-eslint/utils/ts-eslint").RuleModule<"serviceOutlivesReceiver", [{
39
+ additionalCleanupMethods: string[];
40
+ longLivedTypes: string[] | undefined;
41
+ }], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
42
+ name: string;
43
+ };
44
+ 'require-signal-this-arg': import("@typescript-eslint/utils/ts-eslint").RuleModule<"missingThisArg" | "addThisArg", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
45
+ name: string;
46
+ };
47
+ 'prefer-signal-this-arg': import("@typescript-eslint/utils/ts-eslint").RuleModule<"addThisArg" | "preferThisArg" | "boundCallback" | "unbindAndAddThisArg", [{
48
+ longLivedTypes: string[] | undefined;
49
+ }], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
50
+ name: string;
51
+ };
52
+ 'galata-prefer-filebrowser-helper': import("@typescript-eslint/utils/ts-eslint").RuleModule<"preferNotebookOpenByPath" | "preferOpenHomeDirectory" | "preferOpenDirectory" | "preferFilebrowserHelper", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
53
+ name: string;
54
+ };
55
+ 'require-disposable-ownership': import("@typescript-eslint/utils/ts-eslint").RuleModule<"unmanagedDisposable" | "unmanagedDisposableVariable", [{}], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
56
+ name: string;
57
+ };
58
+ 'require-disposable-transfer': import("@typescript-eslint/utils/ts-eslint").RuleModule<"unmanagedDisposableVariable" | "unhandledDisposable", [{}], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
59
+ name: string;
60
+ };
61
+ 'incorrect-translator-usage': import("@typescript-eslint/utils/ts-eslint").RuleModule<"chainedBundleMethod" | "invalidBundleName" | "destructuredBundle", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
62
+ name: string;
63
+ };
32
64
  };
33
65
  configs: {
34
66
  recommended: ({
@@ -38,10 +70,19 @@ declare const plugin: {
38
70
  'jupyter/command-described-by': string;
39
71
  'jupyter/plugin-description': string;
40
72
  'jupyter/no-translation-concatenation': string;
73
+ 'jupyter/no-dynamic-translation': string;
41
74
  'jupyter/token-format': string;
42
75
  'jupyter/no-untranslated-string': string;
76
+ 'jupyter/no-pageconfig-base-url': string;
77
+ 'jupyter/require-signal-cleanup': string;
78
+ 'jupyter/require-signal-this-arg': string;
79
+ 'jupyter/prefer-signal-this-arg': string;
80
+ 'jupyter/require-disposable-ownership': string;
81
+ 'jupyter/require-disposable-transfer': string;
82
+ 'jupyter/incorrect-translator-usage': string;
43
83
  'jupyter/no-schema-enum'?: undefined;
44
84
  'jupyter/require-soft-assertions-before-snapshots'?: undefined;
85
+ 'jupyter/galata-prefer-filebrowser-helper'?: undefined;
45
86
  };
46
87
  languageOptions?: undefined;
47
88
  } | {
@@ -55,20 +96,38 @@ declare const plugin: {
55
96
  'jupyter/command-described-by'?: undefined;
56
97
  'jupyter/plugin-description'?: undefined;
57
98
  'jupyter/no-translation-concatenation'?: undefined;
99
+ 'jupyter/no-dynamic-translation'?: undefined;
58
100
  'jupyter/token-format'?: undefined;
59
101
  'jupyter/no-untranslated-string'?: undefined;
102
+ 'jupyter/no-pageconfig-base-url'?: undefined;
103
+ 'jupyter/require-signal-cleanup'?: undefined;
104
+ 'jupyter/require-signal-this-arg'?: undefined;
105
+ 'jupyter/prefer-signal-this-arg'?: undefined;
106
+ 'jupyter/require-disposable-ownership'?: undefined;
107
+ 'jupyter/require-disposable-transfer'?: undefined;
108
+ 'jupyter/incorrect-translator-usage'?: undefined;
60
109
  'jupyter/require-soft-assertions-before-snapshots'?: undefined;
110
+ 'jupyter/galata-prefer-filebrowser-helper'?: undefined;
61
111
  };
62
112
  } | {
63
113
  files: string[];
64
114
  rules: {
65
115
  'jupyter/require-soft-assertions-before-snapshots': string;
116
+ 'jupyter/galata-prefer-filebrowser-helper': string;
66
117
  'jupyter/plugin-activation-args'?: undefined;
67
118
  'jupyter/command-described-by'?: undefined;
68
119
  'jupyter/plugin-description'?: undefined;
69
120
  'jupyter/no-translation-concatenation'?: undefined;
121
+ 'jupyter/no-dynamic-translation'?: undefined;
70
122
  'jupyter/token-format'?: undefined;
71
123
  'jupyter/no-untranslated-string'?: undefined;
124
+ 'jupyter/no-pageconfig-base-url'?: undefined;
125
+ 'jupyter/require-signal-cleanup'?: undefined;
126
+ 'jupyter/require-signal-this-arg'?: undefined;
127
+ 'jupyter/prefer-signal-this-arg'?: undefined;
128
+ 'jupyter/require-disposable-ownership'?: undefined;
129
+ 'jupyter/require-disposable-transfer'?: undefined;
130
+ 'jupyter/incorrect-translator-usage'?: undefined;
72
131
  'jupyter/no-schema-enum'?: undefined;
73
132
  };
74
133
  languageOptions?: undefined;
@@ -79,14 +138,23 @@ declare const plugin: {
79
138
  'jupyter/command-described-by': string;
80
139
  'jupyter/plugin-description': string;
81
140
  'jupyter/no-translation-concatenation': string;
141
+ 'jupyter/no-dynamic-translation': string;
82
142
  'jupyter/token-format': string;
83
143
  'jupyter/no-untranslated-string': string;
84
144
  'jupyter/no-schema-enum': string;
145
+ 'jupyter/no-pageconfig-base-url': string;
146
+ 'jupyter/require-signal-cleanup': string;
147
+ 'jupyter/require-signal-this-arg': string;
148
+ 'jupyter/prefer-signal-this-arg': string;
149
+ 'jupyter/require-disposable-ownership': string;
150
+ 'jupyter/require-disposable-transfer': string;
151
+ 'jupyter/incorrect-translator-usage': string;
85
152
  };
86
153
  overrides: {
87
154
  files: string[];
88
155
  rules: {
89
156
  'jupyter/require-soft-assertions-before-snapshots': string;
157
+ 'jupyter/galata-prefer-filebrowser-helper': string;
90
158
  };
91
159
  }[];
92
160
  };
package/lib/index.js CHANGED
@@ -44,20 +44,38 @@ const plugin_activation_args_1 = __importDefault(require("./rules/plugin-activat
44
44
  const command_described_by_1 = __importDefault(require("./rules/command-described-by"));
45
45
  const plugin_description_1 = __importDefault(require("./rules/plugin-description"));
46
46
  const no_translation_concatenation_1 = __importDefault(require("./rules/no-translation-concatenation"));
47
+ const no_dynamic_translation_1 = __importDefault(require("./rules/no-dynamic-translation"));
47
48
  const token_format_1 = __importDefault(require("./rules/token-format"));
48
49
  const no_untranslated_string_1 = __importDefault(require("./rules/no-untranslated-string"));
49
50
  const no_schema_enum_1 = __importDefault(require("./rules/no-schema-enum"));
50
51
  const require_soft_assertions_before_snapshots_1 = __importDefault(require("./rules/require-soft-assertions-before-snapshots"));
52
+ const no_pageconfig_base_url_1 = __importDefault(require("./rules/no-pageconfig-base-url"));
53
+ const require_signal_cleanup_1 = __importDefault(require("./rules/require-signal-cleanup"));
54
+ const require_signal_this_arg_1 = __importDefault(require("./rules/require-signal-this-arg"));
55
+ const prefer_signal_this_arg_1 = __importDefault(require("./rules/prefer-signal-this-arg"));
56
+ const galata_prefer_filebrowser_helper_1 = __importDefault(require("./rules/galata-prefer-filebrowser-helper"));
57
+ const require_disposable_ownership_1 = __importDefault(require("./rules/require-disposable-ownership"));
58
+ const require_disposable_transfer_1 = __importDefault(require("./rules/require-disposable-transfer"));
59
+ const incorrect_translator_usage_1 = __importDefault(require("./rules/incorrect-translator-usage"));
51
60
  const plugin = {
52
61
  rules: {
53
62
  'plugin-activation-args': plugin_activation_args_1.default,
54
63
  'command-described-by': command_described_by_1.default,
55
64
  'plugin-description': plugin_description_1.default,
56
65
  'no-translation-concatenation': no_translation_concatenation_1.default,
66
+ 'no-dynamic-translation': no_dynamic_translation_1.default,
57
67
  'token-format': token_format_1.default,
58
68
  'no-untranslated-string': no_untranslated_string_1.default,
59
69
  'no-schema-enum': no_schema_enum_1.default,
60
- 'require-soft-assertions-before-snapshots': require_soft_assertions_before_snapshots_1.default
70
+ 'require-soft-assertions-before-snapshots': require_soft_assertions_before_snapshots_1.default,
71
+ 'no-pageconfig-base-url': no_pageconfig_base_url_1.default,
72
+ 'require-signal-cleanup': require_signal_cleanup_1.default,
73
+ 'require-signal-this-arg': require_signal_this_arg_1.default,
74
+ 'prefer-signal-this-arg': prefer_signal_this_arg_1.default,
75
+ 'galata-prefer-filebrowser-helper': galata_prefer_filebrowser_helper_1.default,
76
+ 'require-disposable-ownership': require_disposable_ownership_1.default,
77
+ 'require-disposable-transfer': require_disposable_transfer_1.default,
78
+ 'incorrect-translator-usage': incorrect_translator_usage_1.default
61
79
  },
62
80
  configs: {
63
81
  recommended: [
@@ -68,8 +86,16 @@ const plugin = {
68
86
  'jupyter/command-described-by': 'warn',
69
87
  'jupyter/plugin-description': 'warn',
70
88
  'jupyter/no-translation-concatenation': 'error',
89
+ 'jupyter/no-dynamic-translation': 'warn',
71
90
  'jupyter/token-format': 'error',
72
- 'jupyter/no-untranslated-string': 'warn'
91
+ 'jupyter/no-untranslated-string': 'warn',
92
+ 'jupyter/no-pageconfig-base-url': 'warn',
93
+ 'jupyter/require-signal-cleanup': 'warn',
94
+ 'jupyter/require-signal-this-arg': 'error',
95
+ 'jupyter/prefer-signal-this-arg': 'warn',
96
+ 'jupyter/require-disposable-ownership': 'warn',
97
+ 'jupyter/require-disposable-transfer': 'warn',
98
+ 'jupyter/incorrect-translator-usage': 'warn'
73
99
  }
74
100
  },
75
101
  {
@@ -80,14 +106,10 @@ const plugin = {
80
106
  }
81
107
  },
82
108
  {
83
- files: [
84
- '**/*.spec.ts',
85
- '**/*.spec.js',
86
- '**/*.test.ts',
87
- '**/*.test.js'
88
- ],
109
+ files: ['**/*.spec.ts', '**/*.spec.js', '**/*.test.ts', '**/*.test.js'],
89
110
  rules: {
90
- 'jupyter/require-soft-assertions-before-snapshots': 'warn'
111
+ 'jupyter/require-soft-assertions-before-snapshots': 'warn',
112
+ 'jupyter/galata-prefer-filebrowser-helper': 'warn'
91
113
  }
92
114
  }
93
115
  ],
@@ -97,9 +119,17 @@ const plugin = {
97
119
  'jupyter/command-described-by': 'warn',
98
120
  'jupyter/plugin-description': 'warn',
99
121
  'jupyter/no-translation-concatenation': 'error',
122
+ 'jupyter/no-dynamic-translation': 'warn',
100
123
  'jupyter/token-format': 'error',
101
124
  'jupyter/no-untranslated-string': 'warn',
102
- 'jupyter/no-schema-enum': 'warn'
125
+ 'jupyter/no-schema-enum': 'warn',
126
+ 'jupyter/no-pageconfig-base-url': 'warn',
127
+ 'jupyter/require-signal-cleanup': 'warn',
128
+ 'jupyter/require-signal-this-arg': 'error',
129
+ 'jupyter/prefer-signal-this-arg': 'warn',
130
+ 'jupyter/require-disposable-ownership': 'warn',
131
+ 'jupyter/require-disposable-transfer': 'warn',
132
+ 'jupyter/incorrect-translator-usage': 'warn'
103
133
  },
104
134
  overrides: [
105
135
  {
@@ -110,7 +140,8 @@ const plugin = {
110
140
  '**/*.test.js'
111
141
  ],
112
142
  rules: {
113
- 'jupyter/require-soft-assertions-before-snapshots': 'warn'
143
+ 'jupyter/require-soft-assertions-before-snapshots': 'warn',
144
+ 'jupyter/galata-prefer-filebrowser-helper': 'warn'
114
145
  }
115
146
  }
116
147
  ]
@@ -0,0 +1,5 @@
1
+ type MessageIds = 'preferNotebookOpenByPath' | 'preferOpenHomeDirectory' | 'preferOpenDirectory' | 'preferFilebrowserHelper';
2
+ declare const galataPreferFilebrowserHelper: import("@typescript-eslint/utils/ts-eslint").RuleModule<MessageIds, [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
3
+ name: string;
4
+ };
5
+ export = galataPreferFilebrowserHelper;
@@ -0,0 +1,105 @@
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
+ const NOTEBOOK_FILE_PATTERN = /\.ipynb\b/;
9
+ const BREADCRUMBS_PATTERN = /jp-BreadCrumbs-home/;
10
+ // Selectors scoped to the file browser region
11
+ const FILEBROWSER_CONTEXT_PATTERN = /File Browser Section|jp-DirListing-item|#filebrowser\b/;
12
+ // The file selector dialog reuses the directory listing markup, but the
13
+ // `page.filebrowser` helpers only drive the sidebar widget, so a selector
14
+ // scoped to a dialog has no helper to recommend.
15
+ const DIALOG_CONTEXT_PATTERN = /jp-Dialog\b/;
16
+ // A dotted file name inside a text match: `text=lorenz.py`,
17
+ // `:has-text("bar.vl.json")`. The dot must sit between word
18
+ // characters and only inside the text portion (which ends at `>>`), so CSS
19
+ // selectors like `span.lm-Menu-itemLabel` or `text=Close >> button.jp-Button`
20
+ // are not mistaken for file names.
21
+ const FILE_TEXT_PATTERN = /(?:text=|has-text\()["']?[^>"')]*\w\.\w/;
22
+ // The name a text match looks for: `text=notebooks`, `:has-text("mydir")`.
23
+ // Requires at least one word character, so the leftover static text of a
24
+ // fully interpolated name (`` `text=${dirName}` ``) does not count as a name.
25
+ const TEXT_MATCH_NAME_PATTERN = /(?:text=|has-text\()["']?([^>"')]*\w[^>"')]*)/;
26
+ const galataPreferFilebrowserHelper = (0, create_rule_1.createRule)({
27
+ name: 'galata-prefer-filebrowser-helper',
28
+ meta: {
29
+ type: 'suggestion',
30
+ docs: {
31
+ description: 'Prefer Galata filebrowser and notebook helpers over raw Playwright selectors for JupyterLab file browser interactions'
32
+ },
33
+ messages: {
34
+ preferNotebookOpenByPath: 'Prefer `page.notebook.openByPath(path)` (or `page.notebook.open(name)`) over raw selectors to open a notebook.',
35
+ preferOpenHomeDirectory: 'Prefer `page.filebrowser.openHomeDirectory()` over clicking the home breadcrumb (`.jp-BreadCrumbs-home`) directly.',
36
+ preferOpenDirectory: 'Prefer `page.filebrowser.openDirectory(path)` over double-clicking a directory. Note that `page.filebrowser.open(filePath)` and `page.notebook.openByPath(filePath)` already open the intermediate directories of the file they are given.',
37
+ preferFilebrowserHelper: 'Prefer the Galata `page.filebrowser` helper (e.g. `page.filebrowser.open(path)` for a file, `page.filebrowser.openDirectory(path)` for a directory) over raw file browser selectors.'
38
+ },
39
+ schema: []
40
+ },
41
+ defaultOptions: [],
42
+ create(context) {
43
+ return {
44
+ CallExpression(node) {
45
+ const match = (0, playwright_selectors_1.matchSelectorInteraction)(node);
46
+ if (!match) {
47
+ return;
48
+ }
49
+ // Right-clicks open the context menu
50
+ if (match.isRightClick) {
51
+ return;
52
+ }
53
+ const selectorText = (0, playwright_selectors_1.combineStaticSelectorText)(match);
54
+ if (selectorText === null) {
55
+ return;
56
+ }
57
+ // The dialog is not the file browser widget the helpers drive.
58
+ if (DIALOG_CONTEXT_PATTERN.test(selectorText)) {
59
+ return;
60
+ }
61
+ // Double-clicking is the open gesture.
62
+ const opensTarget = match.interactionMethod === 'dblclick';
63
+ if (opensTarget && NOTEBOOK_FILE_PATTERN.test(selectorText)) {
64
+ context.report({
65
+ node: match.callNode,
66
+ messageId: 'preferNotebookOpenByPath'
67
+ });
68
+ return;
69
+ }
70
+ if (BREADCRUMBS_PATTERN.test(selectorText)) {
71
+ context.report({
72
+ node: match.callNode,
73
+ messageId: 'preferOpenHomeDirectory'
74
+ });
75
+ return;
76
+ }
77
+ // Double-clicked file browser items and file names are opens; a
78
+ // waitForSelector on the file browser has helper waits instead.
79
+ if (opensTarget &&
80
+ (FILEBROWSER_CONTEXT_PATTERN.test(selectorText) ||
81
+ FILE_TEXT_PATTERN.test(selectorText))) {
82
+ // A name without a dot is a directory, which `open()` cannot handle:
83
+ // it waits for a document tab that a directory never opens.
84
+ const matchedName = TEXT_MATCH_NAME_PATTERN.exec(selectorText)?.[1];
85
+ const opensDirectory = matchedName !== undefined && !matchedName.includes('.');
86
+ context.report({
87
+ node: match.callNode,
88
+ messageId: opensDirectory
89
+ ? 'preferOpenDirectory'
90
+ : 'preferFilebrowserHelper'
91
+ });
92
+ return;
93
+ }
94
+ if (match.interactionMethod === 'waitForSelector' &&
95
+ FILEBROWSER_CONTEXT_PATTERN.test(selectorText)) {
96
+ context.report({
97
+ node: match.callNode,
98
+ messageId: 'preferFilebrowserHelper'
99
+ });
100
+ }
101
+ }
102
+ };
103
+ }
104
+ });
105
+ module.exports = galataPreferFilebrowserHelper;
@@ -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,4 @@
1
+ declare const noDynamicTranslation: import("@typescript-eslint/utils/ts-eslint").RuleModule<"noInterpolation" | "noDynamicMessage", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
2
+ name: string;
3
+ };
4
+ export = noDynamicTranslation;
@@ -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;
@@ -0,0 +1,4 @@
1
+ declare const jupyterNoPageconfigBaseUrl: import("@typescript-eslint/utils/ts-eslint").RuleModule<"noPageconfigBaseUrl", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
2
+ name: string;
3
+ };
4
+ export = jupyterNoPageconfigBaseUrl;