Skip to content

Commit eb6ddf6

Browse files
authored
fix(40671): suggest ConvertStringToTemplateLiteral refactoring for string with property/element acceses elements (microsoft#40942)
1 parent b27d4bf commit eb6ddf6

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/services/refactors/convertStringOrTemplateLiteral.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,19 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
7373
}
7474

7575
function getParentBinaryExpression(expr: Node) {
76-
while (isBinaryExpression(expr.parent) && isNotEqualsOperator(expr.parent)) {
77-
expr = expr.parent;
78-
}
79-
return expr;
76+
const container = findAncestor(expr.parent, n => {
77+
switch (n.kind) {
78+
case SyntaxKind.PropertyAccessExpression:
79+
case SyntaxKind.ElementAccessExpression:
80+
return false;
81+
case SyntaxKind.BinaryExpression:
82+
return !(isBinaryExpression(n.parent) && isNotEqualsOperator(n.parent));
83+
default:
84+
return "quit";
85+
}
86+
});
87+
88+
return container || expr;
8089
}
8190

8291
function isStringConcatenationValid(node: Node): boolean {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////const a = { prop: 1 };
4+
////const b = /*x*/a["prop"]/*y*/ + "a" + "b";
5+
6+
goTo.select("x", "y");
7+
edit.applyRefactor({
8+
refactorName: "Convert to template string",
9+
actionName: "Convert to template string",
10+
actionDescription: ts.Diagnostics.Convert_to_template_string.message,
11+
newContent: [
12+
"const a = { prop: 1 };",
13+
"const b = `${a[\"prop\"]}ab`;"
14+
].join("\n")
15+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////const a = { prop: 1 };
4+
////const b = /*x*/a.prop/*y*/ + "a" + "b";
5+
6+
goTo.select("x", "y");
7+
edit.applyRefactor({
8+
refactorName: "Convert to template string",
9+
actionName: "Convert to template string",
10+
actionDescription: ts.Diagnostics.Convert_to_template_string.message,
11+
newContent: [
12+
"const a = { prop: 1 };",
13+
"const b = `${a.prop}ab`;"
14+
].join("\n")
15+
});

0 commit comments

Comments
 (0)