@@ -33,6 +33,10 @@ function isAPartOfAChainOfStringAdditions(node: ts.Node, typeChecker: ts.TypeChe
33
33
}
34
34
35
35
export class StringConcatToTemplate implements QuickFix {
36
+ backTickCharacter = '`' ;
37
+ backTick = new RegExp ( this . backTickCharacter , 'g' ) ;
38
+ $regex = / \$ / g;
39
+ finalOutput : string [ ] = [ ] ;
36
40
key = StringConcatToTemplate . name ;
37
41
38
42
canProvideFix ( info : QuickFixQueryInformation ) : CanProvideFixResponse {
@@ -50,66 +54,27 @@ export class StringConcatToTemplate implements QuickFix {
50
54
51
55
provideFix ( info : QuickFixQueryInformation ) : Refactoring [ ] {
52
56
var strRoot = isAPartOfAChainOfStringAdditions ( info . positionNode , info . typeChecker ) ;
53
-
54
- let finalOutput : string [ ] = [ ] ;
55
-
56
57
let current : ts . Node = strRoot ;
57
58
58
- var backTickCharacter = '`' ;
59
- var backTick = new RegExp ( backTickCharacter , 'g' ) ;
60
- var $regex = / \$ / g;
61
-
62
59
// We pop of each left node one by one
63
60
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
-
98
61
// if we are still in some sequence of additions
99
62
if ( current . kind == ts . SyntaxKind . BinaryExpression ) {
100
63
let binary = < ts . BinaryExpression > current ;
101
- appendToFinal ( binary . right ) ;
64
+ this . appendToFinal ( binary . right ) ;
102
65
103
66
// Continue with left
104
67
current = binary . left ;
105
68
}
106
69
else {
107
- appendToFinal ( current ) ;
70
+ this . appendToFinal ( current ) ;
108
71
break ;
109
72
}
110
73
}
111
74
112
- let newText = backTickCharacter + finalOutput . join ( '' ) + backTickCharacter ;
75
+ let newText = this . backTickCharacter +
76
+ this . finalOutput . join ( '' ) +
77
+ this . backTickCharacter ;
113
78
114
79
var refactoring : Refactoring = {
115
80
span : {
@@ -122,4 +87,36 @@ export class StringConcatToTemplate implements QuickFix {
122
87
123
88
return [ refactoring ] ;
124
89
}
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
+ }
125
122
}
0 commit comments