@@ -22,7 +22,7 @@ namespace ts.OrganizeImports {
22
22
( s1 , s2 ) => compareImportsOrRequireStatements ( s1 , s2 ) ) ;
23
23
24
24
// 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 ) ) ;
26
26
topLevelImportGroupDecls . forEach ( importGroupDecl => organizeImportsWorker ( importGroupDecl , coalesceAndOrganizeImports ) ) ;
27
27
28
28
// All of the old ExportDeclarations in the file, in syntactic order.
@@ -32,7 +32,7 @@ namespace ts.OrganizeImports {
32
32
for ( const ambientModule of sourceFile . statements . filter ( isAmbientModule ) ) {
33
33
if ( ! ambientModule . body ) continue ;
34
34
35
- const ambientModuleImportGroupDecls = groupImportsByNewlineContiguous ( sourceFile , ambientModule . body . statements . filter ( isImportDeclaration ) ) ;
35
+ const ambientModuleImportGroupDecls = groupImportsByNewlineContiguous ( host , formatContext , ambientModule . body . statements . filter ( isImportDeclaration ) ) ;
36
36
ambientModuleImportGroupDecls . forEach ( importGroupDecl => organizeImportsWorker ( importGroupDecl , coalesceAndOrganizeImports ) ) ;
37
37
38
38
const ambientModuleExportDecls = ambientModule . body . statements . filter ( isExportDeclaration ) ;
@@ -87,12 +87,15 @@ namespace ts.OrganizeImports {
87
87
}
88
88
}
89
89
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 [ ] [ ] {
92
91
const groupImports : ImportDeclaration [ ] [ ] = [ ] ;
92
+ const newLine = getNewLineOrDefaultFromHost ( host , formatContext . options ) ;
93
+
93
94
let groupIndex = 0 ;
94
95
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 ) {
96
99
groupIndex ++ ;
97
100
}
98
101
@@ -106,25 +109,6 @@ namespace ts.OrganizeImports {
106
109
return groupImports ;
107
110
}
108
111
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
-
128
112
function removeUnusedImports ( oldImports : readonly ImportDeclaration [ ] , sourceFile : SourceFile , program : Program , skipDestructiveCodeActions : boolean | undefined ) {
129
113
// As a precaution, consider unused import detection to be destructive (GH #43051)
130
114
if ( skipDestructiveCodeActions ) {
0 commit comments