Skip to content

Commit

Permalink
Create sort groups based on newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
MQuy committed Mar 19, 2022
1 parent 073ac92 commit ff8df38
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/services/organizeImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ namespace ts.OrganizeImports {
preferences: UserPreferences,
skipDestructiveCodeActions?: boolean
) {

const changeTracker = textChanges.ChangeTracker.fromContext({ host, formatContext, preferences });

const coalesceAndOrganizeImports = (importGroup: readonly ImportDeclaration[]) => stableSort(
coalesceImports(removeUnusedImports(importGroup, sourceFile, program, skipDestructiveCodeActions)),
(s1, s2) => compareImportsOrRequireStatements(s1, s2));

// All of the old ImportDeclarations in the file, in syntactic order.
const topLevelImportDecls = sourceFile.statements.filter(isImportDeclaration);
organizeImportsWorker(topLevelImportDecls, coalesceAndOrganizeImports);
const topLevelGroupImportDecls = groupImportsByNewlines(sourceFile.statements.filter(isImportDeclaration), host, formatContext);
topLevelGroupImportDecls.forEach(topLevelGroupImportDecl => organizeImportsWorker(topLevelGroupImportDecl, coalesceAndOrganizeImports));

// All of the old ExportDeclarations in the file, in syntactic order.
const topLevelExportDecls = sourceFile.statements.filter(isExportDeclaration);
Expand All @@ -33,8 +32,8 @@ namespace ts.OrganizeImports {
for (const ambientModule of sourceFile.statements.filter(isAmbientModule)) {
if (!ambientModule.body) continue;

const ambientModuleImportDecls = ambientModule.body.statements.filter(isImportDeclaration);
organizeImportsWorker(ambientModuleImportDecls, coalesceAndOrganizeImports);
const ambientModuleGroupImportDecls = groupImportsByNewlines(ambientModule.body.statements.filter(isImportDeclaration), host, formatContext);
ambientModuleGroupImportDecls.forEach(ambientModuleGroupImportDecl => organizeImportsWorker(ambientModuleGroupImportDecl, coalesceAndOrganizeImports));

const ambientModuleExportDecls = ambientModule.body.statements.filter(isExportDeclaration);
organizeImportsWorker(ambientModuleExportDecls, coalesceExports);
Expand Down Expand Up @@ -88,6 +87,24 @@ namespace ts.OrganizeImports {
}
}

function groupImportsByNewlines(importDecls: ImportDeclaration[], host: LanguageServiceHost, formatContext: formatting.FormatContext): ImportDeclaration[][] {
const groupImports: ImportDeclaration[][] = [];
const newLine = getNewLineOrDefaultFromHost(host, formatContext.options);
const prefixCond = `${newLine}${newLine}`;
for(let index = 0, groupIndex = 0, length = importDecls.length; index < length ; ++index) {
const topLevelImportDecl = importDecls[index];
const leadingText = topLevelImportDecl.getFullText().substring(0, topLevelImportDecl.getStart() - topLevelImportDecl.getFullStart());
if (startsWith(leadingText, prefixCond)) {
groupIndex++;
}
if (!groupImports[groupIndex]) {
groupImports[groupIndex] = [];
}
groupImports[groupIndex].push(topLevelImportDecl);
}
return groupImports;
}

function removeUnusedImports(oldImports: readonly ImportDeclaration[], sourceFile: SourceFile, program: Program, skipDestructiveCodeActions: boolean | undefined) {
// As a precaution, consider unused import detection to be destructive (GH #43051)
if (skipDestructiveCodeActions) {
Expand Down
19 changes: 19 additions & 0 deletions tests/cases/fourslash/organizeImports10.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference path="fourslash.ts" />

////import c from "C";
////
////import d from "D";
////import a from "A";
////import b from "B";
////
////console.log(a, b, c, d)

verify.organizeImports(
`import c from "C";
import a from "A";
import b from "B";
import d from "D";
console.log(a, b, c, d)`
);

0 comments on commit ff8df38

Please sign in to comment.