@@ -375,7 +375,7 @@ function createImportAdderWorker(sourceFile: SourceFile, program: Program, useAu
375
375
newDeclarations = combine ( newDeclarations , declarations ) ;
376
376
} ) ;
377
377
if ( newDeclarations ) {
378
- insertImports ( changeTracker , sourceFile , newDeclarations , /*blankLineBetween*/ true ) ;
378
+ insertImports ( changeTracker , sourceFile , newDeclarations , /*blankLineBetween*/ true , preferences ) ;
379
379
}
380
380
}
381
381
@@ -1221,14 +1221,14 @@ function codeActionForFixWorker(changes: textChanges.ChangeTracker, sourceFile:
1221
1221
const defaultImport : Import | undefined = importKind === ImportKind . Default ? { name : symbolName , addAsTypeOnly } : undefined ;
1222
1222
const namedImports : Import [ ] | undefined = importKind === ImportKind . Named ? [ { name : symbolName , addAsTypeOnly } ] : undefined ;
1223
1223
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 ) ;
1225
1225
return includeSymbolNameInDescription
1226
1226
? [ Diagnostics . Import_0_from_1 , symbolName , moduleSpecifier ]
1227
1227
: [ Diagnostics . Add_import_from_0 , moduleSpecifier ] ;
1228
1228
}
1229
1229
case ImportFixKind . PromoteTypeOnly : {
1230
1230
const { typeOnlyAliasDeclaration } = fix ;
1231
- const promotedDeclaration = promoteFromTypeOnly ( changes , typeOnlyAliasDeclaration , compilerOptions , sourceFile ) ;
1231
+ const promotedDeclaration = promoteFromTypeOnly ( changes , typeOnlyAliasDeclaration , compilerOptions , sourceFile , preferences ) ;
1232
1232
return promotedDeclaration . kind === SyntaxKind . ImportSpecifier
1233
1233
? [ Diagnostics . Remove_type_from_import_of_0_from_1 , symbolName , getModuleSpecifierText ( promotedDeclaration . parent . parent ) ]
1234
1234
: [ Diagnostics . Remove_type_from_import_declaration_from_0 , getModuleSpecifierText ( promotedDeclaration ) ] ;
@@ -1244,17 +1244,18 @@ function getModuleSpecifierText(promotedDeclaration: ImportClause | ImportEquals
1244
1244
: cast ( promotedDeclaration . parent . moduleSpecifier , isStringLiteral ) . text ;
1245
1245
}
1246
1246
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 ) {
1248
1248
// See comment in `doAddExistingFix` on constant with the same name.
1249
1249
const convertExistingToTypeOnly = compilerOptions . preserveValueImports && compilerOptions . isolatedModules ;
1250
1250
switch ( aliasDeclaration . kind ) {
1251
1251
case SyntaxKind . ImportSpecifier :
1252
1252
if ( aliasDeclaration . isTypeOnly ) {
1253
- const sortKind = OrganizeImports . detectImportSpecifierSorting ( aliasDeclaration . parent . elements ) ;
1253
+ const sortKind = OrganizeImports . detectImportSpecifierSorting ( aliasDeclaration . parent . elements , preferences ) ;
1254
1254
if ( aliasDeclaration . parent . elements . length > 1 && sortKind ) {
1255
1255
changes . delete ( sourceFile , aliasDeclaration ) ;
1256
1256
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 ) ;
1258
1259
changes . insertImportSpecifierAtIndex ( sourceFile , newSpecifier , aliasDeclaration . parent , insertionIndex ) ;
1259
1260
}
1260
1261
else {
@@ -1285,7 +1286,7 @@ function promoteFromTypeOnly(changes: textChanges.ChangeTracker, aliasDeclaratio
1285
1286
if ( convertExistingToTypeOnly ) {
1286
1287
const namedImports = tryCast ( importClause . namedBindings , isNamedImports ) ;
1287
1288
if ( namedImports && namedImports . elements . length > 1 ) {
1288
- if ( OrganizeImports . detectImportSpecifierSorting ( namedImports . elements ) &&
1289
+ if ( OrganizeImports . detectImportSpecifierSorting ( namedImports . elements , preferences ) &&
1289
1290
aliasDeclaration . kind === SyntaxKind . ImportSpecifier &&
1290
1291
namedImports . elements . indexOf ( aliasDeclaration ) !== 0
1291
1292
) {
@@ -1348,36 +1349,37 @@ function doAddExistingFix(
1348
1349
ignoreCaseForSorting = preferences . organizeImportsIgnoreCase ;
1349
1350
}
1350
1351
else if ( existingSpecifiers ) {
1351
- const targetImportSorting = OrganizeImports . detectImportSpecifierSorting ( existingSpecifiers ) ;
1352
+ const targetImportSorting = OrganizeImports . detectImportSpecifierSorting ( existingSpecifiers , preferences ) ;
1352
1353
if ( targetImportSorting !== SortKind . Both ) {
1353
1354
ignoreCaseForSorting = targetImportSorting === SortKind . CaseInsensitive ;
1354
1355
}
1355
1356
}
1356
1357
if ( ignoreCaseForSorting === undefined ) {
1357
- ignoreCaseForSorting = OrganizeImports . detectSorting ( sourceFile ) === SortKind . CaseInsensitive ;
1358
+ ignoreCaseForSorting = OrganizeImports . detectSorting ( sourceFile , preferences ) === SortKind . CaseInsensitive ;
1358
1359
}
1359
1360
1361
+ const comparer = OrganizeImports . getOrganizeImportsComparer ( preferences , ignoreCaseForSorting ) ;
1360
1362
const newSpecifiers = stableSort (
1361
1363
namedImports . map ( namedImport => factory . createImportSpecifier (
1362
1364
( ! clause . isTypeOnly || promoteFromTypeOnly ) && needsTypeOnly ( namedImport ) ,
1363
1365
/*propertyName*/ undefined ,
1364
1366
factory . createIdentifier ( namedImport . name ) ) ) ,
1365
- ( s1 , s2 ) => OrganizeImports . compareImportOrExportSpecifiers ( s1 , s2 , ignoreCaseForSorting ) ) ;
1367
+ ( s1 , s2 ) => OrganizeImports . compareImportOrExportSpecifiers ( s1 , s2 , comparer ) ) ;
1366
1368
1367
1369
// The sorting preference computed earlier may or may not have validated that these particular
1368
1370
// import specifiers are sorted. If they aren't, `getImportSpecifierInsertionIndex` will return
1369
1371
// nonsense. So if there are existing specifiers, even if we know the sorting preference, we
1370
1372
// need to ensure that the existing specifiers are sorted according to the preference in order
1371
1373
// to do a sorted insertion.
1372
- const specifierSort = existingSpecifiers ?. length && OrganizeImports . detectImportSpecifierSorting ( existingSpecifiers ) ;
1374
+ const specifierSort = existingSpecifiers ?. length && OrganizeImports . detectImportSpecifierSorting ( existingSpecifiers , preferences ) ;
1373
1375
if ( specifierSort && ! ( ignoreCaseForSorting && specifierSort === SortKind . CaseSensitive ) ) {
1374
1376
for ( const spec of newSpecifiers ) {
1375
1377
// Organize imports puts type-only import specifiers last, so if we're
1376
1378
// adding a non-type-only specifier and converting all the other ones to
1377
1379
// type-only, there's no need to ask for the insertion index - it's 0.
1378
1380
const insertionIndex = convertExistingToTypeOnly && ! spec . isTypeOnly
1379
1381
? 0
1380
- : OrganizeImports . getImportSpecifierInsertionIndex ( existingSpecifiers , spec , ignoreCaseForSorting ) ;
1382
+ : OrganizeImports . getImportSpecifierInsertionIndex ( existingSpecifiers , spec , comparer ) ;
1381
1383
changes . insertImportSpecifierAtIndex ( sourceFile , spec , clause . namedBindings as NamedImports , insertionIndex ) ;
1382
1384
}
1383
1385
}
0 commit comments