only show toString if expression is not binary

This commit is contained in:
BigAru
2018-12-07 13:37:38 +01:00
parent 1594468250
commit cba0ddcbde
2 changed files with 12 additions and 4 deletions
@@ -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,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");