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

LeakyConstantDeclaration - allow definitions on example group #1798

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Support correcting `assert_not_equal` and `assert_not_nil` in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
- Fix a false positive for `RSpec/ExpectActual` when used with rspec-rails routing matchers. ([@naveg])
- Add new `RSpec/RepeatedSubjectCall` cop. ([@drcapulet])
- Fix a false positive for `RSpec/LeakyConstantDeclaration` when defining constants on the example group. ([@naveg])

## 2.26.1 (2024-01-05)

Expand Down
13 changes: 13 additions & 0 deletions lib/rubocop/cop/rspec/leaky_constant_declaration.rb
Original file line number Diff line number Diff line change
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?
end
end
end
end
end
Expand Down
28 changes: 27 additions & 1 deletion spec/rubocop/cop/rspec/leaky_constant_declaration_spec.rb
Original file line number Diff line number Diff line change
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