Skip to content

fix: support whitespace control on comments - #793

Open
wleeaf wants to merge 1 commit into
sunng87:masterfrom
wleeaf:fix-comment-whitespace-control
Open

wleeaf wants to merge 1 commit into
sunng87:masterfrom
wleeaf:fix-comment-whitespace-control

Conversation

@wleeaf

@wleeaf wleeaf commented Oct 24, 2026

Copy link
Copy Markdown

Comments don't accept the ~ whitespace control that every other tag takes, so templates written for handlebars.js fail to compile:

hbs.render_template("a {{~! comment ~}} b", &())
// Err: invalid handlebars syntax: expected identifier, subexpression, or path_inline

handlebars.js renders ab. {{~!-- comment --~}} fails the same way, and {{! comment ~}} parses but keeps the whitespace because the ~ ends up in the comment text.

This allows a ~ after the opening {{ and before the closing }} of both comment forms in the grammar. The template builder then strips the whitespace on that side, the same way it does for expressions. A comment now also sets omit_pro_ws from its own closing tag, so {{foo~}}{{! comment }} b keeps the spaces after the comment like handlebars.js (bar b). Before, the earlier ~ carried over the comment and removed them.

Tests: test_comment_whitespace_control in tests/whitespace.rs covers both comment forms with ~ on either side, a comment containing }}, a standalone comment with a trailing ~, and the ~ that must not carry over a comment. The expected strings are what handlebars.js 4.7.10 renders. The test fails on master with the parse error above and passes with the change. cargo test --all-features, cargo clippy --all-features --all-targets -- -D warnings and cargo fmt -- --check pass.

{{~! comment ~}} and {{~!-- comment --~}} failed to parse, and a ~ before
the closing braces of a compact comment was kept as comment text. Accept
the tildes in the comment rules and strip the surrounding whitespace like
for other tags. A comment also resets the whitespace omission of the tag
before it, as in handlebars.js.
Copilot AI balanced review requested due to automatic review settings October 24, 2026 12:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

This branch has not been deployed

No deployments
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.

2 participants