@dxos/eslint-plugin-rules 0.8.4-main.bc674ce → 0.8.4-main.bcb3aa67d6
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/docs/2025-04-03-translation-key-normalization-design.md +94 -0
- package/docs/README.md +1 -3
- package/index.js +10 -1
- package/moon.yml +1 -1
- package/package.json +1 -1
- package/rules/effect-subpath-imports.js +86 -29
- package/rules/import-as-namespace.js +271 -0
- package/rules/no-bare-dot-imports.js +55 -0
- package/rules/translation-key-format.js +443 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# Translation Key Normalization
|
|
2
|
+
|
|
3
|
+
## Status
|
|
4
|
+
|
|
5
|
+
ESLint rule and migration script ready. Tested on `plugin-chess`.
|
|
6
|
+
|
|
7
|
+
## Problem
|
|
8
|
+
|
|
9
|
+
1,326 translation keys across 56 plugins use inconsistent space-separated naming with no enforced suffix convention. 135 keys lack a type suffix entirely. This makes it hard to grep, autocomplete, or validate keys.
|
|
10
|
+
|
|
11
|
+
## Convention
|
|
12
|
+
|
|
13
|
+
**Format:** `segment.kebab-case.suffix`
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
settings.debug.label
|
|
17
|
+
add-comment.label
|
|
18
|
+
object-name.placeholder
|
|
19
|
+
add-section.before-dialog.title
|
|
20
|
+
typename.label_other
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Rules
|
|
24
|
+
|
|
25
|
+
- Every key ends with a **type suffix**: `label | message | placeholder | title | description | heading | alt | button`.
|
|
26
|
+
- Plural suffixes append after the type suffix: `typename.label_zero`, `lobby.participants_other`.
|
|
27
|
+
- Dots separate hierarchical segments; segments use kebab-case.
|
|
28
|
+
- `plugin name` keys become `plugin.label`.
|
|
29
|
+
|
|
30
|
+
## Tools
|
|
31
|
+
|
|
32
|
+
### 1. ESLint Rule (ongoing enforcement)
|
|
33
|
+
|
|
34
|
+
`packages/common/eslint-plugin-rules/rules/translation-key-format.js`
|
|
35
|
+
|
|
36
|
+
Registered in `.oxlintrc.json` as `@dxos/eslint-plugin-rules/translation-key-format` (currently `warn`).
|
|
37
|
+
|
|
38
|
+
**Checks:**
|
|
39
|
+
|
|
40
|
+
- `useDotsNotSpaces` — flags space-separated keys, suggests dot.kebab-case, auto-fixable.
|
|
41
|
+
- `missingSuffix` — flags keys missing a valid type suffix (not auto-fixable, needs human decision).
|
|
42
|
+
|
|
43
|
+
**Catches violations in:**
|
|
44
|
+
|
|
45
|
+
- `t('key')` calls in source files.
|
|
46
|
+
- Property keys in `translations.ts` files (string literal keys with string literal values).
|
|
47
|
+
|
|
48
|
+
**Auto-fix:** `moon run plugin-name:lint -- --fix` rewrites both definitions and usages.
|
|
49
|
+
|
|
50
|
+
**Promote to error** once migration is complete to prevent regressions.
|
|
51
|
+
|
|
52
|
+
### 2. Checker Script (bulk analysis)
|
|
53
|
+
|
|
54
|
+
`scripts/check-translations.mts` — run via `npx tsx scripts/check-translations.mts`.
|
|
55
|
+
|
|
56
|
+
Reports:
|
|
57
|
+
|
|
58
|
+
| Check | Current Count |
|
|
59
|
+
| -------------------------------------- | ------------- |
|
|
60
|
+
| Missing keys (used but undefined) | 68 |
|
|
61
|
+
| Unused keys (defined but unreferenced) | 444 |
|
|
62
|
+
| Incomplete plurals | 8 |
|
|
63
|
+
| Missing suffix | 135 |
|
|
64
|
+
| Non-hierarchical (space-separated) | 1,326 |
|
|
65
|
+
|
|
66
|
+
Use this for bulk analysis, migration planning, and tracking progress.
|
|
67
|
+
|
|
68
|
+
## Migration Plan
|
|
69
|
+
|
|
70
|
+
### Step 1: Fix known edge cases in normalizer
|
|
71
|
+
|
|
72
|
+
- `plugin name` → `plugin.label` (special case).
|
|
73
|
+
- Keys ending in nouns (`key`, `name`, `count`, `period`) that aren't valid suffixes need a suffix appended.
|
|
74
|
+
|
|
75
|
+
### Step 2: Migrate incrementally with `--fix`
|
|
76
|
+
|
|
77
|
+
Per plugin: `moon run plugin-name:lint -- --fix`, then verify with `moon run plugin-name:lint`.
|
|
78
|
+
|
|
79
|
+
Start with small plugins (`plugin-chess`, `plugin-template`), then batch the rest.
|
|
80
|
+
|
|
81
|
+
### Step 3: Validate
|
|
82
|
+
|
|
83
|
+
- Re-run checker script to confirm counts decrease.
|
|
84
|
+
- Build + test the migrated plugins.
|
|
85
|
+
|
|
86
|
+
### Step 4: Promote to error
|
|
87
|
+
|
|
88
|
+
Change `.oxlintrc.json` from `"warn"` to `"error"` once all plugins are clean.
|
|
89
|
+
|
|
90
|
+
## Out of Scope
|
|
91
|
+
|
|
92
|
+
- Moving to JSON files.
|
|
93
|
+
- Cleaning up unused keys (separate effort using checker output).
|
|
94
|
+
- UI packages / `osTranslations` namespace (phase 2).
|
package/docs/README.md
CHANGED
package/index.js
CHANGED
|
@@ -2,12 +2,16 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
|
|
5
7
|
import comment from './rules/comment.js';
|
|
6
8
|
import effectSubpathImports from './rules/effect-subpath-imports.js';
|
|
7
9
|
import header from './rules/header.js';
|
|
10
|
+
import importAsNamespace from './rules/import-as-namespace.js';
|
|
11
|
+
import noBareDotImports from './rules/no-bare-dot-imports.js';
|
|
8
12
|
import noEffectRunPromise from './rules/no-effect-run-promise.js';
|
|
9
13
|
import noEmptyPromiseCatch from './rules/no-empty-promise-catch.js';
|
|
10
|
-
import
|
|
14
|
+
import translationKeyFormat from './rules/translation-key-format.js';
|
|
11
15
|
|
|
12
16
|
const pkg = JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
|
|
13
17
|
|
|
@@ -21,8 +25,11 @@ const plugin = {
|
|
|
21
25
|
comment,
|
|
22
26
|
'effect-subpath-imports': effectSubpathImports,
|
|
23
27
|
header,
|
|
28
|
+
'import-as-namespace': importAsNamespace,
|
|
29
|
+
'no-bare-dot-imports': noBareDotImports,
|
|
24
30
|
'no-effect-run-promise': noEffectRunPromise,
|
|
25
31
|
'no-empty-promise-catch': noEmptyPromiseCatch,
|
|
32
|
+
'translation-key-format': translationKeyFormat,
|
|
26
33
|
},
|
|
27
34
|
configs: {
|
|
28
35
|
recommended: {
|
|
@@ -32,6 +39,8 @@ const plugin = {
|
|
|
32
39
|
rules: {
|
|
33
40
|
'dxos-plugin/effect-subpath-imports': 'error',
|
|
34
41
|
'dxos-plugin/header': 'error',
|
|
42
|
+
'dxos-plugin/import-as-namespace': 'error',
|
|
43
|
+
'dxos-plugin/no-bare-dot-imports': 'error',
|
|
35
44
|
'dxos-plugin/no-effect-run-promise': 'error',
|
|
36
45
|
'dxos-plugin/no-empty-promise-catch': 'error',
|
|
37
46
|
// TODO(dmaretskyi): Turned off due to large number of errors and no auto-fix.
|
package/moon.yml
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
layer: library
|
|
2
2
|
language: typescript
|
package/package.json
CHANGED
|
@@ -4,9 +4,23 @@
|
|
|
4
4
|
|
|
5
5
|
import { createRequire } from 'node:module';
|
|
6
6
|
|
|
7
|
-
|
|
8
7
|
const EXCLUDED_EFFECT_PACKAGES = ['@effect/vitest'];
|
|
9
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Map of Effect base-package exports that come from a subpath (not a direct segment).
|
|
11
|
+
* Used when resolving imports like `import { pipe } from 'effect'` → effect/Function.
|
|
12
|
+
*/
|
|
13
|
+
const EFFECT_EXPORT_TO_SUBPATH = {
|
|
14
|
+
pipe: 'Function',
|
|
15
|
+
flow: 'Function',
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Subpaths that allow named imports (e.g. `import { pipe, flow } from 'effect/Function'`).
|
|
20
|
+
* Other subpaths still require namespace imports.
|
|
21
|
+
*/
|
|
22
|
+
const NAMED_IMPORT_ALLOWED_SUBPATHS = new Set(['Function']);
|
|
23
|
+
|
|
10
24
|
/**
|
|
11
25
|
* ESLint rule to transform combined imports from 'effect' and '@effect/*'
|
|
12
26
|
* into subpath imports except for the EXCLUDED_EFFECT_PACKAGES.
|
|
@@ -59,6 +73,15 @@ export default {
|
|
|
59
73
|
return exported.has(segment);
|
|
60
74
|
};
|
|
61
75
|
|
|
76
|
+
const resolveExportToSegment = (pkgName, exportName) => {
|
|
77
|
+
if (isValidSubpath(pkgName, exportName)) return exportName;
|
|
78
|
+
if (pkgName === 'effect' && EFFECT_EXPORT_TO_SUBPATH[exportName]) {
|
|
79
|
+
const segment = EFFECT_EXPORT_TO_SUBPATH[exportName];
|
|
80
|
+
return isValidSubpath(pkgName, segment) ? segment : null;
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
};
|
|
84
|
+
|
|
62
85
|
const isEffectPackage = (source) => {
|
|
63
86
|
return source === 'effect' || source.startsWith('effect/') || source.startsWith('@effect/');
|
|
64
87
|
};
|
|
@@ -66,7 +89,7 @@ export default {
|
|
|
66
89
|
const shouldSkipEffectPackage = (basePackage) => {
|
|
67
90
|
return EXCLUDED_EFFECT_PACKAGES.includes(basePackage);
|
|
68
91
|
};
|
|
69
|
-
|
|
92
|
+
|
|
70
93
|
/**
|
|
71
94
|
* Get the base package name from a source string.
|
|
72
95
|
* @param {string} source - The source string to get the base package name from.
|
|
@@ -91,11 +114,13 @@ export default {
|
|
|
91
114
|
const basePackage = getBasePackage(source);
|
|
92
115
|
if (shouldSkipEffectPackage(basePackage)) return;
|
|
93
116
|
|
|
94
|
-
// If it's a subpath import (e.g., 'effect/Schema'), enforce namespace import
|
|
117
|
+
// If it's a subpath import (e.g., 'effect/Schema'), enforce namespace import except for allowed subpaths.
|
|
95
118
|
if (source.startsWith(basePackage + '/')) {
|
|
119
|
+
const segment = source.slice(basePackage.length + 1);
|
|
120
|
+
const allowsNamed = NAMED_IMPORT_ALLOWED_SUBPATHS.has(segment);
|
|
96
121
|
const isNamespaceOnly =
|
|
97
122
|
node.specifiers.length === 1 && node.specifiers[0].type === 'ImportNamespaceSpecifier';
|
|
98
|
-
if (!isNamespaceOnly) {
|
|
123
|
+
if (!allowsNamed && !isNamespaceOnly) {
|
|
99
124
|
context.report({
|
|
100
125
|
node,
|
|
101
126
|
message: 'Use namespace import for Effect subpaths',
|
|
@@ -129,18 +154,22 @@ export default {
|
|
|
129
154
|
else regularImports.push(entry);
|
|
130
155
|
}
|
|
131
156
|
|
|
132
|
-
// Partition into resolvable vs unresolved specifiers.
|
|
157
|
+
// Partition into resolvable vs unresolved specifiers (resolved entries include segment for fix).
|
|
133
158
|
const resolvedType = [];
|
|
134
159
|
const unresolvedType = [];
|
|
135
160
|
const resolvedRegular = [];
|
|
136
161
|
const unresolvedRegular = [];
|
|
137
162
|
|
|
138
|
-
typeImports.forEach((
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
)
|
|
163
|
+
typeImports.forEach((spec) => {
|
|
164
|
+
const segment = resolveExportToSegment(packageName, spec.imported);
|
|
165
|
+
if (segment) resolvedType.push({ ...spec, segment });
|
|
166
|
+
else unresolvedType.push(spec);
|
|
167
|
+
});
|
|
168
|
+
regularImports.forEach((spec) => {
|
|
169
|
+
const segment = resolveExportToSegment(packageName, spec.imported);
|
|
170
|
+
if (segment) resolvedRegular.push({ ...spec, segment });
|
|
171
|
+
else unresolvedRegular.push(spec);
|
|
172
|
+
});
|
|
144
173
|
|
|
145
174
|
const unresolved = [...unresolvedType, ...unresolvedRegular].map(({ imported }) => imported);
|
|
146
175
|
|
|
@@ -160,27 +189,55 @@ export default {
|
|
|
160
189
|
return null;
|
|
161
190
|
}
|
|
162
191
|
|
|
163
|
-
//
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
192
|
+
// Group resolved imports by segment.
|
|
193
|
+
const bySegment = new Map(); // segment -> { regular: [...], type: [...] }
|
|
194
|
+
resolvedRegular.forEach((entry) => {
|
|
195
|
+
const seg = entry.segment;
|
|
196
|
+
let group = bySegment.get(seg);
|
|
197
|
+
if (!group) {
|
|
198
|
+
group = { regular: [], type: [] };
|
|
199
|
+
bySegment.set(seg, group);
|
|
200
|
+
}
|
|
201
|
+
group.regular.push(entry);
|
|
173
202
|
});
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
203
|
+
resolvedType.forEach((entry) => {
|
|
204
|
+
const seg = entry.segment;
|
|
205
|
+
let group = bySegment.get(seg);
|
|
206
|
+
if (!group) {
|
|
207
|
+
group = { regular: [], type: [] };
|
|
208
|
+
bySegment.set(seg, group);
|
|
209
|
+
}
|
|
210
|
+
group.type.push(entry);
|
|
182
211
|
});
|
|
183
212
|
|
|
213
|
+
for (const [segment, group] of bySegment) {
|
|
214
|
+
const useNamed = NAMED_IMPORT_ALLOWED_SUBPATHS.has(segment);
|
|
215
|
+
const merged = [...group.regular];
|
|
216
|
+
for (const t of group.type) {
|
|
217
|
+
if (!group.regular.some((r) => r.local === t.local)) merged.push(t);
|
|
218
|
+
}
|
|
219
|
+
if (useNamed && merged.length > 0) {
|
|
220
|
+
const specParts = merged.map(({ imported, local }) =>
|
|
221
|
+
imported !== local ? `${imported} as ${local}` : imported,
|
|
222
|
+
);
|
|
223
|
+
imports.push(`import { ${specParts.join(', ')} } from '${packageName}/${segment}';`);
|
|
224
|
+
} else {
|
|
225
|
+
const seen = new Set();
|
|
226
|
+
for (const { imported, local } of merged) {
|
|
227
|
+
const alias = imported !== local ? local : imported;
|
|
228
|
+
if (seen.has(alias)) continue;
|
|
229
|
+
seen.add(alias);
|
|
230
|
+
const isTypeOnly =
|
|
231
|
+
group.type.some((t) => t.imported === imported) &&
|
|
232
|
+
!group.regular.some((r) => r.imported === imported);
|
|
233
|
+
const importStr = isTypeOnly
|
|
234
|
+
? `import type * as ${alias} from '${packageName}/${segment}';`
|
|
235
|
+
: `import * as ${alias} from '${packageName}/${segment}';`;
|
|
236
|
+
imports.push(importStr);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
184
241
|
// If there are unresolved, keep them in a single base import.
|
|
185
242
|
if (unresolvedType.length || unresolvedRegular.length) {
|
|
186
243
|
// Prefer value over type for the same local alias when both are present.
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
|
|
8
|
+
const DIRECTIVE_TEXT = '@import-as-namespace';
|
|
9
|
+
const DIRECTIVE_LINE_REGEX = /^\s*\/\/\s*@import-as-namespace\s*$/m;
|
|
10
|
+
const PASCAL_CASE_REGEX = /^[A-Z][a-zA-Z0-9]*$/;
|
|
11
|
+
const TS_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx'];
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* ESLint rule to enforce namespace imports for modules annotated with `// @import-as-namespace`.
|
|
15
|
+
*
|
|
16
|
+
* When a module contains the `// @import-as-namespace` directive comment, this rule enforces:
|
|
17
|
+
* - The module filename is PascalCase (e.g. `LanguageModel.ts`).
|
|
18
|
+
* - All imports of the module use namespace form: `import * as LanguageModel from './LanguageModel'`.
|
|
19
|
+
* - The namespace name matches the filename (without extension), or has a `Module` suffix.
|
|
20
|
+
* - Re-exports use namespace form: `export * as LanguageModel from './LanguageModel'`.
|
|
21
|
+
*
|
|
22
|
+
* The `Module` suffix is allowed as an escape hatch when the expected namespace name conflicts
|
|
23
|
+
* with a local declaration in the importing file (e.g., `import * as ObjModule from './Obj'`
|
|
24
|
+
* when the file also exports its own `Obj` interface).
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // In LanguageModel.ts:
|
|
28
|
+
* // @import-as-namespace
|
|
29
|
+
* export const foo = 1;
|
|
30
|
+
*
|
|
31
|
+
* // ❌ Bad (in another file):
|
|
32
|
+
* import { foo } from './LanguageModel';
|
|
33
|
+
* export { foo } from './LanguageModel';
|
|
34
|
+
* export * from './LanguageModel';
|
|
35
|
+
*
|
|
36
|
+
* // ✅ Good:
|
|
37
|
+
* import * as LanguageModel from './LanguageModel';
|
|
38
|
+
* import * as LanguageModelModule from './LanguageModel'; // Also allowed when needed
|
|
39
|
+
* export * as LanguageModel from './LanguageModel';
|
|
40
|
+
*/
|
|
41
|
+
export default {
|
|
42
|
+
meta: {
|
|
43
|
+
type: 'problem',
|
|
44
|
+
docs: {
|
|
45
|
+
description: 'enforce namespace imports for modules marked with @import-as-namespace',
|
|
46
|
+
},
|
|
47
|
+
fixable: 'code',
|
|
48
|
+
schema: [],
|
|
49
|
+
messages: {
|
|
50
|
+
filenameMustBePascalCase:
|
|
51
|
+
'Module marked with @import-as-namespace must have a PascalCase filename. Got: "{{filename}}".',
|
|
52
|
+
mustUseNamespaceImport:
|
|
53
|
+
'Module "{{source}}" is marked @import-as-namespace. Use: `import * as {{namespace}} from \'{{source}}\'`.',
|
|
54
|
+
namespaceMustMatchFilename: 'Namespace import name "{{actual}}" must match filename "{{expected}}".',
|
|
55
|
+
mustUseNamespaceReexport:
|
|
56
|
+
'Module "{{source}}" is marked @import-as-namespace. Use: `export * as {{namespace}} from \'{{source}}\'`.',
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
create: (context) => {
|
|
60
|
+
const directiveCache = new Map();
|
|
61
|
+
|
|
62
|
+
const fileHasDirective = (filePath) => {
|
|
63
|
+
if (directiveCache.has(filePath)) {
|
|
64
|
+
return directiveCache.get(filePath);
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
68
|
+
const result = DIRECTIVE_LINE_REGEX.test(content);
|
|
69
|
+
directiveCache.set(filePath, result);
|
|
70
|
+
return result;
|
|
71
|
+
} catch {
|
|
72
|
+
directiveCache.set(filePath, false);
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const resolveRelativeImport = (source, currentFile) => {
|
|
78
|
+
if (!source.startsWith('.')) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
const dir = path.dirname(currentFile);
|
|
82
|
+
const resolved = path.resolve(dir, source);
|
|
83
|
+
for (const ext of TS_EXTENSIONS) {
|
|
84
|
+
const filePath = resolved + ext;
|
|
85
|
+
if (fs.existsSync(filePath)) {
|
|
86
|
+
return filePath;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
for (const ext of TS_EXTENSIONS) {
|
|
90
|
+
const filePath = path.join(resolved, 'index' + ext);
|
|
91
|
+
if (fs.existsSync(filePath)) {
|
|
92
|
+
return filePath;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const namespaceFromSource = (source) => {
|
|
99
|
+
const parts = source.split('/');
|
|
100
|
+
const last = parts[parts.length - 1];
|
|
101
|
+
return last.replace(/\.\w+$/, '');
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const buildImportFix = (fixer, node, source, expectedNamespace, context) => {
|
|
105
|
+
const fixes = [];
|
|
106
|
+
const importKeyword = node.importKind === 'type' ? 'import type' : 'import';
|
|
107
|
+
fixes.push(fixer.replaceText(node, `${importKeyword} * as ${expectedNamespace} from '${source}';`));
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
const declaredVars = context.sourceCode.getDeclaredVariables(node);
|
|
111
|
+
for (const variable of declaredVars) {
|
|
112
|
+
for (const ref of variable.references) {
|
|
113
|
+
if (ref.identifier.range[0] >= node.range[0] && ref.identifier.range[1] <= node.range[1]) {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
fixes.push(fixer.replaceText(ref.identifier, `${expectedNamespace}.${variable.name}`));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
} catch {
|
|
120
|
+
// Scope analysis unavailable; fix import declaration only.
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return fixes;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
Program: (node) => {
|
|
128
|
+
const comments = context.sourceCode.getAllComments();
|
|
129
|
+
const hasDirective = comments.some(
|
|
130
|
+
(comment) => comment.type === 'Line' && comment.value.trim() === DIRECTIVE_TEXT,
|
|
131
|
+
);
|
|
132
|
+
if (!hasDirective) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const filename = path.basename(context.getFilename());
|
|
136
|
+
const stem = filename.replace(/\.\w+$/, '');
|
|
137
|
+
if (!PASCAL_CASE_REGEX.test(stem)) {
|
|
138
|
+
context.report({
|
|
139
|
+
node,
|
|
140
|
+
messageId: 'filenameMustBePascalCase',
|
|
141
|
+
data: { filename },
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
ImportDeclaration: (node) => {
|
|
147
|
+
const source = String(node.source.value);
|
|
148
|
+
if (!source.startsWith('.')) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
if (!node.specifiers || node.specifiers.length === 0) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const currentFile = context.getFilename();
|
|
156
|
+
const resolved = resolveRelativeImport(source, currentFile);
|
|
157
|
+
if (!resolved) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (!fileHasDirective(resolved)) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const expectedNamespace = namespaceFromSource(source);
|
|
165
|
+
|
|
166
|
+
const isNamespaceImport =
|
|
167
|
+
node.specifiers.length === 1 && node.specifiers[0].type === 'ImportNamespaceSpecifier';
|
|
168
|
+
|
|
169
|
+
if (!isNamespaceImport) {
|
|
170
|
+
context.report({
|
|
171
|
+
node,
|
|
172
|
+
messageId: 'mustUseNamespaceImport',
|
|
173
|
+
data: { source, namespace: expectedNamespace },
|
|
174
|
+
fix: (fixer) => buildImportFix(fixer, node, source, expectedNamespace, context),
|
|
175
|
+
});
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const actual = node.specifiers[0].local.name;
|
|
180
|
+
const allowedNames = [expectedNamespace, expectedNamespace + 'Module'];
|
|
181
|
+
if (!allowedNames.includes(actual)) {
|
|
182
|
+
context.report({
|
|
183
|
+
node,
|
|
184
|
+
messageId: 'namespaceMustMatchFilename',
|
|
185
|
+
data: { actual, expected: expectedNamespace },
|
|
186
|
+
fix: (fixer) => {
|
|
187
|
+
const importKeyword = node.importKind === 'type' ? 'import type' : 'import';
|
|
188
|
+
return fixer.replaceText(node, `${importKeyword} * as ${expectedNamespace} from '${source}';`);
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
|
|
194
|
+
ExportNamedDeclaration: (node) => {
|
|
195
|
+
if (!node.source) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const source = String(node.source.value);
|
|
199
|
+
if (!source.startsWith('.')) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const currentFile = context.getFilename();
|
|
204
|
+
const resolved = resolveRelativeImport(source, currentFile);
|
|
205
|
+
if (!resolved) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
if (!fileHasDirective(resolved)) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const expectedNamespace = namespaceFromSource(source);
|
|
213
|
+
const exportKeyword = node.exportKind === 'type' ? 'export type' : 'export';
|
|
214
|
+
context.report({
|
|
215
|
+
node,
|
|
216
|
+
messageId: 'mustUseNamespaceReexport',
|
|
217
|
+
data: { source, namespace: expectedNamespace },
|
|
218
|
+
fix: (fixer) => {
|
|
219
|
+
return fixer.replaceText(node, `${exportKeyword} * as ${expectedNamespace} from '${source}';`);
|
|
220
|
+
},
|
|
221
|
+
});
|
|
222
|
+
},
|
|
223
|
+
|
|
224
|
+
ExportAllDeclaration: (node) => {
|
|
225
|
+
if (!node.source) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
const source = String(node.source.value);
|
|
229
|
+
if (!source.startsWith('.')) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const currentFile = context.getFilename();
|
|
234
|
+
const resolved = resolveRelativeImport(source, currentFile);
|
|
235
|
+
if (!resolved) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (!fileHasDirective(resolved)) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const expectedNamespace = namespaceFromSource(source);
|
|
243
|
+
|
|
244
|
+
if (node.exported) {
|
|
245
|
+
const actual = node.exported.name;
|
|
246
|
+
const allowedNames = [expectedNamespace, expectedNamespace + 'Module'];
|
|
247
|
+
if (!allowedNames.includes(actual)) {
|
|
248
|
+
context.report({
|
|
249
|
+
node,
|
|
250
|
+
messageId: 'namespaceMustMatchFilename',
|
|
251
|
+
data: { actual, expected: expectedNamespace },
|
|
252
|
+
fix: (fixer) => {
|
|
253
|
+
return fixer.replaceText(node, `export * as ${expectedNamespace} from '${source}';`);
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
context.report({
|
|
261
|
+
node,
|
|
262
|
+
messageId: 'mustUseNamespaceReexport',
|
|
263
|
+
data: { source, namespace: expectedNamespace },
|
|
264
|
+
fix: (fixer) => {
|
|
265
|
+
return fixer.replaceText(node, `export * as ${expectedNamespace} from '${source}';`);
|
|
266
|
+
},
|
|
267
|
+
});
|
|
268
|
+
},
|
|
269
|
+
};
|
|
270
|
+
},
|
|
271
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* ESLint rule to prevent bare "." or ".." imports.
|
|
7
|
+
* These imports are ambiguous as they don't explicitly show which file is being imported.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // ❌ Bad
|
|
11
|
+
* import { foo } from '.';
|
|
12
|
+
* import { bar } from '..';
|
|
13
|
+
*
|
|
14
|
+
* // ✅ Good
|
|
15
|
+
* import { foo } from './index';
|
|
16
|
+
* import { bar } from '../index';
|
|
17
|
+
*/
|
|
18
|
+
export default {
|
|
19
|
+
meta: {
|
|
20
|
+
type: 'problem',
|
|
21
|
+
docs: {
|
|
22
|
+
description: 'disallow bare "." or ".." in import paths',
|
|
23
|
+
category: 'Best Practices',
|
|
24
|
+
recommended: true,
|
|
25
|
+
},
|
|
26
|
+
fixable: 'code',
|
|
27
|
+
schema: [],
|
|
28
|
+
messages: {
|
|
29
|
+
bareDotImport: 'Use explicit path instead of bare "{{source}}". Consider "{{suggestion}}" instead.',
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
create: (context) => {
|
|
33
|
+
return {
|
|
34
|
+
ImportDeclaration: (node) => {
|
|
35
|
+
const source = node.source.value;
|
|
36
|
+
|
|
37
|
+
// Check if the import is exactly "." or ".."
|
|
38
|
+
if (source === '.' || source === '..') {
|
|
39
|
+
context.report({
|
|
40
|
+
node: node.source,
|
|
41
|
+
messageId: 'bareDotImport',
|
|
42
|
+
data: {
|
|
43
|
+
source,
|
|
44
|
+
suggestion: source === '.' ? './index' : '../index',
|
|
45
|
+
},
|
|
46
|
+
fix: (fixer) => {
|
|
47
|
+
const newSource = source === '.' ? './index' : '../index';
|
|
48
|
+
return fixer.replaceText(node.source, `'${newSource}'`);
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
};
|
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
'use strict';
|
|
6
|
+
|
|
7
|
+
import { readFileSync, existsSync } from 'node:fs';
|
|
8
|
+
import { join, dirname } from 'node:path';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Valid type suffixes for translation keys.
|
|
12
|
+
*/
|
|
13
|
+
const VALID_SUFFIXES = [
|
|
14
|
+
'label',
|
|
15
|
+
'message',
|
|
16
|
+
'placeholder',
|
|
17
|
+
'title',
|
|
18
|
+
'description',
|
|
19
|
+
'heading',
|
|
20
|
+
'alt',
|
|
21
|
+
'button',
|
|
22
|
+
'name',
|
|
23
|
+
'value',
|
|
24
|
+
'icon',
|
|
25
|
+
'menu',
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Plural suffixes appended by i18next.
|
|
30
|
+
*/
|
|
31
|
+
const PLURAL_SUFFIXES = ['_zero', '_one', '_other'];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Strip i18next plural suffix from a key.
|
|
35
|
+
*/
|
|
36
|
+
const stripPluralSuffix = (key) => {
|
|
37
|
+
for (const suffix of PLURAL_SUFFIXES) {
|
|
38
|
+
if (key.endsWith(suffix)) {
|
|
39
|
+
return key.slice(0, -suffix.length);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return key;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Get the plural suffix if present.
|
|
47
|
+
*/
|
|
48
|
+
const getPluralSuffix = (key) => {
|
|
49
|
+
for (const suffix of PLURAL_SUFFIXES) {
|
|
50
|
+
if (key.endsWith(suffix)) {
|
|
51
|
+
return suffix;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return '';
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Check if a key has a valid type suffix (after stripping plural suffix).
|
|
59
|
+
* Only dot-separated suffixes are considered valid (e.g., 'foo.label').
|
|
60
|
+
*/
|
|
61
|
+
const hasValidSuffix = (key) => {
|
|
62
|
+
const base = stripPluralSuffix(key);
|
|
63
|
+
return VALID_SUFFIXES.some((suffix) => {
|
|
64
|
+
return base === suffix || base.endsWith(`.${suffix}`);
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Check if a key has a suffix joined by hyphen instead of dot (e.g., 'plugin-name').
|
|
70
|
+
* Returns the corrected key if fixable, null otherwise.
|
|
71
|
+
*/
|
|
72
|
+
const fixHyphenatedSuffix = (key) => {
|
|
73
|
+
const pluralSuffix = getPluralSuffix(key);
|
|
74
|
+
const base = stripPluralSuffix(key);
|
|
75
|
+
for (const suffix of VALID_SUFFIXES) {
|
|
76
|
+
if (base.endsWith(`-${suffix}`) && base.length > suffix.length + 1) {
|
|
77
|
+
const prefix = base.slice(0, -(suffix.length + 1));
|
|
78
|
+
return `${prefix}.${suffix}${pluralSuffix}`;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return null;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Check if a key uses dot.kebab-case format (no spaces).
|
|
86
|
+
*/
|
|
87
|
+
const isDotNotation = (key) => {
|
|
88
|
+
const base = stripPluralSuffix(key);
|
|
89
|
+
return !base.includes(' ');
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Convert a space-separated key to dot.kebab-case format.
|
|
94
|
+
*/
|
|
95
|
+
const toDotNotation = (key) => {
|
|
96
|
+
const pluralSuffix = getPluralSuffix(key);
|
|
97
|
+
const base = stripPluralSuffix(key);
|
|
98
|
+
const words = base.split(' ');
|
|
99
|
+
|
|
100
|
+
if (words.length <= 1) {
|
|
101
|
+
return key;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const lastWord = words[words.length - 1];
|
|
105
|
+
const isLastWordSuffix = VALID_SUFFIXES.includes(lastWord);
|
|
106
|
+
|
|
107
|
+
if (isLastWordSuffix) {
|
|
108
|
+
const pathWords = words.slice(0, -1);
|
|
109
|
+
const kebab = pathWords.join('-');
|
|
110
|
+
return `${kebab}.${lastWord}${pluralSuffix}`;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// No valid suffix — kebab-case everything.
|
|
114
|
+
const kebab = words.join('-');
|
|
115
|
+
return `${kebab}${pluralSuffix}`;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
// --- Key validation: resolve meta.id and load translations ---
|
|
119
|
+
|
|
120
|
+
/** Cache: pluginDir → { metaId, keys } */
|
|
121
|
+
const pluginCache = new Map();
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Walk up from a file path to find the package root (directory containing src/translations.ts).
|
|
125
|
+
*/
|
|
126
|
+
const findPackageDir = (filePath) => {
|
|
127
|
+
let dir = dirname(filePath);
|
|
128
|
+
for (let i = 0; i < 10; i++) {
|
|
129
|
+
if (existsSync(join(dir, 'src/translations.ts'))) {
|
|
130
|
+
return dir;
|
|
131
|
+
}
|
|
132
|
+
const parent = dirname(dir);
|
|
133
|
+
if (parent === dir) {
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
dir = parent;
|
|
137
|
+
}
|
|
138
|
+
return null;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Resolve the namespace identifier for a package.
|
|
143
|
+
* Plugins use meta.id from src/meta.ts.
|
|
144
|
+
* UI packages use translationKey from src/translations.ts.
|
|
145
|
+
*/
|
|
146
|
+
const resolveNamespace = (packageDir) => {
|
|
147
|
+
// Try plugin pattern: src/meta.ts with id field.
|
|
148
|
+
const metaPath = join(packageDir, 'src/meta.ts');
|
|
149
|
+
if (existsSync(metaPath)) {
|
|
150
|
+
const content = readFileSync(metaPath, 'utf-8');
|
|
151
|
+
const match = content.match(/id:\s*['"]([^'"]+)['"]/);
|
|
152
|
+
if (match) {
|
|
153
|
+
return { namespace: match[1], source: 'meta.id' };
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Try UI package pattern: export const translationKey = '...'.
|
|
158
|
+
const translationsPath = join(packageDir, 'src/translations.ts');
|
|
159
|
+
if (existsSync(translationsPath)) {
|
|
160
|
+
const content = readFileSync(translationsPath, 'utf-8');
|
|
161
|
+
const match = content.match(/export\s+const\s+translationKey\s*=\s*['"]([^'"]+)['"]/);
|
|
162
|
+
if (match) {
|
|
163
|
+
return { namespace: match[1], source: 'translationKey' };
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return null;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Extract translation keys for the primary namespace from a translations.ts file.
|
|
172
|
+
* Handles both [meta.id]: { ... } and [translationKey]: { ... } patterns.
|
|
173
|
+
*/
|
|
174
|
+
const readTranslationKeys = (packageDir, nsSource) => {
|
|
175
|
+
const keys = new Set();
|
|
176
|
+
const translationsPath = join(packageDir, 'src/translations.ts');
|
|
177
|
+
if (!existsSync(translationsPath)) {
|
|
178
|
+
return keys;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const content = readFileSync(translationsPath, 'utf-8');
|
|
182
|
+
const lines = content.split('\n');
|
|
183
|
+
let inBlock = false;
|
|
184
|
+
let braceDepth = 0;
|
|
185
|
+
|
|
186
|
+
// Match the namespace block header based on the source pattern.
|
|
187
|
+
const blockPattern = nsSource === 'meta.id' ? /\[meta\.id\]\s*:\s*\{/ : /\[translationKey\]\s*:\s*\{/;
|
|
188
|
+
|
|
189
|
+
for (const line of lines) {
|
|
190
|
+
const trimmed = line.trim();
|
|
191
|
+
|
|
192
|
+
if (!inBlock && trimmed.match(blockPattern)) {
|
|
193
|
+
inBlock = true;
|
|
194
|
+
braceDepth = 1;
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (inBlock) {
|
|
199
|
+
for (const ch of trimmed) {
|
|
200
|
+
if (ch === '{') {
|
|
201
|
+
braceDepth++;
|
|
202
|
+
}
|
|
203
|
+
if (ch === '}') {
|
|
204
|
+
braceDepth--;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const keyMatch = trimmed.match(/^['"]([^'"]+)['"]\s*:/);
|
|
209
|
+
if (keyMatch && braceDepth >= 1) {
|
|
210
|
+
keys.add(keyMatch[1]);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (braceDepth <= 0) {
|
|
214
|
+
inBlock = false;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return keys;
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Get package info (namespace + valid keys) for a file, with caching.
|
|
224
|
+
*/
|
|
225
|
+
const getPackageInfo = (filePath) => {
|
|
226
|
+
const packageDir = findPackageDir(filePath);
|
|
227
|
+
if (!packageDir) {
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (pluginCache.has(packageDir)) {
|
|
232
|
+
return pluginCache.get(packageDir);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const nsInfo = resolveNamespace(packageDir);
|
|
236
|
+
if (!nsInfo) {
|
|
237
|
+
pluginCache.set(packageDir, null);
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const keys = readTranslationKeys(packageDir, nsInfo.source);
|
|
242
|
+
const info = { namespace: nsInfo.namespace, nsSource: nsInfo.source, keys };
|
|
243
|
+
pluginCache.set(packageDir, info);
|
|
244
|
+
return info;
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
export default {
|
|
248
|
+
meta: {
|
|
249
|
+
type: 'suggestion',
|
|
250
|
+
fixable: 'code',
|
|
251
|
+
docs: {
|
|
252
|
+
description:
|
|
253
|
+
'Enforce translation key format: dot.kebab-case with required type suffix. Validates keys exist in translations.',
|
|
254
|
+
},
|
|
255
|
+
messages: {
|
|
256
|
+
missingSuffix: 'Invalid translation key: "{{key}}"',
|
|
257
|
+
useDotsNotSpaces: 'Translation key "{{key}}" should use dot.kebab-case format. Suggested: "{{suggested}}".',
|
|
258
|
+
undefinedKey: 'Translation key "{{key}}" is not defined in translations for namespace "{{namespace}}".',
|
|
259
|
+
},
|
|
260
|
+
schema: [
|
|
261
|
+
{
|
|
262
|
+
type: 'object',
|
|
263
|
+
properties: {
|
|
264
|
+
suffixes: {
|
|
265
|
+
type: 'array',
|
|
266
|
+
items: { type: 'string' },
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
additionalProperties: false,
|
|
270
|
+
},
|
|
271
|
+
],
|
|
272
|
+
},
|
|
273
|
+
|
|
274
|
+
create(context) {
|
|
275
|
+
const options = context.options[0] || {};
|
|
276
|
+
const suffixes = options.suffixes || VALID_SUFFIXES;
|
|
277
|
+
const filename =
|
|
278
|
+
context.filename || (context.getFilename && context.getFilename()) || context.physicalFilename || '';
|
|
279
|
+
|
|
280
|
+
// Check source text once to determine if this is a translation-aware file.
|
|
281
|
+
const sourceText = (context.sourceCode || context.getSourceCode()).text;
|
|
282
|
+
const usesTranslation = sourceText.includes('useTranslation');
|
|
283
|
+
const usesStaticNamespace =
|
|
284
|
+
sourceText.includes('useTranslation(meta.id)') || sourceText.includes('useTranslation(translationKey)');
|
|
285
|
+
|
|
286
|
+
// Resolve package info for this file (works for both plugins and UI packages).
|
|
287
|
+
const packageInfo = getPackageInfo(filename);
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Check a string literal node that represents a translation key.
|
|
291
|
+
* @param {boolean} isDefinition - true for keys in translations.ts definitions.
|
|
292
|
+
*/
|
|
293
|
+
const checkKeyFormat = (node, key, isDefinition = false) => {
|
|
294
|
+
// Check 1: Must use dot notation (no spaces).
|
|
295
|
+
if (!isDotNotation(key)) {
|
|
296
|
+
const suggested = toDotNotation(key);
|
|
297
|
+
context.report({
|
|
298
|
+
node,
|
|
299
|
+
messageId: 'useDotsNotSpaces',
|
|
300
|
+
data: { key, suggested },
|
|
301
|
+
fix: (fixer) => fixer.replaceText(node, `'${suggested}'`),
|
|
302
|
+
});
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Check 2: Suffix joined by hyphen instead of dot (e.g., 'plugin-name' → 'plugin.name').
|
|
307
|
+
const fixedKey = fixHyphenatedSuffix(key);
|
|
308
|
+
if (fixedKey) {
|
|
309
|
+
context.report({
|
|
310
|
+
node,
|
|
311
|
+
messageId: 'useDotsNotSpaces',
|
|
312
|
+
data: { key, suggested: fixedKey },
|
|
313
|
+
fix: (fixer) => fixer.replaceText(node, `'${fixedKey}'`),
|
|
314
|
+
});
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// Check 3: Must end with a valid suffix (definitions only).
|
|
319
|
+
if (isDefinition && !hasValidSuffix(key)) {
|
|
320
|
+
context.report({
|
|
321
|
+
node,
|
|
322
|
+
messageId: 'missingSuffix',
|
|
323
|
+
data: { key, suffixes: suffixes.join(', ') },
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Check that a key exists in the package's translations.
|
|
330
|
+
*/
|
|
331
|
+
const checkKeyExists = (node, key, hasNsOverride) => {
|
|
332
|
+
// Only check if we resolved the package and the file uses a static namespace.
|
|
333
|
+
if (!packageInfo || !usesStaticNamespace || hasNsOverride) {
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Check exact key, then check plural variants (i18next resolves 'foo.label' with { count } to 'foo.label_one'/'foo.label_other').
|
|
338
|
+
const hasPluralVariant = PLURAL_SUFFIXES.some((suffix) => packageInfo.keys.has(key + suffix));
|
|
339
|
+
if (!packageInfo.keys.has(key) && !hasPluralVariant) {
|
|
340
|
+
context.report({
|
|
341
|
+
node,
|
|
342
|
+
messageId: 'undefinedKey',
|
|
343
|
+
data: { key, namespace: packageInfo.namespace },
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
return {
|
|
349
|
+
// t('some key') or t('some key', { ns: ... }).
|
|
350
|
+
// Only fires in files that use useTranslation.
|
|
351
|
+
CallExpression(node) {
|
|
352
|
+
if (
|
|
353
|
+
!usesTranslation ||
|
|
354
|
+
node.callee.type !== 'Identifier' ||
|
|
355
|
+
node.callee.name !== 't' ||
|
|
356
|
+
node.arguments.length < 1 ||
|
|
357
|
+
node.arguments[0].type !== 'Literal' ||
|
|
358
|
+
typeof node.arguments[0].value !== 'string'
|
|
359
|
+
) {
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const key = node.arguments[0].value;
|
|
364
|
+
|
|
365
|
+
// Check format.
|
|
366
|
+
checkKeyFormat(node.arguments[0], key);
|
|
367
|
+
|
|
368
|
+
// Check if there's an ns override in the second argument.
|
|
369
|
+
let hasNsOverride = false;
|
|
370
|
+
if (node.arguments.length >= 2 && node.arguments[1].type === 'ObjectExpression') {
|
|
371
|
+
hasNsOverride = node.arguments[1].properties.some(
|
|
372
|
+
(prop) =>
|
|
373
|
+
prop.key &&
|
|
374
|
+
((prop.key.type === 'Identifier' && prop.key.name === 'ns') ||
|
|
375
|
+
(prop.key.type === 'Literal' && prop.key.value === 'ns')),
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// Check existence.
|
|
380
|
+
checkKeyExists(node.arguments[0], key, hasNsOverride);
|
|
381
|
+
},
|
|
382
|
+
|
|
383
|
+
// Label tuples: ['key', { ns: meta.id, ... }]
|
|
384
|
+
// Matches ArrayExpression with a string literal first element and an object with an `ns` property.
|
|
385
|
+
ArrayExpression(node) {
|
|
386
|
+
if (
|
|
387
|
+
node.elements.length >= 2 &&
|
|
388
|
+
node.elements[0] &&
|
|
389
|
+
node.elements[0].type === 'Literal' &&
|
|
390
|
+
typeof node.elements[0].value === 'string' &&
|
|
391
|
+
node.elements[1] &&
|
|
392
|
+
node.elements[1].type === 'ObjectExpression' &&
|
|
393
|
+
node.elements[1].properties.some(
|
|
394
|
+
(prop) =>
|
|
395
|
+
prop.key &&
|
|
396
|
+
((prop.key.type === 'Identifier' && prop.key.name === 'ns') ||
|
|
397
|
+
(prop.key.type === 'Literal' && prop.key.value === 'ns')),
|
|
398
|
+
)
|
|
399
|
+
) {
|
|
400
|
+
const key = node.elements[0].value;
|
|
401
|
+
|
|
402
|
+
// Check format.
|
|
403
|
+
checkKeyFormat(node.elements[0], key);
|
|
404
|
+
|
|
405
|
+
// Check existence (determine if ns points to the package's own namespace).
|
|
406
|
+
const nsProp = node.elements[1].properties.find(
|
|
407
|
+
(prop) =>
|
|
408
|
+
prop.key &&
|
|
409
|
+
((prop.key.type === 'Identifier' && prop.key.name === 'ns') ||
|
|
410
|
+
(prop.key.type === 'Literal' && prop.key.value === 'ns')),
|
|
411
|
+
);
|
|
412
|
+
const isOwnNamespace =
|
|
413
|
+
nsProp &&
|
|
414
|
+
nsProp.value &&
|
|
415
|
+
((nsProp.value.type === 'MemberExpression' &&
|
|
416
|
+
nsProp.value.object.type === 'Identifier' &&
|
|
417
|
+
nsProp.value.object.name === 'meta' &&
|
|
418
|
+
nsProp.value.property.type === 'Identifier' &&
|
|
419
|
+
nsProp.value.property.name === 'id') ||
|
|
420
|
+
(nsProp.value.type === 'Identifier' && nsProp.value.name === 'translationKey'));
|
|
421
|
+
checkKeyExists(node.elements[0], key, !isOwnNamespace);
|
|
422
|
+
}
|
|
423
|
+
},
|
|
424
|
+
|
|
425
|
+
// Property keys in translations.ts files.
|
|
426
|
+
// Guard: only check files whose source contains the translations export pattern.
|
|
427
|
+
Property(node) {
|
|
428
|
+
if (!sourceText.includes('satisfies Resource[]')) {
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
if (
|
|
433
|
+
node.key.type === 'Literal' &&
|
|
434
|
+
typeof node.key.value === 'string' &&
|
|
435
|
+
node.value.type === 'Literal' &&
|
|
436
|
+
typeof node.value.value === 'string'
|
|
437
|
+
) {
|
|
438
|
+
checkKeyFormat(node.key, node.key.value, true);
|
|
439
|
+
}
|
|
440
|
+
},
|
|
441
|
+
};
|
|
442
|
+
},
|
|
443
|
+
};
|