@@ -98,9 +98,9 @@ function normalizeDeclarationImportSpecifier(repoRoot: string, value: string): s
9898}
9999
100100function normalizeDeclarationText ( repoRoot : string , value : string ) : string {
101- return value . replaceAll ( / i m p o r t \( " ( [ ^ " ] + ) " \) / g, ( match , specifier : string ) => {
101+ return value . replaceAll ( / i m p o r t \( " ( [ ^ " ] + ) " / g, ( match , specifier : string ) => {
102102 const normalized = normalizeDeclarationImportSpecifier ( repoRoot , specifier ) ;
103- return normalized === specifier ? match : `import("${ normalized } ") ` ;
103+ return normalized === specifier ? match : `import("${ normalized } "` ;
104104 } ) ;
105105}
106106
@@ -116,7 +116,13 @@ function createCompilerContext(repoRoot: string) {
116116 throw new Error ( ts . flattenDiagnosticMessageText ( configFile . error . messageText , "\n" ) ) ;
117117 }
118118 const parsedConfig = ts . parseJsonConfigFileContent ( configFile . config , ts . sys , repoRoot ) ;
119- const program = ts . createProgram ( parsedConfig . fileNames , parsedConfig . options ) ;
119+ const fileNames = parsedConfig . fileNames . toSorted ( ( left , right ) =>
120+ compareText (
121+ relativePath ( repoRoot , path . resolve ( left ) ) ,
122+ relativePath ( repoRoot , path . resolve ( right ) ) ,
123+ ) ,
124+ ) ;
125+ const program = ts . createProgram ( fileNames , parsedConfig . options ) ;
120126 return {
121127 checker : program . getTypeChecker ( ) ,
122128 printer : ts . createPrinter ( { newLine : ts . NewLineKind . LineFeed } ) ,
@@ -199,14 +205,19 @@ function inferExportKind(
199205
200206function resolveSymbolAndDeclaration (
201207 checker : ts . TypeChecker ,
208+ repoRoot : string ,
202209 symbol : ts . Symbol ,
203210) : {
204211 declaration : ts . Declaration | undefined ;
205212 resolvedSymbol : ts . Symbol ;
206213} {
207214 const resolvedSymbol =
208215 symbol . flags & ts . SymbolFlags . Alias ? checker . getAliasedSymbol ( symbol ) : symbol ;
209- const declarations = resolvedSymbol . getDeclarations ( ) ?? symbol . getDeclarations ( ) ?? [ ] ;
216+ const declarations = (
217+ resolvedSymbol . getDeclarations ( ) ??
218+ symbol . getDeclarations ( ) ??
219+ [ ]
220+ ) . toSorted ( ( left , right ) => compareDeclarations ( repoRoot , left , right ) ) ;
210221 const declaration = declarations . find ( ( candidate ) => candidate . kind !== ts . SyntaxKind . SourceFile ) ;
211222 return { declaration, resolvedSymbol } ;
212223}
@@ -290,6 +301,37 @@ function printNode(
290301 : normalizedText ;
291302}
292303
304+ function compareText ( left : string , right : string ) : number {
305+ if ( left < right ) {
306+ return - 1 ;
307+ }
308+ if ( left > right ) {
309+ return 1 ;
310+ }
311+ return 0 ;
312+ }
313+
314+ function compareDeclarations (
315+ repoRoot : string ,
316+ left : ts . Declaration ,
317+ right : ts . Declaration ,
318+ ) : number {
319+ const byPath = compareText (
320+ relativePath ( repoRoot , left . getSourceFile ( ) . fileName ) ,
321+ relativePath ( repoRoot , right . getSourceFile ( ) . fileName ) ,
322+ ) ;
323+ if ( byPath !== 0 ) {
324+ return byPath ;
325+ }
326+
327+ const byStart = left . getStart ( ) - right . getStart ( ) ;
328+ if ( byStart !== 0 ) {
329+ return byStart ;
330+ }
331+
332+ return left . kind - right . kind ;
333+ }
334+
293335function buildExportSurface ( params : {
294336 checker : ts . TypeChecker ;
295337 printer : ts . Printer ;
@@ -298,7 +340,7 @@ function buildExportSurface(params: {
298340 symbol : ts . Symbol ;
299341} ) : PluginSdkApiExport {
300342 const { checker, printer, program, repoRoot, symbol } = params ;
301- const { declaration, resolvedSymbol } = resolveSymbolAndDeclaration ( checker , symbol ) ;
343+ const { declaration, resolvedSymbol } = resolveSymbolAndDeclaration ( checker , repoRoot , symbol ) ;
302344 return {
303345 declaration : declaration ? printNode ( repoRoot , checker , printer , declaration ) : null ,
304346 exportName : symbol . getName ( ) ,
@@ -331,7 +373,7 @@ function sortExports(left: PluginSdkApiExport, right: PluginSdkApiExport): numbe
331373 if ( byKind !== 0 ) {
332374 return byKind ;
333375 }
334- return left . exportName . localeCompare ( right . exportName ) ;
376+ return compareText ( left . exportName , right . exportName ) ;
335377}
336378
337379function buildModuleSurface ( params : {
@@ -424,7 +466,7 @@ export async function renderPluginSdkApiBaseline(params?: {
424466 entrypoint,
425467 } ) ,
426468 )
427- . toSorted ( ( left , right ) => left . importSpecifier . localeCompare ( right . importSpecifier ) ) ;
469+ . toSorted ( ( left , right ) => compareText ( left . importSpecifier , right . importSpecifier ) ) ;
428470
429471 const baseline : PluginSdkApiBaseline = {
430472 generatedBy : GENERATED_BY ,
0 commit comments