2
2
namespace ts . refactor . convertStringOrTemplateLiteral {
3
3
const refactorName = "Convert string concatenation or template literal" ;
4
4
const toTemplateLiteralActionName = "Convert to template literal" ;
5
- const toStringConcatenationActionName = "Convert to string concatenation" ;
6
5
7
6
const refactorDescription = getLocaleSpecificMessage ( Diagnostics . Convert_string_concatenation_or_template_literal ) ;
8
7
const toTemplateLiteralDescription = getLocaleSpecificMessage ( Diagnostics . Convert_to_template_literal ) ;
9
- const toStringConcatenationDescription = getLocaleSpecificMessage ( Diagnostics . Convert_to_string_concatenation ) ;
10
8
11
9
registerRefactor ( refactorName , { getEditsForAction, getAvailableActions } ) ;
12
10
@@ -20,14 +18,6 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
20
18
refactorInfo . actions . push ( { name : toTemplateLiteralActionName , description : toTemplateLiteralDescription } ) ;
21
19
return [ refactorInfo ] ;
22
20
}
23
-
24
- const templateLiteral = findAncestor ( node , n => isTemplateLiteral ( n ) ) ;
25
-
26
- if ( templateLiteral && ! isTaggedTemplateExpression ( templateLiteral . parent ) ) {
27
- refactorInfo . actions . push ( { name : toStringConcatenationActionName , description : toStringConcatenationDescription } ) ;
28
- return [ refactorInfo ] ;
29
- }
30
-
31
21
return emptyArray ;
32
22
}
33
23
@@ -46,17 +36,13 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
46
36
return node ;
47
37
}
48
38
49
- function getEditsForAction ( context : RefactorContext , actionName : string ) : RefactorEditInfo | undefined {
39
+ function getEditsForAction ( context : RefactorContext , actionName : typeof toTemplateLiteralActionName ) : RefactorEditInfo | undefined {
50
40
const { file, startPosition } = context ;
51
41
const node = getNodeOrParentOfParentheses ( file , startPosition ) ;
52
42
53
43
switch ( actionName ) {
54
44
case toTemplateLiteralActionName :
55
45
return { edits : getEditsForToTemplateLiteral ( context , node ) } ;
56
-
57
- case toStringConcatenationActionName :
58
- return { edits : getEditsForToStringConcatenation ( context , node ) } ;
59
-
60
46
default :
61
47
return Debug . fail ( "invalid action" ) ;
62
48
}
@@ -85,33 +71,6 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
85
71
}
86
72
}
87
73
88
- const templateSpanToExpressions = ( file : SourceFile ) => ( templateSpan : TemplateSpan ) : Expression [ ] => {
89
- const { expression, literal } = templateSpan ;
90
- const text = literal . text ;
91
- copyTrailingAsLeadingComments ( templateSpan , expression , file , SyntaxKind . MultiLineCommentTrivia , /* hasTrailingNewLine */ false ) ;
92
- return text . length === 0 ? [ expression ] : [ expression , createStringLiteral ( text ) ] ;
93
- } ;
94
-
95
- function getEditsForToStringConcatenation ( context : RefactorContext , node : Node ) {
96
- const templateLiteral = findAncestor ( node , n => isTemplateLiteral ( n ) ) ! as TemplateLiteral ;
97
-
98
- if ( isTemplateExpression ( templateLiteral ) ) {
99
- const { head, templateSpans } = templateLiteral ;
100
- const spanToExpressionWithComment = templateSpanToExpressions ( context . file ) ;
101
- const arrayOfNodes = templateSpans . map ( spanToExpressionWithComment )
102
- . reduce ( ( accumulator , nextArray ) => accumulator . concat ( nextArray ) ) ;
103
-
104
- if ( head . text . length !== 0 ) arrayOfNodes . unshift ( createStringLiteral ( head . text ) ) ;
105
-
106
- const singleExpressionOrBinary = makeSingleExpressionOrBinary ( arrayOfNodes ) ;
107
- return textChanges . ChangeTracker . with ( context , t => t . replaceNode ( context . file , templateLiteral , singleExpressionOrBinary ) ) ;
108
- }
109
- else {
110
- const stringLiteral = createStringLiteral ( templateLiteral . text ) ;
111
- return textChanges . ChangeTracker . with ( context , t => t . replaceNode ( context . file , node , stringLiteral ) ) ;
112
- }
113
- }
114
-
115
74
function isNotEqualsOperator ( node : BinaryExpression ) {
116
75
return node . operatorToken . kind !== SyntaxKind . EqualsToken ;
117
76
}
@@ -123,26 +82,6 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
123
82
return expr ;
124
83
}
125
84
126
- function makeSingleExpressionOrBinary ( nodes : readonly Expression [ ] ) : Expression {
127
- if ( nodes . length > 1 ) {
128
- const left = nodes [ 0 ] ;
129
- const right = nodes [ 1 ] ;
130
-
131
- const binary = createBinary ( left , SyntaxKind . PlusToken , right ) ;
132
- return arrayToTree ( nodes , 2 , binary ) ;
133
- }
134
-
135
- return nodes [ 0 ] ;
136
- }
137
-
138
- function arrayToTree ( nodes : readonly Expression [ ] , index : number , accumulator : BinaryExpression ) : Expression {
139
- if ( nodes . length === index ) return accumulator ;
140
-
141
- const right = nodes [ index ] ;
142
- const binary = createBinary ( accumulator , SyntaxKind . PlusToken , right ) ;
143
- return arrayToTree ( nodes , index + 1 , binary ) ;
144
- }
145
-
146
85
function isStringConcatenationValid ( node : Node ) : boolean {
147
86
const { containsString, areOperatorsValid } = treeToArray ( node ) ;
148
87
return containsString && areOperatorsValid ;
0 commit comments