Skip to content

feat(lexer): include the offending tag in newline errors - #379

Open
ChrisJr404 wants to merge 1 commit into
flosch:masterfrom
ChrisJr404:lexer-newline-error-snippet
Open

feat(lexer): include the offending tag in newline errors#379
ChrisJr404 wants to merge 1 commit into
flosch:masterfrom
ChrisJr404:lexer-newline-error-snippet

Conversation

@ChrisJr404

Copy link
Copy Markdown

Closes #185.

When a template has a newline inside a {{ }} or {% %}, the lexer reports Newline not allowed within tag/variable. with no hint of which tag it was, which is a pain to track down in a large template. This appends the offending tag/variable to the message, quoted so the newline stays on a single line:

Newline not allowed within tag/variable: "{{test\n}}"

To do that the lexer remembers where the current tag opened (tagStart), since start keeps moving forward as inner tokens are emitted. The snippet runs from the opening delimiter to the first closing one, and it's length-capped so an unterminated tag can't blow up the error message.

Added a regression test in pongo2_issues_test.go covering a variable, a tag, and an unterminated tag. Happy to adjust the exact format if you'd prefer something else.

A newline inside a {{ }} or {% %} reported "Newline not allowed within
tag/variable." with no hint of which tag, which is hard to locate in a
large template. Quote the offending tag/variable in the message instead,
e.g. Newline not allowed within tag/variable: "{{test\n}}".

The lexer now remembers where the current tag opened, since start moves
forward as inner tokens are emitted. The snippet spans the opening
delimiter to the first closing one and is length-capped so an
unterminated tag can't flood the message.

Closes flosch#185
@sonarqubecloud

Copy link
Copy Markdown

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.

pongo lexer does not returns exact tag/variable in case of error

1 participant