forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes rubocop#862
- Loading branch information
Showing
7 changed files
with
390 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
136 changes: 136 additions & 0 deletions
136
lib/rubocop/cop/rspec/memoized_helpers_in_example_group.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,136 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Checks if example groups contain too many `let` and `subject` calls. | ||
# | ||
# This cop is configurable using the `Max` option and the `AllowSubject` | ||
# which will configure the cop to only register offenses on calls to | ||
# `let` and not calls to `subject`. | ||
# | ||
# @example | ||
# # bad | ||
# describe MyClass do | ||
# let(:foo) { [] } | ||
# let(:bar) { [] } | ||
# let!(:booger) { [] } | ||
# subject { {} } | ||
# subject(:wat) { {} } | ||
# subject!(:boo) { {} } | ||
# end | ||
# | ||
# describe MyClass do | ||
# let(:foo) { [] } | ||
# let(:bar) { [] } | ||
# | ||
# context 'when stuff' do | ||
# let!(:booger) { [] } | ||
# subject { {} } | ||
# subject(:wat) { {} } | ||
# subject!(:boo) { {} } | ||
# end | ||
# end | ||
# | ||
# # good | ||
# describe MyClass do | ||
# let(:foo) { [] } | ||
# let!(:booger) { [] } | ||
# subject { {} } | ||
# subject(:wat) { {} } | ||
# subject!(:boo) { {} } | ||
# end | ||
# | ||
# describe MyClass do | ||
# context 'when stuff' do | ||
# let(:foo) { [] } | ||
# let(:bar) { [] } | ||
# let!(:booger) { [] } | ||
# end | ||
# | ||
# context 'when other stuff' do | ||
# subject { {} } | ||
# subject(:wat) { {} } | ||
# subject!(:boo) { {} } | ||
# end | ||
# end | ||
# | ||
# @example with AllowSubject configuration | ||
# | ||
# # rubocop.yml | ||
# # RSpec/MemoizedHelpersInExampleGroup: | ||
# # AllowSubject: true | ||
# | ||
# # bad | ||
# describe MyClass do | ||
# let(:foo) { [] } | ||
# let(:bar) { [] } | ||
# let!(:booger) { [] } | ||
# let(:subject) { {} } | ||
# let(:wat) { {} } | ||
# let!(:boo) { {} } | ||
# end | ||
# | ||
# # good | ||
# describe MyClass do | ||
# let(:foo) { [] } | ||
# let(:bar) { [] } | ||
# let!(:booger) { [] } | ||
# subject { {} } | ||
# subject(:wat) { {} } | ||
# subject!(:boo) { {} } | ||
# end | ||
# | ||
# @example with Max configuration | ||
# | ||
# # rubocop.yml | ||
# # RSpec/MemoizedHelpersInExampleGroup: | ||
# # Max: 0 | ||
# | ||
# # bad | ||
# describe MyClass do | ||
# let(:foo) { [] } | ||
# end | ||
# | ||
# # good | ||
# describe MyClass do | ||
# def foo; []; end | ||
# end | ||
# | ||
class MemoizedHelpersInExampleGroup < Cop | ||
MSG = 'Example has too many memoized helpers [%<count>d/%<max>d]' | ||
|
||
def on_block(node) | ||
return unless spec_group?(node) | ||
|
||
count = count_helpers(node) | ||
|
||
node.each_ancestor(:block) do |ancestor| | ||
count += count_helpers(ancestor) | ||
end | ||
|
||
return if count <= max | ||
|
||
add_offense(node, message: format(MSG, count: count, max: max)) | ||
end | ||
|
||
private | ||
|
||
def count_helpers(node) | ||
example_group = RuboCop::RSpec::ExampleGroup.new(node) | ||
count = example_group.lets.count | ||
count += example_group.subjects.count unless allow_subject? | ||
count | ||
end | ||
|
||
def max | ||
cop_config['Max'] | ||
end | ||
|
||
def allow_subject? | ||
cop_config['AllowSubject'] | ||
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
Oops, something went wrong.