@@ -19,16 +19,35 @@ export type ExportNameCollision = {
1919 sdk ?: true ;
2020} ;
2121
22- type SourceModule = {
22+ export type SourceModule = {
2323 content : string ;
2424 includeDefinitions ?: boolean ;
2525 path : string ;
2626} ;
2727
28- type ModuleExports = {
28+ export type ImportedSymbolReference = {
29+ importedName : string ;
30+ localName : string ;
31+ moduleSpecifier : string ;
32+ } ;
33+
34+ export type NamedReExport = {
35+ exportedName : string ;
36+ importedName : string ;
37+ moduleSpecifier : string ;
38+ } ;
39+
40+ export type ExportedValueDefinition = {
41+ importedReferences : ImportedSymbolReference [ ] ;
42+ name : string ;
43+ } ;
44+
45+ export type ModuleExports = {
2946 definitions : Set < string > ;
3047 exportedNames : Set < string > ;
48+ namedReExports : NamedReExport [ ] ;
3149 starExportSpecifiers : string [ ] ;
50+ valueDefinitions : Map < string , ExportedValueDefinition > ;
3251} ;
3352
3453const exportNameCollisionSchema = z
@@ -75,6 +94,72 @@ function collectBindingNames(name: ts.BindingName, names: Set<string>) {
7594 }
7695}
7796
97+ function resolveImportedReference (
98+ expression : ts . Expression ,
99+ importedSymbolsByLocalName : ReadonlyMap < string , ImportedSymbolReference > ,
100+ namespaceImportsByLocalName : ReadonlyMap < string , string > ,
101+ ) {
102+ const target = unwrapExpression ( expression ) ;
103+ if ( ts . isIdentifier ( target ) ) {
104+ return importedSymbolsByLocalName . get ( target . text ) ;
105+ }
106+ if ( ! ts . isPropertyAccessExpression ( target ) && ! ts . isElementAccessExpression ( target ) ) {
107+ return undefined ;
108+ }
109+ const namespaceName = ts . isPropertyAccessExpression ( target )
110+ ? target . name . text
111+ : ts . isElementAccessExpression ( target ) &&
112+ target . argumentExpression &&
113+ ts . isStringLiteral ( target . argumentExpression )
114+ ? target . argumentExpression . text
115+ : null ;
116+ if ( ! namespaceName || ! ts . isIdentifier ( target . expression ) ) {
117+ return undefined ;
118+ }
119+ const moduleSpecifier = namespaceImportsByLocalName . get ( target . expression . text ) ;
120+ return moduleSpecifier
121+ ? {
122+ importedName : namespaceName ,
123+ localName : `${ target . expression . text } .${ namespaceName } ` ,
124+ moduleSpecifier,
125+ }
126+ : undefined ;
127+ }
128+
129+ function collectImportedReferences (
130+ node : ts . Node ,
131+ importedSymbolsByLocalName : ReadonlyMap < string , ImportedSymbolReference > ,
132+ namespaceImportsByLocalName : ReadonlyMap < string , string > ,
133+ ) {
134+ const references = new Map < string , ImportedSymbolReference > ( ) ;
135+ const addReference = ( reference : ImportedSymbolReference | undefined ) => {
136+ if ( reference ) {
137+ references . set (
138+ `${ reference . moduleSpecifier } \0${ reference . importedName } \0${ reference . localName } ` ,
139+ reference ,
140+ ) ;
141+ }
142+ } ;
143+ const visit = ( current : ts . Node ) : void => {
144+ if ( ts . isCallExpression ( current ) ) {
145+ addReference (
146+ resolveImportedReference (
147+ current . expression ,
148+ importedSymbolsByLocalName ,
149+ namespaceImportsByLocalName ,
150+ ) ,
151+ ) ;
152+ }
153+ ts . forEachChild ( current , visit ) ;
154+ } ;
155+ visit ( node ) ;
156+ return [ ...references . values ( ) ] . toSorted ( ( left , right ) =>
157+ `${ left . moduleSpecifier } \0${ left . importedName } \0${ left . localName } ` . localeCompare (
158+ `${ right . moduleSpecifier } \0${ right . importedName } \0${ right . localName } ` ,
159+ ) ,
160+ ) ;
161+ }
162+
78163function parametersAreForwarded (
79164 parameters : ts . NodeArray < ts . ParameterDeclaration > ,
80165 args : ts . NodeArray < ts . Expression > ,
@@ -238,11 +323,15 @@ function isForwardingOnlyConst(
238323export function collectModuleExportNames ( content : string , fileName = "source.ts" ) : ModuleExports {
239324 const sourceFile = ts . createSourceFile ( fileName , content , ts . ScriptTarget . Latest , true ) ;
240325 const importedNamesByLocalName = new Map < string , string > ( ) ;
326+ const importedSymbolsByLocalName = new Map < string , ImportedSymbolReference > ( ) ;
327+ const namespaceImportsByLocalName = new Map < string , string > ( ) ;
241328 const localConstDeclarations = new Map < string , ts . VariableDeclaration [ ] > ( ) ;
242329 const localFunctions = new Map < string , ts . FunctionDeclaration [ ] > ( ) ;
243330 const directlyExportedNames = new Set < string > ( ) ;
244331 const locallyExportedNames = new Set < string > ( ) ;
245332 const exportedNames = new Set < string > ( ) ;
333+ const namedReExports : NamedReExport [ ] = [ ] ;
334+ const pendingLocalReExports : Array < { exportedName : string ; localName : string } > = [ ] ;
246335 const starExportSpecifiers : string [ ] = [ ] ;
247336
248337 for ( const statement of sourceFile . statements ) {
@@ -251,16 +340,43 @@ export function collectModuleExportNames(content: string, fileName = "source.ts"
251340 if ( bindings && ts . isNamedImports ( bindings ) ) {
252341 for ( const specifier of bindings . elements ) {
253342 if ( ! statement . importClause ?. isTypeOnly && ! specifier . isTypeOnly ) {
254- importedNamesByLocalName . set (
255- specifier . name . text ,
256- specifier . propertyName ?. text ?? specifier . name . text ,
257- ) ;
343+ const importedName = specifier . propertyName ?. text ?? specifier . name . text ;
344+ const moduleSpecifier = ts . isStringLiteral ( statement . moduleSpecifier )
345+ ? statement . moduleSpecifier . text
346+ : "" ;
347+ importedNamesByLocalName . set ( specifier . name . text , importedName ) ;
348+ importedSymbolsByLocalName . set ( specifier . name . text , {
349+ importedName,
350+ localName : specifier . name . text ,
351+ moduleSpecifier,
352+ } ) ;
258353 }
259354 }
355+ } else if (
356+ bindings &&
357+ ts . isNamespaceImport ( bindings ) &&
358+ ! statement . importClause ?. isTypeOnly &&
359+ ts . isStringLiteral ( statement . moduleSpecifier )
360+ ) {
361+ namespaceImportsByLocalName . set ( bindings . name . text , statement . moduleSpecifier . text ) ;
260362 }
261363 continue ;
262364 }
263365
366+ if (
367+ ts . isImportEqualsDeclaration ( statement ) &&
368+ ! statement . isTypeOnly &&
369+ ts . isExternalModuleReference ( statement . moduleReference ) &&
370+ statement . moduleReference . expression &&
371+ ts . isStringLiteral ( statement . moduleReference . expression )
372+ ) {
373+ namespaceImportsByLocalName . set (
374+ statement . name . text ,
375+ statement . moduleReference . expression . text ,
376+ ) ;
377+ continue ;
378+ }
379+
264380 if ( ts . isFunctionDeclaration ( statement ) && statement . name ) {
265381 const name = statement . name . text ;
266382 const declarations = localFunctions . get ( name ) ?? [ ] ;
@@ -325,6 +441,15 @@ export function collectModuleExportNames(content: string, fileName = "source.ts"
325441 continue ;
326442 }
327443 const localName = specifier . propertyName ?. text ?? specifier . name . text ;
444+ if ( statement . moduleSpecifier && ts . isStringLiteral ( statement . moduleSpecifier ) ) {
445+ namedReExports . push ( {
446+ exportedName : specifier . name . text ,
447+ importedName : localName ,
448+ moduleSpecifier : statement . moduleSpecifier . text ,
449+ } ) ;
450+ } else if ( ! statement . moduleSpecifier ) {
451+ pendingLocalReExports . push ( { exportedName : specifier . name . text , localName } ) ;
452+ }
328453 // Renamed re-exports are deliberately outside this guard's first slice.
329454 if ( specifier . name . text !== localName ) {
330455 continue ;
@@ -337,11 +462,51 @@ export function collectModuleExportNames(content: string, fileName = "source.ts"
337462 }
338463 }
339464
465+ for ( const reExport of pendingLocalReExports ) {
466+ const imported = importedSymbolsByLocalName . get ( reExport . localName ) ;
467+ if ( imported ) {
468+ namedReExports . push ( {
469+ exportedName : reExport . exportedName ,
470+ importedName : imported . importedName ,
471+ moduleSpecifier : imported . moduleSpecifier ,
472+ } ) ;
473+ }
474+ }
475+
340476 const definitions = new Set < string > ( ) ;
477+ const valueDefinitions = new Map < string , ExportedValueDefinition > ( ) ;
341478 for ( const name of new Set ( [ ...directlyExportedNames , ...locallyExportedNames ] ) ) {
342479 const constDeclarations = localConstDeclarations . get ( name ) ;
343480 if ( constDeclarations ) {
344481 const [ constDeclaration ] = constDeclarations ;
482+ if ( constDeclarations . length === 1 && constDeclaration ) {
483+ const importedReferences = collectImportedReferences (
484+ constDeclaration ,
485+ importedSymbolsByLocalName ,
486+ namespaceImportsByLocalName ,
487+ ) ;
488+ const initializer = constDeclaration . initializer
489+ ? unwrapExpression ( constDeclaration . initializer )
490+ : undefined ;
491+ if ( initializer ) {
492+ const aliasSource = resolveImportedReference (
493+ initializer ,
494+ importedSymbolsByLocalName ,
495+ namespaceImportsByLocalName ,
496+ ) ;
497+ if ( aliasSource ) {
498+ importedReferences . push ( aliasSource ) ;
499+ }
500+ }
501+ valueDefinitions . set ( name , {
502+ importedReferences : importedReferences . toSorted ( ( left , right ) =>
503+ `${ left . moduleSpecifier } \0${ left . importedName } \0${ left . localName } ` . localeCompare (
504+ `${ right . moduleSpecifier } \0${ right . importedName } \0${ right . localName } ` ,
505+ ) ,
506+ ) ,
507+ name,
508+ } ) ;
509+ }
345510 if (
346511 constDeclarations . length === 1 &&
347512 constDeclaration &&
@@ -357,6 +522,16 @@ export function collectModuleExportNames(content: string, fileName = "source.ts"
357522 continue ;
358523 }
359524 const implementation = functionDeclarations . find ( ( declaration ) => declaration . body ) ;
525+ if ( implementation ?. body ) {
526+ valueDefinitions . set ( name , {
527+ importedReferences : collectImportedReferences (
528+ implementation . body ,
529+ importedSymbolsByLocalName ,
530+ namespaceImportsByLocalName ,
531+ ) ,
532+ name,
533+ } ) ;
534+ }
360535 // Lazy runtime facades are mandated by AGENTS.md. Exempt only exact same-name
361536 // argument forwarding so those boundaries do not become duplicate behavior.
362537 if (
@@ -368,10 +543,20 @@ export function collectModuleExportNames(content: string, fileName = "source.ts"
368543 definitions . add ( name ) ;
369544 }
370545
371- return { definitions, exportedNames, starExportSpecifiers } ;
546+ return {
547+ definitions,
548+ exportedNames,
549+ namedReExports : namedReExports . toSorted ( ( left , right ) =>
550+ `${ left . exportedName } \0${ left . importedName } \0${ left . moduleSpecifier } ` . localeCompare (
551+ `${ right . exportedName } \0${ right . importedName } \0${ right . moduleSpecifier } ` ,
552+ ) ,
553+ ) ,
554+ starExportSpecifiers,
555+ valueDefinitions,
556+ } ;
372557}
373558
374- function resolveStarExportPath (
559+ export function resolveExportModulePath (
375560 sourcePath : string ,
376561 specifier : string ,
377562 modulesByPath : ReadonlyMap < string , ModuleExports > ,
@@ -411,7 +596,7 @@ function collectTransitiveExportNames(
411596 const nextVisiting = new Set ( visiting ) . add ( modulePath ) ;
412597 const names = new Set ( moduleExports . exportedNames ) ;
413598 for ( const specifier of moduleExports . starExportSpecifiers ) {
414- const targetPath = resolveStarExportPath ( modulePath , specifier , modulesByPath ) ;
599+ const targetPath = resolveExportModulePath ( modulePath , specifier , modulesByPath ) ;
415600 if ( ! targetPath ) {
416601 continue ;
417602 }
0 commit comments