@@ -212,7 +212,7 @@ namespace ts.codefix {
212
212
213
213
function getImportFixForSymbol ( sourceFile : SourceFile , exportInfos : readonly SymbolExportInfo [ ] , moduleSymbol : Symbol , symbolName : string , program : Program , position : number | undefined , preferTypeOnlyImport : boolean , useRequire : boolean , host : LanguageServiceHost , preferences : UserPreferences ) {
214
214
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 ) ;
216
216
}
217
217
218
218
function codeFixActionToCodeAction ( { description, changes, commands } : CodeFixAction ) : CodeAction {
@@ -290,7 +290,7 @@ namespace ts.codefix {
290
290
host ,
291
291
preferences ,
292
292
fromCacheOnly ) ;
293
- const result = getBestFix ( fixes , importingFile , host , preferences ) ;
293
+ const result = getBestFix ( fixes , importingFile , program , host , preferences ) ;
294
294
return result && { ...result , computedWithoutCacheCount } ;
295
295
}
296
296
@@ -506,34 +506,41 @@ namespace ts.codefix {
506
506
const info = errorCode === Diagnostics . _0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead . code
507
507
? getFixesInfoForUMDImport ( context , symbolToken )
508
508
: 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 ) } ;
510
510
}
511
511
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 [ ] {
513
513
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 ) ) ;
515
515
}
516
516
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 {
518
518
if ( ! some ( fixes ) ) return ;
519
519
// These will always be placed first if available, and are better than other kinds
520
520
if ( fixes [ 0 ] . kind === ImportFixKind . UseNamespace || fixes [ 0 ] . kind === ImportFixKind . AddToExisting ) {
521
521
return fixes [ 0 ] ;
522
522
}
523
523
const { allowsImportingSpecifier } = createPackageJsonImportFilter ( sourceFile , preferences , host ) ;
524
524
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
526
526
) ;
527
527
}
528
528
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 {
530
530
if ( a . kind !== ImportFixKind . UseNamespace && b . kind !== ImportFixKind . UseNamespace ) {
531
531
return compareBooleans ( allowsImportingSpecifier ( a . moduleSpecifier ) , allowsImportingSpecifier ( b . moduleSpecifier ) )
532
+ || compareNodeCoreModuleSpecifiers ( a . moduleSpecifier , b . moduleSpecifier , importingFile , program )
532
533
|| compareNumberOfDirectorySeparators ( a . moduleSpecifier , b . moduleSpecifier ) ;
533
534
}
534
535
return Comparison . EqualTo ;
535
536
}
536
537
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
+
537
544
function getFixesInfoForUMDImport ( { sourceFile, program, host, preferences } : CodeFixContextBase , token : Node ) : FixesInfo | undefined {
538
545
const checker = program . getTypeChecker ( ) ;
539
546
const umdSymbol = getUmdSymbol ( token , checker ) ;
0 commit comments