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

Improve comment syntax suggestions #324

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
10 changes: 8 additions & 2 deletions lib/erb_lint/linters/comment_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def run(processed_source)
return if file_content.empty?

processed_source.ast.descendants(:erb).each do |erb_node|
indicator_node, _, code_node, _ = *erb_node
indicator_node, trim, code_node, _ = *erb_node
next if code_node.nil?

indicator_node_str = indicator_node&.deconstruct&.last
Expand All @@ -27,7 +27,13 @@ def run(processed_source)
range = find_range(erb_node, code_node_str)
source_range = processed_source.to_source_range(range)

correct_erb_tag = indicator_node_str == "=" ? "<%#=" : "<%#"
correct_erb_tag = if indicator_node_str == "="
"<%#= or <%#"
elsif trim
"<%-#"
else
"<%#"
end

add_offense(
source_range,
Expand Down
18 changes: 14 additions & 4 deletions spec/erb_lint/linters/comment_syntax_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,25 @@
let(:file) { <<~FILE }
<% # first bad comment %>
<%= # second bad comment %>
<%- # third bad comment %>
FILE

it "reports two offenses" do
expect(subject.size).to(eq(2))
it "reports all offenses" do
expect(subject.size).to(eq(file.each_line.count))
end

it "reports the suggested fixes" do
expect(subject.first.message).to(include("Bad ERB comment syntax. Should be <%# without a space between."))
expect(subject.last.message).to(include("Bad ERB comment syntax. Should be <%#= without a space between."))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I almost switched this to <%#, but I realized one might just be trying to temporarily comment something out.

 blah blah
-<%= debugging_something %>
+<%#= debugging_something %>
 blah

expected_messages = [
"Bad ERB comment syntax. Should be <%# without a space between.",
"Bad ERB comment syntax. Should be <%#= or <%# without a space between.",
"Bad ERB comment syntax. Should be <%-# without a space between.",
]
actual_messages = subject.map(&:message)
expect(actual_messages.size).to(eq(expected_messages.size))

expected_messages.zip(actual_messages).each do |expected, actual|
expect(actual).to(include(expected))
end
end
end
end