Skip to content

Commit 29fc8c3

Browse files
committed
support case when variable is re-assigned
1 parent 1bcd8da commit 29fc8c3

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/services/refactors/convertStringOrTemplateLiteral.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,12 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
8383
return text.length === 0 ? [expression] : [expression, createStringLiteral(text)];
8484
}
8585

86+
function isNotEqualsOperator(node: BinaryExpression) {
87+
return node.operatorToken.kind !== SyntaxKind.EqualsToken;
88+
}
89+
8690
function getParentBinaryExpression(expr: Node) {
87-
while (isBinaryExpression(expr.parent)) {
91+
while (isBinaryExpression(expr.parent) && isNotEqualsOperator(expr.parent)) {
8892
expr = expr.parent;
8993
}
9094
return expr;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// let foo = ""
4+
//// /*z*/f/*y*/oo = "/*x*/M/*w*/r Bar" + " is" + /*v*/4/*u*/2 + " years old"
5+
6+
goTo.select("z", "y");
7+
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
8+
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal");
9+
10+
goTo.select("x", "w");
11+
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
12+
verify.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal");
13+
14+
goTo.select("v", "u");
15+
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
16+
verify.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal");

tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateDollar.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ edit.applyRefactor({
1010
newContent:
1111
"const foo = `with $\\\\{dollar}`",
1212
});
13+

0 commit comments

Comments
 (0)