Skip to content

Commit

Permalink
Fix RSpec/FilePath when checking an empty class
Browse files Browse the repository at this point in the history
Remember that `RSpec/FilePath` checks *all* files - not only
**/*_spec.rb. And when some class defines e.g. an empty class, the
`TopLevelGroup` module would raise an error.

Fixes #999.
  • Loading branch information
bquorning committed Aug 17, 2020
1 parent 3d4f211 commit 118f043
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def top_level_group?(node)
end

def top_level_nodes(node)
if node.begin_type?
if node.nil?
[]
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
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@
RUBY
end

# RSpec/FilePath runs on all files - not only **/*_spec.rb
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

0 comments on commit 118f043

Please sign in to comment.