optimize and add more tests for parenthesized case

This commit is contained in:
BigAru 2019-03-27 13:24:01 +01:00
parent 2a15acbbfd
commit 935cf04e40
4 changed files with 36 additions and 1 deletions

View file

@ -33,7 +33,16 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
function getNodeOrParentOfParentheses(file: SourceFile, startPosition: number) {
const node = getTokenAtPosition(file, startPosition);
if (isParenthesizedExpression(node.parent) && isBinaryExpression(node.parent.parent)) return node.parent.parent;
const nestedBinary = getParentBinaryExpression(node);
const isNonStringBinary = !isStringConcatenationValid(nestedBinary);
if (
isNonStringBinary &&
isParenthesizedExpression(nestedBinary.parent) &&
isBinaryExpression(nestedBinary.parent.parent)
) {
return nestedBinary.parent.parent;
}
return node;
}

View file

@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />
//// const foo = "foobar is " + (/*x*/42/*y*/ + 6) + " years old"
goTo.select("x", "y");
edit.applyRefactor({
refactorName: "Convert string concatenation or template literal",
actionName: "Convert to template literal",
actionDescription: "Convert to template literal",
newContent:
`const foo = \`foobar is \${42 + 6} years old\``,
});

View file

@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />
//// const foo = "foobar is " + (/*x*/42/*y*/ + 6 + "str") + " years old"
goTo.select("x", "y");
edit.applyRefactor({
refactorName: "Convert string concatenation or template literal",
actionName: "Convert to template literal",
actionDescription: "Convert to template literal",
newContent:
`const foo = "foobar is " + (\`\${42 + 6}str\`) + " years old"`,
});