@@ -1826,9 +1826,11 @@ function importDeclsOf(ts, file, text) {
18261826 * declaration's own layout (sorted in when its elements are sorted, last
18271827 * otherwise; one line stays one line within 120 columns), else into a new
18281828 * declaration: before `ProtocolHost`'s import, or, for `./protocol.js`, after
1829- * the last import. Pure.
1829+ * the last import. New declarations go in `order` (`protocol.ts`'s import
1830+ * order), a specifier's values before its types, as the first pass writes
1831+ * them. Pure.
18301832 */
1831- function addModuleImports ( ts , file , text , wanted ) {
1833+ function addModuleImports ( ts , file , text , wanted , order = ( ) => 0 ) {
18321834 const { decls, locals } = importDeclsOf ( ts , file , text ) ;
18331835 const edits = [ ] ;
18341836 const fresh = new Map ( ) ; // `${specifier}\0${value}` -> [local]
@@ -1855,9 +1857,9 @@ function addModuleImports(ts, file, text, wanted) {
18551857 const host = decls . find ( ( d ) => d . specifier === './protocol-host.js' ) ;
18561858 const last = decls . at ( - 1 ) ;
18571859 if ( ! last ) refuse ( `${ file } : has no import declaration` ) ;
1858- for ( const [ key , adds ] of fresh ) {
1859- const [ specifier , valueText ] = key . split ( '\0' ) ;
1860- const value = valueText === 'true' ;
1860+ const ranked = [ ... fresh ] . map ( ( [ key , adds ] ) => { const [ specifier , v ] = key . split ( '\0' ) ; return { specifier , value : v === 'true' , adds } ; } )
1861+ . sort ( ( a , b ) => order ( a . specifier ) - order ( b . specifier ) || Number ( b . value ) - Number ( a . value ) || ( a . specifier < b . specifier ? - 1 : a . specifier > b . specifier ? 1 : 0 ) ) ;
1862+ for ( const { specifier , value, adds } of ranked ) {
18611863 const spell = ( a ) => ( a . imported === a . local ? a . local : `${ a . imported } as ${ a . local } ` ) ;
18621864 const d = decls . find ( ( x ) => x . specifier === specifier && x . nb && ! x . defaultName && x . typeOnly === ! value && x . named . every ( ( e ) => ! e . typeOnly ) ) ;
18631865 if ( d ) {
@@ -2025,7 +2027,8 @@ function generateMembers(T, text, hostText, moduleTexts, names, { anchors = SECT
20252027 for ( const [ n , use ] of r . moduleRefs ) want ( n , { specifier : './protocol.js' , form : 'named' , imported : n , value : use . value } ) ;
20262028 want ( 'ProtocolHost' , { specifier : './protocol-host.js' , form : 'named' , imported : 'ProtocolHost' , value : false } ) ;
20272029 }
2028- moduleOut . set ( file , addModuleImports ( ts , file , applyEdits ( mod , modEdits ) , wanted ) ) ;
2030+ const order = ( s ) => ( ctx . scope . importOrder . has ( s ) ? ctx . scope . importOrder . get ( s ) : Number . MAX_SAFE_INTEGER ) ;
2031+ moduleOut . set ( file , addModuleImports ( ts , file , applyEdits ( mod , modEdits ) , wanted , order ) ) ;
20292032 }
20302033 return { host : pruned . text , hostModule : inserted . host , modules : moduleOut , records, pruned : pruned . removed } ;
20312034}
0 commit comments