Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(ruby): properly highlight nested %r{} regex
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
  • Loading branch information
j-erasmus committed Jun 27, 2025
commit 71fdc0dffadd69579b4ff8ebead6f131dbccbe8c
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Core Grammars:
- enh(json) add json5 support [Kerry Shetline][]
- fix(css) `unicode-range` parsing, issue #4253 [Kerry Shetline][]
- fix(csharp) Support digit separators [te-ing][]
- fix(ruby) support nested content in `%r{}` regex literals outside expression contexts [Jacques Erasmus][]

Documentation:

Expand Down Expand Up @@ -53,6 +54,7 @@ CONTRIBUTORS
[Thomas Gorissen]: https://github.com/serrynaimo
[te-ing]: https://github.com/te-ing
[Anthony Martin]: https://github.com/anthony-c-martin
[Jacques Erasmus]: https://github.com/j-erasmus


## Version 11.11.1
Expand Down
38 changes: 22 additions & 16 deletions src/languages/ruby.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,27 @@ export default function(hljs) {
]
};

const REGEXP = {
className: 'regexp',
contains: [
hljs.BACKSLASH_ESCAPE,
SUBST,
{
begin: /\{/, end: /\}/,
Copy link
Member

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.

Copy link
Member

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?

Copy link
Author

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.

contains: ['self', hljs.BACKSLASH_ESCAPE, SUBST],
relevance: 0
}
],
illegal: /\n/,
variants: [
{ begin: /%r\{/, end: /\}[a-z]*/ },
{ begin: '%r\\(', end: '\\)[a-z]*' },
{ begin: '%r!', end: '![a-z]*' },
{ begin: '%r\\[', end: '\\][a-z]*' },
{ begin: '/', end: '/[a-z]*' }
Copy link
Member

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.

Copy link
Author

Choose a reason for hiding this comment

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

@joshgoebel fixed 👍

]
};

const PARAMS = {
variants: [
{
Expand Down Expand Up @@ -324,6 +345,7 @@ export default function(hljs) {
UPPER_CASE_CONSTANT,
CLASS_REFERENCE,
METHOD_DEFINITION,
REGEXP,
{
// swallow namespace qualifiers before symbols
begin: hljs.IDENT_RE + '::' },
Expand Down Expand Up @@ -372,22 +394,6 @@ export default function(hljs) {
{
begin: '/',
end: '/[a-z]*'
},
{
begin: /%r\{/,
end: /\}[a-z]*/
},
{
begin: '%r\\(',
end: '\\)[a-z]*'
},
{
begin: '%r!',
end: '![a-z]*'
},
{
begin: '%r\\[',
end: '\\][a-z]*'
}
]
}
Expand Down
1 change: 1 addition & 0 deletions test/markup/ruby/regexes.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ str =~ <span class="hljs-regexp">%r{foo|bar|buz$}</span>
str =~ <span class="hljs-regexp">%r!foo|bar$!</span>
str =~ <span class="hljs-regexp">%r[foo|bar$]</span>
str =~ <span class="hljs-regexp">%r(\(foo|bar\)$)</span>
str =~ <span class="hljs-regexp">%r{(\.{2}|\A/)}</span>
1 change: 1 addition & 0 deletions test/markup/ruby/regexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ str =~ %r{foo|bar|buz$}
str =~ %r!foo|bar$!
str =~ %r[foo|bar$]
str =~ %r(\(foo|bar\)$)
str =~ %r{(\.{2}|\A/)}