Add python slice support to jinja-compat. #976
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.
Summary
Proposed change:
This pull-request adds support for python-style slice support (e.g.
arr[start:stop:step]
).jinja-compat.js
is the only file in this project that indents with two spaces, so my first commit changes it to four-space indent to bring it in conformity with the rest of the project.There were not any existing tests for jinja compatibility, so I had to make some slight changes to the unit tests to have it work. Part of these changes was having
nunjucks.installJinjaCompat
return a function that, when called, undoes the jinja-compat changes, so that the jinja changes did not apply to other unit tests in the test suite. I wasn't sure if this was worth documenting in the API, since it strikes me as something that is unlikely to be used outside of the tests, but I would be happy to do so if requested.Closes #188 .
Checklist
I've completed the checklist below to ensure I didn't forget anything. This makes reviewing this PR as easy as possible for the maintainers. And it gets this change released as soon as possible.
With regard to the first bullet point above, support for python slice syntax is expressly listed as a feature which falls outside the purpose of Nunjucks. But the same developer that wrote that statement of purpose also suggested in #188 that, if this functionality were to be added, it should be added to jinja-compat. So I assumed that means that while it shouldn't be assumed that nunjucks would support language-specific syntax, it isn't forbidden either. Under this assumption I've added that clarification to the purpose section of CONTRIBUTING.md.