Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'none' as a literal evaluating to null. #480

Merged
merged 2 commits into from
Sep 2, 2015

Conversation

carljm
Copy link
Contributor

@carljm carljm commented Jul 21, 2015

I ran across this in working on the fix for #478.

Jinja2 has three special constants: true, false, and none (mapping to Python's True, False, and None, respectively).

Nunjucks has true and false (mapping to JS true and false), but no explicit support for none.

This has gone unaddressed thus far, because in practice a Jinja template using none ports just fine to nunjucks anyway, as the symbol none evaluates to undefined, which behaves similarly-enough to Python's None (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 a contextOrFrameLookup 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 really null, not undefined. Most of the time this makes no difference, but it does make a difference with the default filter. In Jinja, {{ none|default('d', false) }} evaluates to None (because None is not an undefined value), whereas in nunjucks it prints d (because it is undefined).

This pull request turns none into a lexed constant just like true and false, with the value null.

This could be backwards incompatible, if someone is relying on the current behavior of none with the default filter, or is re-binding the name none 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).

@SamyPesse
Copy link
Contributor

@carljm This PR looks ready to be merged

@carljm
Copy link
Contributor Author

carljm commented Sep 2, 2015

@SamyPesse Thanks for the nudge, I think you're right. Merging now.

* master: (30 commits)
  Consolidate changelog.
  Catch correctly error from loaders
  Add multiline multi block raw test
  Fix "broken raw blocks test" by strictly searching for raw or endraw blocks only
  Fix multiple raw blocks with less greedy regex
  Add test for multiple raw blocks
  add maintenance note
  fix links for docs in french
  add error handler to chokidar instance (fixes mozilla#469)
  fix jshint error
  don't break in environment where Error.prototype.name can't be set (fixes mozilla#454)
  use chokidar correctly (fixes mozilla#450)
  include bin and src when using bower (fixes mozilla#444)
  fix version in browserfiles
  prep for 2.0.0
  Docs in french
  Docs : fix link
  fix docs: typo parameter caseSens
  Update web-loaders.js
  Rewrite Parser.parseRaw
  ...
carljm added a commit that referenced this pull request Sep 2, 2015
Add 'none' as a literal evaluating to null.
@carljm carljm merged commit 7893c26 into mozilla:master Sep 2, 2015
@carljm carljm deleted the none-constant branch September 2, 2015 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants