@@ -68,15 +68,10 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
68
68
if ( isTemplateExpression ( node . parent ) || isTemplateSpan ( node . parent ) ) {
69
69
const templateLiteralExpression = isTemplateSpan ( node . parent ) ? node . parent . parent : node . parent ;
70
70
const { head, templateSpans } = templateLiteralExpression ;
71
- const arrayOfNodes : Expression [ ] = [ ] ;
71
+ const arrayOfNodes = templateSpans . map ( templateSpanToExpressions )
72
+ . reduce ( ( accumulator , nextArray ) => accumulator . concat ( nextArray ) ) ;
72
73
73
- if ( head . text . length !== 0 ) arrayOfNodes . push ( createStringLiteral ( head . text ) ) ;
74
-
75
- templateSpans . forEach ( ts => {
76
- arrayOfNodes . push ( ts . expression ) ;
77
- const text = ts . literal . text ;
78
- if ( text . length !== 0 ) arrayOfNodes . push ( createStringLiteral ( text ) ) ;
79
- } ) ;
74
+ if ( head . text . length !== 0 ) arrayOfNodes . unshift ( createStringLiteral ( head . text ) ) ;
80
75
81
76
const binaryExpression = arrayToTree ( arrayOfNodes ) ;
82
77
return textChanges . ChangeTracker . with ( context , t => t . replaceNode ( context . file , templateLiteralExpression , binaryExpression ) ) ;
@@ -88,6 +83,12 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
88
83
}
89
84
}
90
85
86
+ function templateSpanToExpressions ( templateSpan : TemplateSpan ) : Expression [ ] {
87
+ const { expression, literal } = templateSpan ;
88
+ const text = literal . text ;
89
+ return text . length === 0 ? [ expression ] : [ expression , createStringLiteral ( text ) ] ;
90
+ }
91
+
91
92
function getParentBinaryExpression ( expr : Node ) {
92
93
while ( isBinaryExpression ( expr . parent ) ) {
93
94
expr = expr . parent ;
0 commit comments