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

update PatchCop to support spanning errors #79

Merged
merged 3 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 13 additions & 3 deletions lib/pronto/rubocop/patch_cop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ def messages
return [] unless valid?

offenses.flat_map do |offense|
patch
offending_line = patch
.added_lines
.select { |line| line.new_lineno == offense.line }
.map { |line| OffenseLine.new(self, offense, line).message }
.detect { |line| offense_includes?(offense, line.new_lineno) }
ashkulz marked this conversation as resolved.
Show resolved Hide resolved

if offending_line
[OffenseLine.new(self, offense, offending_line).message]
else
[]
end
end
end

Expand Down Expand Up @@ -67,6 +72,11 @@ def offenses
.reject(&:disabled?)
end

def offense_includes?(offense, line_number)
offense_range = (offense.location.first_line..offense.location.last_line)
offense_range.include?(line_number)
end

def team
@team ||=
if ::RuboCop::Cop::Team.respond_to?(:mobilize)
Expand Down
11 changes: 10 additions & 1 deletion spec/pronto/rubocop/patch_cop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
let(:ast) { double :ast, each_node: nil }
let(:processed_source) { double :processed_source, ast: ast }
let(:team) { double :team, inspect_file: [offense] }
let(:offense) { double :offense, disabled?: false, line: 42 }
let(:offense_location) { double :location, first_line: 42, last_line: 43 }
let(:offense) { double :offense, disabled?: false, location: offense_location }
let(:offense_line) { double :offense_line, message: 'Err' }

before do
Expand All @@ -31,5 +32,13 @@
it do
expect(patch_cop.messages).to eq(['Err'])
end

context 'when there is an error including the patch, but not starting inside it' do
let(:offense_location) { double :location, first_line: 40, last_line: 43 }

it do
expect(patch_cop.messages).to eq(['Err'])
end
end
end
end