copy comments from template literal to string

This commit is contained in:
BigAru
2019-03-27 17:46:21 +01:00
parent 9fa112ee34
commit 17f3861699
5 changed files with 61 additions and 7 deletions

View File

@@ -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;
}

View File

@@ -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"`,
});

View File

@@ -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 */`,
});

View File

@@ -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 */`,
});

View File

@@ -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 */`,
});