@@ -66,6 +66,7 @@ function findReturnArg(fn) {
6666// 记录导入:按模块聚合命名导入;以及通配(* as)导入
6767const namedImports = new Map ( ) ; // module -> Set(symbol)
6868const starImports = new Map ( ) ; // module -> alias
69+ const usedSymbols = new Set ( ) ; // 记录实际被使用的符号
6970let needsStreamLanguage = false ;
7071
7172function 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
238244for ( 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}
243252for ( const [ mod , alias ] of starImports ) {
244253 importLines . push ( `import * as ${ alias } from '${ mod } ';` ) ;
@@ -269,6 +278,22 @@ out += `export function loadLanguage(name: LanguageName) {\n`;
269278out += ` return langs[name] ? langs[name]() : null;\n` ;
270279out += `}\n` ;
271280
281+ // 后处理:移除未使用的 objectiveC 导入
282+ if ( out . includes ( 'objectiveC' ) && ! out . includes ( 'StreamLanguage.define(objectiveC)' ) ) {
283+ // objectiveC 被导入但未使用,从 clike 导入中移除
284+ out = out . replace (
285+ / i m p o r t { ( [ ^ } ] * o b j e c t i v e C [ ^ } ] * ) } f r o m ' @ c o d e m i r r o r \/ l e g a c y - m o d e s \/ m o d e \/ c l i k e ' ; / ,
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+
272297process . stdout . write ( out ) ;
273298
274299// ── 自动同步 package.json 依赖 ──────────────────────────────────
0 commit comments