@@ -3,7 +3,7 @@ import { precompile as glimmerPrecompile } from '@glimmer/compiler';
33import type { SerializedTemplateWithLazyBlock } from '@glimmer/interfaces' ;
44import { setComponentTemplate } from '@glimmer/manager' ;
55import { templateFactory } from '@glimmer/opcode-compiler' ;
6- import compileOptions from './compile-options' ;
6+ import compileOptions , { keywords } from './compile-options' ;
77import type { EmberPrecompileOptions } from './types' ;
88
99type ComponentClass = abstract new ( ...args : any [ ] ) => object ;
@@ -237,38 +237,48 @@ export function template(
237237 templateString : string ,
238238 providedOptions ?: BaseTemplateOptions | BaseClassTemplateOptions < any >
239239) : object {
240- const options : EmberPrecompileOptions = { strictMode : true , ...providedOptions } ;
241- const evaluate = buildEvaluator ( options ) ;
240+ const options = { strictMode : true , ...providedOptions } ;
242241
242+ const evaluate = buildEvaluator ( options ) ;
243243 const normalizedOptions = compileOptions ( options ) ;
244244 const component = normalizedOptions . component ?? templateOnly ( ) ;
245245
246246 const source = glimmerPrecompile ( templateString , normalizedOptions ) ;
247- const template = templateFactory ( evaluate ( `(${ source } )` ) as SerializedTemplateWithLazyBlock ) ;
247+ const wire = evaluate ( `(${ source } )` ) as SerializedTemplateWithLazyBlock ;
248+
249+ const template = templateFactory ( wire ) ;
248250
249251 setComponentTemplate ( template , component ) ;
250252
251253 return component ;
252254}
253255
254256const evaluator = ( source : string ) => {
255- return new Function ( `return ${ source } ` ) ( ) ;
257+ return new Function ( `return ${ source } ` ) ( ) ;
256258} ;
257259
258- function buildEvaluator ( options : Partial < EmberPrecompileOptions > | undefined ) {
259- if ( options === undefined ) {
260- return evaluator ;
261- }
262-
260+ /**
261+ * Builds the source wireformat JSON block
262+ *
263+ * @param options
264+ * @returns
265+ */
266+ function buildEvaluator ( options : Partial < EmberPrecompileOptions > ) {
263267 if ( options . eval ) {
264268 return options . eval ;
265269 } else {
266- const scope = options . scope ?.( ) ;
270+ /**
271+ * This is ran before the template is compiled,
272+ * so we cannot use any information gathered during template compilation.
273+ */
274+ let scope = options . scope ?.( ) ;
267275
268276 if ( ! scope ) {
269277 return evaluator ;
270278 }
271279
280+ scope = Object . assign ( { } , keywords , scope ) ;
281+
272282 return ( source : string ) => {
273283 let hasThis = Object . prototype . hasOwnProperty . call ( scope , 'this' ) ;
274284 let thisValue = hasThis ? ( scope as { this ?: unknown } ) . this : undefined ;
0 commit comments