Skip to content

Commit aa759f0

Browse files
author
MQuy
committed
Update grouping to only check newline
1 parent 24e4cd9 commit aa759f0

File tree

2 files changed

+11
-27
lines changed

2 files changed

+11
-27
lines changed

src/services/organizeImports.ts

+8-24
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace ts.OrganizeImports {
2222
(s1, s2) => compareImportsOrRequireStatements(s1, s2));
2323

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

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

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

3838
const ambientModuleExportDecls = ambientModule.body.statements.filter(isExportDeclaration);
@@ -87,12 +87,15 @@ namespace ts.OrganizeImports {
8787
}
8888
}
8989

90-
function groupImportsByNewlineContiguous(sourceFile: SourceFile,importDecls: ImportDeclaration[]): ImportDeclaration[][] {
91-
const scanner = createScanner(sourceFile.languageVersion, /*skipTrivia*/ false, sourceFile.languageVariant);
90+
function groupImportsByNewlineContiguous(host: LanguageServiceHost, formatContext: formatting.FormatContext, importDecls: ImportDeclaration[]): ImportDeclaration[][] {
9291
const groupImports: ImportDeclaration[][] = [];
92+
const newLine = getNewLineOrDefaultFromHost(host, formatContext.options);
93+
9394
let groupIndex = 0;
9495
for (const topLevelImportDecl of importDecls) {
95-
if (isNewGroup(sourceFile, topLevelImportDecl, scanner)) {
96+
const leadingText = topLevelImportDecl.getFullText().substring(0, topLevelImportDecl.getStart() - topLevelImportDecl.getFullStart());
97+
// a new group is created if an import includes at least two new line
98+
if (leadingText.split(newLine).length > 2) {
9699
groupIndex++;
97100
}
98101

@@ -106,25 +109,6 @@ namespace ts.OrganizeImports {
106109
return groupImports;
107110
}
108111

109-
// a new group is created if an import includes at least two new line
110-
// new line from multi-line comment doesn't count
111-
function isNewGroup(sourceFile: SourceFile, topLevelImportDecl: ImportDeclaration, scanner: Scanner) {
112-
const startPos = topLevelImportDecl.getFullStart();
113-
const endPos = topLevelImportDecl.getStart();
114-
115-
scanner.setText(sourceFile.text, startPos);
116-
let numberOfNewLines = 0;
117-
while (scanner.getTokenPos() < endPos) {
118-
const tokenKind = scanner.scan();
119-
120-
if (tokenKind === SyntaxKind.NewLineTrivia) {
121-
numberOfNewLines++;
122-
}
123-
}
124-
125-
return numberOfNewLines >= 2;
126-
}
127-
128112
function removeUnusedImports(oldImports: readonly ImportDeclaration[], sourceFile: SourceFile, program: Program, skipDestructiveCodeActions: boolean | undefined) {
129113
// As a precaution, consider unused import detection to be destructive (GH #43051)
130114
if (skipDestructiveCodeActions) {

tests/cases/fourslash/organizeImports7.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
//// somethingElse;
1313

1414
verify.organizeImports(
15-
`import * as somethingElse from "anotherpath";
16-
import * as something from "path"; /**
15+
`import * as something from "path"; /**
1716
* some comment here
1817
* and there
1918
*/
19+
import * as somethingElse from "anotherpath";
2020
2121
something;
2222
somethingElse;`
23-
);
23+
);

0 commit comments

Comments
 (0)