@@ -37,7 +37,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
37
37
switch ( actionName ) {
38
38
case toTemplateLiteralActionName :
39
39
const maybeBinary = getParentBinaryExpression ( node ) ;
40
- const arrayOfNodes = treeToArray ( maybeBinary ) ;
40
+ const arrayOfNodes = treeToArray ( maybeBinary ) [ 0 ] ;
41
41
const templateLiteral = nodesToTemplate ( arrayOfNodes ) ;
42
42
43
43
const edits = textChanges . ChangeTracker . with ( context , t => t . replaceNode ( file , maybeBinary , templateLiteral ) ) ;
@@ -106,11 +106,16 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
106
106
return arrayToTree ( nodes . slice ( 1 ) , binary ) ;
107
107
}
108
108
109
- function treeToArray ( node : Node ) : Node [ ] {
109
+ function treeToArray ( node : Node ) : [ Node [ ] , boolean ] {
110
110
if ( isBinaryExpression ( node ) ) {
111
- return treeToArray ( node . left ) . concat ( treeToArray ( node . right ) ) ;
111
+ const [ leftNodes , leftHasString ] = treeToArray ( node . left ) ;
112
+ const [ rightNodes , rightHasString ] = treeToArray ( node . right ) ;
113
+
114
+ if ( ! leftHasString && ! rightHasString ) return [ [ node ] , false ] ;
115
+ return [ leftNodes . concat ( rightNodes ) , true ] ;
112
116
}
113
- return [ node ] ;
117
+
118
+ return [ [ node ] , isStringLiteral ( node ) ] ;
114
119
}
115
120
116
121
function nodesToTemplate ( nodes : Node [ ] ) {
@@ -142,19 +147,6 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
142
147
let current = nodes [ i ] ;
143
148
let templatePart : TemplateMiddle | TemplateTail ;
144
149
145
- if ( head . text . length === 0 && i + 1 < nodes . length && ! isStringLiteral ( nodes [ i + 1 ] ) ) {
146
- let binary = createBinary ( current as Expression , SyntaxKind . PlusToken , nodes [ i + 1 ] as Expression ) ;
147
- current = binary ;
148
- i ++ ;
149
-
150
- while ( i + 1 < nodes . length && ! isStringLiteral ( nodes [ i + 1 ] ) ) {
151
- binary = createBinary ( binary , SyntaxKind . PlusToken , nodes [ i + 1 ] as Expression ) ;
152
- i ++ ;
153
- }
154
-
155
- current = binary ;
156
- }
157
-
158
150
if ( i + 1 < nodes . length && isStringLiteral ( nodes [ i + 1 ] ) ) {
159
151
let next = nodes [ i + 1 ] as StringLiteral ;
160
152
let text = next . text ;
0 commit comments