Skip to content

Commit cb3c05e

Browse files
authored
Engine: Don't compile <% # coments %> (marcoroth#826)
Follow up to marcoroth#825. This pull request improves the engine by not compiling `<% # coments %> comments as part of the template. Outputting `# coments` could lead to some cases, where the template could be compiled but couldn't be rendered, because it would comment out important pieces of the template. ```erb <% if true %><% # comment %><%= "Hello World" %><% end %> ``` **Before** `exe/herb compile test.html.erb` ```ruby __herb = ::Herb::Engine; _buf = ::String.new; if true; # comment _buf << __herb.h(("Hello World")); end; _buf << ' '.freeze; _buf.to_s ``` **After** `exe/herb compile test.html.erb` ```ruby __herb = ::Herb::Engine; _buf = ::String.new; if true; _buf << __herb.h(("Hello World")); end; _buf << ' '.freeze; _buf.to_s ``` ### For reference `bin/erubi-compile test.html.erb` ```ruby __erubi = ::Erubi; _buf = ::String.new; if true ; # comment ; _buf << __erubi.h(( "Hello World" )); end ; _buf << ' '.freeze; _buf.to_s ``` `bin/erubi-render test.html.erb` ``` bin/erubi-render:117:in 'Kernel#eval': (eval at bin/erubi-render:117):2: syntax errors found (SyntaxError) 1 | __erubi = ::Erubi; _buf = ::String.new; if true ; # comment ; _buf << __erubi.h(( "Hello World" )); end ; _buf << ' > 2 | '.freeze; | ^ unterminated string meets end of file 3 | _buf.to_s > 4 | | ^ expected an `end` to close the conditional clause | ^ unexpected end-of-input, assuming it is closing the parent top level context from bin/erubi-render:117:in 'ErubiRenderer#render_template' from bin/erubi-render:31:in 'ErubiRenderer#run' from bin/erubi-render:152:in '<main>' ``` `exe/herb render test.html.erb` ``` Hello World ```
1 parent a772b70 commit cb3c05e

File tree

24 files changed

+238
-1
lines changed

24 files changed

+238
-1
lines changed

lib/herb/ast/helpers.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ def erb_comment?(opening)
2121
def erb_output?(opening)
2222
opening.include?("=")
2323
end
24+
25+
#: (Herb::AST::ERBContentNode) -> bool
26+
def inline_ruby_comment?(node)
27+
return false unless node.is_a?(Herb::AST::ERBContentNode)
28+
return false if erb_comment?(node.tag_opening&.value || "")
29+
30+
content = node.content&.value || ""
31+
stripped = content.lstrip
32+
33+
stripped.start_with?("#") && node.location.start.line == node.location.end.line
34+
end
2435
end
2536
end
2637
end

lib/herb/engine/compiler.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ def visit_cdata_node(node)
162162
end
163163

164164
def visit_erb_content_node(node)
165+
return if inline_ruby_comment?(node)
166+
165167
process_erb_tag(node)
166168
end
167169

sig/herb/ast/helpers.rbs

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/engine/erb_comments_test.rb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../test_helper"
4+
require_relative "../snapshot_utils"
5+
require_relative "../../lib/herb/engine"
6+
7+
module Engine
8+
class ERBCommentsTest < Minitest::Spec
9+
include SnapshotUtils
10+
11+
test "inline ruby comment on same line" do
12+
template = %(<% if true %><% # Comment here %><% end %>)
13+
14+
assert_compiled_snapshot(template)
15+
end
16+
17+
test "inline ruby comment with newline" do
18+
template = "<% if true %><% # Comment here %>\n<% end %>"
19+
20+
assert_compiled_snapshot(template)
21+
end
22+
23+
test "inline ruby comment between code" do
24+
template = %(<% if true %><% # Comment here %><%= "hello" %><% end %>)
25+
26+
assert_compiled_snapshot(template)
27+
end
28+
29+
test "inline ruby comment before and between code" do
30+
template = %(<% # Comment here %><% if true %><% # Comment here %><%= "hello" %><% end %>)
31+
32+
assert_compiled_snapshot(template)
33+
end
34+
35+
test "inline ruby comment with spaces" do
36+
template = %(<% # Comment %> <% code = "test" %><%= code %>)
37+
38+
assert_compiled_snapshot(template)
39+
end
40+
41+
test "inline ruby comment multiline" do
42+
template = "<% # Comment\nmore %> <% code = \"test\" %><%= code %>"
43+
44+
assert_compiled_snapshot(template)
45+
end
46+
47+
test "evaluation: inline ruby comment on same line" do
48+
template = %(<% if true %><% # Comment here %><% end %>)
49+
50+
assert_evaluated_snapshot(template)
51+
end
52+
53+
test "evaluation: inline ruby comment with newline" do
54+
template = "<% if true %><% # Comment here %>\n<% end %>"
55+
56+
assert_evaluated_snapshot(template)
57+
end
58+
59+
test "evaluation: inline ruby comment between code" do
60+
template = %(<% if true %><% # Comment here %><%= "hello" %><% end %>)
61+
62+
assert_evaluated_snapshot(template)
63+
end
64+
65+
test "evaluation: inline ruby comment before and between code" do
66+
template = %(<% # Comment here %><% if true %><% # Comment here %><%= "hello" %><% end %>)
67+
68+
assert_evaluated_snapshot(template)
69+
end
70+
71+
test "evaluation: inline ruby comment with spaces" do
72+
template = %(<% # Comment %> <% code = "test" %><%= code %>)
73+
74+
assert_evaluated_snapshot(template)
75+
end
76+
77+
test "evaluation: inline ruby comment multiline" do
78+
template = "<% # Comment\nmore %> <% code = \"test\" %><%= code %>"
79+
80+
assert_evaluated_snapshot(template, { more: "ignored" })
81+
end
82+
end
83+
end

test/engine/evaluation_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,5 +398,41 @@ class EvaluationTest < Minitest::Spec
398398

399399
assert_evaluated_snapshot(template, {}, { escape: false })
400400
end
401+
402+
test "inline ruby comment on same line" do
403+
template = %(<% if true %><% # Comment here %><% end %>)
404+
405+
assert_evaluated_snapshot(template, {}, { escape: false })
406+
end
407+
408+
test "inline ruby comment with newline" do
409+
template = "<% if true %><% # Comment here %>\n<% end %>"
410+
411+
assert_evaluated_snapshot(template, {}, { escape: false })
412+
end
413+
414+
test "inline ruby comment between code" do
415+
template = %(<% if true %><% # Comment here %><%= "hello" %><% end %>)
416+
417+
assert_evaluated_snapshot(template, {}, { escape: false })
418+
end
419+
420+
test "inline ruby comment before and between code" do
421+
template = %(<% # Comment here %><% if true %><% # Comment here %><%= "hello" %><% end %>)
422+
423+
assert_evaluated_snapshot(template, {}, { escape: false })
424+
end
425+
426+
test "inline ruby comment with spaces" do
427+
template = %(<% # Comment %> <% code = "test" %><%= code %>)
428+
429+
assert_evaluated_snapshot(template, {}, { escape: false })
430+
end
431+
432+
test "inline ruby comment multiline" do
433+
template = "<% # Comment\nmore %> <% code = \"test\" %><%= code %>"
434+
435+
assert_evaluated_snapshot(template, { more: "ignored" }, { escape: false })
436+
end
401437
end
402438
end

test/snapshots/engine/erb_comments_test/test_0001_inline_ruby_comment_on_same_line_ece56a54ce9835acc67012583c42fc01.txt

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/snapshots/engine/erb_comments_test/test_0002_inline_ruby_comment_with_newline_32dd50121c8e2af22b8a7e5efd92940f.txt

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/snapshots/engine/erb_comments_test/test_0003_inline_ruby_comment_between_code_987d6e126382f02311aa0c1a0e0007bc.txt

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/snapshots/engine/erb_comments_test/test_0004_inline_ruby_comment_before_and_between_code_d6e28172fd69bf4538744950bffb2848.txt

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/snapshots/engine/erb_comments_test/test_0005_inline_ruby_comment_with_spaces_a1f69e41bb63eacbc881533c704e17a4.txt

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)