From 38fae5075650ea38db04dfb38d0cdc6fbde210a7 Mon Sep 17 00:00:00 2001 From: Sam Bostock Date: Thu, 22 Jun 2023 14:17:00 -0400 Subject: [PATCH] Improve comment syntax suggestions This adds handling for `<%- #`, and clarifies that `<%#=` is just a comment starting with `=`, as opposed to some special construct. --- lib/erb_lint/linters/comment_syntax.rb | 10 ++++++++-- spec/erb_lint/linters/comment_syntax_spec.rb | 18 ++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/erb_lint/linters/comment_syntax.rb b/lib/erb_lint/linters/comment_syntax.rb index 359e06b4..8400ffd2 100644 --- a/lib/erb_lint/linters/comment_syntax.rb +++ b/lib/erb_lint/linters/comment_syntax.rb @@ -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 @@ -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, diff --git a/spec/erb_lint/linters/comment_syntax_spec.rb b/spec/erb_lint/linters/comment_syntax_spec.rb index 4404a3bd..0ee37486 100644 --- a/spec/erb_lint/linters/comment_syntax_spec.rb +++ b/spec/erb_lint/linters/comment_syntax_spec.rb @@ -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.")) + 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