Skip to content

Commit

Permalink
remove casts
Browse files Browse the repository at this point in the history
  • Loading branch information
iisaduan committed Feb 27, 2024
1 parent eb54e77 commit c7c57f7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/services/organizeImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -813,12 +813,16 @@ function getTopLevelExportGroups(sourceFile: SourceFile) {
return flatMap(topLevelExportGroups, exportGroupDecls => groupByNewlineContiguous(sourceFile, exportGroupDecls));
}

function getModuleNamesFromDecls(decls: readonly AnyImportOrRequireStatement[]): string[] {
return decls.map(s => getExternalModuleName(getModuleSpecifierExpression(s)) || "");
}

/** @internal */
export function detectModuleSpecifierCaseBySort(importDeclsByGroup: ImportDeclaration[][], comparersToTest: Comparer<string>[]) {
export function detectModuleSpecifierCaseBySort(importDeclsByGroup: (readonly AnyImportOrRequireStatement[])[], comparersToTest: Comparer<string>[]) {
const moduleSpecifiersByGroup: string[][] = [];
importDeclsByGroup.forEach(importGroup => {
// turns importdeclbygroup into string[][] of module specifiers by group to detect sorting on module specifiers
moduleSpecifiersByGroup.push(importGroup.map(i => getExternalModuleName(i.moduleSpecifier)!));
// turns importDeclsByGroup into string[][] of module specifiers by group to detect sorting on module specifiers
moduleSpecifiersByGroup.push(getModuleNamesFromDecls(importGroup));
});
return detectCaseSensitivityBySort(moduleSpecifiersByGroup, comparersToTest);
}
Expand Down
7 changes: 1 addition & 6 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2623,12 +2623,7 @@ export function insertImports(changes: textChanges.ChangeTracker, sourceFile: So
const decl = isArray(imports) ? imports[0] : imports;
const importKindPredicate: (node: Node) => node is AnyImportOrRequireStatement = decl.kind === SyntaxKind.VariableStatement ? isRequireVariableStatement : isAnyImportSyntax;
const existingImportStatements = filter(sourceFile.statements, importKindPredicate);
// todo remove typecasts
const moduleSpecifiersToDetect: ImportDeclaration[][] = existingImportStatements.length > 1
? [existingImportStatements as ImportDeclaration[]]
: isArray(imports)
? [imports as ImportDeclaration[]]
: [existingImportStatements as ImportDeclaration[]];
const moduleSpecifiersToDetect = existingImportStatements.length > 1 ? [existingImportStatements] : isArray(imports) ? [imports] : [existingImportStatements];
const { comparer } = OrganizeImports.detectModuleSpecifierCaseBySort(moduleSpecifiersToDetect, OrganizeImports.getDetectionLists(preferences).comparersToTest);
const sortedNewImports = isArray(imports) ? stableSort(imports, (a, b) => OrganizeImports.compareImportsOrRequireStatements(a, b, comparer)) : [imports];
if (!existingImportStatements.length) {
Expand Down

0 comments on commit c7c57f7

Please sign in to comment.