diff --git a/CHANGELOG.md b/CHANGELOG.md index ec552b00c1c..41df973a415 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ * [#8807](https://github.com/rubocop-hq/rubocop/pull/8807): Fix a false positive for `Style/RedundantCondition` when using assignment by hash key access. ([@koic][]) * [#8848](https://github.com/rubocop-hq/rubocop/issues/8848): Fix a false positive for `Style/CombinableLoops` when using the same method with different arguments. ([@dvandersluis][]) * [#8843](https://github.com/rubocop-hq/rubocop/issues/8843): Fix an incorrect autocorrect for `Lint/AmbiguousRegexpLiteral` when sending method to regexp literal receiver. ([@koic][]) +* [#8842](https://github.com/rubocop-hq/rubocop/issues/8842): Save actual status to cache, except corrected. ([@hatkyinc2][]) * [#8835](https://github.com/rubocop-hq/rubocop/issues/8835): Fix an incorrect autocorrect for `Style/RedundantInterpolation` when using string interpolation for non-operator methods. ([@koic][]) ### Changes diff --git a/lib/rubocop/cached_data.rb b/lib/rubocop/cached_data.rb index e7fec3412ca..ece9052aefb 100644 --- a/lib/rubocop/cached_data.rb +++ b/lib/rubocop/cached_data.rb @@ -21,6 +21,7 @@ def to_json(offenses) private def serialize_offense(offense) + status = :uncorrected if %i[corrected corrected_with_todo].include?(offense.status) { # Calling #to_s here ensures that the serialization works when using # other json serializers such as Oj. Some of these gems do not call @@ -32,7 +33,7 @@ def serialize_offense(offense) }, message: message(offense), cop_name: offense.cop_name, - status: :uncorrected + status: status || offense.status } end diff --git a/spec/rubocop/result_cache_spec.rb b/spec/rubocop/result_cache_spec.rb index fae7bea59b3..250a014c895 100644 --- a/spec/rubocop/result_cache_spec.rb +++ b/spec/rubocop/result_cache_spec.rb @@ -60,25 +60,70 @@ def abs(path) end # Fixes https://github.com/rubocop-hq/rubocop/issues/6274 - context 'when offenses are saved by autocorrect run' do - let(:corrected_offense) do - RuboCop::Cop::Offense.new( - :warning, location, 'unused var', 'Lint/UselessAssignment', :corrected - ) + context 'when offenses are saved' do + context 'an offence with status corrected' do + let(:offense) do + RuboCop::Cop::Offense.new( + :warning, location, 'unused var', 'Lint/UselessAssignment', :corrected + ) + end + + it 'serializes them with uncorrected status' do + cache.save([offense]) + expect(cache.load[0].status).to eq(:uncorrected) + end end - let(:uncorrected_offense) do - RuboCop::Cop::Offense.new( - corrected_offense.severity.name, - corrected_offense.location, - corrected_offense.message, - corrected_offense.cop_name, - :uncorrected - ) + + context 'an offence with status corrected_with_todo' do + let(:offense) do + RuboCop::Cop::Offense.new( + :warning, location, 'unused var', 'Lint/UselessAssignment', :corrected_with_todo + ) + end + + it 'serializes them with uncorrected status' do + cache.save([offense]) + expect(cache.load[0].status).to eq(:uncorrected) + end end - it 'serializes them with :uncorrected status' do - cache.save([corrected_offense]) - expect(cache.load).to match_array([uncorrected_offense]) + context 'an offence with status uncorrected' do + let(:offense) do + RuboCop::Cop::Offense.new( + :warning, location, 'unused var', 'Lint/UselessAssignment', :uncorrected + ) + end + + it 'serializes them with uncorrected status' do + cache.save([offense]) + expect(cache.load[0].status).to eq(:uncorrected) + end + end + + context 'an offence with status unsupported' do + let(:offense) do + RuboCop::Cop::Offense.new( + :warning, location, 'unused var', 'Lint/UselessAssignment', :unsupported + ) + end + + it 'serializes them with unsupported status' do + cache.save([offense]) + expect(cache.load[0].status).to eq(:unsupported) + end + end + + context 'an offence with status new_status' do + let(:offense) do + RuboCop::Cop::Offense.new( + :warning, location, 'unused var', 'Lint/UselessAssignment', :new_status + ) + end + + it 'serializes them with new_status status' do + cache.save([offense]) + expect(cache.load[0].status).to eq(:new_status) + end end end