Skip to content

Commit

Permalink
Imrpove performance by limitting on_send calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Darhazer committed Jan 10, 2021
1 parent 01597e5 commit 575c583
Show file tree
Hide file tree
Showing 20 changed files with 31 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Master (Unreleased)

* Fix `HooksBeforeExamples`, `LeadingSubject`, `LetBeforeExamples` and `ScatteredLet` autocorrection to take into account inline comments and comments immediately before the moved node. ([@Darhazer][])
* Improve rubocop-rspec performance. ([@Darhazer][])

## 2.1.0 (2020-12-17)

Expand Down
16 changes: 6 additions & 10 deletions lib/rubocop/cop/rspec/any_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,14 @@ module RSpec
# end
class AnyInstance < Base
MSG = 'Avoid stubbing using `%<method>s`.'

def_node_matcher :disallowed_stub, <<-PATTERN
(send _ ${:any_instance :allow_any_instance_of :expect_any_instance_of} ...)
PATTERN
RESTRICT_ON_SEND = %i[
any_instance
allow_any_instance_of
expect_any_instance_of
].freeze

def on_send(node)
disallowed_stub(node) do |method|
add_offense(
node,
message: format(MSG, method: method)
)
end
add_offense(node, message: format(MSG, method: node.method_name))
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/be_eql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class BeEql < Base
extend AutoCorrector

MSG = 'Prefer `be` over `eql`.'
RESTRICT_ON_SEND = %i[to].freeze

def_node_matcher :eql_type_with_identity, <<-PATTERN
(send _ :to $(send nil? :eql {true false int float sym nil_type?}))
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/rspec/before_after_all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class BeforeAfterAll < Base
'`use_transactional_fixtures` is enabled, then records created ' \
'in `%<hook>s` are not automatically rolled back.'

RESTRICT_ON_SEND = %i[before after].freeze

def_node_matcher :before_or_after_all, <<-PATTERN
$(send _ {:before :after} (sym {:all :context}))
PATTERN
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/rspec/capybara/current_path_expectation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class CurrentPathExpectation < Base
'Capybara feature specs - instead, use the ' \
'`have_current_path` matcher on `page`'

RESTRICT_ON_SEND = %i[expect].freeze

def_node_matcher :expectation_set_on_current_path, <<-PATTERN
(send nil? :expect (send {(send nil? :page) nil?} :current_path))
PATTERN
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/rspec/capybara/visibility_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class VisibilityMatcher < Base
have_content
].freeze

RESTRICT_ON_SEND = CAPYBARA_MATCHER_METHODS

def_node_matcher :visible_true?, <<~PATTERN
(send nil? #capybara_matcher? ... (hash <$(pair (sym :visible) true) ...>))
PATTERN
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/describe_symbol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module RSpec
# @see https://github.com/rspec/rspec-core/issues/1610
class DescribeSymbol < Base
MSG = 'Avoid describing symbols.'
RESTRICT_ON_SEND = %i[describe].freeze

def_node_matcher :describe_symbol?, <<-PATTERN
(send #rspec? :describe $sym ...)
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/factory_bot/factory_class_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class FactoryClassName < Base
MSG = "Pass '%<class_name>s' string instead of `%<class_name>s` " \
'constant.'
ALLOWED_CONSTANTS = %w[Hash OpenStruct].freeze
RESTRICT_ON_SEND = %i[factory].freeze

def_node_matcher :class_name, <<~PATTERN
(send _ :factory _ (hash <(pair (sym :class) $(const ...)) ...>))
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/implicit_block_expectation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module RSpec
# end
class ImplicitBlockExpectation < Base
MSG = 'Avoid implicit block expectations.'
RESTRICT_ON_SEND = %i[is_expected should should_not].freeze

def_node_matcher :lambda?, <<-PATTERN
{
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/implicit_subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ImplicitSubject < Base
include ConfigurableEnforcedStyle

MSG = "Don't use implicit subject."
RESTRICT_ON_SEND = %i[is_expected should should_not].freeze

def_node_matcher :implicit_subject?, <<-PATTERN
(send nil? {:should :should_not :is_expected} ...)
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/it_behaves_like.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ItBehavesLike < Base

MSG = 'Prefer `%<replacement>s` over `%<original>s` when including ' \
'examples in a nested context.'
RESTRICT_ON_SEND = %i[it_behaves_like it_should_behave_like].freeze

def_node_matcher :example_inclusion_offense, '(send _ % ...)'

Expand Down
14 changes: 4 additions & 10 deletions lib/rubocop/cop/rspec/message_chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,12 @@ module RSpec
#
class MessageChain < Base
MSG = 'Avoid stubbing using `%<method>s`.'

def_node_matcher :message_chain, <<-PATTERN
(send _ {:receive_message_chain :stub_chain} ...)
PATTERN
RESTRICT_ON_SEND = %i[receive_message_chain stub_chain].freeze

def on_send(node)
message_chain(node) do
add_offense(
node.loc.selector,
message: format(MSG, method: node.method_name)
)
end
add_offense(
node.loc.selector, message: format(MSG, method: node.method_name)
)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/not_to_not.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class NotToNot < Base
include ConfigurableEnforcedStyle

MSG = 'Prefer `%<replacement>s` over `%<original>s`.'
RESTRICT_ON_SEND = %i[not_to to_not].freeze

def_node_matcher :not_to_not_offense, '(send _ % ...)'

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/rails/http_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module Rails
class HttpStatus < Base
extend AutoCorrector
include ConfigurableEnforcedStyle
RESTRICT_ON_SEND = %i[have_http_status].freeze

def_node_matcher :http_status, <<-PATTERN
(send nil? :have_http_status ${int sym})
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/receive_never.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module RSpec
class ReceiveNever < Base
extend AutoCorrector
MSG = 'Use `not_to receive` instead of `never`.'
RESTRICT_ON_SEND = %i[never].freeze

def_node_search :method_on_stub?, '(send nil? :receive ...)'

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/return_from_stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ReturnFromStub < Base

MSG_AND_RETURN = 'Use `and_return` for static values.'
MSG_BLOCK = 'Use block for static values.'
RESTRICT_ON_SEND = %i[and_return].freeze

def_node_search :contains_stub?, '(send nil? :receive (...))'
def_node_matcher :stub_with_block?, '(block #contains_stub? ...)'
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/single_argument_message_chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class SingleArgumentMessageChain < Base

MSG = 'Use `%<recommended>s` instead of calling ' \
'`%<called>s` with a single argument.'
RESTRICT_ON_SEND = %i[receive_message_chain stub_chain].freeze

def_node_matcher :message_chain, <<-PATTERN
(send _ {:receive_message_chain :stub_chain} $_)
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/unspecified_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module RSpec
# expect { do_something }.not_to raise_error
class UnspecifiedException < Base
MSG = 'Specify the exception being captured'
RESTRICT_ON_SEND = %i[to].freeze

def_node_matcher :empty_raise_error_or_exception, <<-PATTERN
(send
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/verified_doubles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module RSpec
# end
class VerifiedDoubles < Base
MSG = 'Prefer using verifying doubles over normal doubles.'
RESTRICT_ON_SEND = %i[double spy].freeze

def_node_matcher :unverified_double, <<-PATTERN
{(send nil? {:double :spy} $...)}
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/void_expect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module RSpec
class VoidExpect < Base
MSG = 'Do not use `expect()` without `.to` or `.not_to`. ' \
'Chain the methods or remove it.'
RESTRICT_ON_SEND = %i[expect].freeze

def_node_matcher :expect?, <<-PATTERN
(send nil? :expect ...)
Expand Down

0 comments on commit 575c583

Please sign in to comment.