Skip to content

Commit 9b9aa35

Browse files
committed
optimize getEdits string concatenation
1 parent ad0614a commit 9b9aa35

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/services/refactors/convertStringOrTemplateLiteral.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,10 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
6868
if (isTemplateExpression(node.parent) || isTemplateSpan(node.parent)) {
6969
const templateLiteralExpression = isTemplateSpan(node.parent) ? node.parent.parent : node.parent;
7070
const { head, templateSpans } = templateLiteralExpression;
71-
const arrayOfNodes: Expression[] = [];
71+
const arrayOfNodes = templateSpans.map(templateSpanToExpressions)
72+
.reduce((accumulator, nextArray) => accumulator.concat(nextArray));
7273

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));
8075

8176
const binaryExpression = arrayToTree(arrayOfNodes);
8277
return textChanges.ChangeTracker.with(context, t => t.replaceNode(context.file, templateLiteralExpression, binaryExpression));
@@ -88,6 +83,12 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
8883
}
8984
}
9085

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+
9192
function getParentBinaryExpression(expr: Node) {
9293
while (isBinaryExpression(expr.parent)) {
9394
expr = expr.parent;

0 commit comments

Comments
 (0)