@@ -8,8 +8,6 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
8
8
const toTemplateLiteralDescription = getLocaleSpecificMessage ( Diagnostics . Convert_to_template_literal ) ;
9
9
const toStringConcatenationDescription = getLocaleSpecificMessage ( Diagnostics . Convert_to_string_concatenation ) ;
10
10
11
- // TODO let a = 45 - 45 + " ee" - 33;
12
-
13
11
registerRefactor ( refactorName , { getEditsForAction, getAvailableActions } ) ;
14
12
15
13
function getAvailableActions ( context : RefactorContext ) : ReadonlyArray < ApplicableRefactorInfo > {
@@ -19,7 +17,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
19
17
const maybeTemplateExpression = findAncestor ( node , n => isTemplateExpression ( n ) ) ;
20
18
const actions : RefactorActionInfo [ ] = [ ] ;
21
19
22
- if ( ( isBinaryExpression ( maybeBinary ) || isStringLiteral ( maybeBinary ) ) && containsString ( maybeBinary ) ) {
20
+ if ( ( isBinaryExpression ( maybeBinary ) || isStringLiteral ( maybeBinary ) ) && isStringConcatenationValid ( maybeBinary ) ) {
23
21
actions . push ( { name : toTemplateLiteralActionName , description : toTemplateLiteralDescription } ) ;
24
22
}
25
23
@@ -39,7 +37,6 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
39
37
const maybeBinary = getParentBinaryExpression ( node ) ;
40
38
const arrayOfNodes = treeToArray ( maybeBinary ) [ 0 ] ;
41
39
const templateLiteral = nodesToTemplate ( arrayOfNodes ) ;
42
-
43
40
const edits = textChanges . ChangeTracker . with ( context , t => t . replaceNode ( file , maybeBinary , templateLiteral ) ) ;
44
41
return { edits } ;
45
42
@@ -97,7 +94,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
97
94
const left = nodes [ 0 ] ;
98
95
const right = nodes [ 1 ] ;
99
96
100
- const binary = createBinary ( left , SyntaxKind . PlusToken , right ) ;
97
+ const binary = createBinary ( left , SyntaxKind . PlusToken , right ) ;
101
98
return arrayToTree ( nodes . slice ( 2 ) , binary ) ;
102
99
}
103
100
@@ -106,16 +103,25 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
106
103
return arrayToTree ( nodes . slice ( 1 ) , binary ) ;
107
104
}
108
105
109
- function treeToArray ( node : Node ) : [ Node [ ] , boolean ] {
106
+ function isStringConcatenationValid ( node : Node ) : boolean {
107
+ const [ , containsString , areOperatorsValid ] = treeToArray ( node ) ;
108
+ return containsString && areOperatorsValid ;
109
+ }
110
+
111
+ function treeToArray ( node : Node ) : [ Node [ ] , /* containsString */ boolean , /* areOperatorsValid */ boolean ] {
110
112
if ( isBinaryExpression ( node ) ) {
111
- const [ leftNodes , leftHasString ] = treeToArray ( node . left ) ;
112
- const [ rightNodes , rightHasString ] = treeToArray ( node . right ) ;
113
+ const [ leftNodes , leftHasString , leftOp ] = treeToArray ( node . left ) ;
114
+ const [ rightNodes , rightHasString , rightOp ] = treeToArray ( node . right ) ;
115
+
116
+ if ( ! leftHasString && ! rightHasString ) return [ [ node ] , false , true ] ;
117
+
118
+ const currentOp = node . operatorToken . kind === SyntaxKind . PlusToken ;
119
+ const isPlus = leftOp && currentOp && rightOp ;
113
120
114
- if ( ! leftHasString && ! rightHasString ) return [ [ node ] , false ] ;
115
- return [ leftNodes . concat ( rightNodes ) , true ] ;
121
+ return [ leftNodes . concat ( rightNodes ) , true , isPlus ] ;
116
122
}
117
123
118
- return [ [ node ] , isStringLiteral ( node ) ] ;
124
+ return [ [ node ] , isStringLiteral ( node ) , true ] ;
119
125
}
120
126
121
127
function nodesToTemplate ( nodes : Node [ ] ) {
0 commit comments