Skip to content

Commit

Permalink
fix: errors
Browse files Browse the repository at this point in the history
  • Loading branch information
learosema committed Oct 9, 2024
1 parent 67fa60d commit dfd50d9
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/transforms/template-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ function htmlEscape(input) {
}

function safeEval(snippet, context) {
const s = new vm.Script(snippet);
let result = context && vm.isContext(context) ? s.runInContext(context) : s.runInNewContext(context || {Object, Array});
return result;
try {
const s = new vm.Script(snippet);
let result = context && vm.isContext(context) ? s.runInContext(context) : s.runInNewContext(context || {Object, Array});
return result;
} catch (err) {
if (err.name === 'ReferenceError') {
console.warn(`ReferenceError: ${err.message}`);
return '';
}
throw err;
}
}

export function parseFilterExpression(expr, ctx) {
Expand Down Expand Up @@ -53,6 +61,9 @@ export function template(str) {
const mainExpression = expressions[0];
const filterExpressions = expressions.slice(1);
let result = safeEval(mainExpression, context);
if (typeof result === 'undefined') {
result = '';
}
if (typeof result === 'function') {
result = result();
}
Expand Down

0 comments on commit dfd50d9

Please sign in to comment.