Skip to content

Report a syntax error for unclosed brackets in expressions#968

Merged
RobLoach merged 1 commit into
twigjs:masterfrom
apoorva-01:fix/896-unclosed-brace-syntax-error
Jul 6, 2026
Merged

Report a syntax error for unclosed brackets in expressions#968
RobLoach merged 1 commit into
twigjs:masterfrom
apoorva-01:fix/896-unclosed-brace-syntax-error

Conversation

@apoorva-01

Copy link
Copy Markdown
Contributor

{{{ foo }} rendered as [object Object] instead of a syntax error (TwigPHP throws Unexpected "}" here, per @ericmorand on the issue). The extra { opens an object that never closes, so its group-start token is left on the shunting-yard stack, flushed into the output, and handed back by the parser as-is. Twig.expression.compile now throws if an opening {, [ or ( is still on the stack once the expression is consumed. Valid expressions always pop their start at the matching close, so it can't fire on well-formed input. (By default twig swallows compile errors and renders empty, which is why the reporter's try/catch never fired; rethrow: true surfaces it like any other syntax error.)

Two calls for you: I made the check cover all four grouping constructs, not just the object from the issue, but I'll narrow it to { if you'd rather. And the message is Expected closing '}' rather than TwigPHP's Unexpected "}" at the trailing brace; matching that exactly is more work, so say the word.

Tests are in test/test.regression.js.

closes #896

`{{{ foo }}` rendered as `[object Object]` instead of failing. The extra
brace opens an object that never closes, so its group-start token was
left on the shunting-yard stack, flushed into the output, and then
handed straight back by the parser.

Check the stack once the expression is consumed and throw if an opening
`{`, `[` or `(` was never matched. A well formed expression always pops
its start at the matching close, so this only fires on genuinely
unbalanced input.
@willrowe willrowe requested a review from RobLoach July 2, 2026 14:17
@RobLoach RobLoach merged commit 04d87f1 into twigjs:master Jul 6, 2026
3 checks passed
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.

Bug when using triple starting braces.

3 participants