@@ -33,6 +33,10 @@ function isAPartOfAChainOfStringAdditions(node: ts.Node, typeChecker: ts.TypeChe
3333}
3434
3535export 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