@@ -79,8 +79,8 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
79
79
80
80
if ( head . text . length !== 0 ) arrayOfNodes . unshift ( createStringLiteral ( head . text ) ) ;
81
81
82
- const binaryExpression = arrayToTree ( arrayOfNodes ) ;
83
- return textChanges . ChangeTracker . with ( context , t => t . replaceNode ( context . file , templateLiteral , binaryExpression ) ) ;
82
+ const singleExpressionOrBinary = makeSingleExpressionOrBinary ( arrayOfNodes ) ;
83
+ return textChanges . ChangeTracker . with ( context , t => t . replaceNode ( context . file , templateLiteral , singleExpressionOrBinary ) ) ;
84
84
}
85
85
else {
86
86
const stringLiteral = createStringLiteral ( templateLiteral . text ) ;
@@ -105,20 +105,24 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
105
105
return expr ;
106
106
}
107
107
108
- function arrayToTree ( nodes : ReadonlyArray < Expression > , accumulator ?: BinaryExpression ) : BinaryExpression {
109
- if ( nodes . length === 0 ) return accumulator ! ;
110
-
111
- if ( ! accumulator ) {
108
+ function makeSingleExpressionOrBinary ( nodes : ReadonlyArray < Expression > ) : Expression {
109
+ if ( nodes . length > 1 ) {
112
110
const left = nodes [ 0 ] ;
113
111
const right = nodes [ 1 ] ;
114
112
115
113
const binary = createBinary ( left , SyntaxKind . PlusToken , right ) ;
116
- return arrayToTree ( nodes . slice ( 2 ) , binary ) ;
114
+ return arrayToTree ( nodes , 2 , binary ) ;
117
115
}
118
116
119
- const right = nodes [ 0 ] ;
117
+ return nodes [ 0 ] ;
118
+ }
119
+
120
+ function arrayToTree ( nodes : ReadonlyArray < Expression > , index : number , accumulator : BinaryExpression ) : Expression {
121
+ if ( nodes . length === index ) return accumulator ;
122
+
123
+ const right = nodes [ index ] ;
120
124
const binary = createBinary ( accumulator , SyntaxKind . PlusToken , right ) ;
121
- return arrayToTree ( nodes . slice ( 1 ) , binary ) ;
125
+ return arrayToTree ( nodes , index + 1 , binary ) ;
122
126
}
123
127
124
128
function isStringConcatenationValid ( node : Node ) : boolean {
0 commit comments