Skip to content

Commit d86b8d1

Browse files
authored
Merge pull request #12853 from Earlopain/global-offense-cache
[Fix #12852] Correctly deserialize a global offense
2 parents 16913ee + 1d8c357 commit d86b8d1

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#12852](https://github.com/rubocop/rubocop/issues/12852): Fix an error for `Lint/EmptyFile` in formatters when using cache. ([@earlopain][])

lib/rubocop/cached_data.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,19 @@ def deserialize_offenses(offenses)
4848
source_buffer = Parser::Source::Buffer.new(@filename)
4949
source_buffer.source = File.read(@filename, encoding: Encoding::UTF_8)
5050
offenses.map! do |o|
51-
location = Parser::Source::Range.new(source_buffer,
52-
o['location']['begin_pos'],
53-
o['location']['end_pos'])
51+
location = location_from_source_buffer(o, source_buffer)
5452
Cop::Offense.new(o['severity'], location, o['message'], o['cop_name'], o['status'].to_sym)
5553
end
5654
end
55+
56+
def location_from_source_buffer(offense, source_buffer)
57+
begin_pos = offense['location']['begin_pos']
58+
end_pos = offense['location']['end_pos']
59+
if begin_pos.zero? && end_pos.zero?
60+
Cop::Offense::NO_LOCATION
61+
else
62+
Parser::Source::Range.new(source_buffer, begin_pos, end_pos)
63+
end
64+
end
5765
end
5866
end

spec/rubocop/result_cache_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ def abs(path)
113113
expect(cache.load[0].status).to eq(:new_status)
114114
end
115115
end
116+
117+
context 'a global offense' do
118+
let(:no_location) { RuboCop::Cop::Offense::NO_LOCATION }
119+
let(:global_offense) do
120+
RuboCop::Cop::Offense.new(:warning, no_location, 'empty file', 'Lint/EmptyFile',
121+
:unsupported)
122+
end
123+
124+
it 'serializes the range correctly' do
125+
cache.save([global_offense])
126+
expect(cache.load[0].location).to eq(no_location)
127+
end
128+
end
116129
end
117130

118131
context 'when no option is given' do

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy