extract creation of templateHead

This commit is contained in:
BigAru
2018-12-07 14:06:41 +01:00
parent cba0ddcbde
commit 6721966504

View File

@@ -15,14 +15,12 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
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[] = [];
if ((isBinaryExpression(maybeBinary) || isStringLiteral(maybeBinary)) && isStringConcatenationValid(maybeBinary)) {
actions.push({ name: toTemplateLiteralActionName, description: toTemplateLiteralDescription });
}
// if ((isNoSubstitutionTemplateLiteral(node) && !isTaggedTemplateExpression(node.parent)) || (maybeTemplateExpression && !isTaggedTemplateExpression(maybeTemplateExpression.parent))) {
if (isTemplateLike(node)) {
actions.push({ name: toStringConcatenationActionName, description: toStringConcatenationDescription });
}
@@ -31,10 +29,14 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
}
function isTemplateLike(node: Node) {
const isEmptyTL = isNoSubstitutionTemplateLiteral(node) && !isTaggedTemplateExpression(node.parent);
const is = (isTemplateHead(node) || isTemplateMiddleOrTemplateTail(node)) && !isTaggedTemplateExpression(node.parent.parent);
const ise = (isTemplateSpan(node.parent)) && !isTaggedTemplateExpression(node.parent.parent.parent);
return isEmptyTL || is || ise;
const isEmptyTemplate = isNoSubstitutionTemplateLiteral(node) && isNotTagged(node);
const isTemplate = (isTemplateHead(node) || isTemplateMiddleOrTemplateTail(node)) && isNotTagged(node.parent);
const isTemplateFromExpression = isTemplateSpan(node.parent) && isNotTagged(node.parent.parent);
return isEmptyTemplate || isTemplate || isTemplateFromExpression;
}
function isNotTagged(templateExpression: Node) {
return !isTaggedTemplateExpression(templateExpression.parent);
}
function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
@@ -126,27 +128,24 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
return [[node], isStringLiteral(node), true];
}
function nodesToTemplate(nodes: Node[]) {
function createHead(nodes: Node[]): [number, TemplateHead] {
let begin = 0;
const head = createTemplateHead("");
const firstNode = nodes[0];
const spans: TemplateSpan[] = [];
if (isStringLiteral(firstNode)) {
head.text = firstNode.text;
while (begin < nodes.length && isStringLiteral(nodes[begin])) {
const next = nodes[begin] as StringLiteral;
head.text = head.text + next.text;
begin++;
while (begin < nodes.length && isStringLiteral(nodes[begin])) {
const next = nodes[begin] as StringLiteral;
head.text = head.text + next.text;
begin++;
}
head.text = cleanString(head.text);
}
head.text = escapeText(head.text);
return [begin, head];
}
function nodesToTemplate(nodes: Node[]) {
const spans: TemplateSpan[] = [];
const [begin, head] = createHead(nodes);
if (begin === nodes.length) {
return createNoSubstitutionTemplateLiteral(head.text);
}
@@ -166,7 +165,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
i++;
}
text = cleanString(text);
text = escapeText(text);
templatePart = i === nodes.length - 1 ? createTemplateTail(text) : createTemplateMiddle(text);
}
else {
@@ -180,7 +179,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
return createTemplateExpression(head, spans);
}
function cleanString(content: string) {
function escapeText(content: string) {
return content.replace("`", "\`").replace("\${", `$\\{`);
}