@@ -92,7 +92,7 @@ namespace ts.OrganizeImports {
92
92
const groupImports : ImportDeclaration [ ] [ ] = [ ] ;
93
93
let groupIndex = 0 ;
94
94
for ( const topLevelImportDecl of importDecls ) {
95
- if ( isNewGroup ( sourceFile , topLevelImportDecl , scanner ) ) {
95
+ if ( isNewGroup ( topLevelImportDecl , scanner ) ) {
96
96
groupIndex ++ ;
97
97
}
98
98
@@ -108,18 +108,20 @@ namespace ts.OrganizeImports {
108
108
109
109
// a new group is created if an import includes at least two new line
110
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 ( ) ;
111
+ function isNewGroup ( topLevelImportDecl : ImportDeclaration , scanner : Scanner ) {
112
+ const leadingText = topLevelImportDecl . getFullText ( ) . substring ( 0 , topLevelImportDecl . getLeadingTriviaWidth ( ) ) ;
113
+ scanner . setText ( leadingText ) ;
114
114
115
- scanner . setText ( sourceFile . text , startPos ) ;
116
115
let numberOfNewLines = 0 ;
117
- while ( scanner . getTokenPos ( ) < endPos ) {
116
+ while ( true ) {
118
117
const tokenKind = scanner . scan ( ) ;
119
118
120
119
if ( tokenKind === SyntaxKind . NewLineTrivia ) {
121
120
numberOfNewLines ++ ;
122
121
}
122
+ else if ( tokenKind === SyntaxKind . EndOfFileToken ) {
123
+ break ;
124
+ }
123
125
}
124
126
125
127
return numberOfNewLines >= 2 ;
0 commit comments