Skip to content

Commit ad0614a

Browse files
committed
extract getEdits for string concatenation
1 parent 08ed6cf commit ad0614a

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

src/services/refactors/convertStringOrTemplateLiteral.ts

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

1313
function getAvailableActions(context: RefactorContext): ReadonlyArray<ApplicableRefactorInfo> {
1414
const { file, startPosition } = context;
15-
let node = getTokenAtPosition(file, startPosition);
16-
if (isParenthesizedExpression(node.parent) && isBinaryExpression(node.parent.parent)) node = node.parent.parent;
15+
const node = getNodeOrParentOfBraces(file, startPosition);
1716
const maybeBinary = getParentBinaryExpression(node);
1817
const actions: RefactorActionInfo[] = [];
1918

@@ -28,11 +27,17 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
2827
return [{ name: refactorName, description: refactorDescription, actions }];
2928
}
3029

30+
function getNodeOrParentOfBraces(file: SourceFile, startPosition: number) {
31+
const node = getTokenAtPosition(file, startPosition);
32+
if (isParenthesizedExpression(node.parent) && isBinaryExpression(node.parent.parent)) return node.parent.parent;
33+
return node;
34+
}
35+
3136
function isTemplateLike(node: Node) {
32-
const isEmptyTemplate = isNoSubstitutionTemplateLiteral(node) && isNotTagged(node);
37+
const isTemplateWithoutSubstitution = isNoSubstitutionTemplateLiteral(node) && isNotTagged(node);
3338
const isTemplate = (isTemplateHead(node) || isTemplateMiddleOrTemplateTail(node)) && isNotTagged(node.parent);
3439
const isTemplateFromExpression = isTemplateSpan(node.parent) && isNotTagged(node.parent.parent);
35-
return isEmptyTemplate || isTemplate || isTemplateFromExpression;
40+
return isTemplateWithoutSubstitution || isTemplate || isTemplateFromExpression;
3641
}
3742

3843
function isNotTagged(templateExpression: Node) {
@@ -41,46 +46,45 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
4146

4247
function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
4348
const { file, startPosition } = context;
44-
let node = getTokenAtPosition(file, startPosition);
49+
const node = getNodeOrParentOfBraces(file, startPosition);
4550

4651
switch (actionName) {
4752
case toTemplateLiteralActionName:
48-
if (isParenthesizedExpression(node.parent) && isBinaryExpression(node.parent.parent)) node = node.parent.parent;
4953
const maybeBinary = getParentBinaryExpression(node);
50-
5154
const arrayOfNodes = transformTreeToArray(maybeBinary);
5255
const templateLiteral = nodesToTemplate(arrayOfNodes);
5356
const edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, maybeBinary, templateLiteral));
5457
return { edits };
5558

5659
case toStringConcatenationActionName:
57-
if (isNoSubstitutionTemplateLiteral(node)) {
58-
const stringLiteral = createStringLiteral(node.text);
59-
return { edits: textChanges.ChangeTracker.with(context, t => t.replaceNode(file, node, stringLiteral)) };
60+
return { edits: getEditsForToStringConcatenation(context, node) };
6061

61-
}
62-
if (isTemplateExpression(node.parent) || isTemplateSpan(node.parent)) {
63-
const templateLiteralExpression = isTemplateSpan(node.parent) ? node.parent.parent : node.parent;
64-
const nodesArray: Expression[] = [];
65-
66-
if (templateLiteralExpression.head.text.length !== 0) nodesArray.push(createStringLiteral(templateLiteralExpression.head.text));
62+
default:
63+
return Debug.fail("invalid action");
64+
}
65+
}
6766

68-
templateLiteralExpression.templateSpans.forEach(ts => {
69-
nodesArray.push(ts.expression);
70-
const str = ts.literal.text;
71-
if (str.length !== 0) {
72-
nodesArray.push(createStringLiteral(str));
73-
}
74-
});
67+
function getEditsForToStringConcatenation(context: RefactorContext, node: Node) {
68+
if (isTemplateExpression(node.parent) || isTemplateSpan(node.parent)) {
69+
const templateLiteralExpression = isTemplateSpan(node.parent) ? node.parent.parent : node.parent;
70+
const { head, templateSpans } = templateLiteralExpression;
71+
const arrayOfNodes: Expression[] = [];
7572

76-
const binaryExpression = arrayToTree(nodesArray);
77-
return { edits: textChanges.ChangeTracker.with(context, t => t.replaceNode(file, templateLiteralExpression, binaryExpression)) };
78-
}
73+
if (head.text.length !== 0) arrayOfNodes.push(createStringLiteral(head.text));
7974

80-
break;
75+
templateSpans.forEach(ts => {
76+
arrayOfNodes.push(ts.expression);
77+
const text = ts.literal.text;
78+
if (text.length !== 0) arrayOfNodes.push(createStringLiteral(text));
79+
});
8180

82-
default:
83-
return Debug.fail("invalid action");
81+
const binaryExpression = arrayToTree(arrayOfNodes);
82+
return textChanges.ChangeTracker.with(context, t => t.replaceNode(context.file, templateLiteralExpression, binaryExpression));
83+
}
84+
else {
85+
const templateWithoutSubstitution = node as NoSubstitutionTemplateLiteral;
86+
const stringLiteral = createStringLiteral(templateWithoutSubstitution.text);
87+
return textChanges.ChangeTracker.with(context, t => t.replaceNode(context.file, node, stringLiteral));
8488
}
8589
}
8690

0 commit comments

Comments
 (0)