@@ -14,19 +14,21 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
14
14
const { file, startPosition } = context ;
15
15
const node = getNodeOrParentOfParentheses ( file , startPosition ) ;
16
16
const maybeBinary = getParentBinaryExpression ( node ) ;
17
- const actions : RefactorActionInfo [ ] = [ ] ;
17
+ const refactorInfo : ApplicableRefactorInfo = { name : refactorName , description : refactorDescription , actions : [ ] } ;
18
18
19
19
if ( ( isBinaryExpression ( maybeBinary ) || isStringLiteral ( maybeBinary ) ) && isStringConcatenationValid ( maybeBinary ) ) {
20
- actions . push ( { name : toTemplateLiteralActionName , description : toTemplateLiteralDescription } ) ;
20
+ refactorInfo . actions . push ( { name : toTemplateLiteralActionName , description : toTemplateLiteralDescription } ) ;
21
+ return [ refactorInfo ] ;
21
22
}
22
23
23
24
const templateLiteral = findAncestor ( node , n => isTemplateLiteral ( n ) ) ;
24
25
25
26
if ( templateLiteral && ! isTaggedTemplateExpression ( templateLiteral . parent ) ) {
26
- actions . push ( { name : toStringConcatenationActionName , description : toStringConcatenationDescription } ) ;
27
+ refactorInfo . actions . push ( { name : toStringConcatenationActionName , description : toStringConcatenationDescription } ) ;
28
+ return [ refactorInfo ] ;
27
29
}
28
30
29
- return [ { name : refactorName , description : refactorDescription , actions } ] ;
31
+ return emptyArray ;
30
32
}
31
33
32
34
function getNodeOrParentOfParentheses ( file : SourceFile , startPosition : number ) {
@@ -137,46 +139,35 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
137
139
return { nodes : [ node as Expression ] , containsString : isStringLiteral ( node ) , areOperatorsValid : true } ;
138
140
}
139
141
140
- function createHead ( nodes : ReadonlyArray < Expression > ) : [ number , TemplateHead ] {
141
- let begin = 0 ;
142
+ function concatConsecutiveString ( index : number , nodes : ReadonlyArray < Expression > ) : [ number , string ] {
142
143
let text = "" ;
143
144
144
- while ( begin < nodes . length && isStringLiteral ( nodes [ begin ] ) ) {
145
- const next = nodes [ begin ] as StringLiteral ;
146
- text = text + decodeRawString ( next . getText ( ) ) ;
147
- begin ++ ;
145
+ while ( index < nodes . length && isStringLiteral ( nodes [ index ] ) ) {
146
+ text = text + decodeRawString ( nodes [ index ] . getText ( ) ) ;
147
+ index ++ ;
148
148
}
149
149
150
150
text = escapeText ( text ) ;
151
- return [ begin , createTemplateHead ( text ) ] ;
151
+ return [ index , text ] ;
152
152
}
153
153
154
154
function nodesToTemplate ( nodes : ReadonlyArray < Expression > ) {
155
155
const templateSpans : TemplateSpan [ ] = [ ] ;
156
- const [ begin , head ] = createHead ( nodes ) ;
156
+ const [ begin , headText ] = concatConsecutiveString ( 0 , nodes ) ;
157
+ const templateHead = createTemplateHead ( headText ) ;
157
158
158
- if ( begin === nodes . length ) {
159
- return createNoSubstitutionTemplateLiteral ( head . text ) ;
160
- }
159
+ if ( begin === nodes . length ) return createNoSubstitutionTemplateLiteral ( headText ) ;
161
160
162
161
for ( let i = begin ; i < nodes . length ; i ++ ) {
163
- let current = nodes [ i ] ;
164
- let text = "" ;
165
-
166
- while ( i + 1 < nodes . length && isStringLiteral ( nodes [ i + 1 ] ) ) {
167
- const next = nodes [ i + 1 ] as StringLiteral ;
168
- text = text + decodeRawString ( next . getText ( ) ) ;
169
- i ++ ;
170
- }
162
+ const expression = isParenthesizedExpression ( nodes [ i ] ) ? ( nodes [ i ] as ParenthesizedExpression ) . expression : nodes [ i ] ;
163
+ const [ newIndex , subsequentText ] = concatConsecutiveString ( i + 1 , nodes ) ;
164
+ i = newIndex - 1 ;
171
165
172
- text = escapeText ( text ) ;
173
- const templatePart = i === nodes . length - 1 ? createTemplateTail ( text ) : createTemplateMiddle ( text ) ;
174
-
175
- if ( isParenthesizedExpression ( current ) ) current = current . expression ;
176
- templateSpans . push ( createTemplateSpan ( current , templatePart ) ) ;
166
+ const templatePart = i === nodes . length - 1 ? createTemplateTail ( subsequentText ) : createTemplateMiddle ( subsequentText ) ;
167
+ templateSpans . push ( createTemplateSpan ( expression , templatePart ) ) ;
177
168
}
178
169
179
- return createTemplateExpression ( head , templateSpans ) ;
170
+ return createTemplateExpression ( templateHead , templateSpans ) ;
180
171
}
181
172
182
173
const octalToUnicode = ( _match : string , grp : string ) => String . fromCharCode ( parseInt ( grp , 8 ) ) ;
@@ -196,3 +187,4 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
196
187
}
197
188
198
189
}
190
+
0 commit comments