Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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
45 changes: 29 additions & 16 deletions src/languages/ruby.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,34 @@ export default function(hljs) {
]
};

const REGEXP = {
className: 'regexp',
contains: [
hljs.BACKSLASH_ESCAPE,
SUBST,
{
begin: /\[/,
end: /\]/,
contains: [
hljs.BACKSLASH_ESCAPE,
{ begin: /\\./ }
]
},
{
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]*/ }
]
};

const PARAMS = {
variants: [
{
Expand Down Expand Up @@ -324,6 +352,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 +401,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
6 changes: 6 additions & 0 deletions test/markup/ruby/regexes.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ 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>
str =~ <span class="hljs-regexp">/[{}]/</span>
str =~ <span class="hljs-regexp">/[\]]/</span>
str =~ <span class="hljs-regexp">/[a-z]{3}/</span>
str =~ <span class="hljs-regexp">%r{[{}]+}</span>
str =~ <span class="hljs-regexp">/<span class="hljs-subst">#{var}</span>[{}]/</span>
6 changes: 6 additions & 0 deletions test/markup/ruby/regexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ str =~ %r{foo|bar|buz$}
str =~ %r!foo|bar$!
str =~ %r[foo|bar$]
str =~ %r(\(foo|bar\)$)
str =~ %r{(\.{2}|\A/)}
str =~ /[{}]/
str =~ /[\]]/
str =~ /[a-z]{3}/
str =~ %r{[{}]+}
str =~ /#{var}[{}]/