@@ -12,41 +12,32 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
12
12
13
13
function getAvailableActions ( context : RefactorContext ) : ReadonlyArray < ApplicableRefactorInfo > {
14
14
const { file, startPosition } = context ;
15
- const node = getNodeOrParentOfBraces ( file , startPosition ) ;
15
+ const node = getNodeOrParentOfParentheses ( file , startPosition ) ;
16
16
const maybeBinary = getParentBinaryExpression ( node ) ;
17
17
const actions : RefactorActionInfo [ ] = [ ] ;
18
18
19
19
if ( ( isBinaryExpression ( maybeBinary ) || isStringLiteral ( maybeBinary ) ) && isStringConcatenationValid ( maybeBinary ) ) {
20
20
actions . push ( { name : toTemplateLiteralActionName , description : toTemplateLiteralDescription } ) ;
21
21
}
22
22
23
- if ( isTemplateLike ( node ) ) {
23
+ const templateLiteral = findAncestor ( node , n => isTemplateLiteral ( n ) ) ;
24
+
25
+ if ( templateLiteral && ! isTaggedTemplateExpression ( templateLiteral . parent ) ) {
24
26
actions . push ( { name : toStringConcatenationActionName , description : toStringConcatenationDescription } ) ;
25
27
}
26
28
27
29
return [ { name : refactorName , description : refactorDescription , actions } ] ;
28
30
}
29
31
30
- function getNodeOrParentOfBraces ( file : SourceFile , startPosition : number ) {
32
+ function getNodeOrParentOfParentheses ( file : SourceFile , startPosition : number ) {
31
33
const node = getTokenAtPosition ( file , startPosition ) ;
32
34
if ( isParenthesizedExpression ( node . parent ) && isBinaryExpression ( node . parent . parent ) ) return node . parent . parent ;
33
35
return node ;
34
36
}
35
37
36
- function isTemplateLike ( node : Node ) {
37
- const isTemplateWithoutSubstitution = isNoSubstitutionTemplateLiteral ( node ) && isNotTagged ( node ) ;
38
- const isTemplate = ( isTemplateHead ( node ) || isTemplateMiddleOrTemplateTail ( node ) ) && isNotTagged ( node . parent ) ;
39
- const isTemplateFromExpression = isTemplateSpan ( node . parent ) && isNotTagged ( node . parent . parent ) ;
40
- return isTemplateWithoutSubstitution || isTemplate || isTemplateFromExpression ;
41
- }
42
-
43
- function isNotTagged ( templateExpression : Node ) {
44
- return ! isTaggedTemplateExpression ( templateExpression . parent ) ;
45
- }
46
-
47
38
function getEditsForAction ( context : RefactorContext , actionName : string ) : RefactorEditInfo | undefined {
48
39
const { file, startPosition } = context ;
49
- const node = getNodeOrParentOfBraces ( file , startPosition ) ;
40
+ const node = getNodeOrParentOfParentheses ( file , startPosition ) ;
50
41
51
42
switch ( actionName ) {
52
43
case toTemplateLiteralActionName :
@@ -65,20 +56,20 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
65
56
}
66
57
67
58
function getEditsForToStringConcatenation ( context : RefactorContext , node : Node ) {
68
- if ( isTemplateExpression ( node . parent ) || isTemplateSpan ( node . parent ) ) {
69
- const templateLiteralExpression = isTemplateSpan ( node . parent ) ? node . parent . parent : node . parent ;
70
- const { head, templateSpans } = templateLiteralExpression ;
59
+ const templateLiteral = findAncestor ( node , n => isTemplateLiteral ( n ) ) ! as TemplateLiteral ;
60
+
61
+ if ( isTemplateExpression ( templateLiteral ) ) {
62
+ const { head, templateSpans } = templateLiteral ;
71
63
const arrayOfNodes = templateSpans . map ( templateSpanToExpressions )
72
64
. reduce ( ( accumulator , nextArray ) => accumulator . concat ( nextArray ) ) ;
73
65
74
66
if ( head . text . length !== 0 ) arrayOfNodes . unshift ( createStringLiteral ( head . text ) ) ;
75
67
76
68
const binaryExpression = arrayToTree ( arrayOfNodes ) ;
77
- return textChanges . ChangeTracker . with ( context , t => t . replaceNode ( context . file , templateLiteralExpression , binaryExpression ) ) ;
69
+ return textChanges . ChangeTracker . with ( context , t => t . replaceNode ( context . file , templateLiteral , binaryExpression ) ) ;
78
70
}
79
71
else {
80
- const templateWithoutSubstitution = node as NoSubstitutionTemplateLiteral ;
81
- const stringLiteral = createStringLiteral ( templateWithoutSubstitution . text ) ;
72
+ const stringLiteral = createStringLiteral ( templateLiteral . text ) ;
82
73
return textChanges . ChangeTracker . with ( context , t => t . replaceNode ( context . file , node , stringLiteral ) ) ;
83
74
}
84
75
}
0 commit comments