22namespace ts . refactor . convertStringOrTemplateLiteral {
33 const refactorName = "Convert string concatenation or template literal" ;
44 const toTemplateLiteralActionName = "Convert to template literal" ;
5- const toStringConcatenationActionName = "Convert to string concatenation" ;
65
76 const refactorDescription = getLocaleSpecificMessage ( Diagnostics . Convert_string_concatenation_or_template_literal ) ;
87 const toTemplateLiteralDescription = getLocaleSpecificMessage ( Diagnostics . Convert_to_template_literal ) ;
9- const toStringConcatenationDescription = getLocaleSpecificMessage ( Diagnostics . Convert_to_string_concatenation ) ;
108
119 registerRefactor ( refactorName , { getEditsForAction, getAvailableActions } ) ;
1210
@@ -20,14 +18,6 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
2018 refactorInfo . actions . push ( { name : toTemplateLiteralActionName , description : toTemplateLiteralDescription } ) ;
2119 return [ refactorInfo ] ;
2220 }
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-
3121 return emptyArray ;
3222 }
3323
@@ -46,17 +36,13 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
4636 return node ;
4737 }
4838
49- function getEditsForAction ( context : RefactorContext , actionName : string ) : RefactorEditInfo | undefined {
39+ function getEditsForAction ( context : RefactorContext , actionName : typeof toTemplateLiteralActionName ) : RefactorEditInfo | undefined {
5040 const { file, startPosition } = context ;
5141 const node = getNodeOrParentOfParentheses ( file , startPosition ) ;
5242
5343 switch ( actionName ) {
5444 case toTemplateLiteralActionName :
5545 return { edits : getEditsForToTemplateLiteral ( context , node ) } ;
56-
57- case toStringConcatenationActionName :
58- return { edits : getEditsForToStringConcatenation ( context , node ) } ;
59-
6046 default :
6147 return Debug . fail ( "invalid action" ) ;
6248 }
@@ -85,33 +71,6 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
8571 }
8672 }
8773
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-
11574 function isNotEqualsOperator ( node : BinaryExpression ) {
11675 return node . operatorToken . kind !== SyntaxKind . EqualsToken ;
11776 }
@@ -123,26 +82,6 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
12382 return expr ;
12483 }
12584
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-
14685 function isStringConcatenationValid ( node : Node ) : boolean {
14786 const { containsString, areOperatorsValid } = treeToArray ( node ) ;
14887 return containsString && areOperatorsValid ;
0 commit comments