Nunjucks: Object and array literals don't allow trailing commas

Created on 7 Jun 2017  路  3Comments  路  Source: mozilla/nunjucks

When using object or array literals in templates, if I put a comma after the last item I get a Template render error with a message unexpected token: ] for arrays and unexpected token: } for objects.

All of the examples below trigger these errors for me

object (single line)
{% set obj = { key: "value", anotherKey: "anotherValue", } %}
object (multiple lines)
{% set obj = {
     key: "value",
     anotherKey: "anotherValue",
} %}
array (single line)
{% set arr = [ "value", "anotherValue", ] %}
array (multiple lines)
{% set arr = [
     "value",
     "anotherValue",
] %}

Most helpful comment

I think this is valid. It appears that Jinja2 does allow trailing commas in the {% set %} tag, and it is also valid javascript since ECMAScript 5 for objects, earlier for arrays (IE 8 threw a syntax error, which is why many linters did not allow it).

All 3 comments

Well, that's how it works. Trailing commas are not allowed. As far as I know, same goes for original Jinja2

I think this is valid. It appears that Jinja2 does allow trailing commas in the {% set %} tag, and it is also valid javascript since ECMAScript 5 for objects, earlier for arrays (IE 8 threw a syntax error, which is why many linters did not allow it).

did anyone make progress on this? Happy to try and help out with a PR, its something that trips people up a lot when we're teaching them Nunjucks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gaboesquivel picture gaboesquivel  路  4Comments

sandiprb picture sandiprb  路  5Comments

boutell picture boutell  路  6Comments

mintyPT picture mintyPT  路  4Comments

kamlekar picture kamlekar  路  7Comments