Skip to content

Commit

Permalink
Update grouping to only check newline
Browse files Browse the repository at this point in the history
  • Loading branch information
MQuy committed Apr 5, 2022
1 parent 24e4cd9 commit aa759f0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 27 deletions.
32 changes: 8 additions & 24 deletions src/services/organizeImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace ts.OrganizeImports {
(s1, s2) => compareImportsOrRequireStatements(s1, s2));

// All of the old ImportDeclarations in the file, in syntactic order.
const topLevelImportGroupDecls = groupImportsByNewlineContiguous(sourceFile, sourceFile.statements.filter(isImportDeclaration));
const topLevelImportGroupDecls = groupImportsByNewlineContiguous(host, formatContext, sourceFile.statements.filter(isImportDeclaration));
topLevelImportGroupDecls.forEach(importGroupDecl => organizeImportsWorker(importGroupDecl, coalesceAndOrganizeImports));

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

const ambientModuleImportGroupDecls = groupImportsByNewlineContiguous(sourceFile, ambientModule.body.statements.filter(isImportDeclaration));
const ambientModuleImportGroupDecls = groupImportsByNewlineContiguous(host, formatContext, ambientModule.body.statements.filter(isImportDeclaration));
ambientModuleImportGroupDecls.forEach(importGroupDecl => organizeImportsWorker(importGroupDecl, coalesceAndOrganizeImports));

const ambientModuleExportDecls = ambientModule.body.statements.filter(isExportDeclaration);
Expand Down Expand Up @@ -87,12 +87,15 @@ namespace ts.OrganizeImports {
}
}

function groupImportsByNewlineContiguous(sourceFile: SourceFile,importDecls: ImportDeclaration[]): ImportDeclaration[][] {
const scanner = createScanner(sourceFile.languageVersion, /*skipTrivia*/ false, sourceFile.languageVariant);
function groupImportsByNewlineContiguous(host: LanguageServiceHost, formatContext: formatting.FormatContext, importDecls: ImportDeclaration[]): ImportDeclaration[][] {
const groupImports: ImportDeclaration[][] = [];
const newLine = getNewLineOrDefaultFromHost(host, formatContext.options);

let groupIndex = 0;
for (const topLevelImportDecl of importDecls) {
if (isNewGroup(sourceFile, topLevelImportDecl, scanner)) {
const leadingText = topLevelImportDecl.getFullText().substring(0, topLevelImportDecl.getStart() - topLevelImportDecl.getFullStart());
// a new group is created if an import includes at least two new line
if (leadingText.split(newLine).length > 2) {
groupIndex++;
}

Expand All @@ -106,25 +109,6 @@ namespace ts.OrganizeImports {
return groupImports;
}

// a new group is created if an import includes at least two new line
// new line from multi-line comment doesn't count
function isNewGroup(sourceFile: SourceFile, topLevelImportDecl: ImportDeclaration, scanner: Scanner) {
const startPos = topLevelImportDecl.getFullStart();
const endPos = topLevelImportDecl.getStart();

scanner.setText(sourceFile.text, startPos);
let numberOfNewLines = 0;
while (scanner.getTokenPos() < endPos) {
const tokenKind = scanner.scan();

if (tokenKind === SyntaxKind.NewLineTrivia) {
numberOfNewLines++;
}
}

return numberOfNewLines >= 2;
}

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
6 changes: 3 additions & 3 deletions tests/cases/fourslash/organizeImports7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
//// somethingElse;

verify.organizeImports(
`import * as somethingElse from "anotherpath";
import * as something from "path"; /**
`import * as something from "path"; /**
* some comment here
* and there
*/
import * as somethingElse from "anotherpath";
something;
somethingElse;`
);
);

0 comments on commit aa759f0

Please sign in to comment.