Fix escaped-mustache highlighting for odd backslash runs#117
Merged
Conversation
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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughThe 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. ChangesHandlebars escaping
Estimated code review effort: 2 (Simple) | ~15 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
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.
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_expressionrule 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:\{{foo}}\\{{foo}}\\\{{foo}}\\\\{{foo}}So
\\\{{foo}}was highlighted as a live expression when it should render literally.The fix
Add an
escaped_backslashrule that matches each\\pair leading up to a mustache and scopes it asconstant.character.escape.handlebars. Since those pairs are consumed first,escaped_expressiononly ever sees the lone leftover backslash of an odd-length run: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.jswith 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
Tests