Skip to content

Commit 17f3861

Browse files
committed
copy comments from template literal to string
1 parent 9fa112e commit 17f3861

5 files changed

+61
-7
lines changed

src/services/refactors/convertStringOrTemplateLiteral.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,20 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
6969
return textChanges.ChangeTracker.with(context, t => t.replaceNode(context.file, maybeBinary, templateLiteral));
7070
}
7171

72+
const templateSpanToExpressions = (file: SourceFile) => (templateSpan: TemplateSpan): Expression[] => {
73+
const { expression, literal } = templateSpan;
74+
const text = literal.text;
75+
copyTrailingAsLeadingComments(templateSpan, expression, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false);
76+
return text.length === 0 ? [expression] : [expression, createStringLiteral(text)];
77+
};
78+
7279
function getEditsForToStringConcatenation(context: RefactorContext, node: Node) {
7380
const templateLiteral = findAncestor(node, n => isTemplateLiteral(n))! as TemplateLiteral;
7481

7582
if (isTemplateExpression(templateLiteral)) {
7683
const { head, templateSpans } = templateLiteral;
77-
const arrayOfNodes = templateSpans.map(templateSpanToExpressions)
84+
const spanToExpressionWithComment = templateSpanToExpressions(context.file);
85+
const arrayOfNodes = templateSpans.map(spanToExpressionWithComment)
7886
.reduce((accumulator, nextArray) => accumulator.concat(nextArray));
7987

8088
if (head.text.length !== 0) arrayOfNodes.unshift(createStringLiteral(head.text));
@@ -88,12 +96,6 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
8896
}
8997
}
9098

91-
function templateSpanToExpressions(templateSpan: TemplateSpan): Expression[] {
92-
const { expression, literal } = templateSpan;
93-
const text = literal.text;
94-
return text.length === 0 ? [expression] : [expression, createStringLiteral(text)];
95-
}
96-
9799
function isNotEqualsOperator(node: BinaryExpression) {
98100
return node.operatorToken.kind !== SyntaxKind.EqualsToken;
99101
}
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 = `/*x*/H/*y*/EAD ${ /* C0 */ 42 /* C1 */} Span1 ${ /* C2 */ 43 /* C3 */} Span2 ${ /* C4 */ 44 /* C5 */} Span3`
4+
5+
goTo.select("x", "y");
6+
edit.applyRefactor({
7+
refactorName: "Convert string concatenation or template literal",
8+
actionName: "Convert to string concatenation",
9+
actionDescription: "Convert to string concatenation",
10+
newContent:
11+
`const foo = "HEAD " + /* C0 */ 42 /* C1 */ + " Span1 " + /* C2 */ 43 /* C3 */ + " Span2 " + /* C4 */ 44 /* C5 */ + " Span3"`,
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 = /*x*/`/*y*/${/* C0 */ 42 /* C1 */}`
4+
5+
goTo.select("x", "y");
6+
edit.applyRefactor({
7+
refactorName: "Convert string concatenation or template literal",
8+
actionName: "Convert to string concatenation",
9+
actionDescription: "Convert to string concatenation",
10+
newContent:
11+
`const foo = /* C0 */ 42 /* C1 */`,
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 = `/*x*/H/*y*/EAD | ${ /* C0 */ 42 /* C1 */}${ /* C2 */ 43 /* C3 */}${ /* C4 */ 44 /* C5 */}`
4+
5+
goTo.select("x", "y");
6+
edit.applyRefactor({
7+
refactorName: "Convert string concatenation or template literal",
8+
actionName: "Convert to string concatenation",
9+
actionDescription: "Convert to string concatenation",
10+
newContent:
11+
`const foo = "HEAD | " + /* C0 */ 42 /* C1 */ + /* C2 */ 43 /* C3 */ + /* C4 */ 44 /* C5 */`,
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 = /* C0 */ /*x*/"/*y*/foobar" /* C1 */ + " is" /* C2 */ + 42 /* C3 */ + "years old" /* C4 */
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 = /* C0 */ \`foobar is \${ /* C1 */ /* C2 */ 42 /* C3 */}years old\` /* C4 */`,
12+
});
13+

0 commit comments

Comments
 (0)