@@ -179,10 +179,7 @@ function createCompilerContext(repoRoot: string, entrypoints: readonly string[])
179179 const program = ts . createProgram ( fileNames , parsedConfig . options ) ;
180180 return {
181181 checker : program . getTypeChecker ( ) ,
182- printer : ts . createPrinter ( {
183- newLine : ts . NewLineKind . LineFeed ,
184- removeComments : true ,
185- } ) ,
182+ printer : ts . createPrinter ( { newLine : ts . NewLineKind . LineFeed , removeComments : true } ) ,
186183 program,
187184 } ;
188185}
@@ -232,26 +229,18 @@ function inferExportKind(
232229 }
233230 }
234231
235- if ( symbol . flags & ts . SymbolFlags . Function ) {
236- return "function" ;
237- }
238- if ( symbol . flags & ts . SymbolFlags . Class ) {
239- return "class" ;
240- }
241- if ( symbol . flags & ts . SymbolFlags . Interface ) {
242- return "interface" ;
243- }
244- if ( symbol . flags & ts . SymbolFlags . TypeAlias ) {
245- return "type" ;
246- }
247- if ( symbol . flags & ts . SymbolFlags . ConstEnum || symbol . flags & ts . SymbolFlags . RegularEnum ) {
248- return "enum" ;
249- }
250- if ( symbol . flags & ts . SymbolFlags . Variable ) {
251- return "variable" ;
252- }
253- if ( symbol . flags & ts . SymbolFlags . NamespaceModule || symbol . flags & ts . SymbolFlags . ValueModule ) {
254- return "namespace" ;
232+ for ( const [ flag , kind ] of [
233+ [ ts . SymbolFlags . Function , "function" ] ,
234+ [ ts . SymbolFlags . Class , "class" ] ,
235+ [ ts . SymbolFlags . Interface , "interface" ] ,
236+ [ ts . SymbolFlags . TypeAlias , "type" ] ,
237+ [ ts . SymbolFlags . ConstEnum | ts . SymbolFlags . RegularEnum , "enum" ] ,
238+ [ ts . SymbolFlags . Variable , "variable" ] ,
239+ [ ts . SymbolFlags . NamespaceModule | ts . SymbolFlags . ValueModule , "namespace" ] ,
240+ ] as const ) {
241+ if ( symbol . flags & flag ) {
242+ return kind ;
243+ }
255244 }
256245 return "unknown" ;
257246}
@@ -460,6 +449,36 @@ function printTypeParameters(printer: ts.Printer, declaration: ts.TypeAliasDecla
460449 return `<${ parameters . join ( ", " ) } >` ;
461450}
462451
452+ /** Render tuple-derived literal unions in declaration order, independent of compiler traversal. */
453+ export function formatPluginSdkApiTypeAlias (
454+ checker : ts . TypeChecker ,
455+ declaration : ts . TypeAliasDeclaration ,
456+ ) : string {
457+ const type = checker . getTypeAtLocation ( declaration ) ;
458+ if (
459+ type . isUnion ( ) &&
460+ ts . isIndexedAccessTypeNode ( declaration . type ) &&
461+ declaration . type . indexType . kind === ts . SyntaxKind . NumberKeyword
462+ ) {
463+ const tuple = checker . getTypeFromTypeNode ( declaration . type . objectType ) ;
464+ const members = checker . isTupleType ( tuple )
465+ ? [ ...new Set ( checker . getTypeArguments ( tuple as ts . TypeReference ) ) ]
466+ : [ ] ;
467+ if (
468+ members . length === type . types . length &&
469+ members . every (
470+ ( member ) =>
471+ ( member . isStringLiteral ( ) || member . isNumberLiteral ( ) ) && type . types . includes ( member ) ,
472+ )
473+ ) {
474+ return members
475+ . map ( ( member ) => checker . typeToString ( member , declaration , DECLARATION_TYPE_FORMAT_FLAGS ) )
476+ . join ( " | " ) ;
477+ }
478+ }
479+ return checker . typeToString ( type , declaration , DECLARATION_TYPE_FORMAT_FLAGS ) ;
480+ }
481+
463482function printNode (
464483 repoRoot : string ,
465484 checker : ts . TypeChecker ,
@@ -504,15 +523,10 @@ function printNode(
504523 }
505524
506525 if ( ts . isTypeAliasDeclaration ( declaration ) ) {
507- const type = checker . getTypeAtLocation ( declaration ) ;
508526 const typeParameters = printTypeParameters ( printer , declaration ) ;
509527 return normalizePluginSdkApiDeclarationText (
510528 repoRoot ,
511- `export type ${ exportName } ${ typeParameters } = ${ checker . typeToString (
512- type ,
513- declaration ,
514- DECLARATION_TYPE_FORMAT_FLAGS ,
515- ) } ;`,
529+ `export type ${ exportName } ${ typeParameters } = ${ formatPluginSdkApiTypeAlias ( checker , declaration ) } ;` ,
516530 ) ;
517531 }
518532
@@ -545,20 +559,14 @@ function compareDeclarations(
545559 left : ts . Declaration ,
546560 right : ts . Declaration ,
547561) : number {
548- const byPath = compareText (
549- relativePath ( repoRoot , left . getSourceFile ( ) . fileName ) ,
550- relativePath ( repoRoot , right . getSourceFile ( ) . fileName ) ,
562+ return (
563+ compareText (
564+ relativePath ( repoRoot , left . getSourceFile ( ) . fileName ) ,
565+ relativePath ( repoRoot , right . getSourceFile ( ) . fileName ) ,
566+ ) ||
567+ left . getStart ( ) - right . getStart ( ) ||
568+ left . kind - right . kind
551569 ) ;
552- if ( byPath !== 0 ) {
553- return byPath ;
554- }
555-
556- const byStart = left . getStart ( ) - right . getStart ( ) ;
557- if ( byStart !== 0 ) {
558- return byStart ;
559- }
560-
561- return left . kind - right . kind ;
562570}
563571
564572function buildExportSurface ( params : {
@@ -593,11 +601,9 @@ function sortExports(left: PluginSdkApiExport, right: PluginSdkApiExport): numbe
593601 unknown : 8 ,
594602 } ;
595603
596- const byKind = kindRank [ left . kind ] - kindRank [ right . kind ] ;
597- if ( byKind !== 0 ) {
598- return byKind ;
599- }
600- return compareText ( left . exportName , right . exportName ) ;
604+ return (
605+ kindRank [ left . kind ] - kindRank [ right . kind ] || compareText ( left . exportName , right . exportName )
606+ ) ;
601607}
602608
603609function buildModuleSurface ( params : {
0 commit comments