Skip to content

Commit 882e616

Browse files
committed
optimize preceding expression
1 parent 6de23d7 commit 882e616

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

src/services/refactors/convertStringOrTemplateLiteral.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
3737
switch (actionName) {
3838
case toTemplateLiteralActionName:
3939
const maybeBinary = getParentBinaryExpression(node);
40-
const arrayOfNodes = treeToArray(maybeBinary);
40+
const arrayOfNodes = treeToArray(maybeBinary)[0];
4141
const templateLiteral = nodesToTemplate(arrayOfNodes);
4242

4343
const edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, maybeBinary, templateLiteral));
@@ -106,11 +106,16 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
106106
return arrayToTree(nodes.slice(1), binary);
107107
}
108108

109-
function treeToArray(node: Node): Node[] {
109+
function treeToArray(node: Node): [Node[], boolean] {
110110
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];
112116
}
113-
return [node];
117+
118+
return [[node], isStringLiteral(node)];
114119
}
115120

116121
function nodesToTemplate(nodes: Node[]) {
@@ -142,19 +147,6 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
142147
let current = nodes[i];
143148
let templatePart: TemplateMiddle | TemplateTail;
144149

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-
158150
if (i + 1 < nodes.length && isStringLiteral(nodes[i + 1])) {
159151
let next = nodes[i + 1] as StringLiteral;
160152
let text = next.text;

0 commit comments

Comments
 (0)