Skip to content

Commit 118f043

Browse files
committed
Fix RSpec/FilePath when checking an empty class
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.
1 parent 3d4f211 commit 118f043

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Master (Unreleased)
44

5+
* Fix `RSpec/FilePath` when checking a file defining e.g. an empty class. ([@bquorning][])
6+
57
## 1.43.0 (2020-08-17)
68

79
* 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][])

lib/rubocop/rspec/top_level_group.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ def top_level_group?(node)
3838
end
3939

4040
def top_level_nodes(node)
41-
if node.begin_type?
41+
if node.nil?
42+
[]
43+
elsif node.begin_type?
4244
node.children
4345
elsif node.module_type? || node.class_type?
4446
top_level_nodes(node.body)

spec/rubocop/cop/rspec/file_path_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@
204204
RUBY
205205
end
206206

207+
# RSpec/FilePath runs on all files - not only **/*_spec.rb
208+
it 'works on files defining an empty class' do
209+
expect_no_offenses(<<-RUBY)
210+
class Foo
211+
end
212+
RUBY
213+
end
214+
207215
context 'when configured with CustomTransform' do
208216
let(:cop_config) { { 'CustomTransform' => { 'FooFoo' => 'foofoo' } } }
209217

0 commit comments

Comments
 (0)