Repository navigation
Report token and location for common template syntax errors - #680
Merged
Merged
Conversation
- Using '!' as an argument (e.g. {{#if !value}}) now throws a
HandlebarsCompilerException naming the offending token and its location
and pointing to #unless, instead of an internal "must be reducible node"
error wrapped twice.
- "Reached end of template before block expression ... was closed" now
reports the location of the unclosed opening tag.
- "A closing element ... was found without a matching open element" now
reports the location of the stray closing tag.
- "Starting and ending handlebars do not match" now says which braces
were used to open and close the expression.
Source locations were already tracked on tokens but were dropped when
helper arguments were accumulated and when words became path
expressions; they are now carried through. Exception types are
unchanged, and existing message text is kept as a prefix.
Refs #314
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
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.



Refs #314
Improves compile-time error messages for several malformed-template cases from #314. Exception types stay the same, and existing message text is kept as a prefix, so callers matching on type or message prefix keep working.
{{#if !File}} {{/if}}An unhandled exception occurred while trying to compile the template→ innerArgumentException: must be reducible nodeUnexpected '!File' inside an expression: '!' can only start a comment (e.g. '{{! comment }}') and is not a negation operator. Use '#unless' to negate a condition+Occured at: 0:6{{#each items}}test(never closed)Reached end of template before block expression '#each' was closed{{#each}}{{#if 0}}test{{/unless}}A closing element '/unless' was found without a matching open element{{/unless}}{{File}}}Starting and ending handlebars do not match+ location: expression opened with '{{' was closed with '}}}'How
HelperArgumentAccumulatorrebuiltHelperExpressionwithout its context) and when words becamePathExpressions. Both now keep the location, which also gives location info to other existing errors that already passedhelperExpression.Context(e.g.#if requires exactly one argument).CommentToken/LayoutTokennow record where they start.CommentAndLayoutConverterrejects a!comment that doesn't come straight after the opening braces. Every such template already failed to compile (verified for block helpers, simple helpers,#if/#unless/#each/#with,else if, hash arguments, partials and sub-expressions), so the only change is a clearer error. Normal comments ({{! x }},{{ ! x }},{{!-- ... --}},{{~! x ~}}) and layouts ({{!< layout}}) behave as before.Not changed
{{#File}}, whereFileisn't a registered block helper and no{{/File}}follows, still compiles and rendersFile's value. Handlebars.js rejects this, but making it throw would break existing templates, so that decision is left to a maintainer.line:columnformat with 0-based line numbers.Tests
New cases in
ExceptionTestscover each message and its reported location, and check that comments still compile. The full suite passes (2224 tests).🤖 Generated with Claude Code
Closes #314