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 an offense creation for Lint/EmptyFile #8676

Merged
merged 1 commit into from Sep 14, 2020
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.md
Expand Up @@ -12,6 +12,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][])
Expand Down
5 changes: 1 addition & 4 deletions lib/rubocop/cop/lint/empty_file.rb
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions 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
Expand All @@ -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