Open fdb opened 9 months ago
If memory serves correctly this issue is raised in another ticket, likely to not be obvious at first but when digging in to the issue it's all the same (again if memory serves).
The work-around is to make your negative value an 'expression' ie. "add(5, 0 - 3)" instead of "add(5, -3)"
Thanks! Stupid but it works :-) I now have the following code:
// Jexl can't deal with unary "-" operators. This function replaces this with a binary operator.
// It's ridiculous, but it works.
// So an expression like this will fail: add(1, -1)
// We replace it with: add(1, 0-1)
// We don't do full tokenization, we just use regular expressions to fix common cases.
function fixupExpression(expr) {
return expr.replace(/,\s*-/g, ', 0-');
}
export function evalExpression(expr) {
expr = fixupExpression(expr);
return jexl.evalSync(expr, context);
}
I wanted to use a function with negative values. In the first argument position this works. However in the second argument position Jexl is confused because it expects a binaryOp and this is a unaryOp.
Here's the stack trace:
Here's a demo on Replit: https://replit.com/@fdb/JexlBinaryOpConfusion