Add 'none' as a literal evaluating to null. #480
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I ran across this in working on the fix for #478.
Jinja2 has three special constants:
true
,false
, andnone
(mapping to Python'sTrue
,False
, andNone
, respectively).Nunjucks has
true
andfalse
(mapping to JStrue
andfalse
), but no explicit support fornone
.This has gone unaddressed thus far, because in practice a Jinja template using
none
ports just fine to nunjucks anyway, as the symbolnone
evaluates toundefined
, which behaves similarly-enough to Python'sNone
(mostly just in that it's falsy) to make no real difference.But there are some subtle problems with this.
First, it's pointlessly inefficient. There's no reason for use of
none
to require acontextOrFrameLookup
at runtime every place it is used.Second, it allows for the name
none
to be re-bound to some other value, resulting in confusing code and needless Jinja incompatibility.Third, the logical mapping for
none
to JS is reallynull
, notundefined
. Most of the time this makes no difference, but it does make a difference with thedefault
filter. In Jinja,{{ none|default('d', false) }}
evaluates toNone
(becauseNone
is not an undefined value), whereas in nunjucks it printsd
(because it is undefined).This pull request turns
none
into a lexed constant just liketrue
andfalse
, with the valuenull
.This could be backwards incompatible, if someone is relying on the current behavior of
none
with thedefault
filter, or is re-binding the namenone
to some other value. I think this backwards-incompatibility is acceptable as a bugfix, and the time to do it is now (before 2.0 is released).