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

fix(ruby) Heredoc without interpolation #3154

Merged
merged 6 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Parser:

Grammars:

- enh(crystal) highlight variables (#3154) [Josh Goebel][]
- fix(ruby) Heredoc without interpolation (#3154) [Josh Goebel][]
- enh(swift) add `@resultBuilder` attribute (#3151) [Bradley Mackey][]
- enh(processing) added `pde` alias (#3142) [Dylan McBean][]
- enh(thrift) Use proper scope for types [Josh Goebel][]
Expand Down
8 changes: 8 additions & 0 deletions src/languages/crystal.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ export default function(hljs) {
end: /\}/,
keywords: CRYSTAL_KEYWORDS
};
// borrowed from Ruby
const VARIABLE = {
// negative-look forward attemps to prevent false matches like:
// @ident@ or $ident$ that might indicate this is not ruby at all
className: "variable",
begin: '(\\$\\W)|((\\$|@@?)(\\w+))(?=[^@$?])' + `(?![A-Za-z])(?![@$?'])`
};
const EXPANSION = {
className: 'template-variable',
variants: [
Expand Down Expand Up @@ -211,6 +218,7 @@ export default function(hljs) {
REGEXP2,
REGEXP,
ATTRIBUTE,
VARIABLE,
hljs.HASH_COMMENT_MODE,
{
className: 'class',
Expand Down
14 changes: 8 additions & 6 deletions src/languages/ruby.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,15 @@ export default function(hljs) {
{
begin: /\B\?\\?\S/
},
{ // heredocs
begin: /<<[-~]?'?(\w+)\n(?:[^\n]*\n)*?\s*\1\b/,
returnBegin: true,
// heredocs
{
// this guard makes sure that we have an entire heredoc and not a false
// positive (auto-detect, etc.)
begin: regex.concat(
/<<[-~]?'?/,
regex.lookahead(/(\w+)(?=\W)[^\n]*\n(?:[^\n]*\n)*?\s*\1\b/)
),
contains: [
{
begin: /<<[-~]?'?/
},
hljs.END_SAME_AS_BEGIN({
begin: /(\w+)/,
end: /(\w+)/,
Expand Down
18 changes: 17 additions & 1 deletion test/markup/ruby/heredoc.expect.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
<span class="hljs-comment"># standard heredoc</span>
message = <span class="hljs-string">&lt;&lt;-MESSAGE
This looks good
MESSAGE</span>

<span class="hljs-comment"># heredoc without interpolation</span>
message = <span class="hljs-string">&lt;&lt;-&#x27;MESSAGE&#x27;
This isn&#x27;t highlighted correctly
MESSAGE</span>

<span class="hljs-comment"># with a method call</span>
message = <span class="hljs-string">&lt;&lt;-MESSAGE.chomp
This looks good
MESSAGE</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">foo</span><span class="hljs-params">()</span></span>
msg = <span class="hljs-string">&lt;&lt;-HTML
&lt;div&gt;
Expand All @@ -12,4 +27,5 @@
&lt;h4&gt;<span class="hljs-subst">#{bar}</span>&lt;/h4&gt;
&lt;/div&gt;
FOO</span>
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>

18 changes: 17 additions & 1 deletion test/markup/ruby/heredoc.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# standard heredoc
message = <<-MESSAGE
This looks good
MESSAGE

# heredoc without interpolation
message = <<-'MESSAGE'
This isn't highlighted correctly
MESSAGE

# with a method call
message = <<-MESSAGE.chomp
This looks good
MESSAGE

def foo()
msg = <<-HTML
<div>
Expand All @@ -12,4 +27,5 @@ def baz()
<h4>#{bar}</h4>
</div>
FOO
end
end