From 43e1edf10a3cc8986cf72316f7630194b6f92ded Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Fri, 18 May 2018 10:54:40 -0700 Subject: [PATCH] Eliminate runtime type check --- src/services/organizeImports.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/services/organizeImports.ts b/src/services/organizeImports.ts index 4bee286789c..5476352dbf5 100644 --- a/src/services/organizeImports.ts +++ b/src/services/organizeImports.ts @@ -17,27 +17,32 @@ namespace ts.OrganizeImports { const changeTracker = textChanges.ChangeTracker.fromContext({ host, formatContext }); + const coalesceAndOrganizeImports = (importGroup: ReadonlyArray) => coalesceImports(removeUnusedImports(importGroup, sourceFile, program)); + // All of the old ImportDeclarations in the file, in syntactic order. const topLevelImportDecls = sourceFile.statements.filter(isImportDeclaration); - organizeImportsWorker(topLevelImportDecls); + organizeImportsWorker(topLevelImportDecls, coalesceAndOrganizeImports); // All of the old ExportDeclarations in the file, in syntactic order. const topLevelExportDecls = sourceFile.statements.filter(isExportDeclaration); - organizeImportsWorker(topLevelExportDecls); + organizeImportsWorker(topLevelExportDecls, coalesceExports); for (const ambientModule of sourceFile.statements.filter(isAmbientModule)) { const ambientModuleBody = getModuleBlock(ambientModule as ModuleDeclaration); const ambientModuleImportDecls = ambientModuleBody.statements.filter(isImportDeclaration); - organizeImportsWorker(ambientModuleImportDecls); + organizeImportsWorker(ambientModuleImportDecls, coalesceAndOrganizeImports); const ambientModuleExportDecls = ambientModuleBody.statements.filter(isExportDeclaration); - organizeImportsWorker(ambientModuleExportDecls); + organizeImportsWorker(ambientModuleExportDecls, coalesceExports); } return changeTracker.getChanges(); - function organizeImportsWorker(oldImportDecls: ReadonlyArray) { + function organizeImportsWorker( + oldImportDecls: ReadonlyArray, + coalesce: (group: ReadonlyArray) => ReadonlyArray) { + if (length(oldImportDecls) === 0) { return; } @@ -49,15 +54,11 @@ namespace ts.OrganizeImports { // but the consequences of being wrong are very minor. suppressLeadingTrivia(oldImportDecls[0]); - const areImports = isImportDeclaration(oldImportDecls[0]); - const oldImportGroups = group(oldImportDecls, importDecl => getExternalModuleName(importDecl.moduleSpecifier)); const sortedImportGroups = stableSort(oldImportGroups, (group1, group2) => compareModuleSpecifiers(group1[0].moduleSpecifier, group2[0].moduleSpecifier)); const newImportDecls = flatMap(sortedImportGroups, importGroup => getExternalModuleName(importGroup[0].moduleSpecifier) - ? areImports - ? coalesceImports(removeUnusedImports(importGroup as ReadonlyArray, sourceFile, program)) - : coalesceExports(importGroup as ReadonlyArray) + ? coalesce(importGroup) : importGroup); // Delete or replace the first import.