Skip to content
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

enh(parser) better regular expression detection #2380

Merged
merged 3 commits into from Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGES.md
Expand Up @@ -10,7 +10,7 @@ New themes:

Core Changes:

- none.
- Improve regular expression detect (less false-positives) (#2380) [Josh Goebel][]

Language Improvements:

Expand All @@ -22,6 +22,7 @@ Developer Tools:

- none.

[Josh Goebel]: https://github.com/yyyc514
[Sam Miller]: https://github.com/smillerc
[Robert Riebisch]: https://github.com/bttrx
[Taufik Nurrohman]: https://github.com/taufik-nurrohman
Expand Down
31 changes: 20 additions & 11 deletions src/highlight.js
Expand Up @@ -1095,17 +1095,26 @@ https://highlightjs.org/
relevance: 0
};
hljs.REGEXP_MODE = {
className: 'regexp',
begin: /\//, end: /\/[gimuy]*/,
illegal: /\n/,
contains: [
hljs.BACKSLASH_ESCAPE,
{
begin: /\[/, end: /\]/,
relevance: 0,
contains: [hljs.BACKSLASH_ESCAPE]
}
]
// this outer rule makes sure we actually have a WHOLE regex and not simply
// an expression such as:
//
// 3 / something
//
// (which will then blow up when regex's `illegal` sees the newline)
begin: /(?=\/[^\/\n]*\/)/,
contains: [{
className: 'regexp',
begin: /\//, end: /\/[gimuy]*/,
illegal: /\n/,
contains: [
hljs.BACKSLASH_ESCAPE,
{
begin: /\[/, end: /\]/,
relevance: 0,
contains: [hljs.BACKSLASH_ESCAPE]
}
]
}]
};
hljs.TITLE_MODE = {
className: 'title',
Expand Down
5 changes: 5 additions & 0 deletions test/markup/javascript/regex.expect.txt
@@ -0,0 +1,5 @@
<span class="hljs-comment">// examples that have proven problematic in the past</span>
<span class="hljs-string">`Bad <span class="hljs-subst">${foo / <span class="hljs-number">1000</span>}</span>`</span>
foo = <span class="hljs-number">2</span> / <span class="hljs-number">2</span> + <span class="hljs-number">2</span> / <span class="hljs-number">2</span>;
foo = <span class="hljs-regexp">/ 2 + 2 /</span>;
b = a++ / <span class="hljs-number">2</span>;
5 changes: 5 additions & 0 deletions test/markup/javascript/regex.txt
@@ -0,0 +1,5 @@
// examples that have proven problematic in the past
`Bad ${foo / 1000}`
foo = 2 / 2 + 2 / 2;
foo = / 2 + 2 /;
b = a++ / 2;