Skip to content

Commit

Permalink
refactor(json-schema): use with statement for compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Aug 12, 2021
1 parent c54a2df commit f913b35
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/json-schema/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ const ExpRE = /^\s*\{\{([\s\S]*)\}\}\s*$/
const actionsSymbol = Symbol.for('__REVA_ACTIONS')
const ENVS = {
silent: false,
compile(expression: string, scope: any) {
const vars = Object.keys(scope || {})
const params = vars.map((key) => scope[key])
compile(expression: string, scope = {}) {
if (ENVS.silent) {
try {
return new Function(...vars, `return (${expression});`)(...params)
return new Function('$root', `with($root) { return (${expression}); }`)(
scope
)
} catch {}
} else {
return new Function(...vars, `return (${expression});`)(...params)
return new Function('$root', `with($root) { return (${expression}); }`)(
scope
)
}
},
}
Expand Down

0 comments on commit f913b35

Please sign in to comment.