mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
only show toString if expression is not binary
This commit is contained in:
@@ -15,20 +15,28 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
|
||||
let node = getTokenAtPosition(file, startPosition);
|
||||
if (isParenthesizedExpression(node.parent) && isBinaryExpression(node.parent.parent)) node = node.parent.parent;
|
||||
const maybeBinary = getParentBinaryExpression(node);
|
||||
const maybeTemplateExpression = findAncestor(node, n => isTemplateExpression(n));
|
||||
// const maybeTemplateExpression = findAncestor(node, n => isTemplateExpression(n));
|
||||
const actions: RefactorActionInfo[] = [];
|
||||
|
||||
if ((isBinaryExpression(maybeBinary) || isStringLiteral(maybeBinary)) && isStringConcatenationValid(maybeBinary)) {
|
||||
actions.push({ name: toTemplateLiteralActionName, description: toTemplateLiteralDescription });
|
||||
}
|
||||
|
||||
if ((isNoSubstitutionTemplateLiteral(node) && !isTaggedTemplateExpression(node.parent)) || (maybeTemplateExpression && !isTaggedTemplateExpression(maybeTemplateExpression.parent))) {
|
||||
// if ((isNoSubstitutionTemplateLiteral(node) && !isTaggedTemplateExpression(node.parent)) || (maybeTemplateExpression && !isTaggedTemplateExpression(maybeTemplateExpression.parent))) {
|
||||
if (isTemplateLike(node)) {
|
||||
actions.push({ name: toStringConcatenationActionName, description: toStringConcatenationDescription });
|
||||
}
|
||||
|
||||
return [{ name: refactorName, description: refactorDescription, actions }];
|
||||
}
|
||||
|
||||
function isTemplateLike(node: Node) {
|
||||
const isEmptyTL = isNoSubstitutionTemplateLiteral(node) && !isTaggedTemplateExpression(node.parent);
|
||||
const is = (isTemplateHead(node) || isTemplateMiddleOrTemplateTail(node)) && !isTaggedTemplateExpression(node.parent.parent);
|
||||
const ise = (isTemplateSpan(node.parent)) && !isTaggedTemplateExpression(node.parent.parent.parent);
|
||||
return isEmptyTL || is || ise;
|
||||
}
|
||||
|
||||
function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
|
||||
const { file, startPosition } = context;
|
||||
let node = getTokenAtPosition(file, startPosition);
|
||||
|
||||
+2
-2
@@ -2,7 +2,7 @@
|
||||
|
||||
//// const age = 22
|
||||
//// const name = "Eddy"
|
||||
//// const /*z*/f/*y*/oo = /*x*/`/*w*/M/*v*/r/*u*/ /*t*/$/*s*/{ /*r*/n/*q*/ame } is $/*p*/{/*o*/ age } years old`
|
||||
//// 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`
|
||||
|
||||
goTo.select("z", "y");
|
||||
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
|
||||
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal");
|
||||
|
||||
goTo.select("p", "o");
|
||||
verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
|
||||
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation");
|
||||
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal");
|
||||
|
||||
Reference in New Issue
Block a user