From 672196650452f43294d06287a3f27e313a678453 Mon Sep 17 00:00:00 2001 From: BigAru Date: Fri, 7 Dec 2018 14:06:41 +0100 Subject: [PATCH] extract creation of templateHead --- .../convertStringOrTemplateLiteral.ts | 45 +++++++++---------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/src/services/refactors/convertStringOrTemplateLiteral.ts b/src/services/refactors/convertStringOrTemplateLiteral.ts index cc360e5b991..eebb27c676c 100644 --- a/src/services/refactors/convertStringOrTemplateLiteral.ts +++ b/src/services/refactors/convertStringOrTemplateLiteral.ts @@ -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("\${", `$\\{`); }