mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-16 15:51:35 -05:00
copy comments from template literal to string
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
//// 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"`,
|
||||
});
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
//// 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 */`,
|
||||
});
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
//// 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 */`,
|
||||
});
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
//// 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 */`,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user