Skip to content

Commit

Permalink
fix: support for hermes
Browse files Browse the repository at this point in the history
  • Loading branch information
StarHosea committed Sep 2, 2023
1 parent 22d3f8e commit 5f017a8
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packages/json-schema/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@ const ExpRE = /^\s*\{\{([\s\S]*)\}\}\s*$/
const Registry = {
silent: false,
compile(expression: string, scope = {}) {
expression = expression.replaceAll(/(\$\w+)/g, '$root.$1');
// 检查如果 scope 前缀都有 $ ,生成的 function 可以不使用 with 操作符
const prefixed = Object.keys(scope).every((key) => key.startsWith('$'))
if (prefixed) {
expression = expression.replaceAll(/(\$\w+)/g, '$root.$1')
}
expression = prefixed
? expression.replaceAll(/(\$\w+)/g, '$root.$1')
: expression
const funBody = prefixed
? `return (${expression});`
: `with($root){ return (${expression});}`

if (Registry.silent) {
try {
return new Function('$root', `return (${expression});`)(
scope
)
return new Function('$root', funBody)(scope)
} catch {}
} else {
return new Function('$root', `return (${expression});`)(
scope
)
return new Function('$root', funBody)(scope)
}
},
}
Expand Down

0 comments on commit 5f017a8

Please sign in to comment.