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

[Fix #12852] Correctly deserialize a global offense #12853

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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