Skip to content

Commit 1594468

Browse files
committed
add possibility to invoke from parentheses
1 parent 74e3cd7 commit 1594468

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/services/refactors/convertStringOrTemplateLiteral.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
1212

1313
function getAvailableActions(context: RefactorContext): ReadonlyArray<ApplicableRefactorInfo> {
1414
const { file, startPosition } = context;
15-
const node = getTokenAtPosition(file, startPosition);
16-
const maybeBinary = getParentBinaryExpression(node); containsString(maybeBinary);
15+
let node = getTokenAtPosition(file, startPosition);
16+
if (isParenthesizedExpression(node.parent) && isBinaryExpression(node.parent.parent)) node = node.parent.parent;
17+
const maybeBinary = getParentBinaryExpression(node);
1718
const maybeTemplateExpression = findAncestor(node, n => isTemplateExpression(n));
1819
const actions: RefactorActionInfo[] = [];
1920

@@ -30,11 +31,13 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
3031

3132
function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
3233
const { file, startPosition } = context;
33-
const node = getTokenAtPosition(file, startPosition);
34+
let node = getTokenAtPosition(file, startPosition);
3435

3536
switch (actionName) {
3637
case toTemplateLiteralActionName:
38+
if (isParenthesizedExpression(node.parent) && isBinaryExpression(node.parent.parent)) node = node.parent.parent;
3739
const maybeBinary = getParentBinaryExpression(node);
40+
3841
const arrayOfNodes = treeToArray(maybeBinary)[0];
3942
const templateLiteral = nodesToTemplate(arrayOfNodes);
4043
const edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, maybeBinary, templateLiteral));
@@ -78,15 +81,6 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
7881
return expr;
7982
}
8083

81-
function containsString(node: Node): boolean {
82-
if (isBinaryExpression(node)) {
83-
return containsString(node.left) || containsString(node.right);
84-
}
85-
86-
if (isStringLiteral(node)) return true;
87-
return false;
88-
}
89-
9084
function arrayToTree(nodes: Expression[], bexpr: BinaryExpression | undefined): BinaryExpression {
9185
if (nodes.length === 0) return bexpr!;
9286

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const foo = "foobar is " + /*x*/(/*y*/42 + 6) + " years old"
4+
5+
goTo.select("x", "y");
6+
edit.applyRefactor({
7+
refactorName: "Convert string concatenation or template literal",
8+
actionName: "Convert to template literal",
9+
actionDescription: "Convert to template literal",
10+
newContent:
11+
`const foo = \`foobar is \${42 + 6} years old\``,
12+
});

0 commit comments

Comments
 (0)