@@ -21,28 +21,36 @@ import type {
2121 Parameter ,
2222} from '@angular/compiler-cli/src/ngtsc/translator/src/api/ast_factory' ;
2323
24+ const OBJECT_LITERAL_PREFIX = '\0obj_' ;
25+
2426/**
2527 * An implementation of `AstFactory` that generates JavaScript code strings directly.
2628 */
2729export class StringAstFactory implements AstFactory < string , unknown , string > {
2830 constructor ( private readonly sourceCode : string = '' ) { }
2931
3032 private render ( expr : unknown ) : string {
33+ let rendered : string ;
3134 if ( typeof expr === 'string' ) {
32- return expr ;
33- }
34- if (
35+ rendered = expr ;
36+ } else if (
3537 typeof expr === 'object' &&
3638 expr !== null &&
3739 typeof ( expr as { start ?: number ; end ?: number } ) . start === 'number' &&
3840 typeof ( expr as { end ?: number } ) . end === 'number'
3941 ) {
4042 const { start, end } = expr as { start : number ; end : number } ;
4143
42- return this . sourceCode . slice ( start , end ) ;
44+ rendered = this . sourceCode . slice ( start , end ) ;
45+ } else {
46+ rendered = String ( expr ) ;
4347 }
4448
45- return String ( expr ) ;
49+ if ( rendered . startsWith ( OBJECT_LITERAL_PREFIX ) ) {
50+ return rendered . slice ( OBJECT_LITERAL_PREFIX . length ) ;
51+ }
52+
53+ return rendered ;
4654 }
4755
4856 /**
@@ -177,9 +185,8 @@ export class StringAstFactory implements AstFactory<string, unknown, string> {
177185
178186 createArrowFunctionExpression ( parameters : Parameter < string > [ ] , body : unknown ) : string {
179187 const params = parameters . map ( ( p ) => p . name ) . join ( ', ' ) ;
188+ const isObjectLiteral = typeof body === 'string' && body . startsWith ( OBJECT_LITERAL_PREFIX ) ;
180189 const renderedBody = this . render ( body ) ;
181- const isObjectLiteral =
182- renderedBody . startsWith ( '{' ) && renderedBody . endsWith ( '}' ) && ! renderedBody . includes ( ';' ) ;
183190 const formattedBody = isObjectLiteral ? `(${ renderedBody } )` : renderedBody ;
184191
185192 return `(${ params } ) => ${ formattedBody } ` ;
@@ -222,7 +229,7 @@ export class StringAstFactory implements AstFactory<string, unknown, string> {
222229 return `${ key } : ${ this . render ( p . value ) } ` ;
223230 } ) ;
224231
225- return `{\n${ props . join ( ',\n' ) } \n}` ;
232+ return `${ OBJECT_LITERAL_PREFIX } {\n${ props . join ( ',\n' ) } \n}` ;
226233 }
227234
228235 createParenthesizedExpression ( expression : unknown ) : string {
0 commit comments