@@ -252,29 +252,39 @@ async function processTemplates(
252252}
253253
254254// Expand strings with @{} expression in them by evaluating and then interpreting as JSON.
255- function expandSchema ( schema : any , scope : any ) : any {
255+ function expandSchema ( schema : any , scope : any , path : string , inProperties : boolean , missingIsError : boolean , feedback : Feedback ) : any {
256256 let newSchema = schema
257257 if ( Array . isArray ( schema ) ) {
258258 newSchema = [ ]
259259 for ( let val of schema ) {
260- let newVal = expandSchema ( val , scope )
260+ let newVal = expandSchema ( val , scope , path , false , missingIsError , feedback )
261261 newSchema . push ( newVal )
262262 }
263263 } else if ( typeof schema === 'object' ) {
264264 newSchema = { }
265265 for ( let [ key , val ] of Object . entries ( schema ) ) {
266- let newVal = expandSchema ( val , scope )
266+ let newPath = path
267+ if ( inProperties ) {
268+ newPath += newPath === '' ? key : '.' + key ;
269+ }
270+ let newVal = expandSchema ( val , { ...scope , property : newPath } , newPath , key === 'properties' , missingIsError , feedback )
267271 newSchema [ key ] = newVal
268272 }
269273 } else if ( typeof schema === 'string' && schema . startsWith ( '@{' ) ) {
270274 let expr = schema . substring ( 2 , schema . length - 1 )
271275 try {
272276 let { value , error } = expressionEngine . parse ( expr ) . tryEvaluate ( scope )
273- if ( ! error ) {
277+ if ( ! error && value ) {
274278 newSchema = value
279+ } else {
280+ if ( missingIsError ) {
281+ feedback ( FeedbackType . error , `${ expr } : ${ error } ` )
282+ } else {
283+ newSchema = expr ;
284+ }
275285 }
276286 } catch ( e ) {
277- throw new Error ( `${ expr } : ${ e . message } ` )
287+ feedback ( FeedbackType . error , `${ expr } : ${ e . message } ` )
278288 }
279289 }
280290 return newSchema
@@ -332,6 +342,7 @@ export async function generate(
332342 try {
333343 await fs . ensureDir ( outDir )
334344 let schema = await processSchemas ( schemaPath , templateDirs , feedback )
345+ schema . schema = expandSchema ( schema . schema , { } , '' , false , false , feedback )
335346 let scope : any = {
336347 locales : allLocales ,
337348 schemaName : schema . name ( ) ,
@@ -351,7 +362,7 @@ export async function generate(
351362
352363 // Expand schema expressions
353364 let name = s . Schema . basename ( schemaPath )
354- let expanded = expandSchema ( schema . schema , scope )
365+ let expanded = expandSchema ( schema . schema , scope , '' , false , true , feedback )
355366 let body = JSON . stringify ( expanded , ( key , val ) => ( key === '$templates' || key === '$requires' ) ? undefined : val , 4 )
356367 await writeFile ( ppath . join ( outDir , `${ name } .schema.dialog` ) , body , force , feedback )
357368 } catch ( e ) {
0 commit comments