Skip to content
Open
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
11 changes: 11 additions & 0 deletions lib/erb_lint/linters/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 43 additions & 0 deletions spec/erb_lint/linters/rubocop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down