diff --git a/CHANGELOG.md b/CHANGELOG.md index a7b7b7908a8..160acd93bf9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * [#8627](https://github.com/rubocop-hq/rubocop/issues/8627): Fix a false positive for `Lint/DuplicateRequire` when same feature argument but different require method. ([@koic][]) * [#8674](https://github.com/rubocop-hq/rubocop/issues/8674): Fix an error for `Layout/EmptyLineAfterMultilineCondition` when conditional is at the top level. ([@fatkodima][]) * [#8658](https://github.com/rubocop-hq/rubocop/pull/8658): Fix a false positive for `Style/RedundantSelfAssignment` when calling coercion methods. ([@fatkodima][]) +* [#8669](https://github.com/rubocop-hq/rubocop/issues/8669): Fix an offense creation for `Lint/EmptyFile`. ([@fatkodima][]) * [#8607](https://github.com/rubocop-hq/rubocop/issues/8607): Fix a false positive for `Lint/UnreachableLoop` when conditional branch includes continue statement preceding break statement. ([@fatkodima][]) * [#8572](https://github.com/rubocop-hq/rubocop/issues/8572): Fix a false positive for `Style/RedundantParentheses` when parentheses are used like method argument parentheses. ([@koic][]) * [#8630](https://github.com/rubocop-hq/rubocop/issues/8630): Fix some false positives for `Style/HashTransformKeys` and `Style/HashTransformValues` when the receiver is an array. ([@eugeneius][]) diff --git a/lib/rubocop/cop/lint/empty_file.rb b/lib/rubocop/cop/lint/empty_file.rb index 1825be082e2..db67dd045dc 100644 --- a/lib/rubocop/cop/lint/empty_file.rb +++ b/lib/rubocop/cop/lint/empty_file.rb @@ -26,10 +26,7 @@ class EmptyFile < Base MSG = 'Empty file detected.' def on_new_investigation - return unless offending? - - range = source_range(processed_source.buffer, 1, 0) - add_offense(range) + add_global_offense(MSG) if offending? end private diff --git a/spec/rubocop/cop/lint/empty_file_spec.rb b/spec/rubocop/cop/lint/empty_file_spec.rb index e5f4dbb24ea..699d54328cf 100644 --- a/spec/rubocop/cop/lint/empty_file_spec.rb +++ b/spec/rubocop/cop/lint/empty_file_spec.rb @@ -1,16 +1,18 @@ # frozen_string_literal: true RSpec.describe RuboCop::Cop::Lint::EmptyFile, :config do - subject(:cop) { described_class.new(config) } - + let(:commissioner) { RuboCop::Cop::Commissioner.new([cop]) } + let(:offenses) { commissioner.investigate(processed_source).offenses } let(:cop_config) do { 'AllowComments' => true } end + let(:source) { '' } it 'registers an offense when the file is empty' do - expect_offense(<<~RUBY) - ^ Empty file detected. - RUBY + expect(offenses.size).to eq(1) + offense = offenses.first + expect(offense.message).to eq('Empty file detected.') + expect(offense.severity).to eq(:warning) end it 'does not register an offense when the file contains code' do @@ -29,12 +31,10 @@ let(:cop_config) do { 'AllowComments' => false } end + let(:source) { '# comment' } it 'registers an offense when the file contains comments' do - expect_offense(<<~RUBY) - # comment - ^ Empty file detected. - RUBY + expect(offenses.size).to eq(1) end end end