Skip to content

Commit 7ccf523

Browse files
GregOnNetbasarat
authored andcommitted
Moves appendToFinal away from while loop into own private method (#949)
1 parent a6b421f commit 7ccf523

File tree

2 files changed

+73
-74
lines changed

2 files changed

+73
-74
lines changed

dist/main/lang/fixmyts/quickFixes/stringConcatToTemplate.js

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ function isAPartOfAChainOfStringAdditions(node, typeChecker) {
2222
}
2323
var StringConcatToTemplate = (function () {
2424
function StringConcatToTemplate() {
25+
this.backTickCharacter = '`';
26+
this.backTick = new RegExp(this.backTickCharacter, 'g');
27+
this.$regex = /\$/g;
28+
this.finalOutput = [];
2529
this.key = StringConcatToTemplate.name;
2630
}
2731
StringConcatToTemplate.prototype.canProvideFix = function (info) {
@@ -32,46 +36,21 @@ var StringConcatToTemplate = (function () {
3236
};
3337
StringConcatToTemplate.prototype.provideFix = function (info) {
3438
var strRoot = isAPartOfAChainOfStringAdditions(info.positionNode, info.typeChecker);
35-
var finalOutput = [];
3639
var current = strRoot;
37-
var backTickCharacter = '`';
38-
var backTick = new RegExp(backTickCharacter, 'g');
39-
var $regex = /\$/g;
4040
while (true) {
41-
function appendToFinal(node) {
42-
if (node.kind == ts.SyntaxKind.StringLiteral) {
43-
var text = node.getText();
44-
var quoteCharacter = text.trim()[0];
45-
var quoteRegex = new RegExp(quoteCharacter, 'g');
46-
var escapedQuoteRegex = new RegExp("\\\\" + quoteCharacter, 'g');
47-
var newText_1 = text
48-
.replace(backTick, "\\" + backTickCharacter)
49-
.replace(escapedQuoteRegex, quoteCharacter)
50-
.replace($regex, '\\$');
51-
newText_1 = newText_1.substr(1, newText_1.length - 2);
52-
finalOutput.unshift(newText_1);
53-
}
54-
else if (node.kind == ts.SyntaxKind.TemplateExpression || node.kind == ts.SyntaxKind.NoSubstitutionTemplateLiteral) {
55-
var text = node.getText();
56-
text = text.trim();
57-
text = text.substr(1, text.length - 2);
58-
finalOutput.unshift(text);
59-
}
60-
else {
61-
finalOutput.unshift('${' + node.getText() + '}');
62-
}
63-
}
6441
if (current.kind == ts.SyntaxKind.BinaryExpression) {
6542
var binary = current;
66-
appendToFinal(binary.right);
43+
this.appendToFinal(binary.right);
6744
current = binary.left;
6845
}
6946
else {
70-
appendToFinal(current);
47+
this.appendToFinal(current);
7148
break;
7249
}
7350
}
74-
var newText = backTickCharacter + finalOutput.join('') + backTickCharacter;
51+
var newText = this.backTickCharacter +
52+
this.finalOutput.join('') +
53+
this.backTickCharacter;
7554
var refactoring = {
7655
span: {
7756
start: strRoot.getStart(),
@@ -82,6 +61,29 @@ var StringConcatToTemplate = (function () {
8261
};
8362
return [refactoring];
8463
};
64+
StringConcatToTemplate.prototype.appendToFinal = function (node) {
65+
if (node.kind == ts.SyntaxKind.StringLiteral) {
66+
var text = node.getText();
67+
var quoteCharacter = text.trim()[0];
68+
var quoteRegex = new RegExp(quoteCharacter, 'g');
69+
var escapedQuoteRegex = new RegExp("\\\\" + quoteCharacter, 'g');
70+
var newText = text
71+
.replace(this.backTick, "\\" + this.backTickCharacter)
72+
.replace(escapedQuoteRegex, quoteCharacter)
73+
.replace(this.$regex, '\\$');
74+
newText = newText.substr(1, newText.length - 2);
75+
this.finalOutput.unshift(newText);
76+
}
77+
else if (node.kind == ts.SyntaxKind.TemplateExpression || node.kind == ts.SyntaxKind.NoSubstitutionTemplateLiteral) {
78+
var text = node.getText();
79+
text = text.trim();
80+
text = text.substr(1, text.length - 2);
81+
this.finalOutput.unshift(text);
82+
}
83+
else {
84+
this.finalOutput.unshift('${' + node.getText() + '}');
85+
}
86+
};
8587
return StringConcatToTemplate;
8688
}());
8789
exports.StringConcatToTemplate = StringConcatToTemplate;

lib/main/lang/fixmyts/quickFixes/stringConcatToTemplate.ts

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ function isAPartOfAChainOfStringAdditions(node: ts.Node, typeChecker: ts.TypeChe
3333
}
3434

3535
export class StringConcatToTemplate implements QuickFix {
36+
backTickCharacter = '`';
37+
backTick = new RegExp(this.backTickCharacter, 'g');
38+
$regex = /\$/g;
39+
finalOutput: string[] = [];
3640
key = StringConcatToTemplate.name;
3741

3842
canProvideFix(info: QuickFixQueryInformation): CanProvideFixResponse {
@@ -50,66 +54,27 @@ export class StringConcatToTemplate implements QuickFix {
5054

5155
provideFix(info: QuickFixQueryInformation): Refactoring[] {
5256
var strRoot = isAPartOfAChainOfStringAdditions(info.positionNode, info.typeChecker);
53-
54-
let finalOutput: string[] = [];
55-
5657
let current: ts.Node = strRoot;
5758

58-
var backTickCharacter = '`';
59-
var backTick = new RegExp(backTickCharacter, 'g');
60-
var $regex = /\$/g;
61-
6259
// We pop of each left node one by one
6360
while (true) {
64-
65-
function appendToFinal(node: ts.Node) {
66-
// Each string literal needs :
67-
// to be checked that it doesn't contain (`) and those need to be escaped.
68-
// Also `$` needs escaping
69-
// Also the quote characters at the limits need to be removed
70-
if (node.kind == ts.SyntaxKind.StringLiteral) {
71-
let text = node.getText();
72-
let quoteCharacter = text.trim()[0];
73-
74-
let quoteRegex = new RegExp(quoteCharacter, 'g')
75-
let escapedQuoteRegex = new RegExp(`\\\\${quoteCharacter}`, 'g')
76-
77-
let newText = text
78-
.replace(backTick, `\\${backTickCharacter}`)
79-
.replace(escapedQuoteRegex, quoteCharacter)
80-
.replace($regex, '\\$');
81-
82-
newText = newText.substr(1, newText.length - 2);
83-
finalOutput.unshift(newText);
84-
}
85-
else if (node.kind == ts.SyntaxKind.TemplateExpression || node.kind == ts.SyntaxKind.NoSubstitutionTemplateLiteral)
86-
{
87-
let text = node.getText();
88-
text = text.trim();
89-
text = text.substr(1, text.length - 2);
90-
finalOutput.unshift(text);
91-
}
92-
// Each expression that isn't a string literal will just be escaped `${}`
93-
else {
94-
finalOutput.unshift('${' + node.getText() + '}');
95-
}
96-
}
97-
9861
// if we are still in some sequence of additions
9962
if (current.kind == ts.SyntaxKind.BinaryExpression) {
10063
let binary = <ts.BinaryExpression>current;
101-
appendToFinal(binary.right);
64+
this.appendToFinal(binary.right);
10265

10366
// Continue with left
10467
current = binary.left;
10568
}
10669
else {
107-
appendToFinal(current);
70+
this.appendToFinal(current);
10871
break;
10972
}
11073
}
11174

112-
let newText = backTickCharacter + finalOutput.join('') + backTickCharacter;
75+
let newText = this.backTickCharacter +
76+
this.finalOutput.join('') +
77+
this.backTickCharacter;
11378

11479
var refactoring: Refactoring = {
11580
span: {
@@ -122,4 +87,36 @@ export class StringConcatToTemplate implements QuickFix {
12287

12388
return [refactoring];
12489
}
90+
91+
private appendToFinal(node: ts.Node) {
92+
// Each string literal needs :
93+
// to be checked that it doesn't contain (`) and those need to be escaped.
94+
// Also `$` needs escaping
95+
// Also the quote characters at the limits need to be removed
96+
if (node.kind == ts.SyntaxKind.StringLiteral) {
97+
let text = node.getText();
98+
let quoteCharacter = text.trim()[0];
99+
100+
let quoteRegex = new RegExp(quoteCharacter, 'g')
101+
let escapedQuoteRegex = new RegExp(`\\\\${quoteCharacter}`, 'g')
102+
103+
let newText = text
104+
.replace(this.backTick, `\\${this.backTickCharacter}`)
105+
.replace(escapedQuoteRegex, quoteCharacter)
106+
.replace(this.$regex, '\\$');
107+
108+
newText = newText.substr(1, newText.length - 2);
109+
this.finalOutput.unshift(newText);
110+
}
111+
else if (node.kind == ts.SyntaxKind.TemplateExpression || node.kind == ts.SyntaxKind.NoSubstitutionTemplateLiteral) {
112+
let text = node.getText();
113+
text = text.trim();
114+
text = text.substr(1, text.length - 2);
115+
this.finalOutput.unshift(text);
116+
}
117+
// Each expression that isn't a string literal will just be escaped `${}`
118+
else {
119+
this.finalOutput.unshift('${' + node.getText() + '}');
120+
}
121+
}
125122
}

0 commit comments

Comments
 (0)