@dxos/eslint-plugin-rules 0.8.4-main.f9ba587 → 0.8.4-main.fcc0d83b33
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 +52 -4
- package/moon.yml +1 -1
- package/package.json +9 -2
- package/rules/comment.js +3 -2
- package/rules/consistent-update-param.js +143 -0
- package/rules/effect-subpath-imports.js +317 -0
- package/rules/header.js +2 -2
- package/rules/import-as-namespace.js +271 -0
- package/rules/no-bare-dot-imports.js +55 -0
- package/rules/no-effect-run-promise.js +52 -0
- package/rules/no-empty-promise-catch.js +1 -1
- package/rules/translation-key-format.js +443 -0
- package/tsconfig.json +7 -0
- package/.eslintrc.cjs +0 -9
|
@@ -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,10 +2,58 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
|
|
7
|
+
import comment from './rules/comment.js';
|
|
8
|
+
import consistentUpdateParam from './rules/consistent-update-param.js';
|
|
9
|
+
import effectSubpathImports from './rules/effect-subpath-imports.js';
|
|
10
|
+
import header from './rules/header.js';
|
|
11
|
+
import importAsNamespace from './rules/import-as-namespace.js';
|
|
12
|
+
import noBareDotImports from './rules/no-bare-dot-imports.js';
|
|
13
|
+
import noEffectRunPromise from './rules/no-effect-run-promise.js';
|
|
14
|
+
import noEmptyPromiseCatch from './rules/no-empty-promise-catch.js';
|
|
15
|
+
import translationKeyFormat from './rules/translation-key-format.js';
|
|
16
|
+
|
|
17
|
+
const pkg = JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
|
|
18
|
+
|
|
19
|
+
const plugin = {
|
|
20
|
+
meta: {
|
|
21
|
+
name: pkg.name,
|
|
22
|
+
version: pkg.version,
|
|
23
|
+
namespace: 'example',
|
|
24
|
+
},
|
|
6
25
|
rules: {
|
|
7
|
-
comment
|
|
8
|
-
|
|
9
|
-
'
|
|
26
|
+
comment,
|
|
27
|
+
'consistent-update-param': consistentUpdateParam,
|
|
28
|
+
'effect-subpath-imports': effectSubpathImports,
|
|
29
|
+
header,
|
|
30
|
+
'import-as-namespace': importAsNamespace,
|
|
31
|
+
'no-bare-dot-imports': noBareDotImports,
|
|
32
|
+
'no-effect-run-promise': noEffectRunPromise,
|
|
33
|
+
'no-empty-promise-catch': noEmptyPromiseCatch,
|
|
34
|
+
'translation-key-format': translationKeyFormat,
|
|
35
|
+
},
|
|
36
|
+
configs: {
|
|
37
|
+
recommended: {
|
|
38
|
+
plugins: {
|
|
39
|
+
'dxos-plugin': null,
|
|
40
|
+
},
|
|
41
|
+
rules: {
|
|
42
|
+
'dxos-plugin/effect-subpath-imports': 'error',
|
|
43
|
+
'dxos-plugin/header': 'error',
|
|
44
|
+
'dxos-plugin/import-as-namespace': 'error',
|
|
45
|
+
'dxos-plugin/no-bare-dot-imports': 'error',
|
|
46
|
+
'dxos-plugin/no-effect-run-promise': 'error',
|
|
47
|
+
'dxos-plugin/no-empty-promise-catch': 'error',
|
|
48
|
+
// TODO(dmaretskyi): Turned off due to large number of errors and no auto-fix.
|
|
49
|
+
// 'dxos-plugin/comment': 'error',
|
|
50
|
+
},
|
|
51
|
+
},
|
|
10
52
|
},
|
|
11
53
|
};
|
|
54
|
+
|
|
55
|
+
Object.assign(plugin.configs.recommended.plugins, {
|
|
56
|
+
'dxos-plugin': plugin,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
export default plugin;
|
package/moon.yml
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
layer: library
|
|
2
2
|
language: typescript
|
package/package.json
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/eslint-plugin-rules",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.fcc0d83b33",
|
|
4
4
|
"homepage": "https://dxos.org",
|
|
5
5
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/dxos/dxos"
|
|
9
|
+
},
|
|
6
10
|
"license": "MIT",
|
|
7
11
|
"author": "info@dxos.org",
|
|
8
12
|
"sideEffects": true,
|
|
9
|
-
"
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": "./index.js"
|
|
16
|
+
},
|
|
10
17
|
"publishConfig": {
|
|
11
18
|
"access": "public"
|
|
12
19
|
}
|
package/rules/comment.js
CHANGED
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
// Requires
|
|
11
11
|
// ------------------------------------------------------------------------------
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
import header from './header.js';
|
|
14
|
+
const HEADER_PATTERN = header.pattern;
|
|
14
15
|
|
|
15
16
|
// ------------------------------------------------------------------------------
|
|
16
17
|
// Helpers
|
|
@@ -88,7 +89,7 @@ const createRegExpForIgnorePatterns = (normalizedOptions) => {
|
|
|
88
89
|
// Rule Definition
|
|
89
90
|
// ------------------------------------------------------------------------------
|
|
90
91
|
|
|
91
|
-
|
|
92
|
+
export default {
|
|
92
93
|
meta: {
|
|
93
94
|
type: 'layout',
|
|
94
95
|
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* ESLint rule to enforce that the callback parameter name in Obj.update(),
|
|
7
|
+
* Relation.update(), and Entity.update() matches the first argument name.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // ❌ Bad
|
|
11
|
+
* Obj.update(trigger, (t) => { t.enabled = true; });
|
|
12
|
+
* Obj.update(trigger, (mutableTrigger) => { mutableTrigger.enabled = true; });
|
|
13
|
+
*
|
|
14
|
+
* // ✅ Good
|
|
15
|
+
* Obj.update(trigger, (trigger) => { trigger.enabled = true; });
|
|
16
|
+
*/
|
|
17
|
+
export default {
|
|
18
|
+
meta: {
|
|
19
|
+
type: 'suggestion',
|
|
20
|
+
docs: {
|
|
21
|
+
description:
|
|
22
|
+
'Enforce callback parameter name matches the first argument in Obj.update, Relation.update, and Entity.update.',
|
|
23
|
+
category: 'Best Practices',
|
|
24
|
+
recommended: true,
|
|
25
|
+
},
|
|
26
|
+
fixable: 'code',
|
|
27
|
+
schema: [],
|
|
28
|
+
messages: {
|
|
29
|
+
mismatchedName:
|
|
30
|
+
'Callback parameter "{{callbackParam}}" should be "{{firstArg}}" to match the first argument of {{caller}}.update().',
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
create(context) {
|
|
34
|
+
/** Yield all Identifier reference nodes for `name` inside `node`, respecting scope shadowing. */
|
|
35
|
+
function* collectReferences(node, name) {
|
|
36
|
+
if (!node || typeof node !== 'object' || !node.type) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (
|
|
41
|
+
node.type === 'ArrowFunctionExpression' ||
|
|
42
|
+
node.type === 'FunctionExpression' ||
|
|
43
|
+
node.type === 'FunctionDeclaration'
|
|
44
|
+
) {
|
|
45
|
+
if (node.params?.some((p) => p.type === 'Identifier' && p.name === name)) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
yield* collectReferences(node.body, name);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (node.type === 'Identifier' && node.name === name) {
|
|
53
|
+
yield node;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
for (const key of Object.keys(node)) {
|
|
58
|
+
if (key === 'parent') {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (node.type === 'MemberExpression' && key === 'property' && !node.computed) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (node.type === 'Property' && key === 'key' && !node.computed && !node.shorthand) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const child = node[key];
|
|
69
|
+
if (Array.isArray(child)) {
|
|
70
|
+
for (const item of child) {
|
|
71
|
+
if (item && typeof item === 'object' && item.type) {
|
|
72
|
+
yield* collectReferences(item, name);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
} else if (child && typeof child === 'object' && child.type) {
|
|
76
|
+
yield* collectReferences(child, name);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
CallExpression(node) {
|
|
83
|
+
const { callee } = node;
|
|
84
|
+
|
|
85
|
+
if (
|
|
86
|
+
callee.type !== 'MemberExpression' ||
|
|
87
|
+
callee.object.type !== 'Identifier' ||
|
|
88
|
+
!['Obj', 'Relation', 'Entity'].includes(callee.object.name) ||
|
|
89
|
+
callee.property.type !== 'Identifier' ||
|
|
90
|
+
callee.property.name !== 'update'
|
|
91
|
+
) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const args = node.arguments;
|
|
96
|
+
if (args.length < 2) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const firstArg = args[0];
|
|
101
|
+
const callback = args[1];
|
|
102
|
+
|
|
103
|
+
if (firstArg.type !== 'Identifier') {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (callback.type !== 'ArrowFunctionExpression' && callback.type !== 'FunctionExpression') {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (callback.params.length !== 1) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const param = callback.params[0];
|
|
114
|
+
if (param.type !== 'Identifier') {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (param.name === firstArg.name) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
context.report({
|
|
122
|
+
node: param,
|
|
123
|
+
messageId: 'mismatchedName',
|
|
124
|
+
data: {
|
|
125
|
+
callbackParam: param.name,
|
|
126
|
+
firstArg: firstArg.name,
|
|
127
|
+
caller: callee.object.name,
|
|
128
|
+
},
|
|
129
|
+
fix(fixer) {
|
|
130
|
+
const oldName = param.name;
|
|
131
|
+
const newName = firstArg.name;
|
|
132
|
+
|
|
133
|
+
const fixes = [fixer.replaceTextRange([param.range[0], param.range[0] + oldName.length], newName)];
|
|
134
|
+
for (const ref of collectReferences(callback.body, oldName)) {
|
|
135
|
+
fixes.push(fixer.replaceTextRange([ref.range[0], ref.range[0] + oldName.length], newName));
|
|
136
|
+
}
|
|
137
|
+
return fixes;
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
},
|
|
143
|
+
};
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { createRequire } from 'node:module';
|
|
6
|
+
|
|
7
|
+
const EXCLUDED_EFFECT_PACKAGES = ['@effect/vitest'];
|
|
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
|
+
|
|
24
|
+
/**
|
|
25
|
+
* ESLint rule to transform combined imports from 'effect' and '@effect/*'
|
|
26
|
+
* into subpath imports except for the EXCLUDED_EFFECT_PACKAGES.
|
|
27
|
+
* @example
|
|
28
|
+
* // before
|
|
29
|
+
* import { type Schema, SchemaAST } from 'effect';
|
|
30
|
+
*
|
|
31
|
+
* // after
|
|
32
|
+
* import type * as Schema from 'effect/Schema'
|
|
33
|
+
* import * as SchemaAST from 'effect/SchemaAST'
|
|
34
|
+
*/
|
|
35
|
+
export default {
|
|
36
|
+
meta: {
|
|
37
|
+
type: 'suggestion',
|
|
38
|
+
docs: {
|
|
39
|
+
description: 'enforce subpath imports for Effect packages',
|
|
40
|
+
},
|
|
41
|
+
fixable: 'code',
|
|
42
|
+
schema: [],
|
|
43
|
+
},
|
|
44
|
+
create: (context) => {
|
|
45
|
+
// Resolver and caches are scoped to this lint run.
|
|
46
|
+
const requireForFile = createRequire(context.getFilename());
|
|
47
|
+
const exportsCache = new Map(); // packageName -> Set<segment>
|
|
48
|
+
|
|
49
|
+
const loadExportsForPackage = (pkgName) => {
|
|
50
|
+
if (exportsCache.has(pkgName)) {
|
|
51
|
+
return exportsCache.get(pkgName);
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
const pkgJson = requireForFile(`${pkgName}/package.json`);
|
|
55
|
+
const ex = pkgJson && pkgJson.exports;
|
|
56
|
+
const segments = new Set();
|
|
57
|
+
if (ex && typeof ex === 'object') {
|
|
58
|
+
for (const key of Object.keys(ex)) {
|
|
59
|
+
// Keys like './Schema', './SchemaAST', './Function' (skip '.' and './package.json').
|
|
60
|
+
if (key === '.' || key === './package.json') {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (key.startsWith('./')) {
|
|
64
|
+
segments.add(key.slice(2));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exportsCache.set(pkgName, segments);
|
|
69
|
+
return segments;
|
|
70
|
+
} catch {
|
|
71
|
+
const empty = new Set();
|
|
72
|
+
exportsCache.set(pkgName, empty);
|
|
73
|
+
return empty;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const isValidSubpath = (pkgName, segment) => {
|
|
78
|
+
const exported = loadExportsForPackage(pkgName);
|
|
79
|
+
return exported.has(segment);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const resolveExportToSegment = (pkgName, exportName) => {
|
|
83
|
+
if (isValidSubpath(pkgName, exportName)) {
|
|
84
|
+
return exportName;
|
|
85
|
+
}
|
|
86
|
+
if (pkgName === 'effect' && EFFECT_EXPORT_TO_SUBPATH[exportName]) {
|
|
87
|
+
const segment = EFFECT_EXPORT_TO_SUBPATH[exportName];
|
|
88
|
+
return isValidSubpath(pkgName, segment) ? segment : null;
|
|
89
|
+
}
|
|
90
|
+
return null;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const isEffectPackage = (source) => {
|
|
94
|
+
return source === 'effect' || source.startsWith('effect/') || source.startsWith('@effect/');
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const shouldSkipEffectPackage = (basePackage) => {
|
|
98
|
+
return EXCLUDED_EFFECT_PACKAGES.includes(basePackage);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Get the base package name from a source string.
|
|
103
|
+
* @param {string} source - The source string to get the base package name from.
|
|
104
|
+
* @returns {string} The base package name.
|
|
105
|
+
* @example
|
|
106
|
+
* getBasePackage('effect/Schema') // 'effect'
|
|
107
|
+
* getBasePackage('@effect/ai/openai') // '@effect/ai'
|
|
108
|
+
*/
|
|
109
|
+
const getBasePackage = (source) => {
|
|
110
|
+
if (source.startsWith('@')) {
|
|
111
|
+
const parts = source.split('/');
|
|
112
|
+
return parts.length >= 2 ? `${parts[0]}/${parts[1]}` : source;
|
|
113
|
+
} else {
|
|
114
|
+
return source.split('/')[0];
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
ImportDeclaration: (node) => {
|
|
120
|
+
const source = String(node.source.value);
|
|
121
|
+
if (!isEffectPackage(source)) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const basePackage = getBasePackage(source);
|
|
125
|
+
if (shouldSkipEffectPackage(basePackage)) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// If it's a subpath import (e.g., 'effect/Schema'), enforce namespace import except for allowed subpaths.
|
|
130
|
+
if (source.startsWith(basePackage + '/')) {
|
|
131
|
+
const segment = source.slice(basePackage.length + 1);
|
|
132
|
+
const allowsNamed = NAMED_IMPORT_ALLOWED_SUBPATHS.has(segment);
|
|
133
|
+
const isNamespaceOnly =
|
|
134
|
+
node.specifiers.length === 1 && node.specifiers[0].type === 'ImportNamespaceSpecifier';
|
|
135
|
+
if (!allowsNamed && !isNamespaceOnly) {
|
|
136
|
+
context.report({
|
|
137
|
+
node,
|
|
138
|
+
message: 'Use namespace import for Effect subpaths',
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// From here on, we only handle root package imports like 'effect'.
|
|
145
|
+
const packageName = basePackage;
|
|
146
|
+
|
|
147
|
+
// Only process imports with specifiers.
|
|
148
|
+
if (!node.specifiers || node.specifiers.length === 0) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Only process named imports.
|
|
153
|
+
const hasNamedImports = node.specifiers.some((spec) => spec.type === 'ImportSpecifier');
|
|
154
|
+
|
|
155
|
+
if (!hasNamedImports) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Group specifiers by type.
|
|
160
|
+
const typeImports = [];
|
|
161
|
+
const regularImports = [];
|
|
162
|
+
for (const specifier of node.specifiers) {
|
|
163
|
+
if (specifier.type !== 'ImportSpecifier') {
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
const entry = { imported: specifier.imported.name, local: specifier.local.name };
|
|
167
|
+
if (specifier.importKind === 'type') {
|
|
168
|
+
typeImports.push(entry);
|
|
169
|
+
} else {
|
|
170
|
+
regularImports.push(entry);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Partition into resolvable vs unresolved specifiers (resolved entries include segment for fix).
|
|
175
|
+
const resolvedType = [];
|
|
176
|
+
const unresolvedType = [];
|
|
177
|
+
const resolvedRegular = [];
|
|
178
|
+
const unresolvedRegular = [];
|
|
179
|
+
|
|
180
|
+
typeImports.forEach((spec) => {
|
|
181
|
+
const segment = resolveExportToSegment(packageName, spec.imported);
|
|
182
|
+
if (segment) {
|
|
183
|
+
resolvedType.push({ ...spec, segment });
|
|
184
|
+
} else {
|
|
185
|
+
unresolvedType.push(spec);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
regularImports.forEach((spec) => {
|
|
189
|
+
const segment = resolveExportToSegment(packageName, spec.imported);
|
|
190
|
+
if (segment) {
|
|
191
|
+
resolvedRegular.push({ ...spec, segment });
|
|
192
|
+
} else {
|
|
193
|
+
unresolvedRegular.push(spec);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const unresolved = [...unresolvedType, ...unresolvedRegular].map(({ imported }) => imported);
|
|
198
|
+
|
|
199
|
+
// Report and autofix: fix resolvable ones; keep unresolved in a remaining combined import.
|
|
200
|
+
context.report({
|
|
201
|
+
node,
|
|
202
|
+
message:
|
|
203
|
+
unresolved.length > 0
|
|
204
|
+
? `Use subpath imports for Effect packages; unresolved kept in base import: ${unresolved.join(', ')}`
|
|
205
|
+
: 'Use subpath imports for Effect packages',
|
|
206
|
+
fix: (fixer) => {
|
|
207
|
+
const sourceCode = context.getSourceCode();
|
|
208
|
+
const imports = [];
|
|
209
|
+
|
|
210
|
+
// Idempotency guard: if nothing is resolvable, do not rewrite.
|
|
211
|
+
if (resolvedType.length === 0 && resolvedRegular.length === 0) {
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Group resolved imports by segment.
|
|
216
|
+
const bySegment = new Map(); // segment -> { regular: [...], type: [...] }
|
|
217
|
+
resolvedRegular.forEach((entry) => {
|
|
218
|
+
const seg = entry.segment;
|
|
219
|
+
let group = bySegment.get(seg);
|
|
220
|
+
if (!group) {
|
|
221
|
+
group = { regular: [], type: [] };
|
|
222
|
+
bySegment.set(seg, group);
|
|
223
|
+
}
|
|
224
|
+
group.regular.push(entry);
|
|
225
|
+
});
|
|
226
|
+
resolvedType.forEach((entry) => {
|
|
227
|
+
const seg = entry.segment;
|
|
228
|
+
let group = bySegment.get(seg);
|
|
229
|
+
if (!group) {
|
|
230
|
+
group = { regular: [], type: [] };
|
|
231
|
+
bySegment.set(seg, group);
|
|
232
|
+
}
|
|
233
|
+
group.type.push(entry);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
for (const [segment, group] of bySegment) {
|
|
237
|
+
const useNamed = NAMED_IMPORT_ALLOWED_SUBPATHS.has(segment);
|
|
238
|
+
const merged = [...group.regular];
|
|
239
|
+
for (const t of group.type) {
|
|
240
|
+
if (!group.regular.some((r) => r.local === t.local)) {
|
|
241
|
+
merged.push(t);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (useNamed && merged.length > 0) {
|
|
245
|
+
const specParts = merged.map(({ imported, local }) =>
|
|
246
|
+
imported !== local ? `${imported} as ${local}` : imported,
|
|
247
|
+
);
|
|
248
|
+
imports.push(`import { ${specParts.join(', ')} } from '${packageName}/${segment}';`);
|
|
249
|
+
} else {
|
|
250
|
+
const seen = new Set();
|
|
251
|
+
for (const { imported, local } of merged) {
|
|
252
|
+
const alias = imported !== local ? local : imported;
|
|
253
|
+
if (seen.has(alias)) {
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
seen.add(alias);
|
|
257
|
+
const isTypeOnly =
|
|
258
|
+
group.type.some((t) => t.imported === imported) &&
|
|
259
|
+
!group.regular.some((r) => r.imported === imported);
|
|
260
|
+
const importStr = isTypeOnly
|
|
261
|
+
? `import type * as ${alias} from '${packageName}/${segment}';`
|
|
262
|
+
: `import * as ${alias} from '${packageName}/${segment}';`;
|
|
263
|
+
imports.push(importStr);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// If there are unresolved, keep them in a single base import.
|
|
269
|
+
if (unresolvedType.length || unresolvedRegular.length) {
|
|
270
|
+
// Prefer value over type for the same local alias when both are present.
|
|
271
|
+
const byLocal = new Map(); // local -> { value?: {imported,local}, type?: {imported,local} }
|
|
272
|
+
unresolvedRegular.forEach((s) => {
|
|
273
|
+
const entry = byLocal.get(s.local) ?? {};
|
|
274
|
+
entry.value = s;
|
|
275
|
+
byLocal.set(s.local, entry);
|
|
276
|
+
});
|
|
277
|
+
unresolvedType.forEach((s) => {
|
|
278
|
+
const entry = byLocal.get(s.local) ?? {};
|
|
279
|
+
if (!entry.value) {
|
|
280
|
+
entry.type = s;
|
|
281
|
+
} // only keep type if no value for same local
|
|
282
|
+
byLocal.set(s.local, entry);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
const specParts = [];
|
|
286
|
+
for (const entry of byLocal.values()) {
|
|
287
|
+
if (entry.value) {
|
|
288
|
+
const { imported, local } = entry.value;
|
|
289
|
+
const part = imported !== local ? `${imported} as ${local}` : `${imported}`;
|
|
290
|
+
specParts.push(part);
|
|
291
|
+
} else if (entry.type) {
|
|
292
|
+
const { imported, local } = entry.type;
|
|
293
|
+
const part = imported !== local ? `type ${imported} as ${local}` : `type ${imported}`;
|
|
294
|
+
specParts.push(part);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (specParts.length) {
|
|
298
|
+
imports.push(`import { ${specParts.join(', ')} } from '${packageName}';`);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Get the original import's indentation.
|
|
303
|
+
const importIndent = sourceCode.text.slice(node.range[0] - node.loc.start.column, node.range[0]);
|
|
304
|
+
|
|
305
|
+
// Join imports with newline and proper indentation.
|
|
306
|
+
if (imports.length === 0) {
|
|
307
|
+
return null;
|
|
308
|
+
} // nothing to change
|
|
309
|
+
const newImports = imports.join('\n' + importIndent);
|
|
310
|
+
|
|
311
|
+
return fixer.replaceText(node, newImports);
|
|
312
|
+
},
|
|
313
|
+
});
|
|
314
|
+
},
|
|
315
|
+
};
|
|
316
|
+
},
|
|
317
|
+
};
|
package/rules/header.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
const REGEX = /Copyright [0-9]+ DXOS.org/;
|
|
6
6
|
const TEMPLATE = ['//', `// Copyright ${new Date().getFullYear()} DXOS.org`, '//', ''].join('\n') + '\n';
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
export default {
|
|
9
9
|
pattern: REGEX,
|
|
10
10
|
meta: {
|
|
11
11
|
type: 'layout',
|
|
@@ -19,7 +19,7 @@ module.exports = {
|
|
|
19
19
|
create: (context) => {
|
|
20
20
|
return {
|
|
21
21
|
Program: (node) => {
|
|
22
|
-
if (!context.
|
|
22
|
+
if (!context.sourceCode.getText().match(REGEX)) {
|
|
23
23
|
context.report({
|
|
24
24
|
node,
|
|
25
25
|
message: 'Missing copyright header',
|