@@ -212,7 +212,7 @@ namespace ts.codefix {
212212
213213 function getImportFixForSymbol ( sourceFile : SourceFile , exportInfos : readonly SymbolExportInfo [ ] , moduleSymbol : Symbol , symbolName : string , program : Program , position : number | undefined , preferTypeOnlyImport : boolean , useRequire : boolean , host : LanguageServiceHost , preferences : UserPreferences ) {
214214 Debug . assert ( exportInfos . some ( info => info . moduleSymbol === moduleSymbol ) , "Some exportInfo should match the specified moduleSymbol" ) ;
215- return getBestFix ( getImportFixes ( exportInfos , symbolName , position , preferTypeOnlyImport , useRequire , program , sourceFile , host , preferences ) , sourceFile , host , preferences ) ;
215+ return getBestFix ( getImportFixes ( exportInfos , symbolName , position , preferTypeOnlyImport , useRequire , program , sourceFile , host , preferences ) , sourceFile , program , host , preferences ) ;
216216 }
217217
218218 function codeFixActionToCodeAction ( { description, changes, commands } : CodeFixAction ) : CodeAction {
@@ -290,7 +290,7 @@ namespace ts.codefix {
290290 host ,
291291 preferences ,
292292 fromCacheOnly ) ;
293- const result = getBestFix ( fixes , importingFile , host , preferences ) ;
293+ const result = getBestFix ( fixes , importingFile , program , host , preferences ) ;
294294 return result && { ...result , computedWithoutCacheCount } ;
295295 }
296296
@@ -506,34 +506,41 @@ namespace ts.codefix {
506506 const info = errorCode === Diagnostics . _0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead . code
507507 ? getFixesInfoForUMDImport ( context , symbolToken )
508508 : isIdentifier ( symbolToken ) ? getFixesInfoForNonUMDImport ( context , symbolToken , useAutoImportProvider ) : undefined ;
509- return info && { ...info , fixes : sortFixes ( info . fixes , context . sourceFile , context . host , context . preferences ) } ;
509+ return info && { ...info , fixes : sortFixes ( info . fixes , context . sourceFile , context . program , context . host , context . preferences ) } ;
510510 }
511511
512- function sortFixes ( fixes : readonly ImportFix [ ] , sourceFile : SourceFile , host : LanguageServiceHost , preferences : UserPreferences ) : readonly ImportFix [ ] {
512+ function sortFixes ( fixes : readonly ImportFix [ ] , sourceFile : SourceFile , program : Program , host : LanguageServiceHost , preferences : UserPreferences ) : readonly ImportFix [ ] {
513513 const { allowsImportingSpecifier } = createPackageJsonImportFilter ( sourceFile , preferences , host ) ;
514- return sort ( fixes , ( a , b ) => compareValues ( a . kind , b . kind ) || compareModuleSpecifiers ( a , b , allowsImportingSpecifier ) ) ;
514+ return sort ( fixes , ( a , b ) => compareValues ( a . kind , b . kind ) || compareModuleSpecifiers ( a , b , sourceFile , program , allowsImportingSpecifier ) ) ;
515515 }
516516
517- function getBestFix < T extends ImportFix > ( fixes : readonly T [ ] , sourceFile : SourceFile , host : LanguageServiceHost , preferences : UserPreferences ) : T | undefined {
517+ function getBestFix < T extends ImportFix > ( fixes : readonly T [ ] , sourceFile : SourceFile , program : Program , host : LanguageServiceHost , preferences : UserPreferences ) : T | undefined {
518518 if ( ! some ( fixes ) ) return ;
519519 // These will always be placed first if available, and are better than other kinds
520520 if ( fixes [ 0 ] . kind === ImportFixKind . UseNamespace || fixes [ 0 ] . kind === ImportFixKind . AddToExisting ) {
521521 return fixes [ 0 ] ;
522522 }
523523 const { allowsImportingSpecifier } = createPackageJsonImportFilter ( sourceFile , preferences , host ) ;
524524 return fixes . reduce ( ( best , fix ) =>
525- compareModuleSpecifiers ( fix , best , allowsImportingSpecifier ) === Comparison . LessThan ? fix : best
525+ compareModuleSpecifiers ( fix , best , sourceFile , program , allowsImportingSpecifier ) === Comparison . LessThan ? fix : best
526526 ) ;
527527 }
528528
529- function compareModuleSpecifiers ( a : ImportFix , b : ImportFix , allowsImportingSpecifier : ( specifier : string ) => boolean ) : Comparison {
529+ function compareModuleSpecifiers ( a : ImportFix , b : ImportFix , importingFile : SourceFile , program : Program , allowsImportingSpecifier : ( specifier : string ) => boolean ) : Comparison {
530530 if ( a . kind !== ImportFixKind . UseNamespace && b . kind !== ImportFixKind . UseNamespace ) {
531531 return compareBooleans ( allowsImportingSpecifier ( a . moduleSpecifier ) , allowsImportingSpecifier ( b . moduleSpecifier ) )
532+ || compareNodeCoreModuleSpecifiers ( a . moduleSpecifier , b . moduleSpecifier , importingFile , program )
532533 || compareNumberOfDirectorySeparators ( a . moduleSpecifier , b . moduleSpecifier ) ;
533534 }
534535 return Comparison . EqualTo ;
535536 }
536537
538+ function compareNodeCoreModuleSpecifiers ( a : string , b : string , importingFile : SourceFile , program : Program ) : Comparison {
539+ if ( startsWith ( a , "node:" ) && ! startsWith ( b , "node:" ) ) return shouldUseUriStyleNodeCoreModules ( importingFile , program ) ? Comparison . LessThan : Comparison . GreaterThan ;
540+ if ( startsWith ( b , "node:" ) && ! startsWith ( a , "node:" ) ) return shouldUseUriStyleNodeCoreModules ( importingFile , program ) ? Comparison . GreaterThan : Comparison . LessThan ;
541+ return Comparison . EqualTo ;
542+ }
543+
537544 function getFixesInfoForUMDImport ( { sourceFile, program, host, preferences } : CodeFixContextBase , token : Node ) : FixesInfo | undefined {
538545 const checker = program . getTypeChecker ( ) ;
539546 const umdSymbol = getUmdSymbol ( token , checker ) ;
0 commit comments