Skip to content

Commit f594067

Browse files
author
MQuy
committed
Only scan in top level import trivia
1 parent 24e4cd9 commit f594067

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/services/organizeImports.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ namespace ts.OrganizeImports {
9292
const groupImports: ImportDeclaration[][] = [];
9393
let groupIndex = 0;
9494
for (const topLevelImportDecl of importDecls) {
95-
if (isNewGroup(sourceFile, topLevelImportDecl, scanner)) {
95+
if (isNewGroup(topLevelImportDecl, scanner)) {
9696
groupIndex++;
9797
}
9898

@@ -108,18 +108,20 @@ namespace ts.OrganizeImports {
108108

109109
// a new group is created if an import includes at least two new line
110110
// 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();
111+
function isNewGroup(topLevelImportDecl: ImportDeclaration, scanner: Scanner) {
112+
const leadingText = topLevelImportDecl.getFullText().substring(0, topLevelImportDecl.getLeadingTriviaWidth());
113+
scanner.setText(leadingText);
114114

115-
scanner.setText(sourceFile.text, startPos);
116115
let numberOfNewLines = 0;
117-
while (scanner.getTokenPos() < endPos) {
116+
while (true) {
118117
const tokenKind = scanner.scan();
119118

120119
if (tokenKind === SyntaxKind.NewLineTrivia) {
121120
numberOfNewLines++;
122121
}
122+
else if (tokenKind === SyntaxKind.EndOfFileToken) {
123+
break;
124+
}
123125
}
124126

125127
return numberOfNewLines >= 2;

0 commit comments

Comments
 (0)