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

Use node pattern generic parameters to simplify a cop #930

Merged
merged 1 commit into from Jul 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 6 additions & 10 deletions lib/rubocop/cop/rspec/multiple_expectations.rb
Expand Up @@ -50,17 +50,13 @@ class MultipleExpectations < Cop

MSG = 'Example has too many expectations [%<total>d/%<max>d].'

def_node_matcher :aggregate_failures?, <<-PATTERN
(block {
(send _ _ <(sym :aggregate_failures) ...>)
(send _ _ ... (hash <(pair (sym :aggregate_failures) true) ...>))
} ...)
PATTERN
ANYTHING = ->(_node) { true }
TRUE = ->(node) { node.true_type? }

def_node_matcher :aggregate_failures_present?, <<-PATTERN
def_node_matcher :aggregate_failures?, <<-PATTERN
(block {
(send _ _ <(sym :aggregate_failures) ...>)
(send _ _ ... (hash <(pair (sym :aggregate_failures) _) ...>))
(send _ _ ... (hash <(pair (sym :aggregate_failures) %1) ...>))
} ...)
PATTERN

Expand Down Expand Up @@ -89,12 +85,12 @@ def example_with_aggregate_failures?(example_node)
node_with_aggregate_failures = find_aggregate_failures(example_node)
return false unless node_with_aggregate_failures

aggregate_failures?(node_with_aggregate_failures)
aggregate_failures?(node_with_aggregate_failures, TRUE)
end

def find_aggregate_failures(example_node)
example_node.send_node.each_ancestor(:block)
.find { |block_node| aggregate_failures_present?(block_node) }
.find { |block_node| aggregate_failures?(block_node, ANYTHING) }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new addition to rubocop-ast made it possible to pass in true and _-alike's.
That was it became possible to collapse two node matchers into one, since they only had that distinction.

One is used to match:

describe aggregate_examples: true do
# or
describe :aggregate_examples do

the other any of:

describe aggregate_examples: false do
# or
describe aggregate_examples: true do
# or
describe :aggregate_examples do

end

def find_expectation(node, &block)
Expand Down