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 RSpec/FilePath when checking an empty class #1000

Merged
merged 1 commit into from Aug 17, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

* Fix `RSpec/FilePath` when checking a file defining e.g. an empty class. ([@bquorning][])

## 1.43.0 (2020-08-17)

* Add a new base cop class `::RuboCop::Cop::RSpec::Base`. The old base class `::RuboCop::Cop::RSpec::Cop` is deprecated, and will be removed in the next major release. ([@bquorning][])
Expand Down
4 changes: 3 additions & 1 deletion lib/rubocop/rspec/top_level_group.rb
Expand Up @@ -38,7 +38,9 @@ def top_level_group?(node)
end

def top_level_nodes(node)
if node.begin_type?
if node.nil?
[]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember we were discussing this with Maxim and settled on

return unless root_node

Do you think both checks are necessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you mention, we already have return unless root_node inside #on_new_investigation. The problem happens when the root node is an empty module or class:

elsif node.module_type? || node.class_type?
  top_level_nodes(node.body)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, we could move the check into the elsif body:

elsif node.module_type? || node.class_type?
  node.body.nil? ? [] : top_level_nodes(node.body)

or

elsif node.module_type? || node.class_type?
  if node.body
    top_level_nodes(node.body)
  else
    []
  end

elsif node.begin_type?
node.children
elsif node.module_type? || node.class_type?
top_level_nodes(node.body)
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/rspec/file_path_spec.rb
Expand Up @@ -204,6 +204,14 @@
RUBY
end

# RSpec/FilePath runs on all files - not only **/*_spec.rb
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering, why so?
For some reason relevant_rubocop_rspec_file? evaluates to true for it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see it

        def relevant_rubocop_rspec_file?(_file)
          true
        end

it 'works on files defining an empty class' do
expect_no_offenses(<<-RUBY)
class Foo
end
RUBY
end

context 'when configured with CustomTransform' do
let(:cop_config) { { 'CustomTransform' => { 'FooFoo' => 'foofoo' } } }

Expand Down