diff --git a/dist/main/lang/fixmyts/quickFixes/stringConcatToTemplate.js b/dist/main/lang/fixmyts/quickFixes/stringConcatToTemplate.js index 987d69fdd..ed12cc922 100644 --- a/dist/main/lang/fixmyts/quickFixes/stringConcatToTemplate.js +++ b/dist/main/lang/fixmyts/quickFixes/stringConcatToTemplate.js @@ -22,6 +22,10 @@ function isAPartOfAChainOfStringAdditions(node, typeChecker) { } var StringConcatToTemplate = (function () { function StringConcatToTemplate() { + this.backTickCharacter = '`'; + this.backTick = new RegExp(this.backTickCharacter, 'g'); + this.$regex = /\$/g; + this.finalOutput = []; this.key = StringConcatToTemplate.name; } StringConcatToTemplate.prototype.canProvideFix = function (info) { @@ -32,46 +36,21 @@ var StringConcatToTemplate = (function () { }; StringConcatToTemplate.prototype.provideFix = function (info) { var strRoot = isAPartOfAChainOfStringAdditions(info.positionNode, info.typeChecker); - var finalOutput = []; var current = strRoot; - var backTickCharacter = '`'; - var backTick = new RegExp(backTickCharacter, 'g'); - var $regex = /\$/g; while (true) { - function appendToFinal(node) { - if (node.kind == ts.SyntaxKind.StringLiteral) { - var text = node.getText(); - var quoteCharacter = text.trim()[0]; - var quoteRegex = new RegExp(quoteCharacter, 'g'); - var escapedQuoteRegex = new RegExp("\\\\" + quoteCharacter, 'g'); - var newText_1 = text - .replace(backTick, "\\" + backTickCharacter) - .replace(escapedQuoteRegex, quoteCharacter) - .replace($regex, '\\$'); - newText_1 = newText_1.substr(1, newText_1.length - 2); - finalOutput.unshift(newText_1); - } - else if (node.kind == ts.SyntaxKind.TemplateExpression || node.kind == ts.SyntaxKind.NoSubstitutionTemplateLiteral) { - var text = node.getText(); - text = text.trim(); - text = text.substr(1, text.length - 2); - finalOutput.unshift(text); - } - else { - finalOutput.unshift('${' + node.getText() + '}'); - } - } if (current.kind == ts.SyntaxKind.BinaryExpression) { var binary = current; - appendToFinal(binary.right); + this.appendToFinal(binary.right); current = binary.left; } else { - appendToFinal(current); + this.appendToFinal(current); break; } } - var newText = backTickCharacter + finalOutput.join('') + backTickCharacter; + var newText = this.backTickCharacter + + this.finalOutput.join('') + + this.backTickCharacter; var refactoring = { span: { start: strRoot.getStart(), @@ -82,6 +61,29 @@ var StringConcatToTemplate = (function () { }; return [refactoring]; }; + StringConcatToTemplate.prototype.appendToFinal = function (node) { + if (node.kind == ts.SyntaxKind.StringLiteral) { + var text = node.getText(); + var quoteCharacter = text.trim()[0]; + var quoteRegex = new RegExp(quoteCharacter, 'g'); + var escapedQuoteRegex = new RegExp("\\\\" + quoteCharacter, 'g'); + var newText = text + .replace(this.backTick, "\\" + this.backTickCharacter) + .replace(escapedQuoteRegex, quoteCharacter) + .replace(this.$regex, '\\$'); + newText = newText.substr(1, newText.length - 2); + this.finalOutput.unshift(newText); + } + else if (node.kind == ts.SyntaxKind.TemplateExpression || node.kind == ts.SyntaxKind.NoSubstitutionTemplateLiteral) { + var text = node.getText(); + text = text.trim(); + text = text.substr(1, text.length - 2); + this.finalOutput.unshift(text); + } + else { + this.finalOutput.unshift('${' + node.getText() + '}'); + } + }; return StringConcatToTemplate; }()); exports.StringConcatToTemplate = StringConcatToTemplate; diff --git a/lib/main/lang/fixmyts/quickFixes/stringConcatToTemplate.ts b/lib/main/lang/fixmyts/quickFixes/stringConcatToTemplate.ts index 63139c7b1..3e3861d8b 100644 --- a/lib/main/lang/fixmyts/quickFixes/stringConcatToTemplate.ts +++ b/lib/main/lang/fixmyts/quickFixes/stringConcatToTemplate.ts @@ -33,6 +33,10 @@ function isAPartOfAChainOfStringAdditions(node: ts.Node, typeChecker: ts.TypeChe } export class StringConcatToTemplate implements QuickFix { + backTickCharacter = '`'; + backTick = new RegExp(this.backTickCharacter, 'g'); + $regex = /\$/g; + finalOutput: string[] = []; key = StringConcatToTemplate.name; canProvideFix(info: QuickFixQueryInformation): CanProvideFixResponse { @@ -50,66 +54,27 @@ export class StringConcatToTemplate implements QuickFix { provideFix(info: QuickFixQueryInformation): Refactoring[] { var strRoot = isAPartOfAChainOfStringAdditions(info.positionNode, info.typeChecker); - - let finalOutput: string[] = []; - let current: ts.Node = strRoot; - var backTickCharacter = '`'; - var backTick = new RegExp(backTickCharacter, 'g'); - var $regex = /\$/g; - // We pop of each left node one by one while (true) { - - function appendToFinal(node: ts.Node) { - // Each string literal needs : - // to be checked that it doesn't contain (`) and those need to be escaped. - // Also `$` needs escaping - // Also the quote characters at the limits need to be removed - if (node.kind == ts.SyntaxKind.StringLiteral) { - let text = node.getText(); - let quoteCharacter = text.trim()[0]; - - let quoteRegex = new RegExp(quoteCharacter, 'g') - let escapedQuoteRegex = new RegExp(`\\\\${quoteCharacter}`, 'g') - - let newText = text - .replace(backTick, `\\${backTickCharacter}`) - .replace(escapedQuoteRegex, quoteCharacter) - .replace($regex, '\\$'); - - newText = newText.substr(1, newText.length - 2); - finalOutput.unshift(newText); - } - else if (node.kind == ts.SyntaxKind.TemplateExpression || node.kind == ts.SyntaxKind.NoSubstitutionTemplateLiteral) - { - let text = node.getText(); - text = text.trim(); - text = text.substr(1, text.length - 2); - finalOutput.unshift(text); - } - // Each expression that isn't a string literal will just be escaped `${}` - else { - finalOutput.unshift('${' + node.getText() + '}'); - } - } - // if we are still in some sequence of additions if (current.kind == ts.SyntaxKind.BinaryExpression) { let binary = current; - appendToFinal(binary.right); + this.appendToFinal(binary.right); // Continue with left current = binary.left; } else { - appendToFinal(current); + this.appendToFinal(current); break; } } - let newText = backTickCharacter + finalOutput.join('') + backTickCharacter; + let newText = this.backTickCharacter + + this.finalOutput.join('') + + this.backTickCharacter; var refactoring: Refactoring = { span: { @@ -122,4 +87,36 @@ export class StringConcatToTemplate implements QuickFix { return [refactoring]; } + + private appendToFinal(node: ts.Node) { + // Each string literal needs : + // to be checked that it doesn't contain (`) and those need to be escaped. + // Also `$` needs escaping + // Also the quote characters at the limits need to be removed + if (node.kind == ts.SyntaxKind.StringLiteral) { + let text = node.getText(); + let quoteCharacter = text.trim()[0]; + + let quoteRegex = new RegExp(quoteCharacter, 'g') + let escapedQuoteRegex = new RegExp(`\\\\${quoteCharacter}`, 'g') + + let newText = text + .replace(this.backTick, `\\${this.backTickCharacter}`) + .replace(escapedQuoteRegex, quoteCharacter) + .replace(this.$regex, '\\$'); + + newText = newText.substr(1, newText.length - 2); + this.finalOutput.unshift(newText); + } + else if (node.kind == ts.SyntaxKind.TemplateExpression || node.kind == ts.SyntaxKind.NoSubstitutionTemplateLiteral) { + let text = node.getText(); + text = text.trim(); + text = text.substr(1, text.length - 2); + this.finalOutput.unshift(text); + } + // Each expression that isn't a string literal will just be escaped `${}` + else { + this.finalOutput.unshift('${' + node.getText() + '}'); + } + } }