@dxos/eslint-plugin-rules 0.8.4-main.ae835ea → 0.8.4-main.bbf232bc24
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 +15 -1
- package/moon.yml +1 -1
- package/package.json +5 -1
- package/rules/consistent-update-param.js +143 -0
- package/rules/effect-subpath-imports.js +130 -40
- 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/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,11 +2,17 @@
|
|
|
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';
|
|
8
|
+
import consistentUpdateParam from './rules/consistent-update-param.js';
|
|
6
9
|
import effectSubpathImports from './rules/effect-subpath-imports.js';
|
|
7
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';
|
|
8
14
|
import noEmptyPromiseCatch from './rules/no-empty-promise-catch.js';
|
|
9
|
-
import
|
|
15
|
+
import translationKeyFormat from './rules/translation-key-format.js';
|
|
10
16
|
|
|
11
17
|
const pkg = JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
|
|
12
18
|
|
|
@@ -18,9 +24,14 @@ const plugin = {
|
|
|
18
24
|
},
|
|
19
25
|
rules: {
|
|
20
26
|
comment,
|
|
27
|
+
'consistent-update-param': consistentUpdateParam,
|
|
21
28
|
'effect-subpath-imports': effectSubpathImports,
|
|
22
29
|
header,
|
|
30
|
+
'import-as-namespace': importAsNamespace,
|
|
31
|
+
'no-bare-dot-imports': noBareDotImports,
|
|
32
|
+
'no-effect-run-promise': noEffectRunPromise,
|
|
23
33
|
'no-empty-promise-catch': noEmptyPromiseCatch,
|
|
34
|
+
'translation-key-format': translationKeyFormat,
|
|
24
35
|
},
|
|
25
36
|
configs: {
|
|
26
37
|
recommended: {
|
|
@@ -30,6 +41,9 @@ const plugin = {
|
|
|
30
41
|
rules: {
|
|
31
42
|
'dxos-plugin/effect-subpath-imports': 'error',
|
|
32
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',
|
|
33
47
|
'dxos-plugin/no-empty-promise-catch': 'error',
|
|
34
48
|
// TODO(dmaretskyi): Turned off due to large number of errors and no auto-fix.
|
|
35
49
|
// 'dxos-plugin/comment': 'error',
|
package/moon.yml
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
layer: library
|
|
2
2
|
language: typescript
|
package/package.json
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/eslint-plugin-rules",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.bbf232bc24",
|
|
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,
|
|
@@ -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
|
+
};
|
|
@@ -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.
|
|
@@ -33,7 +47,9 @@ export default {
|
|
|
33
47
|
const exportsCache = new Map(); // packageName -> Set<segment>
|
|
34
48
|
|
|
35
49
|
const loadExportsForPackage = (pkgName) => {
|
|
36
|
-
if (exportsCache.has(pkgName))
|
|
50
|
+
if (exportsCache.has(pkgName)) {
|
|
51
|
+
return exportsCache.get(pkgName);
|
|
52
|
+
}
|
|
37
53
|
try {
|
|
38
54
|
const pkgJson = requireForFile(`${pkgName}/package.json`);
|
|
39
55
|
const ex = pkgJson && pkgJson.exports;
|
|
@@ -41,8 +57,12 @@ export default {
|
|
|
41
57
|
if (ex && typeof ex === 'object') {
|
|
42
58
|
for (const key of Object.keys(ex)) {
|
|
43
59
|
// Keys like './Schema', './SchemaAST', './Function' (skip '.' and './package.json').
|
|
44
|
-
if (key === '.' || key === './package.json')
|
|
45
|
-
|
|
60
|
+
if (key === '.' || key === './package.json') {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (key.startsWith('./')) {
|
|
64
|
+
segments.add(key.slice(2));
|
|
65
|
+
}
|
|
46
66
|
}
|
|
47
67
|
}
|
|
48
68
|
exportsCache.set(pkgName, segments);
|
|
@@ -59,6 +79,17 @@ export default {
|
|
|
59
79
|
return exported.has(segment);
|
|
60
80
|
};
|
|
61
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
|
+
|
|
62
93
|
const isEffectPackage = (source) => {
|
|
63
94
|
return source === 'effect' || source.startsWith('effect/') || source.startsWith('@effect/');
|
|
64
95
|
};
|
|
@@ -66,7 +97,7 @@ export default {
|
|
|
66
97
|
const shouldSkipEffectPackage = (basePackage) => {
|
|
67
98
|
return EXCLUDED_EFFECT_PACKAGES.includes(basePackage);
|
|
68
99
|
};
|
|
69
|
-
|
|
100
|
+
|
|
70
101
|
/**
|
|
71
102
|
* Get the base package name from a source string.
|
|
72
103
|
* @param {string} source - The source string to get the base package name from.
|
|
@@ -87,15 +118,21 @@ export default {
|
|
|
87
118
|
return {
|
|
88
119
|
ImportDeclaration: (node) => {
|
|
89
120
|
const source = String(node.source.value);
|
|
90
|
-
if (!isEffectPackage(source))
|
|
121
|
+
if (!isEffectPackage(source)) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
91
124
|
const basePackage = getBasePackage(source);
|
|
92
|
-
if (shouldSkipEffectPackage(basePackage))
|
|
125
|
+
if (shouldSkipEffectPackage(basePackage)) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
93
128
|
|
|
94
|
-
// If it's a subpath import (e.g., 'effect/Schema'), enforce namespace import
|
|
129
|
+
// If it's a subpath import (e.g., 'effect/Schema'), enforce namespace import except for allowed subpaths.
|
|
95
130
|
if (source.startsWith(basePackage + '/')) {
|
|
131
|
+
const segment = source.slice(basePackage.length + 1);
|
|
132
|
+
const allowsNamed = NAMED_IMPORT_ALLOWED_SUBPATHS.has(segment);
|
|
96
133
|
const isNamespaceOnly =
|
|
97
134
|
node.specifiers.length === 1 && node.specifiers[0].type === 'ImportNamespaceSpecifier';
|
|
98
|
-
if (!isNamespaceOnly) {
|
|
135
|
+
if (!allowsNamed && !isNamespaceOnly) {
|
|
99
136
|
context.report({
|
|
100
137
|
node,
|
|
101
138
|
message: 'Use namespace import for Effect subpaths',
|
|
@@ -123,24 +160,39 @@ export default {
|
|
|
123
160
|
const typeImports = [];
|
|
124
161
|
const regularImports = [];
|
|
125
162
|
for (const specifier of node.specifiers) {
|
|
126
|
-
if (specifier.type !== 'ImportSpecifier')
|
|
163
|
+
if (specifier.type !== 'ImportSpecifier') {
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
127
166
|
const entry = { imported: specifier.imported.name, local: specifier.local.name };
|
|
128
|
-
if (specifier.importKind === 'type')
|
|
129
|
-
|
|
167
|
+
if (specifier.importKind === 'type') {
|
|
168
|
+
typeImports.push(entry);
|
|
169
|
+
} else {
|
|
170
|
+
regularImports.push(entry);
|
|
171
|
+
}
|
|
130
172
|
}
|
|
131
173
|
|
|
132
|
-
// Partition into resolvable vs unresolved specifiers.
|
|
174
|
+
// Partition into resolvable vs unresolved specifiers (resolved entries include segment for fix).
|
|
133
175
|
const resolvedType = [];
|
|
134
176
|
const unresolvedType = [];
|
|
135
177
|
const resolvedRegular = [];
|
|
136
178
|
const unresolvedRegular = [];
|
|
137
179
|
|
|
138
|
-
typeImports.forEach((
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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
|
+
});
|
|
144
196
|
|
|
145
197
|
const unresolved = [...unresolvedType, ...unresolvedRegular].map(({ imported }) => imported);
|
|
146
198
|
|
|
@@ -160,27 +212,59 @@ export default {
|
|
|
160
212
|
return null;
|
|
161
213
|
}
|
|
162
214
|
|
|
163
|
-
//
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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);
|
|
173
225
|
});
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
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);
|
|
182
234
|
});
|
|
183
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
|
+
|
|
184
268
|
// If there are unresolved, keep them in a single base import.
|
|
185
269
|
if (unresolvedType.length || unresolvedRegular.length) {
|
|
186
270
|
// Prefer value over type for the same local alias when both are present.
|
|
@@ -192,7 +276,9 @@ export default {
|
|
|
192
276
|
});
|
|
193
277
|
unresolvedType.forEach((s) => {
|
|
194
278
|
const entry = byLocal.get(s.local) ?? {};
|
|
195
|
-
if (!entry.value)
|
|
279
|
+
if (!entry.value) {
|
|
280
|
+
entry.type = s;
|
|
281
|
+
} // only keep type if no value for same local
|
|
196
282
|
byLocal.set(s.local, entry);
|
|
197
283
|
});
|
|
198
284
|
|
|
@@ -208,14 +294,18 @@ export default {
|
|
|
208
294
|
specParts.push(part);
|
|
209
295
|
}
|
|
210
296
|
}
|
|
211
|
-
if (specParts.length)
|
|
297
|
+
if (specParts.length) {
|
|
298
|
+
imports.push(`import { ${specParts.join(', ')} } from '${packageName}';`);
|
|
299
|
+
}
|
|
212
300
|
}
|
|
213
301
|
|
|
214
302
|
// Get the original import's indentation.
|
|
215
303
|
const importIndent = sourceCode.text.slice(node.range[0] - node.loc.start.column, node.range[0]);
|
|
216
304
|
|
|
217
305
|
// Join imports with newline and proper indentation.
|
|
218
|
-
if (imports.length === 0)
|
|
306
|
+
if (imports.length === 0) {
|
|
307
|
+
return null;
|
|
308
|
+
} // nothing to change
|
|
219
309
|
const newImports = imports.join('\n' + importIndent);
|
|
220
310
|
|
|
221
311
|
return fixer.replaceText(node, newImports);
|