diff --git a/src/services/refactors/convertStringOrTemplateLiteral.ts b/src/services/refactors/convertStringOrTemplateLiteral.ts
index d1bb86121a4..fb8be410684 100644
--- a/src/services/refactors/convertStringOrTemplateLiteral.ts
+++ b/src/services/refactors/convertStringOrTemplateLiteral.ts
@@ -69,12 +69,20 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
return textChanges.ChangeTracker.with(context, t => t.replaceNode(context.file, maybeBinary, templateLiteral));
}
+ const templateSpanToExpressions = (file: SourceFile) => (templateSpan: TemplateSpan): Expression[] => {
+ const { expression, literal } = templateSpan;
+ const text = literal.text;
+ copyTrailingAsLeadingComments(templateSpan, expression, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false);
+ return text.length === 0 ? [expression] : [expression, createStringLiteral(text)];
+ };
+
function getEditsForToStringConcatenation(context: RefactorContext, node: Node) {
const templateLiteral = findAncestor(node, n => isTemplateLiteral(n))! as TemplateLiteral;
if (isTemplateExpression(templateLiteral)) {
const { head, templateSpans } = templateLiteral;
- const arrayOfNodes = templateSpans.map(templateSpanToExpressions)
+ const spanToExpressionWithComment = templateSpanToExpressions(context.file);
+ const arrayOfNodes = templateSpans.map(spanToExpressionWithComment)
.reduce((accumulator, nextArray) => accumulator.concat(nextArray));
if (head.text.length !== 0) arrayOfNodes.unshift(createStringLiteral(head.text));
@@ -88,12 +96,6 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
}
}
- function templateSpanToExpressions(templateSpan: TemplateSpan): Expression[] {
- const { expression, literal } = templateSpan;
- const text = literal.text;
- return text.length === 0 ? [expression] : [expression, createStringLiteral(text)];
- }
-
function isNotEqualsOperator(node: BinaryExpression) {
return node.operatorToken.kind !== SyntaxKind.EqualsToken;
}
diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringComment.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringComment.ts
new file mode 100644
index 00000000000..123236d948f
--- /dev/null
+++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringComment.ts
@@ -0,0 +1,13 @@
+///
+
+//// const foo = `/*x*/H/*y*/EAD ${ /* C0 */ 42 /* C1 */} Span1 ${ /* C2 */ 43 /* C3 */} Span2 ${ /* C4 */ 44 /* C5 */} Span3`
+
+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 = "HEAD " + /* C0 */ 42 /* C1 */ + " Span1 " + /* C2 */ 43 /* C3 */ + " Span2 " + /* C4 */ 44 /* C5 */ + " Span3"`,
+});
+
diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringCommentOnlyExpr.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringCommentOnlyExpr.ts
new file mode 100644
index 00000000000..8f85a76b7e1
--- /dev/null
+++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringCommentOnlyExpr.ts
@@ -0,0 +1,13 @@
+///
+
+//// const foo = /*x*/`/*y*/${/* C0 */ 42 /* C1 */}`
+
+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 = /* C0 */ 42 /* C1 */`,
+});
+
diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringCommentWithoutStr.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringCommentWithoutStr.ts
new file mode 100644
index 00000000000..8a06197b782
--- /dev/null
+++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringCommentWithoutStr.ts
@@ -0,0 +1,13 @@
+///
+
+//// const foo = `/*x*/H/*y*/EAD | ${ /* C0 */ 42 /* C1 */}${ /* C2 */ 43 /* C3 */}${ /* C4 */ 44 /* C5 */}`
+
+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 = "HEAD | " + /* C0 */ 42 /* C1 */ + /* C2 */ 43 /* C3 */ + /* C4 */ 44 /* C5 */`,
+});
+
diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateComment.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateComment.ts
new file mode 100644
index 00000000000..617e95b86e7
--- /dev/null
+++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToTemplateComment.ts
@@ -0,0 +1,13 @@
+///
+
+//// const foo = /* C0 */ /*x*/"/*y*/foobar" /* C1 */ + " is" /* C2 */ + 42 /* C3 */ + "years old" /* C4 */
+
+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 = /* C0 */ \`foobar is \${ /* C1 */ /* C2 */ 42 /* C3 */}years old\` /* C4 */`,
+});
+