Skip to content

Commit 935cf04

Browse files
committed
optimize and add more tests for parenthesized case
1 parent 2a15acb commit 935cf04

4 files changed

+36
-1
lines changed

src/services/refactors/convertStringOrTemplateLiteral.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
3333

3434
function getNodeOrParentOfParentheses(file: SourceFile, startPosition: number) {
3535
const node = getTokenAtPosition(file, startPosition);
36-
if (isParenthesizedExpression(node.parent) && isBinaryExpression(node.parent.parent)) return node.parent.parent;
36+
const nestedBinary = getParentBinaryExpression(node);
37+
const isNonStringBinary = !isStringConcatenationValid(nestedBinary);
38+
39+
if (
40+
isNonStringBinary &&
41+
isParenthesizedExpression(nestedBinary.parent) &&
42+
isBinaryExpression(nestedBinary.parent.parent)
43+
) {
44+
return nestedBinary.parent.parent;
45+
}
3746
return node;
3847
}
3948

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = "foobar is " + (/*x*/42/*y*/ + 6) + " years old"
4+
5+
goTo.select("x", "y");
6+
edit.applyRefactor({
7+
refactorName: "Convert string concatenation or template literal",
8+
actionName: "Convert to template literal",
9+
actionDescription: "Convert to template literal",
10+
newContent:
11+
`const foo = \`foobar is \${42 + 6} years old\``,
12+
});
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = "foobar is " + (/*x*/42/*y*/ + 6 + "str") + " years old"
4+
5+
goTo.select("x", "y");
6+
edit.applyRefactor({
7+
refactorName: "Convert string concatenation or template literal",
8+
actionName: "Convert to template literal",
9+
actionDescription: "Convert to template literal",
10+
newContent:
11+
`const foo = "foobar is " + (\`\${42 + 6}str\`) + " years old"`,
12+
});
13+

0 commit comments

Comments
 (0)