TomFrost / Jexl

Javascript Expression Language: Powerful context-based expression parser and evaluator
MIT License
559 stars 90 forks source link

Operator precedence between || and && #91

Open wasimshariff opened 3 years ago

wasimshariff commented 3 years ago

Hi Tom,

i encountered one issue with operator precedence in jexl library. Wanted to check with you before making a change at my end locally.

In grammer.js :

'&&': { type: 'binaryOp', precedence: 11, eval: (left, right) => left && right }, '||': { type: 'binaryOp', precedence: 10, eval: (left, right) => left || right }

Why does && and !! have same precedence. Usually in JS world, && has higher precedence.

Let me know, if you need any more info.

Thanks, Wasim

TomFrost commented 3 years ago

Hey Wasim,

That's a really great catch. You're absolutely right -- && has higher precedence in most languages. That's a bug in the initial design for sure.

Changing it would be a breaking change, but I can look at introducing that in a major 3.0 release. Until then, the workarounds would be to use parentheses in your expression to force precedence, or just call addBinaryOp to re-add the && operator with higher precedence. That would allow you to keep upgrading Jexl and not edit the source directly.

wasimshariff commented 3 years ago

Cool, thank you Tom. I cant rewrite the expression. I will patch the jexl library locally using patch-package and get rid of it when 3.x is out. Thanks