@@ -12,8 +12,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
12
12
13
13
function getAvailableActions ( context : RefactorContext ) : ReadonlyArray < ApplicableRefactorInfo > {
14
14
const { file, startPosition } = context ;
15
- let node = getTokenAtPosition ( file , startPosition ) ;
16
- if ( isParenthesizedExpression ( node . parent ) && isBinaryExpression ( node . parent . parent ) ) node = node . parent . parent ;
15
+ const node = getNodeOrParentOfBraces ( file , startPosition ) ;
17
16
const maybeBinary = getParentBinaryExpression ( node ) ;
18
17
const actions : RefactorActionInfo [ ] = [ ] ;
19
18
@@ -28,11 +27,17 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
28
27
return [ { name : refactorName , description : refactorDescription , actions } ] ;
29
28
}
30
29
30
+ function getNodeOrParentOfBraces ( file : SourceFile , startPosition : number ) {
31
+ const node = getTokenAtPosition ( file , startPosition ) ;
32
+ if ( isParenthesizedExpression ( node . parent ) && isBinaryExpression ( node . parent . parent ) ) return node . parent . parent ;
33
+ return node ;
34
+ }
35
+
31
36
function isTemplateLike ( node : Node ) {
32
- const isEmptyTemplate = isNoSubstitutionTemplateLiteral ( node ) && isNotTagged ( node ) ;
37
+ const isTemplateWithoutSubstitution = isNoSubstitutionTemplateLiteral ( node ) && isNotTagged ( node ) ;
33
38
const isTemplate = ( isTemplateHead ( node ) || isTemplateMiddleOrTemplateTail ( node ) ) && isNotTagged ( node . parent ) ;
34
39
const isTemplateFromExpression = isTemplateSpan ( node . parent ) && isNotTagged ( node . parent . parent ) ;
35
- return isEmptyTemplate || isTemplate || isTemplateFromExpression ;
40
+ return isTemplateWithoutSubstitution || isTemplate || isTemplateFromExpression ;
36
41
}
37
42
38
43
function isNotTagged ( templateExpression : Node ) {
@@ -41,46 +46,45 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
41
46
42
47
function getEditsForAction ( context : RefactorContext , actionName : string ) : RefactorEditInfo | undefined {
43
48
const { file, startPosition } = context ;
44
- let node = getTokenAtPosition ( file , startPosition ) ;
49
+ const node = getNodeOrParentOfBraces ( file , startPosition ) ;
45
50
46
51
switch ( actionName ) {
47
52
case toTemplateLiteralActionName :
48
- if ( isParenthesizedExpression ( node . parent ) && isBinaryExpression ( node . parent . parent ) ) node = node . parent . parent ;
49
53
const maybeBinary = getParentBinaryExpression ( node ) ;
50
-
51
54
const arrayOfNodes = transformTreeToArray ( maybeBinary ) ;
52
55
const templateLiteral = nodesToTemplate ( arrayOfNodes ) ;
53
56
const edits = textChanges . ChangeTracker . with ( context , t => t . replaceNode ( file , maybeBinary , templateLiteral ) ) ;
54
57
return { edits } ;
55
58
56
59
case toStringConcatenationActionName :
57
- if ( isNoSubstitutionTemplateLiteral ( node ) ) {
58
- const stringLiteral = createStringLiteral ( node . text ) ;
59
- return { edits : textChanges . ChangeTracker . with ( context , t => t . replaceNode ( file , node , stringLiteral ) ) } ;
60
+ return { edits : getEditsForToStringConcatenation ( context , node ) } ;
60
61
61
- }
62
- if ( isTemplateExpression ( node . parent ) || isTemplateSpan ( node . parent ) ) {
63
- const templateLiteralExpression = isTemplateSpan ( node . parent ) ? node . parent . parent : node . parent ;
64
- const nodesArray : Expression [ ] = [ ] ;
65
-
66
- if ( templateLiteralExpression . head . text . length !== 0 ) nodesArray . push ( createStringLiteral ( templateLiteralExpression . head . text ) ) ;
62
+ default :
63
+ return Debug . fail ( "invalid action" ) ;
64
+ }
65
+ }
67
66
68
- templateLiteralExpression . templateSpans . forEach ( ts => {
69
- nodesArray . push ( ts . expression ) ;
70
- const str = ts . literal . text ;
71
- if ( str . length !== 0 ) {
72
- nodesArray . push ( createStringLiteral ( str ) ) ;
73
- }
74
- } ) ;
67
+ 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 ;
71
+ const arrayOfNodes : Expression [ ] = [ ] ;
75
72
76
- const binaryExpression = arrayToTree ( nodesArray ) ;
77
- return { edits : textChanges . ChangeTracker . with ( context , t => t . replaceNode ( file , templateLiteralExpression , binaryExpression ) ) } ;
78
- }
73
+ if ( head . text . length !== 0 ) arrayOfNodes . push ( createStringLiteral ( head . text ) ) ;
79
74
80
- break ;
75
+ templateSpans . forEach ( ts => {
76
+ arrayOfNodes . push ( ts . expression ) ;
77
+ const text = ts . literal . text ;
78
+ if ( text . length !== 0 ) arrayOfNodes . push ( createStringLiteral ( text ) ) ;
79
+ } ) ;
81
80
82
- default :
83
- return Debug . fail ( "invalid action" ) ;
81
+ const binaryExpression = arrayToTree ( arrayOfNodes ) ;
82
+ return textChanges . ChangeTracker . with ( context , t => t . replaceNode ( context . file , templateLiteralExpression , binaryExpression ) ) ;
83
+ }
84
+ else {
85
+ const templateWithoutSubstitution = node as NoSubstitutionTemplateLiteral ;
86
+ const stringLiteral = createStringLiteral ( templateWithoutSubstitution . text ) ;
87
+ return textChanges . ChangeTracker . with ( context , t => t . replaceNode ( context . file , node , stringLiteral ) ) ;
84
88
}
85
89
}
86
90
0 commit comments