Skip to content

Commit

Permalink
LeakyConstantDeclaration - allow definitions on example group
Browse files Browse the repository at this point in the history
Constant, classes, and modules that are defined on the example group do
not leak into the global namespace. While doing this may not be
a particularly good practice, it should ideally not result in
an offense.

The practice is described here: https://makandracards.com/makandra/47189-rspec-how-to-define-classes-for-specs#section-1-defining-the-constant-on-the-example-class
  • Loading branch information
naveg committed Feb 5, 2024
1 parent 31c6344 commit 51f9e38
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/rubocop/cop/rspec/leaky_constant_declaration.rb
Expand Up @@ -100,18 +100,21 @@ class LeakyConstantDeclaration < Base

def on_casgn(node)
return unless inside_describe_block?(node)
return if defined_on_example_group?(node)

add_offense(node, message: MSG_CONST)
end

def on_class(node)
return unless inside_describe_block?(node)
return if defined_on_example_group?(node)

add_offense(node, message: MSG_CLASS)
end

def on_module(node)
return unless inside_describe_block?(node)
return if defined_on_example_group?(node)

add_offense(node, message: MSG_MODULE)
end
Expand All @@ -121,6 +124,16 @@ def on_module(node)
def inside_describe_block?(node)
node.each_ancestor(:block).any?(&method(:spec_group?))
end

def defined_on_example_group?(node)
if node.is_a?(RuboCop::AST::ClassNode) || node.is_a?(RuboCop::AST::ModuleNode)
node.loc.name.source.start_with?('self::')
elsif node.is_a?(RuboCop::AST::CasgnNode)
node.namespace&.self_type?
else
false
end
end
end
end
end
Expand Down
28 changes: 27 additions & 1 deletion spec/rubocop/cop/rspec/leaky_constant_declaration_spec.rb
Expand Up @@ -31,6 +31,14 @@
RUBY
end

it 'ignores constant defined on the example group' do
expect_no_offenses(<<~RUBY)
describe SomeClass do
self::CONSTANT = "Accessible as self.class::CONSTANT".freeze
end
RUBY
end

it 'ignores outside of example/shared group' do
expect_no_offenses(<<~RUBY)
factory :some_class do
Expand Down Expand Up @@ -60,7 +68,16 @@ def method
end
end
end
end
end
RUBY
end

it 'ignores classes defined on the example group' do
expect_no_offenses(<<~RUBY)
describe SomeClass do
class self::DummyClass
end
end
RUBY
end

Expand All @@ -85,5 +102,14 @@ module DummyModule
end
RUBY
end

it 'ignores modules defined on the example group' do
expect_no_offenses(<<~RUBY)
describe SomeClass do
module self::DummyModule
end
end
RUBY
end
end
end

0 comments on commit 51f9e38

Please sign in to comment.