@@ -375,7 +375,7 @@ function createImportAdderWorker(sourceFile: SourceFile, program: Program, useAu
375375 newDeclarations = combine ( newDeclarations , declarations ) ;
376376 } ) ;
377377 if ( newDeclarations ) {
378- insertImports ( changeTracker , sourceFile , newDeclarations , /*blankLineBetween*/ true ) ;
378+ insertImports ( changeTracker , sourceFile , newDeclarations , /*blankLineBetween*/ true , preferences ) ;
379379 }
380380 }
381381
@@ -1221,14 +1221,14 @@ function codeActionForFixWorker(changes: textChanges.ChangeTracker, sourceFile:
12211221 const defaultImport : Import | undefined = importKind === ImportKind . Default ? { name : symbolName , addAsTypeOnly } : undefined ;
12221222 const namedImports : Import [ ] | undefined = importKind === ImportKind . Named ? [ { name : symbolName , addAsTypeOnly } ] : undefined ;
12231223 const namespaceLikeImport = importKind === ImportKind . Namespace || importKind === ImportKind . CommonJS ? { importKind, name : symbolName , addAsTypeOnly } : undefined ;
1224- insertImports ( changes , sourceFile , getDeclarations ( moduleSpecifier , quotePreference , defaultImport , namedImports , namespaceLikeImport ) , /*blankLineBetween*/ true ) ;
1224+ insertImports ( changes , sourceFile , getDeclarations ( moduleSpecifier , quotePreference , defaultImport , namedImports , namespaceLikeImport ) , /*blankLineBetween*/ true , preferences ) ;
12251225 return includeSymbolNameInDescription
12261226 ? [ Diagnostics . Import_0_from_1 , symbolName , moduleSpecifier ]
12271227 : [ Diagnostics . Add_import_from_0 , moduleSpecifier ] ;
12281228 }
12291229 case ImportFixKind . PromoteTypeOnly : {
12301230 const { typeOnlyAliasDeclaration } = fix ;
1231- const promotedDeclaration = promoteFromTypeOnly ( changes , typeOnlyAliasDeclaration , compilerOptions , sourceFile ) ;
1231+ const promotedDeclaration = promoteFromTypeOnly ( changes , typeOnlyAliasDeclaration , compilerOptions , sourceFile , preferences ) ;
12321232 return promotedDeclaration . kind === SyntaxKind . ImportSpecifier
12331233 ? [ Diagnostics . Remove_type_from_import_of_0_from_1 , symbolName , getModuleSpecifierText ( promotedDeclaration . parent . parent ) ]
12341234 : [ Diagnostics . Remove_type_from_import_declaration_from_0 , getModuleSpecifierText ( promotedDeclaration ) ] ;
@@ -1244,17 +1244,18 @@ function getModuleSpecifierText(promotedDeclaration: ImportClause | ImportEquals
12441244 : cast ( promotedDeclaration . parent . moduleSpecifier , isStringLiteral ) . text ;
12451245}
12461246
1247- function promoteFromTypeOnly ( changes : textChanges . ChangeTracker , aliasDeclaration : TypeOnlyAliasDeclaration , compilerOptions : CompilerOptions , sourceFile : SourceFile ) {
1247+ function promoteFromTypeOnly ( changes : textChanges . ChangeTracker , aliasDeclaration : TypeOnlyAliasDeclaration , compilerOptions : CompilerOptions , sourceFile : SourceFile , preferences : UserPreferences ) {
12481248 // See comment in `doAddExistingFix` on constant with the same name.
12491249 const convertExistingToTypeOnly = compilerOptions . preserveValueImports && compilerOptions . isolatedModules ;
12501250 switch ( aliasDeclaration . kind ) {
12511251 case SyntaxKind . ImportSpecifier :
12521252 if ( aliasDeclaration . isTypeOnly ) {
1253- const sortKind = OrganizeImports . detectImportSpecifierSorting ( aliasDeclaration . parent . elements ) ;
1253+ const sortKind = OrganizeImports . detectImportSpecifierSorting ( aliasDeclaration . parent . elements , preferences ) ;
12541254 if ( aliasDeclaration . parent . elements . length > 1 && sortKind ) {
12551255 changes . delete ( sourceFile , aliasDeclaration ) ;
12561256 const newSpecifier = factory . updateImportSpecifier ( aliasDeclaration , /*isTypeOnly*/ false , aliasDeclaration . propertyName , aliasDeclaration . name ) ;
1257- const insertionIndex = OrganizeImports . getImportSpecifierInsertionIndex ( aliasDeclaration . parent . elements , newSpecifier , sortKind === SortKind . CaseInsensitive ) ;
1257+ const comparer = OrganizeImports . getOrganizeImportsComparer ( preferences , sortKind === SortKind . CaseInsensitive ) ;
1258+ const insertionIndex = OrganizeImports . getImportSpecifierInsertionIndex ( aliasDeclaration . parent . elements , newSpecifier , comparer ) ;
12581259 changes . insertImportSpecifierAtIndex ( sourceFile , newSpecifier , aliasDeclaration . parent , insertionIndex ) ;
12591260 }
12601261 else {
@@ -1285,7 +1286,7 @@ function promoteFromTypeOnly(changes: textChanges.ChangeTracker, aliasDeclaratio
12851286 if ( convertExistingToTypeOnly ) {
12861287 const namedImports = tryCast ( importClause . namedBindings , isNamedImports ) ;
12871288 if ( namedImports && namedImports . elements . length > 1 ) {
1288- if ( OrganizeImports . detectImportSpecifierSorting ( namedImports . elements ) &&
1289+ if ( OrganizeImports . detectImportSpecifierSorting ( namedImports . elements , preferences ) &&
12891290 aliasDeclaration . kind === SyntaxKind . ImportSpecifier &&
12901291 namedImports . elements . indexOf ( aliasDeclaration ) !== 0
12911292 ) {
@@ -1348,36 +1349,37 @@ function doAddExistingFix(
13481349 ignoreCaseForSorting = preferences . organizeImportsIgnoreCase ;
13491350 }
13501351 else if ( existingSpecifiers ) {
1351- const targetImportSorting = OrganizeImports . detectImportSpecifierSorting ( existingSpecifiers ) ;
1352+ const targetImportSorting = OrganizeImports . detectImportSpecifierSorting ( existingSpecifiers , preferences ) ;
13521353 if ( targetImportSorting !== SortKind . Both ) {
13531354 ignoreCaseForSorting = targetImportSorting === SortKind . CaseInsensitive ;
13541355 }
13551356 }
13561357 if ( ignoreCaseForSorting === undefined ) {
1357- ignoreCaseForSorting = OrganizeImports . detectSorting ( sourceFile ) === SortKind . CaseInsensitive ;
1358+ ignoreCaseForSorting = OrganizeImports . detectSorting ( sourceFile , preferences ) === SortKind . CaseInsensitive ;
13581359 }
13591360
1361+ const comparer = OrganizeImports . getOrganizeImportsComparer ( preferences , ignoreCaseForSorting ) ;
13601362 const newSpecifiers = stableSort (
13611363 namedImports . map ( namedImport => factory . createImportSpecifier (
13621364 ( ! clause . isTypeOnly || promoteFromTypeOnly ) && needsTypeOnly ( namedImport ) ,
13631365 /*propertyName*/ undefined ,
13641366 factory . createIdentifier ( namedImport . name ) ) ) ,
1365- ( s1 , s2 ) => OrganizeImports . compareImportOrExportSpecifiers ( s1 , s2 , ignoreCaseForSorting ) ) ;
1367+ ( s1 , s2 ) => OrganizeImports . compareImportOrExportSpecifiers ( s1 , s2 , comparer ) ) ;
13661368
13671369 // The sorting preference computed earlier may or may not have validated that these particular
13681370 // import specifiers are sorted. If they aren't, `getImportSpecifierInsertionIndex` will return
13691371 // nonsense. So if there are existing specifiers, even if we know the sorting preference, we
13701372 // need to ensure that the existing specifiers are sorted according to the preference in order
13711373 // to do a sorted insertion.
1372- const specifierSort = existingSpecifiers ?. length && OrganizeImports . detectImportSpecifierSorting ( existingSpecifiers ) ;
1374+ const specifierSort = existingSpecifiers ?. length && OrganizeImports . detectImportSpecifierSorting ( existingSpecifiers , preferences ) ;
13731375 if ( specifierSort && ! ( ignoreCaseForSorting && specifierSort === SortKind . CaseSensitive ) ) {
13741376 for ( const spec of newSpecifiers ) {
13751377 // Organize imports puts type-only import specifiers last, so if we're
13761378 // adding a non-type-only specifier and converting all the other ones to
13771379 // type-only, there's no need to ask for the insertion index - it's 0.
13781380 const insertionIndex = convertExistingToTypeOnly && ! spec . isTypeOnly
13791381 ? 0
1380- : OrganizeImports . getImportSpecifierInsertionIndex ( existingSpecifiers , spec , ignoreCaseForSorting ) ;
1382+ : OrganizeImports . getImportSpecifierInsertionIndex ( existingSpecifiers , spec , comparer ) ;
13811383 changes . insertImportSpecifierAtIndex ( sourceFile , spec , clause . namedBindings as NamedImports , insertionIndex ) ;
13821384 }
13831385 }
0 commit comments