Add keepTrailingNewline option to LegacyOverrides to match Python Jinja2 default behaviour#1311
Open
jmoraleda wants to merge 1 commit intoHubSpot:masterfrom
Open
Add keepTrailingNewline option to LegacyOverrides to match Python Jinja2 default behaviour#1311jmoraleda wants to merge 1 commit intoHubSpot:masterfrom
jmoraleda wants to merge 1 commit intoHubSpot:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description:
By default, Python Jinja2 strips a single trailing newline from rendered output (
keep_trailing_newline=False). Jinjava has historically preserved trailing newlines, causing a subtle but consistent difference when porting templates from Python Jinja2.This PR adds a
keepTrailingNewlineflag toLegacyOverridesthat allows users to opt into Python-compatible behaviour.Changes:
LegacyOverrides: addsisKeepTrailingNewline(), defaulting totrue(preserve existing Jinjava behaviour — no impact on current users).JinjavaInterpreter: applies the strip instripTrailingNewlineIfNeeded(), called at the normal return points of the privaterender()method. Early-exit error paths (OutputTooBigException,CollectionTooBigException) are intentionally unaffected.TrailingNewlineTest: covers both flag values, theNONE/THREE_POINT_0/ALLpresets, and edge cases (no trailing newline, multiple trailing newlines, rendered expressions).Usage:
To opt into Python-compatible behaviour:
Compatibility note:
keepTrailingNewlineis set totrue(legacy behaviour) inTHREE_POINT_0to avoid breaking existing tests. It is set tofalseinALL. Maintainers may wish to move it tofalseinTHREE_POINT_0as well if they consider stripping the trailing newline to be part of the canonical Python-compatible behaviour set — the change is intentionally conservative here to leave that decision to the maintainers.