Skip to content

Fixing Travis CI Build #949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 32 additions & 30 deletions dist/main/lang/fixmyts/quickFixes/stringConcatToTemplate.js
Original file line number Diff line number Diff line change
@@ -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;
85 changes: 41 additions & 44 deletions lib/main/lang/fixmyts/quickFixes/stringConcatToTemplate.ts
Original file line number Diff line number Diff line change
@@ -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 = <ts.BinaryExpression>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() + '}');
}
}
}