Skip to content

Commit

Permalink
Merge pull request #12853 from Earlopain/global-offense-cache
Browse files Browse the repository at this point in the history
[Fix #12852] Correctly deserialize a global offense
  • Loading branch information
koic committed Apr 17, 2024
2 parents 16913ee + 1d8c357 commit d86b8d1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/fix_error_for_lint_empty_file_with_cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12852](https://github.com/rubocop/rubocop/issues/12852): Fix an error for `Lint/EmptyFile` in formatters when using cache. ([@earlopain][])
14 changes: 11 additions & 3 deletions lib/rubocop/cached_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,19 @@ def deserialize_offenses(offenses)
source_buffer = Parser::Source::Buffer.new(@filename)
source_buffer.source = File.read(@filename, encoding: Encoding::UTF_8)
offenses.map! do |o|
location = Parser::Source::Range.new(source_buffer,
o['location']['begin_pos'],
o['location']['end_pos'])
location = location_from_source_buffer(o, source_buffer)
Cop::Offense.new(o['severity'], location, o['message'], o['cop_name'], o['status'].to_sym)
end
end

def location_from_source_buffer(offense, source_buffer)
begin_pos = offense['location']['begin_pos']
end_pos = offense['location']['end_pos']
if begin_pos.zero? && end_pos.zero?
Cop::Offense::NO_LOCATION
else
Parser::Source::Range.new(source_buffer, begin_pos, end_pos)
end
end
end
end
13 changes: 13 additions & 0 deletions spec/rubocop/result_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ def abs(path)
expect(cache.load[0].status).to eq(:new_status)
end
end

context 'a global offense' do
let(:no_location) { RuboCop::Cop::Offense::NO_LOCATION }
let(:global_offense) do
RuboCop::Cop::Offense.new(:warning, no_location, 'empty file', 'Lint/EmptyFile',
:unsupported)
end

it 'serializes the range correctly' do
cache.save([global_offense])
expect(cache.load[0].location).to eq(no_location)
end
end
end

context 'when no option is given' do
Expand Down

0 comments on commit d86b8d1

Please sign in to comment.