wolfendale / scala-nunjucks

5 stars 1 forks source link

Filter call precedence is wrong #15

Open wolfendale opened 4 years ago

wolfendale commented 4 years ago

In nunjucks:

{{ 10 / -5 | abs }}

outputs 2

In this library it outputs -2 because filter call binds extremely loosely.

I'm not sure of the exact precedence it should have, one to experiment with.

amaalali commented 4 years ago

@wolfendale Following up on some of our investigations, I've looked at the javascript nunjucks implementation and found the following so far:

  1. filters have higher precedence than power, eg for {{ 2 ** -5 | abs }} the result is 32 instead of 0.03125

  2. filters have lower precedence than expressions with unary modifiers - and +

    1. for unary -: eg for {{ -5.5 | round }} the result is -5 instead of -6
    2. for unary +: eg for {{ +true | float }} the result is 1 and for {{ +(true | float) }} the result is NaN

Open for investigation still:

  1. Filters precedence with unary modifiers not is unclear
    {{ false | default("foo", true) }} // foo
    {{ not false | default("foo", true) }} // false
    {{ not (false | default("foo", true)) }} // false
    {{ true | default("foo", true) }} // true
    {{ not true | default("foo", true) }} // false
wolfendale commented 4 years ago

@amaalali really interesting, currently the we give unary +, - and not equal precedence so that hints at another subtle bug.