Skip to content

Commit 3b28488

Browse files
committed
add toString visibility from expression and from middle part
1 parent 576271e commit 3b28488

4 files changed

+34
-10
lines changed

src/services/refactors/convertStringOrTemplateLiteral.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
99
const toStringConcatenationDescription = getLocaleSpecificMessage(Diagnostics.Convert_to_string_concatenation);
1010

1111
// TODO let a = 45 + 45 + " ee" + 33;
12+
// TODO let a = 45 - 45 + " ee" - 33;
1213
// TODO let a = tag `aaa`;
1314

1415
registerRefactor(refactorName, { getEditsForAction, getAvailableActions });
@@ -19,22 +20,17 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
1920
const maybeBinary = getParentBinaryExpression(node); containsString(maybeBinary);
2021
const actions: RefactorActionInfo[] = [];
2122

22-
if (!isTemplateLike(node) && (isBinaryExpression(maybeBinary) || isStringLiteral(maybeBinary)) && containsString(maybeBinary)) {
23+
if ((isBinaryExpression(maybeBinary) || isStringLiteral(maybeBinary)) && containsString(maybeBinary)) {
2324
actions.push({ name: toTemplateLiteralActionName, description: toTemplateLiteralDescription });
2425
}
2526

26-
27-
if (isTemplateLike(node)) {
27+
if (isNoSubstitutionTemplateLiteral(node) || isTemplateHead(node) || isTemplateSpan(node.parent)) {
2828
actions.push({ name: toStringConcatenationActionName, description: toStringConcatenationDescription });
2929
}
3030

3131
return [{ name: refactorName, description: refactorDescription, actions }];
3232
}
3333

34-
function isTemplateLike(node: Node): boolean {
35-
return isNoSubstitutionTemplateLiteral(node) || isTemplateHead(node) || isTemplateMiddleOrTemplateTail(node);
36-
}
37-
3834
function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
3935
const { file, startPosition } = context;
4036
const node = getTokenAtPosition(file, startPosition);
@@ -55,8 +51,8 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
5551
return { edits: textChanges.ChangeTracker.with(context, t => t.replaceNode(file, node, stringLiteral)) };
5652

5753
}
58-
if (isTemplateExpression(node.parent)) {
59-
const templateLiteralExpression = node.parent;
54+
if (isTemplateExpression(node.parent) || isTemplateSpan(node.parent)) {
55+
const templateLiteralExpression = isTemplateSpan(node.parent) ? node.parent.parent : node.parent;
6056
const nodesArray: Expression[] = [];
6157

6258
if (templateLiteralExpression.head.text.length !== 0) nodesArray.push(createStringLiteral(templateLiteralExpression.head.text));

tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringAvailability.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ verify.refactorAvailable("Convert string concatenation or template literal", "Co
2121
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal");
2222

2323
goTo.select("r", "q");
24-
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
24+
verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
2525
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal");
2626

2727
goTo.select("p", "o");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const age = 42
4+
//// const foo = `foobar is ${ /*x*/a/*y*/ge } years old`
5+
6+
goTo.select("x", "y");
7+
edit.applyRefactor({
8+
refactorName: "Convert string concatenation or template literal",
9+
actionName: "Convert to string concatenation",
10+
actionDescription: "Convert to string concatenation",
11+
newContent:
12+
`const age = 42
13+
const foo = "foobar is " + age + " years old"`,
14+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const age = 42
4+
//// const foo = `foobar is ${ age } /*x*/y/*y*/ears old ${ false }`
5+
6+
goTo.select("x", "y");
7+
edit.applyRefactor({
8+
refactorName: "Convert string concatenation or template literal",
9+
actionName: "Convert to string concatenation",
10+
actionDescription: "Convert to string concatenation",
11+
newContent:
12+
`const age = 42
13+
const foo = "foobar is " + age + " years old " + false`,
14+
});

0 commit comments

Comments
 (0)