diff --git a/src/services/refactors/convertStringOrTemplateLiteral.ts b/src/services/refactors/convertStringOrTemplateLiteral.ts index 0036fc9f68d..e63ab98e6e5 100644 --- a/src/services/refactors/convertStringOrTemplateLiteral.ts +++ b/src/services/refactors/convertStringOrTemplateLiteral.ts @@ -3,6 +3,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral { const refactorName = "Convert string concatenation or template literal"; const toTemplateLiteralActionName = "Convert to template literal"; // const toStringConcatenationActionName = "Convert to string concatenation"; + // let str = ""; str; const refactorDescription = getLocaleSpecificMessage(Diagnostics.Convert_string_concatenation_or_template_literal); const toTemplateLiteralDescription = getLocaleSpecificMessage(Diagnostics.Convert_to_template_literal); @@ -11,8 +12,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral { registerRefactor(refactorName, { getEditsForAction, getAvailableActions }); function getAvailableActions(context: RefactorContext): ReadonlyArray { - const { file, startPosition } = context; file; startPosition; - + const { file, startPosition } = context; const node = getTokenAtPosition(file, startPosition); const maybeBinary = getParentBinaryExpression(node); containsString(maybeBinary); if (!(isBinaryExpression(maybeBinary) || isStringLiteral(maybeBinary)) || !containsString(maybeBinary)) return emptyArray; @@ -34,24 +34,23 @@ namespace ts.refactor.convertStringOrTemplateLiteral { } function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined { - const { file, startPosition } = context; file; startPosition; actionName; + const { file, startPosition } = context; const node = getTokenAtPosition(file, startPosition); - switch (actionName) { case toTemplateLiteralActionName: const maybeBinary = getParentBinaryExpression(node); let templateLiteral: TemplateExpression | NoSubstitutionTemplateLiteral; if (isStringLiteral(maybeBinary)) { - templateLiteral = createNoSubstitutionTemplateLiteral(maybeBinary.text); + templateLiteral = createNoSubstitutionTemplateLiteral(cleanString(maybeBinary.text)); } else { - const arrayOfNodes = treeToArray(maybeBinary); arrayOfNodes; + const arrayOfNodes = treeToArray(maybeBinary); templateLiteral = nodesToTemplate(arrayOfNodes); } const edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, maybeBinary, templateLiteral)); - return {edits}; + return { edits }; default: return Debug.fail("invalid action"); @@ -76,7 +75,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral { function treeToArray(node: Node): Node[] { if (isBinaryExpression(node)) { - return treeToArray(node.left).concat(treeToArray(node.right)) + return treeToArray(node.left).concat(treeToArray(node.right)); } return [node]; } @@ -88,46 +87,56 @@ namespace ts.refactor.convertStringOrTemplateLiteral { const firstNode = nodes[0]; const spans: TemplateSpan[] = []; - if (isStringLiteral(firstNode)){ + if (isStringLiteral(firstNode)) { head.text = firstNode.text; begin++; - while(begin < nodes.length && isStringLiteral(nodes[begin])){ + while (begin < nodes.length && isStringLiteral(nodes[begin])) { - let next = nodes[begin] as StringLiteral; + const next = nodes[begin] as StringLiteral; head.text = head.text + next.text; begin++; } + + head.text = cleanString(head.text); } - if(begin === nodes.length){ + if (begin === nodes.length) { return createNoSubstitutionTemplateLiteral(head.text); } - for(let i = begin; i < nodes.length; i++){ - const current = nodes[i]; + for (let i = begin; i < nodes.length; i++) { + let current = nodes[i]; + let templatePart: TemplateMiddle | TemplateTail; - if (i+1 < nodes.length && isStringLiteral(nodes[i+1])){ - let next = nodes[i+1] as StringLiteral; + if (i + 1 < nodes.length && isStringLiteral(nodes[i + 1])) { + let next = nodes[i + 1] as StringLiteral; let text = next.text; i++; - while(i+1 < nodes.length && isStringLiteral(nodes[i+1])){ - next = nodes[i+1] as StringLiteral; + while (i + 1 < nodes.length && isStringLiteral(nodes[i + 1])) { + next = nodes[i + 1] as StringLiteral; text = text + next.text; i++; } - const templatePart = i === nodes.length-1 ? createTemplateTail(text) : createTemplateMiddle(text); - spans.push(createTemplateSpan(current as Expression, templatePart)); + if (isParenthesizedExpression(current)) current = current.expression; + text = cleanString(text); + + templatePart = i === nodes.length - 1 ? createTemplateTail(text) : createTemplateMiddle(text); } else { - const templatePart = i === nodes.length-1 ? createTemplateTail("") : createTemplateMiddle(""); - spans.push(createTemplateSpan(current as Expression, templatePart)); + templatePart = i === nodes.length - 1 ? createTemplateTail("") : createTemplateMiddle(""); } + + spans.push(createTemplateSpan(current as Expression, templatePart)); } return createTemplateExpression(head, spans); } + function cleanString(content: string) { + return content.replace("`", "\`").replace("\${", `$\\{`); + } + } diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringAvailability.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringAvailability.ts index 0ebde2b46ad..439b5cd80e7 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringAvailability.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringAvailability.ts @@ -1,30 +1,30 @@ -/// - -//// const age = 22 -//// const name = "Eddy" -//// const /*z*/f/*y*/oo = /*x*/`/*w*/M/*v*/r/*u*/ /*t*/$/*s*/{ /*r*/n/*q*/ame } is $/*p*/{/*o*/ age } years old` - -goTo.select("z", "y"); -verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") -verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") - -goTo.select("x", "w"); -verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") -verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") - -goTo.select("v", "u"); -verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") -verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") - -goTo.select("t", "s"); -verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") -verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") - -goTo.select("r", "q"); -verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") -verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") - -goTo.select("p", "o"); -verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") -verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") - +/// + +//// const age = 22 +//// const name = "Eddy" +//// const /*z*/f/*y*/oo = /*x*/`/*w*/M/*v*/r/*u*/ /*t*/$/*s*/{ /*r*/n/*q*/ame } is $/*p*/{/*o*/ age } years old` + +goTo.select("z", "y"); +verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") +verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") + +goTo.select("x", "w"); +verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") +verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") + +goTo.select("v", "u"); +verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") +verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") + +goTo.select("t", "s"); +verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") +verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") + +goTo.select("r", "q"); +verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") +verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") + +goTo.select("p", "o"); +verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") +verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") + diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringBackTick.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringBackTick.ts index 5025c5df7c7..6587f7037f0 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringBackTick.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringBackTick.ts @@ -1,12 +1,12 @@ -/// - -//// const foo = `/*x*/w/*y*/ith back\`tick` - -goTo.select("x", "y"); -edit.applyRefactor({ - refactorName: "Convert string concatenation or template literal", - actionName: "Convert to string concatenation", - actionDescription: "Convert to string concatenation", - newContent: -"const foo = \"with back`tick\"", -}); +/// + +//// const foo = `/*x*/w/*y*/ith back\`tick` + +goTo.select("x", "y"); +edit.applyRefactor({ + refactorName: "Convert string concatenation or template literal", + actionName: "Convert to string concatenation", + actionDescription: "Convert to string concatenation", + newContent: +"const foo = \"with back`tick\"", +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringBinaryExpr.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringBinaryExpr.ts index b545b7861c3..1850b40f22c 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringBinaryExpr.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringBinaryExpr.ts @@ -1,12 +1,12 @@ -/// - -//// const foo = `/*x*/f/*y*/oobar is ${ 42 + 6 } years old` - -goTo.select("x", "y"); -edit.applyRefactor({ - refactorName: "Convert string concatenation or template literal", - actionName: "Convert to string concatenation", - actionDescription: "Convert to string concatenation", - newContent: -`const foo = "foobar is " + (42 + 6) + " years old"`, -}); +/// + +//// const foo = `/*x*/f/*y*/oobar is ${ 42 + 6 } years old` + +goTo.select("x", "y"); +edit.applyRefactor({ + refactorName: "Convert string concatenation or template literal", + actionName: "Convert to string concatenation", + actionDescription: "Convert to string concatenation", + newContent: +`const foo = "foobar is " + (42 + 6) + " years old"`, +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringDollar.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringDollar.ts index 1629f7e7df9..3b19470d7a7 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringDollar.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringDollar.ts @@ -1,12 +1,12 @@ -/// - -//// const foo = `/*x*/w/*y*/ith \${dollar}` - -goTo.select("x", "y"); -edit.applyRefactor({ - refactorName: "Convert string concatenation or template literal", - actionName: "Convert to string concatenation", - actionDescription: "Convert to string concatenation", - newContent: -"const foo = \"with ${dollar}\"", -}); +/// + +//// const foo = `/*x*/w/*y*/ith \${dollar}` + +goTo.select("x", "y"); +edit.applyRefactor({ + refactorName: "Convert string concatenation or template literal", + actionName: "Convert to string concatenation", + actionDescription: "Convert to string concatenation", + newContent: +"const foo = \"with ${dollar}\"", +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringExprInRow.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringExprInRow.ts index 93a3897f67c..4b8e581b8c6 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringExprInRow.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringExprInRow.ts @@ -1,12 +1,12 @@ -/// - -//// const foo = `/*x*/f/*y*/oobar is ${ 42 }${ 6 } years old` - -goTo.select("x", "y"); -edit.applyRefactor({ - refactorName: "Convert string concatenation or template literal", - actionName: "Convert to string concatenation", - actionDescription: "Convert to string concatenation", - newContent: -`const foo = "foobar is " + 42 + 6 + " years old"`, -}); +/// + +//// const foo = `/*x*/f/*y*/oobar is ${ 42 }${ 6 } years old` + +goTo.select("x", "y"); +edit.applyRefactor({ + refactorName: "Convert string concatenation or template literal", + actionName: "Convert to string concatenation", + actionDescription: "Convert to string concatenation", + newContent: +`const foo = "foobar is " + 42 + 6 + " years old"`, +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringMultiExpr.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringMultiExpr.ts index 7f4255b6ef4..79f624118d7 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringMultiExpr.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringMultiExpr.ts @@ -1,16 +1,16 @@ -/// - -//// const age = 22 -//// const name = "Eddy" -//// const foo = `${ /*x*/n/*y*/ame } is ${ age } years old` - -goTo.select("x", "y"); -edit.applyRefactor({ - refactorName: "Convert string concatenation or template literal", - actionName: "Convert to string concatenation", - actionDescription: "Convert to string concatenation", - newContent: -`const age = 22 -const name = "Eddy" -const foo = name + " is " + age + " years old"`, -}); +/// + +//// const age = 22 +//// const name = "Eddy" +//// const foo = `${ /*x*/n/*y*/ame } is ${ age } years old` + +goTo.select("x", "y"); +edit.applyRefactor({ + refactorName: "Convert string concatenation or template literal", + actionName: "Convert to string concatenation", + actionDescription: "Convert to string concatenation", + newContent: +`const age = 22 +const name = "Eddy" +const foo = name + " is " + age + " years old"`, +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringNested.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringNested.ts index f3240a10b92..d7a8f52b374 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringNested.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringNested.ts @@ -1,14 +1,14 @@ -/// - -//// const age = 42 -//// const foo = `/*x*/f/*y*/oobar is a ${ age < 18 ? 'child' : `grown-up ${ age > 40 ? 'who needs probaply assistance': ''}` }` - -goTo.select("x", "y"); -edit.applyRefactor({ - refactorName: "Convert string concatenation or template literal", - actionName: "Convert to string concatenation", - actionDescription: "Convert to string concatenation", - newContent: -`const age = 42 -const foo = "foobar is a " + ( age < 18 ? 'child' : \`grown-up \${ age > 40 ? 'who needs probaply assistance': ''}\` ) `, -}); +/// + +//// const age = 42 +//// const foo = `/*x*/f/*y*/oobar is a ${ age < 18 ? 'child' : `grown-up ${ age > 40 ? 'who needs probaply assistance': ''}` }` + +goTo.select("x", "y"); +edit.applyRefactor({ + refactorName: "Convert string concatenation or template literal", + actionName: "Convert to string concatenation", + actionDescription: "Convert to string concatenation", + newContent: +`const age = 42 +const foo = "foobar is a " + ( age < 18 ? 'child' : \`grown-up \${ age > 40 ? 'who needs probaply assistance': ''}\` ) `, +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringOneExpr.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringOneExpr.ts index a6bf5c4d192..0e5cb6ae257 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringOneExpr.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringOneExpr.ts @@ -1,14 +1,14 @@ -/// - -//// const age = 42 -//// const foo = `/*x*/f/*y*/oobar is ${ age } years old` - -goTo.select("x", "y"); -edit.applyRefactor({ - refactorName: "Convert string concatenation or template literal", - actionName: "Convert to string concatenation", - actionDescription: "Convert to string concatenation", - newContent: -`const age = 42 -const foo = "foobar is " + age + " years old"`, -}); +/// + +//// const age = 42 +//// const foo = `/*x*/f/*y*/oobar is ${ age } years old` + +goTo.select("x", "y"); +edit.applyRefactor({ + refactorName: "Convert string concatenation or template literal", + actionName: "Convert to string concatenation", + actionDescription: "Convert to string concatenation", + newContent: +`const age = 42 +const foo = "foobar is " + age + " years old"`, +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringSimple.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringSimple.ts index 7c6496be7b5..3da488fcf25 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringSimple.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringSimple.ts @@ -1,12 +1,12 @@ -/// - -//// const foo = `/*x*/f/*y*/oobar rocks` - -goTo.select("x", "y"); -edit.applyRefactor({ - refactorName: "Convert string concatenation or template literal", - actionName: "Convert to string concatenation", - actionDescription: "Convert to string concatenation", - newContent: -`const foo = "foobar rocks"`, -}); +/// + +//// const foo = `/*x*/f/*y*/oobar rocks` + +goTo.select("x", "y"); +edit.applyRefactor({ + refactorName: "Convert string concatenation or template literal", + actionName: "Convert to string concatenation", + actionDescription: "Convert to string concatenation", + newContent: +`const foo = "foobar rocks"`, +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateAvailability.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateAvailability.ts index ab26de51270..fa58f01392d 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateAvailability.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateAvailability.ts @@ -1,30 +1,30 @@ -/// - -//// const age = 22 -//// const name = "Eddy" -//// const /*z*/f/*y*/oo = /*x*/"/*w*/M/*v*/r/*u*/ " /*t*/+/*s*/ /*r*/n/*q*/ame + " is " + /*p*/a/*o*/ge + " years old" - -goTo.select("z", "y"); -verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") -verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") - -goTo.select("x", "w"); -verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") -verify.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") - -goTo.select("v", "u"); -verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") -verify.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") - -goTo.select("t", "s"); -verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") -verify.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") - -goTo.select("r", "q"); -verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") -verify.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") - -goTo.select("p", "o"); -verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation") -verify.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal") - +/// + +//// const age = 22 +//// const name = "Eddy" +//// const /*z*/f/*y*/oo = /*x*/"/*w*/M/*v*/r/*u*/ " /*t*/+/*s*/ /*r*/n/*q*/ame + " is " + /*p*/a/*o*/ge + " years old" + +goTo.select("z", "y"); +verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation"); +verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal"); + +goTo.select("x", "w"); +verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation"); +verify.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal"); + +goTo.select("v", "u"); +verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation"); +verify.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal"); + +goTo.select("t", "s"); +verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation"); +verify.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal"); + +goTo.select("r", "q"); +verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation"); +verify.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal"); + +goTo.select("p", "o"); +verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation"); +verify.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal"); + diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateBackTick.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateBackTick.ts index 8c7b2a93833..205f84c19a1 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateBackTick.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateBackTick.ts @@ -1,12 +1,12 @@ -/// - -//// const foo = "/*x*/w/*y*/ith back`tick" - -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 = `with back\`tick`", -}); +/// + +//// const foo = "/*x*/w/*y*/ith back`tick" + +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 = `with back\\`tick`", +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateBinaryExpr.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateBinaryExpr.ts index 54b3f985910..2519ac34664 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateBinaryExpr.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateBinaryExpr.ts @@ -1,12 +1,12 @@ -/// - -//// const foo = "/*x*/f/*y*/oobar is " + (42 + 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\``, -}); +/// + +//// const foo = "/*x*/f/*y*/oobar is " + (42 + 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\``, +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateConsecutiveStr.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateConsecutiveStr.ts new file mode 100644 index 00000000000..a053a6c5e3b --- /dev/null +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateConsecutiveStr.ts @@ -0,0 +1,12 @@ +/// + +//// const foo = "/*x*/f/*y*/oobar is " + 42 + " years" + " old" + " and " + 6 + " cars" + " are" + " missing" + +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} years old and \${6} cars are missing\``, +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateDollar.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateDollar.ts index 884b3020235..bfad8d47cd1 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateDollar.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateDollar.ts @@ -1,12 +1,12 @@ -/// - -//// const foo = "/*x*/w/*y*/ith ${dollar}" - -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 = `with \${dollar}`", -}); +/// + +//// const foo = "/*x*/w/*y*/ith ${dollar}" + +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 = `with $\\{dollar}`", +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateExprInRow.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateExprInRow.ts index 1c3af854309..aadc6068a68 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateExprInRow.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateExprInRow.ts @@ -1,12 +1,12 @@ -/// - -//// const foo = "/*x*/f/*y*/oobar is " + 42 + 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\``, -}); +/// + +//// const foo = "/*x*/f/*y*/oobar is " + 42 + 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\``, +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateMultiExpr.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateMultiExpr.ts index 6aa095d67d3..e781bae6634 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateMultiExpr.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateMultiExpr.ts @@ -1,16 +1,16 @@ -/// - -//// const age = 22 -//// const name = "Eddy" -//// const foo = /*x*/n/*y*/ame + " is " + age + " 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 age = 22 -const name = "Eddy" -const foo = \`\${ name } is \${ age } years old\``, -}); +/// + +//// const age = 22 +//// const name = "Eddy" +//// const foo = /*x*/n/*y*/ame + " is " + age + " 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 age = 22 +const name = "Eddy" +const foo = \`\${name} is \${age} years old\``, +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateMultiLines.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateMultiLines.ts index e58111ebfb2..94cd3202512 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateMultiLines.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateMultiLines.ts @@ -1,18 +1,18 @@ -/// - -//// const foo = "/*x*/w/*y*/ait for others\n" -//// + "D'oh!\n" -//// + ""Yada, yada, yada\n" -//// + "Hasta la vista, baby!" - -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 = \`wait for others -D'oh! -Yada, yada, yada -Hasta la vista, baby!\``, -}); +/// + +//// const foo = "/*x*/w/*y*/ait for others\n" +//// + "D'oh!\n" +//// + ""Yada, yada, yada\n" +//// + "Hasta la vista, baby!" + +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 = \`wait for others +D'oh! +Yada, yada, yada +Hasta la vista, baby!\``, +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateNewLine.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateNewLine.ts index 577c1de4efb..836b06f98d4 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateNewLine.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateNewLine.ts @@ -1,14 +1,14 @@ -/// - -//// const foo = "/*x*/w/*y*/ait for new line\n" -//// + "bada bum!" - -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 = \`wait for new line -bada bum!\``, -}); +/// + +//// const foo = "/*x*/w/*y*/ait for new line\n" +//// + "bada bum!" + +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 = \`wait for new line +bada bum!\``, +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateOneExpr.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateOneExpr.ts index 8899f99c864..a790a623aaa 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateOneExpr.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateOneExpr.ts @@ -1,14 +1,14 @@ -/// - -//// const age = 42 -//// const foo = "/*x*/f/*y*/oobar is " + age + " 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 age = 42 -const foo = \`foobar is \${ age } years old\``, -}); +/// + +//// const age = 42 +//// const foo = "/*x*/f/*y*/oobar is " + age + " 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 age = 42 +const foo = \`foobar is \${age} years old\``, +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateOnlyStr.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateOnlyStr.ts new file mode 100644 index 00000000000..bd1fa6009fc --- /dev/null +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateOnlyStr.ts @@ -0,0 +1,12 @@ +/// + +//// const foo = "/*x*/f/*y*/oobar " + "rocks" + " fantastically" + +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 rocks fantastically`", +}); diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateSimple.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateSimple.ts index 35b3e17519d..2c5663b07dc 100644 --- a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateSimple.ts +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateSimple.ts @@ -1,12 +1,12 @@ -/// - -//// const foo = "/*x*/f/*y*/oobar rocks" - -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 rocks\``, -}); +/// + +//// const foo = "/*x*/f/*y*/oobar rocks" + +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 rocks\``, +});