Respect explicitly-set undefined or null values in a frame. #481
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.
This fixes #478, which is a nasty subtle bug that leads to very strange and difficult-to-diagnose behaviors in calling template macros with arguments whose default value is
none
.The core of the problem is nunjucks' loose handling of
null
andundefined
in symbol value lookups. Symbol values are looked up at runtime usingcontextOrFrameLookup
. If the frame lookup returnsnull
orundefined
,contextOrFrameLookup
considers that a "not found" and looks up the name in the context instead. Similarly, with stacked frames, if anundefined
ornull
value is found for the name in one frame, it ignores that and goes up to the parent frame instead. This means that you effectively cannot have a variable explicitly set to a value ofundefined
ornull
in a scope - if a parent scope (frame or context) has some other value for that symbol name, it will be used instead.This pull request changes that behavior to instead use
hasOwnProperty
to determine whether a variable is present in a given frame or not, and never go up the frame stack (or fall back to context) if the variable is present, regardless of its value.(If #480 is accepted, this PR could be changed to still consider
undefined
the same as "not found", but supportnull
. That would be enough to support variables being set tonone
, which is really the motivating use case here.)