mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-16 15:51:35 -05:00
add possibility to invoke from parentheses
This commit is contained in:
@@ -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!;
|
||||
|
||||
|
||||
@@ -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\``,
|
||||
});
|
||||
Reference in New Issue
Block a user