Skip to content

Commit d58a043

Browse files
authored
Merge pull request #1117 from rubocop-hq/improve-on-send-performance
Imrpove performance by limitting on_send calls
2 parents 01597e5 + 575c583 commit d58a043

20 files changed

+31
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Master (Unreleased)
44

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

78
## 2.1.0 (2020-12-17)
89

lib/rubocop/cop/rspec/any_instance.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,14 @@ module RSpec
2424
# end
2525
class AnyInstance < Base
2626
MSG = 'Avoid stubbing using `%<method>s`.'
27-
28-
def_node_matcher :disallowed_stub, <<-PATTERN
29-
(send _ ${:any_instance :allow_any_instance_of :expect_any_instance_of} ...)
30-
PATTERN
27+
RESTRICT_ON_SEND = %i[
28+
any_instance
29+
allow_any_instance_of
30+
expect_any_instance_of
31+
].freeze
3132

3233
def on_send(node)
33-
disallowed_stub(node) do |method|
34-
add_offense(
35-
node,
36-
message: format(MSG, method: method)
37-
)
38-
end
34+
add_offense(node, message: format(MSG, method: node.method_name))
3935
end
4036
end
4137
end

lib/rubocop/cop/rspec/be_eql.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class BeEql < Base
3939
extend AutoCorrector
4040

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

4344
def_node_matcher :eql_type_with_identity, <<-PATTERN
4445
(send _ :to $(send nil? :eql {true false int float sym nil_type?}))

lib/rubocop/cop/rspec/before_after_all.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class BeforeAfterAll < Base
2929
'`use_transactional_fixtures` is enabled, then records created ' \
3030
'in `%<hook>s` are not automatically rolled back.'
3131

32+
RESTRICT_ON_SEND = %i[before after].freeze
33+
3234
def_node_matcher :before_or_after_all, <<-PATTERN
3335
$(send _ {:before :after} (sym {:all :context}))
3436
PATTERN

lib/rubocop/cop/rspec/capybara/current_path_expectation.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class CurrentPathExpectation < Base
3030
'Capybara feature specs - instead, use the ' \
3131
'`have_current_path` matcher on `page`'
3232

33+
RESTRICT_ON_SEND = %i[expect].freeze
34+
3335
def_node_matcher :expectation_set_on_current_path, <<-PATTERN
3436
(send nil? :expect (send {(send nil? :page) nil?} :current_path))
3537
PATTERN

lib/rubocop/cop/rspec/capybara/visibility_matcher.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class VisibilityMatcher < Base
4444
have_content
4545
].freeze
4646

47+
RESTRICT_ON_SEND = CAPYBARA_MATCHER_METHODS
48+
4749
def_node_matcher :visible_true?, <<~PATTERN
4850
(send nil? #capybara_matcher? ... (hash <$(pair (sym :visible) true) ...>))
4951
PATTERN

lib/rubocop/cop/rspec/describe_symbol.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module RSpec
1919
# @see https://github.com/rspec/rspec-core/issues/1610
2020
class DescribeSymbol < Base
2121
MSG = 'Avoid describing symbols.'
22+
RESTRICT_ON_SEND = %i[describe].freeze
2223

2324
def_node_matcher :describe_symbol?, <<-PATTERN
2425
(send #rspec? :describe $sym ...)

lib/rubocop/cop/rspec/factory_bot/factory_class_name.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class FactoryClassName < Base
2525
MSG = "Pass '%<class_name>s' string instead of `%<class_name>s` " \
2626
'constant.'
2727
ALLOWED_CONSTANTS = %w[Hash OpenStruct].freeze
28+
RESTRICT_ON_SEND = %i[factory].freeze
2829

2930
def_node_matcher :class_name, <<~PATTERN
3031
(send _ :factory _ (hash <(pair (sym :class) $(const ...)) ...>))

lib/rubocop/cop/rspec/implicit_block_expectation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module RSpec
1818
# end
1919
class ImplicitBlockExpectation < Base
2020
MSG = 'Avoid implicit block expectations.'
21+
RESTRICT_ON_SEND = %i[is_expected should should_not].freeze
2122

2223
def_node_matcher :lambda?, <<-PATTERN
2324
{

lib/rubocop/cop/rspec/implicit_subject.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ImplicitSubject < Base
3131
include ConfigurableEnforcedStyle
3232

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

3536
def_node_matcher :implicit_subject?, <<-PATTERN
3637
(send nil? {:should :should_not :is_expected} ...)

lib/rubocop/cop/rspec/it_behaves_like.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ItBehavesLike < Base
2424

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

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

lib/rubocop/cop/rspec/message_chain.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,12 @@ module RSpec
1515
#
1616
class MessageChain < Base
1717
MSG = 'Avoid stubbing using `%<method>s`.'
18-
19-
def_node_matcher :message_chain, <<-PATTERN
20-
(send _ {:receive_message_chain :stub_chain} ...)
21-
PATTERN
18+
RESTRICT_ON_SEND = %i[receive_message_chain stub_chain].freeze
2219

2320
def on_send(node)
24-
message_chain(node) do
25-
add_offense(
26-
node.loc.selector,
27-
message: format(MSG, method: node.method_name)
28-
)
29-
end
21+
add_offense(
22+
node.loc.selector, message: format(MSG, method: node.method_name)
23+
)
3024
end
3125
end
3226
end

lib/rubocop/cop/rspec/not_to_not.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class NotToNot < Base
2020
include ConfigurableEnforcedStyle
2121

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

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

lib/rubocop/cop/rspec/rails/http_status.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ module Rails
3333
class HttpStatus < Base
3434
extend AutoCorrector
3535
include ConfigurableEnforcedStyle
36+
RESTRICT_ON_SEND = %i[have_http_status].freeze
3637

3738
def_node_matcher :http_status, <<-PATTERN
3839
(send nil? :have_http_status ${int sym})

lib/rubocop/cop/rspec/receive_never.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module RSpec
1616
class ReceiveNever < Base
1717
extend AutoCorrector
1818
MSG = 'Use `not_to receive` instead of `never`.'
19+
RESTRICT_ON_SEND = %i[never].freeze
1920

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

lib/rubocop/cop/rspec/return_from_stub.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class ReturnFromStub < Base
3939

4040
MSG_AND_RETURN = 'Use `and_return` for static values.'
4141
MSG_BLOCK = 'Use block for static values.'
42+
RESTRICT_ON_SEND = %i[and_return].freeze
4243

4344
def_node_search :contains_stub?, '(send nil? :receive (...))'
4445
def_node_matcher :stub_with_block?, '(block #contains_stub? ...)'

lib/rubocop/cop/rspec/single_argument_message_chain.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class SingleArgumentMessageChain < Base
2121

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

2526
def_node_matcher :message_chain, <<-PATTERN
2627
(send _ {:receive_message_chain :stub_chain} $_)

lib/rubocop/cop/rspec/unspecified_exception.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module RSpec
3232
# expect { do_something }.not_to raise_error
3333
class UnspecifiedException < Base
3434
MSG = 'Specify the exception being captured'
35+
RESTRICT_ON_SEND = %i[to].freeze
3536

3637
def_node_matcher :empty_raise_error_or_exception, <<-PATTERN
3738
(send

lib/rubocop/cop/rspec/verified_doubles.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module RSpec
2424
# end
2525
class VerifiedDoubles < Base
2626
MSG = 'Prefer using verifying doubles over normal doubles.'
27+
RESTRICT_ON_SEND = %i[double spy].freeze
2728

2829
def_node_matcher :unverified_double, <<-PATTERN
2930
{(send nil? {:double :spy} $...)}

lib/rubocop/cop/rspec/void_expect.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module RSpec
1414
class VoidExpect < Base
1515
MSG = 'Do not use `expect()` without `.to` or `.not_to`. ' \
1616
'Chain the methods or remove it.'
17+
RESTRICT_ON_SEND = %i[expect].freeze
1718

1819
def_node_matcher :expect?, <<-PATTERN
1920
(send nil? :expect ...)

0 commit comments

Comments
 (0)