-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(ruby): properly highlight %r{} regex literals containing nested patterns #4286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix(ruby): properly highlight %r{} regex literals containing nested patterns #4286
Conversation
Previously, the Ruby grammar only recognized %r{} regex literals in limited contexts, causing patterns like %r{(\.{2}|\A/)} to break syntax highlighting. This fix: - Adds a top-level REGEXP mode to support all %r{} variations globally - Enables nested brace handling inside %r{...} - Removes redundant %r handling tied to RE_STARTERS_RE - Adds a test case to regexes.txt to prevent regression
src/languages/ruby.js
Outdated
{ begin: '%r\\(', end: '\\)[a-z]*' }, | ||
{ begin: '%r!', end: '![a-z]*' }, | ||
{ begin: '%r\\[', end: '\\][a-z]*' }, | ||
{ begin: '/', end: '/[a-z]*' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these should be regex literals, not strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joshgoebel fixed 👍
hljs.BACKSLASH_ESCAPE, | ||
SUBST, | ||
{ | ||
begin: /\{/, end: /\}/, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about a {
or }
inside of a character group []
... please adds some tests and show that this would also be properly handled... I worry this is going to get very hard to get right without a full regex parser.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just handling []
with escapes inside (to allow escaping a other closing ]
) would be sufficient though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joshgoebel this should now be fixed, I also added some tests to verify these cases.
101142167176
***@***.***
Jacques Erasmus ***@***.***> 于 2025年7月4日周五 19:49写道:
… ***@***.**** commented on this pull request.
------------------------------
In src/languages/ruby.js
<#4286 (comment)>
:
> @@ -227,6 +227,27 @@ export default function(hljs) {
]
};
+ const REGEXP = {
+ className: 'regexp',
+ contains: [
+ hljs.BACKSLASH_ESCAPE,
+ SUBST,
+ {
+ begin: /\{/, end: /\}/,
@joshgoebel <https://github.com/joshgoebel> this should now be fixed, I
also added some tests to verify these cases.
—
Reply to this email directly, view it on GitHub
<#4286 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BK2N76VR5FECLYCE47MX7X33GZS4NAVCNFSM6AAAAACAIM4ROGVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDSOBWHA3DINRWHA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
- Add character class handling to prevent {} inside [] from being treated as interpolation - Support escaped closing brackets (\]) within character classes - Ensure quantifiers like {3} still work outside character classes - Add test cases to verify edge cases are handled properly
eb40cc7
to
10e9c97
Compare
Summary
This fixes an issue where
%r{}
-style regular expressions in Ruby were not correctly highlighted when they contained nested patterns or complex escape sequences. Patterns like(\.{2}|\A/)
would either break highlighting or be ignored entirely due to improper handling of nesting inside%r{}
.Resolves: #4281
Root Cause
Highlight.js previously treated
%r{}
regex literals as flat strings. This caused the parser to:{2}
(quantifiers)\A
Fix
REGEXP
mode to match%r{}
,%r()
,%r[]
,%r!
, etc. anywhere in Ruby code%r{}
usingcontains: ['self']
%r
variants from the legacyRE_STARTERS_RE
block (which still handles/.../
contexts)Screenshots