Skip to content

Fix escaped-mustache highlighting for odd backslash runs#117

Merged
daaain merged 1 commit into
masterfrom
claude/handlebars-escaped-backslashes-xirrma
Jul 16, 2026
Merged

Fix escaped-mustache highlighting for odd backslash runs#117
daaain merged 1 commit into
masterfrom
claude/handlebars-escaped-backslashes-xirrma

Conversation

@daaain

@daaain daaain commented Jul 16, 2026

Copy link
Copy Markdown
Owner

Background

Follow-up to #116, which added highlighting for escaped mustaches (\{{foo}}). A review comment on that PR pointed out that it fails on \\\{{foo}} and asked for a rule for escaped backslashes.

The bug

The escaped_expression rule used a negative lookbehind (?<!\\) to avoid treating a double backslash (\\{{foo}}) as a mustache escape. That handles one-vs-two backslashes, but whether a mustache is escaped actually depends on the parity of the whole backslash run in front of it — because backslashes escape each other too:

Input Backslashes Meaning Before
\{{foo}} 1 (odd) escaped mustache ✅ escaped
\\{{foo}} 2 (even) escaped backslash, mustache evaluates ✅ evaluates
\\\{{foo}} 3 (odd) escaped backslash + escaped mustache wrongly evaluated
\\\\{{foo}} 4 (even) two escaped backslashes, mustache evaluates ✅ evaluates

So \\\{{foo}} was highlighted as a live expression when it should render literally.

The fix

Add an escaped_backslash rule that matches each \\ pair leading up to a mustache and scopes it as constant.character.escape.handlebars. Since those pairs are consumed first, escaped_expression only ever sees the lone leftover backslash of an odd-length run:

  • odd run → a backslash is left over → mustache is escaped
  • even run → no backslash left → mustache evaluates

This matches Handlebars semantics for any number of backslashes, so the lookbehind is no longer needed and is dropped. The new rule's lookahead ((?=\\*\{{2,3})) keeps bare backslashes in plain text — e.g. Windows paths — from being highlighted as escapes.

Wired into the main patterns and the inline <script> template body across all three grammar files (Handlebars.json, Handlebars.sublime-syntax, Handlebars.tmLanguage).

Tests

Extends test/escaping.test.js with coverage for three- and four-backslash runs, escaped-backslash highlighting, and bare backslashes in text. Full suite passes (57 tests).

🤖 Generated with Claude Code

https://claude.ai/code/session_011RgLiYPtaei9qTBqYbAGeN


Generated by Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved syntax highlighting for Handlebars expressions preceded by backslashes.
    • Correctly distinguishes escaped mustaches from literal backslash pairs.
    • Handles odd and even backslash sequences consistently, including within inline scripts.
  • Tests

    • Expanded coverage for single, double, triple, and quadruple backslash escape scenarios.
    • Verified that unrelated backslashes are not incorrectly highlighted.

The escaped_expression rule added in #116 used a negative lookbehind
(?<!\\) to avoid treating a double backslash (\\{{foo}}) as a mustache
escape. That works for one vs two backslashes, but it breaks for longer
runs: whether a mustache is escaped depends on the *parity* of the
backslash run in front of it, not just on whether a backslash precedes.
So \\\{{foo}} (three backslashes: an escaped backslash plus an escaping
backslash) was wrongly highlighted as a live expression instead of an
escaped mustache. Reported on PR #116.

Add an escaped_backslash rule that matches each \\ pair leading up to a
mustache and scopes it as constant.character.escape.handlebars. Because
those pairs are consumed first, escaped_expression only ever sees the
lone leftover backslash of an odd-length run — so an odd run escapes the
mustache and an even run leaves it to evaluate, matching Handlebars. The
lookbehind is no longer needed and is dropped. The rule's lookahead keeps
bare backslashes in plain text (e.g. Windows paths) from being
highlighted as escapes.

Wired into the main patterns and the inline <script> template body across
all three grammar files. Extends test/escaping.test.js with coverage for
three- and four-backslash runs, escaped-backslash highlighting, and bare
backslashes in text.
@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 105148a1-7b19-452f-9c8d-566105ca83c2

📥 Commits

Reviewing files that changed from the base of the PR and between a5aa65d and 0ffd0a2.

📒 Files selected for processing (4)
  • grammars/Handlebars.json
  • grammars/Handlebars.sublime-syntax
  • grammars/Handlebars.tmLanguage
  • test/escaping.test.js

📝 Walkthrough

Walkthrough

The Handlebars grammars now parse escaped backslash pairs separately from escaped mustaches across JSON, Sublime, and TextMate formats. Escaping tests cover odd and even backslash parity, escape scopes, and unrelated backslashes.

Changes

Handlebars escaping

Layer / File(s) Summary
Separate escaped backslash parsing
grammars/Handlebars.json, grammars/Handlebars.sublime-syntax, grammars/Handlebars.tmLanguage
Adds escaped_backslash handling before escaped_expression in top-level and embedded inline-script parsing, replacing the previous negative-lookbehind behavior.
Backslash parity coverage
test/escaping.test.js
Tests double-, triple-, and quadruple-backslash runs, escaped-backslash scopes, mustache tokenization, and backslashes not preceding mustaches.

Estimated code review effort: 2 (Simple) | ~15 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: fixing escaped-mustache highlighting for odd backslash runs.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/handlebars-escaped-backslashes-xirrma

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@daaain
daaain merged commit 443233e into master Jul 16, 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.

2 participants