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} ...)

0 commit comments

Comments
 (0)