Skip to content

Commit cba0ddc

Browse files
committed
only show toString if expression is not binary
1 parent 1594468 commit cba0ddc

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/services/refactors/convertStringOrTemplateLiteral.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,28 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
1515
let node = getTokenAtPosition(file, startPosition);
1616
if (isParenthesizedExpression(node.parent) && isBinaryExpression(node.parent.parent)) node = node.parent.parent;
1717
const maybeBinary = getParentBinaryExpression(node);
18-
const maybeTemplateExpression = findAncestor(node, n => isTemplateExpression(n));
18+
// const maybeTemplateExpression = findAncestor(node, n => isTemplateExpression(n));
1919
const actions: RefactorActionInfo[] = [];
2020

2121
if ((isBinaryExpression(maybeBinary) || isStringLiteral(maybeBinary)) && isStringConcatenationValid(maybeBinary)) {
2222
actions.push({ name: toTemplateLiteralActionName, description: toTemplateLiteralDescription });
2323
}
2424

25-
if ((isNoSubstitutionTemplateLiteral(node) && !isTaggedTemplateExpression(node.parent)) || (maybeTemplateExpression && !isTaggedTemplateExpression(maybeTemplateExpression.parent))) {
25+
// if ((isNoSubstitutionTemplateLiteral(node) && !isTaggedTemplateExpression(node.parent)) || (maybeTemplateExpression && !isTaggedTemplateExpression(maybeTemplateExpression.parent))) {
26+
if (isTemplateLike(node)) {
2627
actions.push({ name: toStringConcatenationActionName, description: toStringConcatenationDescription });
2728
}
2829

2930
return [{ name: refactorName, description: refactorDescription, actions }];
3031
}
3132

33+
function isTemplateLike(node: Node) {
34+
const isEmptyTL = isNoSubstitutionTemplateLiteral(node) && !isTaggedTemplateExpression(node.parent);
35+
const is = (isTemplateHead(node) || isTemplateMiddleOrTemplateTail(node)) && !isTaggedTemplateExpression(node.parent.parent);
36+
const ise = (isTemplateSpan(node.parent)) && !isTaggedTemplateExpression(node.parent.parent.parent);
37+
return isEmptyTL || is || ise;
38+
}
39+
3240
function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
3341
const { file, startPosition } = context;
3442
let node = getTokenAtPosition(file, startPosition);

tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringAvailability.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
//// const age = 22
44
//// const name = "Eddy"
5-
//// const /*z*/f/*y*/oo = /*x*/`/*w*/M/*v*/r/*u*/ /*t*/$/*s*/{ /*r*/n/*q*/ame } is $/*p*/{/*o*/ age } years old`
5+
//// const /*z*/f/*y*/oo = /*x*/`/*w*/M/*v*/r/*u*/ /*t*/$/*s*/{ /*r*/n/*q*/ame } is ${ /*p*/a/*o*/ge + 34 } years old`
66

77
goTo.select("z", "y");
88
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
@@ -25,5 +25,5 @@ verify.refactorAvailable("Convert string concatenation or template literal", "Co
2525
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal");
2626

2727
goTo.select("p", "o");
28-
verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
28+
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
2929
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal");

0 commit comments

Comments
 (0)