@@ -4102,87 +4102,22 @@ namespace ts {
4102
4102
* @param node A TemplateExpression node.
4103
4103
*/
4104
4104
function visitTemplateExpression ( node : TemplateExpression ) : Expression {
4105
- const expressions : Expression [ ] = [ ] ;
4106
- addTemplateHead ( expressions , node ) ;
4107
- addTemplateSpans ( expressions , node ) ;
4108
-
4109
- // createAdd will check if each expression binds less closely than binary '+'.
4110
- // If it does, it wraps the expression in parentheses. Otherwise, something like
4111
- // `abc${ 1 << 2 }`
4112
- // becomes
4113
- // "abc" + 1 << 2 + ""
4114
- // which is really
4115
- // ("abc" + 1) << (2 + "")
4116
- // rather than
4117
- // "abc" + (1 << 2) + ""
4118
- const expression = reduceLeft ( expressions , factory . createAdd ) ! ;
4119
- if ( nodeIsSynthesized ( expression ) ) {
4120
- setTextRange ( expression , node ) ;
4121
- }
4122
-
4123
- return expression ;
4124
- }
4125
-
4126
- /**
4127
- * Gets a value indicating whether we need to include the head of a TemplateExpression.
4128
- *
4129
- * @param node A TemplateExpression node.
4130
- */
4131
- function shouldAddTemplateHead ( node : TemplateExpression ) {
4132
- // If this expression has an empty head literal and the first template span has a non-empty
4133
- // literal, then emitting the empty head literal is not necessary.
4134
- // `${ foo } and ${ bar }`
4135
- // can be emitted as
4136
- // foo + " and " + bar
4137
- // This is because it is only required that one of the first two operands in the emit
4138
- // output must be a string literal, so that the other operand and all following operands
4139
- // are forced into strings.
4140
- //
4141
- // If the first template span has an empty literal, then the head must still be emitted.
4142
- // `${ foo }${ bar }`
4143
- // must still be emitted as
4144
- // "" + foo + bar
4145
-
4146
- // There is always atleast one templateSpan in this code path, since
4147
- // NoSubstitutionTemplateLiterals are directly emitted via emitLiteral()
4148
- Debug . assert ( node . templateSpans . length !== 0 ) ;
4105
+ let expression : Expression = factory . createStringLiteral ( node . head . text ) ;
4106
+ for ( const span of node . templateSpans ) {
4107
+ const args = [ visitNode ( span . expression , visitor , isExpression ) ] ;
4149
4108
4150
- const span = node . templateSpans [ 0 ] ;
4151
- return node . head . text . length !== 0 || span . literal . text . length === 0 || ! ! length ( getLeadingCommentRangesOfNode ( span . expression , currentSourceFile ) ) ;
4152
- }
4109
+ if ( span . literal . text . length > 0 ) {
4110
+ args . push ( factory . createStringLiteral ( span . literal . text ) ) ;
4111
+ }
4153
4112
4154
- /**
4155
- * Adds the head of a TemplateExpression to an array of expressions.
4156
- *
4157
- * @param expressions An array of expressions.
4158
- * @param node A TemplateExpression node.
4159
- */
4160
- function addTemplateHead ( expressions : Expression [ ] , node : TemplateExpression ) : void {
4161
- if ( ! shouldAddTemplateHead ( node ) ) {
4162
- return ;
4113
+ expression = factory . createCallExpression (
4114
+ factory . createPropertyAccessExpression ( expression , "concat" ) ,
4115
+ /*typeArguments*/ undefined ,
4116
+ args ,
4117
+ ) ;
4163
4118
}
4164
4119
4165
- expressions . push ( factory . createStringLiteral ( node . head . text ) ) ;
4166
- }
4167
-
4168
- /**
4169
- * Visits and adds the template spans of a TemplateExpression to an array of expressions.
4170
- *
4171
- * @param expressions An array of expressions.
4172
- * @param node A TemplateExpression node.
4173
- */
4174
- function addTemplateSpans ( expressions : Expression [ ] , node : TemplateExpression ) : void {
4175
- for ( const span of node . templateSpans ) {
4176
- expressions . push ( visitNode ( span . expression , visitor , isExpression ) ) ;
4177
-
4178
- // Only emit if the literal is non-empty.
4179
- // The binary '+' operator is left-associative, so the first string concatenation
4180
- // with the head will force the result up to this point to be a string.
4181
- // Emitting a '+ ""' has no semantic effect for middles and tails.
4182
- if ( span . literal . text . length !== 0 ) {
4183
- expressions . push ( factory . createStringLiteral ( span . literal . text ) ) ;
4184
- }
4185
- }
4120
+ return setTextRange ( expression , node ) ;
4186
4121
}
4187
4122
4188
4123
/**
0 commit comments