@@ -4102,86 +4102,22 @@ namespace ts {
41024102 * @param node A TemplateExpression node.
41034103 */
41044104 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 ) ] ;
41494108
4150- return node . head . text . length !== 0 || node . templateSpans [ 0 ] . literal . text . length === 0 ;
4151- }
4109+ if ( span . literal . text . length > 0 ) {
4110+ args . push ( factory . createStringLiteral ( span . literal . text ) ) ;
4111+ }
41524112
4153- /**
4154- * Adds the head of a TemplateExpression to an array of expressions.
4155- *
4156- * @param expressions An array of expressions.
4157- * @param node A TemplateExpression node.
4158- */
4159- function addTemplateHead ( expressions : Expression [ ] , node : TemplateExpression ) : void {
4160- if ( ! shouldAddTemplateHead ( node ) ) {
4161- return ;
4113+ expression = factory . createCallExpression (
4114+ factory . createPropertyAccessExpression ( expression , "concat" ) ,
4115+ /*typeArguments*/ undefined ,
4116+ args ,
4117+ ) ;
41624118 }
41634119
4164- expressions . push ( factory . createStringLiteral ( node . head . text ) ) ;
4165- }
4166-
4167- /**
4168- * Visits and adds the template spans of a TemplateExpression to an array of expressions.
4169- *
4170- * @param expressions An array of expressions.
4171- * @param node A TemplateExpression node.
4172- */
4173- function addTemplateSpans ( expressions : Expression [ ] , node : TemplateExpression ) : void {
4174- for ( const span of node . templateSpans ) {
4175- expressions . push ( visitNode ( span . expression , visitor , isExpression ) ) ;
4176-
4177- // Only emit if the literal is non-empty.
4178- // The binary '+' operator is left-associative, so the first string concatenation
4179- // with the head will force the result up to this point to be a string.
4180- // Emitting a '+ ""' has no semantic effect for middles and tails.
4181- if ( span . literal . text . length !== 0 ) {
4182- expressions . push ( factory . createStringLiteral ( span . literal . text ) ) ;
4183- }
4184- }
4120+ return setTextRange ( expression , node ) ;
41854121 }
41864122
41874123 /**
0 commit comments