@@ -15,14 +15,12 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
15
15
let node = getTokenAtPosition ( file , startPosition ) ;
16
16
if ( isParenthesizedExpression ( node . parent ) && isBinaryExpression ( node . parent . parent ) ) node = node . parent . parent ;
17
17
const maybeBinary = getParentBinaryExpression ( node ) ;
18
- // const maybeTemplateExpression = findAncestor(node, n => isTemplateExpression(n));
19
18
const actions : RefactorActionInfo [ ] = [ ] ;
20
19
21
20
if ( ( isBinaryExpression ( maybeBinary ) || isStringLiteral ( maybeBinary ) ) && isStringConcatenationValid ( maybeBinary ) ) {
22
21
actions . push ( { name : toTemplateLiteralActionName , description : toTemplateLiteralDescription } ) ;
23
22
}
24
23
25
- // if ((isNoSubstitutionTemplateLiteral(node) && !isTaggedTemplateExpression(node.parent)) || (maybeTemplateExpression && !isTaggedTemplateExpression(maybeTemplateExpression.parent))) {
26
24
if ( isTemplateLike ( node ) ) {
27
25
actions . push ( { name : toStringConcatenationActionName , description : toStringConcatenationDescription } ) ;
28
26
}
@@ -31,10 +29,14 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
31
29
}
32
30
33
31
function isTemplateLike ( node : Node ) {
34
- const isEmptyTL = isNoSubstitutionTemplateLiteral ( node ) && ! isTaggedTemplateExpression ( node . parent ) ;
35
- const is = ( isTemplateHead ( node ) || isTemplateMiddleOrTemplateTail ( node ) ) && ! isTaggedTemplateExpression ( node . parent . parent ) ;
36
- const ise = ( isTemplateSpan ( node . parent ) ) && ! isTaggedTemplateExpression ( node . parent . parent . parent ) ;
37
- return isEmptyTL || is || ise ;
32
+ const isEmptyTemplate = isNoSubstitutionTemplateLiteral ( node ) && isNotTagged ( node ) ;
33
+ const isTemplate = ( isTemplateHead ( node ) || isTemplateMiddleOrTemplateTail ( node ) ) && isNotTagged ( node . parent ) ;
34
+ const isTemplateFromExpression = isTemplateSpan ( node . parent ) && isNotTagged ( node . parent . parent ) ;
35
+ return isEmptyTemplate || isTemplate || isTemplateFromExpression ;
36
+ }
37
+
38
+ function isNotTagged ( templateExpression : Node ) {
39
+ return ! isTaggedTemplateExpression ( templateExpression . parent ) ;
38
40
}
39
41
40
42
function getEditsForAction ( context : RefactorContext , actionName : string ) : RefactorEditInfo | undefined {
@@ -126,26 +128,23 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
126
128
return [ [ node ] , isStringLiteral ( node ) , true ] ;
127
129
}
128
130
129
- function nodesToTemplate ( nodes : Node [ ] ) {
131
+ function createHead ( nodes : Node [ ] ) : [ number , TemplateHead ] {
130
132
let begin = 0 ;
131
-
132
133
const head = createTemplateHead ( "" ) ;
133
- const firstNode = nodes [ 0 ] ;
134
- const spans : TemplateSpan [ ] = [ ] ;
135
134
136
- if ( isStringLiteral ( firstNode ) ) {
137
- head . text = firstNode . text ;
135
+ while ( begin < nodes . length && isStringLiteral ( nodes [ begin ] ) ) {
136
+ const next = nodes [ begin ] as StringLiteral ;
137
+ head . text = head . text + next . text ;
138
138
begin ++ ;
139
+ }
139
140
140
- while ( begin < nodes . length && isStringLiteral ( nodes [ begin ] ) ) {
141
-
142
- const next = nodes [ begin ] as StringLiteral ;
143
- head . text = head . text + next . text ;
144
- begin ++ ;
145
- }
141
+ head . text = escapeText ( head . text ) ;
142
+ return [ begin , head ] ;
143
+ }
146
144
147
- head . text = cleanString ( head . text ) ;
148
- }
145
+ function nodesToTemplate ( nodes : Node [ ] ) {
146
+ const spans : TemplateSpan [ ] = [ ] ;
147
+ const [ begin , head ] = createHead ( nodes ) ;
149
148
150
149
if ( begin === nodes . length ) {
151
150
return createNoSubstitutionTemplateLiteral ( head . text ) ;
@@ -166,7 +165,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
166
165
i ++ ;
167
166
}
168
167
169
- text = cleanString ( text ) ;
168
+ text = escapeText ( text ) ;
170
169
templatePart = i === nodes . length - 1 ? createTemplateTail ( text ) : createTemplateMiddle ( text ) ;
171
170
}
172
171
else {
@@ -180,7 +179,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
180
179
return createTemplateExpression ( head , spans ) ;
181
180
}
182
181
183
- function cleanString ( content : string ) {
182
+ function escapeText ( content : string ) {
184
183
return content . replace ( "`" , "\`" ) . replace ( "\${" , `$\\{` ) ;
185
184
}
186
185
0 commit comments