add possibility to invoke from parentheses

This commit is contained in:
BigAru
2018-12-07 12:58:35 +01:00
parent 74e3cd758e
commit 1594468250
2 changed files with 18 additions and 12 deletions

View File

@@ -12,8 +12,9 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
function getAvailableActions(context: RefactorContext): ReadonlyArray<ApplicableRefactorInfo> {
const { file, startPosition } = context;
const node = getTokenAtPosition(file, startPosition);
const maybeBinary = getParentBinaryExpression(node); containsString(maybeBinary);
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 actions: RefactorActionInfo[] = [];
@@ -30,11 +31,13 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
const { file, startPosition } = context;
const node = getTokenAtPosition(file, startPosition);
let node = getTokenAtPosition(file, startPosition);
switch (actionName) {
case toTemplateLiteralActionName:
if (isParenthesizedExpression(node.parent) && isBinaryExpression(node.parent.parent)) node = node.parent.parent;
const maybeBinary = getParentBinaryExpression(node);
const arrayOfNodes = treeToArray(maybeBinary)[0];
const templateLiteral = nodesToTemplate(arrayOfNodes);
const edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, maybeBinary, templateLiteral));
@@ -78,15 +81,6 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
return expr;
}
function containsString(node: Node): boolean {
if (isBinaryExpression(node)) {
return containsString(node.left) || containsString(node.right);
}
if (isStringLiteral(node)) return true;
return false;
}
function arrayToTree(nodes: Expression[], bexpr: BinaryExpression | undefined): BinaryExpression {
if (nodes.length === 0) return bexpr!;

View File

@@ -0,0 +1,12 @@
/// <reference path='fourslash.ts' />
//// const foo = "foobar is " + /*x*/(/*y*/42 + 6) + " years old"
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 = \`foobar is \${42 + 6} years old\``,
});