Skip to content

Commit d052ce2

Browse files
committed
fix(langs): remove unused imports in generated index.ts #761
1 parent b39c29e commit d052ce2

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

extensions/langs/gen-langs-map.cjs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ function findReturnArg(fn) {
6666
// 记录导入:按模块聚合命名导入;以及通配(* as)导入
6767
const namedImports = new Map(); // module -> Set(symbol)
6868
const starImports = new Map(); // module -> alias
69+
const usedSymbols = new Set(); // 记录实际被使用的符号
6970
let needsStreamLanguage = false;
7071

7172
function addNamed(module, sym) {
@@ -120,12 +121,14 @@ function parseLoadSpec(loadNode) {
120121
if (inner?.type === 'MemberExpression') {
121122
const symbol = inner.property.name || inner.property.value;
122123
addNamed(modulePath, symbol);
124+
usedSymbols.add(symbol);
123125
return { kind: 'legacy', modulePath, symbol, call: false, args: [] };
124126
}
125127
if (inner?.type === 'CallExpression' && inner.callee.type === 'MemberExpression') {
126128
const symbol = inner.callee.property.name || inner.callee.property.value;
127129
const args = inner.arguments.map(a => codeFrom(a));
128130
addNamed(modulePath, symbol);
131+
usedSymbols.add(symbol);
129132
return { kind: 'legacy', modulePath, symbol, call: true, args };
130133
}
131134
return null;
@@ -136,11 +139,13 @@ function parseLoadSpec(loadNode) {
136139
const symbol = body.callee.property.name || body.callee.property.value;
137140
const args = body.arguments.map(a => codeFrom(a));
138141
addNamed(modulePath, symbol);
142+
usedSymbols.add(symbol);
139143
return { kind: 'modern', modulePath, symbol, args };
140144
}
141145
if (body.type === 'MemberExpression') {
142146
const symbol = body.property.name || body.property.value;
143147
addNamed(modulePath, symbol);
148+
usedSymbols.add(symbol);
144149
return { kind: 'modern', modulePath, symbol, args: [] };
145150
}
146151
}
@@ -216,6 +221,7 @@ for (const el of languagesArray.elements) {
216221
expr = `() => StreamLanguage.define(${inner})`;
217222
if (spec.modulePath) {
218223
addNamed(spec.modulePath, spec.symbol);
224+
usedSymbols.add(spec.symbol);
219225
}
220226
} else {
221227
continue;
@@ -237,8 +243,11 @@ if (needsStreamLanguage) {
237243

238244
for (const [mod, set] of namedImports) {
239245
if (mod === '@codemirror/lang-sql') continue; // 由星号导入覆盖
240-
const names = Array.from(set).sort();
241-
importLines.push(`import { ${names.join(', ')} } from '${mod}';`);
246+
// 只导入实际被使用的符号
247+
const usedNames = Array.from(set).filter(name => usedSymbols.has(name)).sort();
248+
if (usedNames.length > 0) {
249+
importLines.push(`import { ${usedNames.join(', ')} } from '${mod}';`);
250+
}
242251
}
243252
for (const [mod, alias] of starImports) {
244253
importLines.push(`import * as ${alias} from '${mod}';`);
@@ -269,6 +278,22 @@ out += `export function loadLanguage(name: LanguageName) {\n`;
269278
out += ` return langs[name] ? langs[name]() : null;\n`;
270279
out += `}\n`;
271280

281+
// 后处理:移除未使用的 objectiveC 导入
282+
if (out.includes('objectiveC') && !out.includes('StreamLanguage.define(objectiveC)')) {
283+
// objectiveC 被导入但未使用,从 clike 导入中移除
284+
out = out.replace(
285+
/import { ([^}]*objectiveC[^}]*) } from '@codemirror\/legacy-modes\/mode\/clike';/,
286+
(match, imports) => {
287+
const cleanImports = imports
288+
.split(',')
289+
.map(s => s.trim())
290+
.filter(s => s !== 'objectiveC')
291+
.join(', ');
292+
return `import { ${cleanImports} } from '@codemirror/legacy-modes/mode/clike';`;
293+
}
294+
);
295+
}
296+
272297
process.stdout.write(out);
273298

274299
// ── 自动同步 package.json 依赖 ──────────────────────────────────

extensions/langs/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { asciiArmor } from '@codemirror/legacy-modes/mode/asciiarmor';
2323
import { asn1 } from '@codemirror/legacy-modes/mode/asn1';
2424
import { brainfuck } from '@codemirror/legacy-modes/mode/brainfuck';
2525
import { cobol } from '@codemirror/legacy-modes/mode/cobol';
26-
import { csharp, dart, kotlin, objectiveC, objectiveCpp, scala, squirrel } from '@codemirror/legacy-modes/mode/clike';
26+
import { csharp, dart, kotlin, objectiveCpp, scala, squirrel } from '@codemirror/legacy-modes/mode/clike';
2727
import { clojure } from '@codemirror/legacy-modes/mode/clojure';
2828
import { gss } from '@codemirror/legacy-modes/mode/css';
2929
import { cmake } from '@codemirror/legacy-modes/mode/cmake';

0 commit comments

Comments
 (0)