Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #831 from kellysutton/ks-nested-module-cop
Implement RSpec/DescribedClassModuleWrapping cop
- Loading branch information
Showing
7 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Avoid opening modules and defining specs within them. | ||
# | ||
# @example | ||
# # bad | ||
# module MyModule | ||
# RSpec.describe MyClass do | ||
# # ... | ||
# end | ||
# end | ||
# | ||
# # good | ||
# RSpec.describe MyModule::MyClass do | ||
# # ... | ||
# end | ||
# | ||
# @see https://github.com/rubocop-hq/rubocop-rspec/issues/735 | ||
class DescribedClassModuleWrapping < Cop | ||
MSG = 'Avoid opening modules and defining specs within them.' | ||
|
||
def_node_search :find_rspec_blocks, | ||
ExampleGroups::ALL.block_pattern | ||
|
||
def on_module(node) | ||
find_rspec_blocks(node) do | ||
add_offense(node) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
spec/rubocop/cop/rspec/described_class_module_wrapping_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::DescribedClassModuleWrapping do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'allows a describe block in the outermost scope' do | ||
expect_no_offenses(<<-RUBY) | ||
RSpec.describe MyClass do | ||
subject { "MyClass" } | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when RSpec.describe is nested within a module' do | ||
expect_offense(<<-RUBY) | ||
module MyModule | ||
^^^^^^^^^^^^^^^ Avoid opening modules and defining specs within them. | ||
RSpec.describe MyClass do | ||
subject { "MyClass" } | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when RSpec.describe is nested within two modules' do | ||
expect_offense(<<-RUBY) | ||
module MyFirstModule | ||
^^^^^^^^^^^^^^^^^^^^ Avoid opening modules and defining specs within them. | ||
module MySecondModule | ||
^^^^^^^^^^^^^^^^^^^^^ Avoid opening modules and defining specs within them. | ||
RSpec.describe MyClass do | ||
subject { "MyClass" } | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'allows a module that does not contain RSpec.describe' do | ||
expect_no_offenses(<<-RUBY) | ||
module MyModule | ||
def some_method | ||
end | ||
end | ||
RUBY | ||
end | ||
end |