diff --git a/lib/erb_lint/linters/rubocop.rb b/lib/erb_lint/linters/rubocop.rb index abe8e3b..a45570f 100644 --- a/lib/erb_lint/linters/rubocop.rb +++ b/lib/erb_lint/linters/rubocop.rb @@ -79,10 +79,21 @@ def activate_team(processed_source, source, offset, code_node, team) .to_source_range(rubocop_offense.location) .offset(offset) + next if offense_removes_erb_tag(rubocop_offense, processed_source) + add_offense(rubocop_offense, offense_range, correction, offset, code_node.loc.range) end end + def offense_removes_erb_tag(rubocop_offense, processed_source) + range = rubocop_offense.location + + removed_lines = processed_source.to_source_range(range).source + + erb_tag_pattern = /\A<%=?\z/ + removed_lines.match?(erb_tag_pattern) + end + def tempfile_from(filename, content) Tempfile.create(File.basename(filename), Dir.pwd) do |tempfile| tempfile.write(content) diff --git a/spec/erb_lint/linters/rubocop_spec.rb b/spec/erb_lint/linters/rubocop_spec.rb index f969e81..7a733d0 100644 --- a/spec/erb_lint/linters/rubocop_spec.rb +++ b/spec/erb_lint/linters/rubocop_spec.rb @@ -271,6 +271,49 @@ end end + context "code includes an opening erb tag followed by ruby on subsequent line" do + let(:linter_config) do + described_class.config_schema.new( + only: only_cops, + rubocop_config: { + AllCops: { + TargetRubyVersion: "2.5", + }, + } + ) + end + + let(:only_cops) { ["Layout/TrailingWhitespace"] } + + let(:file) { <<~FILE } + <% + some_ruby_code + %> + FILE + + it "does not consider the opening erb line to be a whitespace violation" do + expect(subject).to(eq([])) + end + + context "when the opening erb line includes other violations" do + let(:only_cops) { ["Layout/TrailingWhitespace", "Layout/EmptyComment"] } + let(:file) { <<~FILE } + <% # + some_ruby_code + %> + FILE + + it "adds offenses" do + expect(subject.size).to(eq(1)) + expect(subject[0].source_range.begin_pos).to(eq(3)) + expect(subject[0].source_range.end_pos).to(eq(4)) + expect(subject[0].source_range.source).to(eq("#")) + expect(subject[0].line_range).to(eq(1..1)) + expect(subject[0].message).to(eq("Layout/EmptyComment: Source code comment is empty.")) + end + end + end + context "code is aligned to the column matching start of ruby code" do let(:linter_config) do described_class.config_schema.new(