Skip to content

Commit 6721966

Browse files
committed
extract creation of templateHead
1 parent cba0ddc commit 6721966

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

src/services/refactors/convertStringOrTemplateLiteral.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
1515
let node = getTokenAtPosition(file, startPosition);
1616
if (isParenthesizedExpression(node.parent) && isBinaryExpression(node.parent.parent)) node = node.parent.parent;
1717
const maybeBinary = getParentBinaryExpression(node);
18-
// const maybeTemplateExpression = findAncestor(node, n => isTemplateExpression(n));
1918
const actions: RefactorActionInfo[] = [];
2019

2120
if ((isBinaryExpression(maybeBinary) || isStringLiteral(maybeBinary)) && isStringConcatenationValid(maybeBinary)) {
2221
actions.push({ name: toTemplateLiteralActionName, description: toTemplateLiteralDescription });
2322
}
2423

25-
// if ((isNoSubstitutionTemplateLiteral(node) && !isTaggedTemplateExpression(node.parent)) || (maybeTemplateExpression && !isTaggedTemplateExpression(maybeTemplateExpression.parent))) {
2624
if (isTemplateLike(node)) {
2725
actions.push({ name: toStringConcatenationActionName, description: toStringConcatenationDescription });
2826
}
@@ -31,10 +29,14 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
3129
}
3230

3331
function isTemplateLike(node: Node) {
34-
const isEmptyTL = isNoSubstitutionTemplateLiteral(node) && !isTaggedTemplateExpression(node.parent);
35-
const is = (isTemplateHead(node) || isTemplateMiddleOrTemplateTail(node)) && !isTaggedTemplateExpression(node.parent.parent);
36-
const ise = (isTemplateSpan(node.parent)) && !isTaggedTemplateExpression(node.parent.parent.parent);
37-
return isEmptyTL || is || ise;
32+
const isEmptyTemplate = isNoSubstitutionTemplateLiteral(node) && isNotTagged(node);
33+
const isTemplate = (isTemplateHead(node) || isTemplateMiddleOrTemplateTail(node)) && isNotTagged(node.parent);
34+
const isTemplateFromExpression = isTemplateSpan(node.parent) && isNotTagged(node.parent.parent);
35+
return isEmptyTemplate || isTemplate || isTemplateFromExpression;
36+
}
37+
38+
function isNotTagged(templateExpression: Node) {
39+
return !isTaggedTemplateExpression(templateExpression.parent);
3840
}
3941

4042
function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
@@ -126,26 +128,23 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
126128
return [[node], isStringLiteral(node), true];
127129
}
128130

129-
function nodesToTemplate(nodes: Node[]) {
131+
function createHead(nodes: Node[]): [number, TemplateHead] {
130132
let begin = 0;
131-
132133
const head = createTemplateHead("");
133-
const firstNode = nodes[0];
134-
const spans: TemplateSpan[] = [];
135134

136-
if (isStringLiteral(firstNode)) {
137-
head.text = firstNode.text;
135+
while (begin < nodes.length && isStringLiteral(nodes[begin])) {
136+
const next = nodes[begin] as StringLiteral;
137+
head.text = head.text + next.text;
138138
begin++;
139+
}
139140

140-
while (begin < nodes.length && isStringLiteral(nodes[begin])) {
141-
142-
const next = nodes[begin] as StringLiteral;
143-
head.text = head.text + next.text;
144-
begin++;
145-
}
141+
head.text = escapeText(head.text);
142+
return [begin, head];
143+
}
146144

147-
head.text = cleanString(head.text);
148-
}
145+
function nodesToTemplate(nodes: Node[]) {
146+
const spans: TemplateSpan[] = [];
147+
const [begin, head] = createHead(nodes);
149148

150149
if (begin === nodes.length) {
151150
return createNoSubstitutionTemplateLiteral(head.text);
@@ -166,7 +165,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
166165
i++;
167166
}
168167

169-
text = cleanString(text);
168+
text = escapeText(text);
170169
templatePart = i === nodes.length - 1 ? createTemplateTail(text) : createTemplateMiddle(text);
171170
}
172171
else {
@@ -180,7 +179,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
180179
return createTemplateExpression(head, spans);
181180
}
182181

183-
function cleanString(content: string) {
182+
function escapeText(content: string) {
184183
return content.replace("`", "\`").replace("\${", `$\\{`);
185184
}
186185

0 commit comments

Comments
 (0)