Skip to content

Commit

Permalink
Add SelectorSet#send_pattern and #block_pattern
Browse files Browse the repository at this point in the history
Resolves duplicated pattern definitions of the formats

    (block (send _ #{SELECTOR_SET.node_pattern_union}))

and

    (send _ #{SELECTOR_SET.node_pattern_union})
  • Loading branch information
backus committed Dec 21, 2016
1 parent 7343505 commit bd88c07
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 24 deletions.
7 changes: 2 additions & 5 deletions lib/rubocop/cop/rspec/described_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@ class DescribedClass < Cop
DESCRIBED_CLASS = 'described_class'.freeze
MSG = "Use `#{DESCRIBED_CLASS}` instead of `%s`".freeze

RSPEC_BLOCK_METHODS = RuboCop::RSpec::Language::ALL.node_pattern_union

def_node_matcher :common_instance_exec_closure?, <<-PATTERN
(block (send (const nil {:Class :Module}) :new ...) ...)
PATTERN

def_node_matcher :rspec_block?, <<-PATTERN
(block (send nil #{RSPEC_BLOCK_METHODS} ...) ...)
PATTERN
def_node_matcher :rspec_block?,
RuboCop::RSpec::Language::ALL.block_pattern

def_node_matcher :scope_changing_syntax?, '{def class module}'

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/empty_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class EmptyExampleGroup < Cop

def_node_search :contains_example?, <<-PATTERN
{
(send _ #{(Examples::ALL + Includes::ALL).node_pattern_union} ...)
#{(Examples::ALL + Includes::ALL).send_pattern}
(send _ #custom_include? ...)
}
PATTERN
Expand Down
5 changes: 1 addition & 4 deletions lib/rubocop/cop/rspec/focus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class Focus < Cop
focused = ExampleGroups::FOCUSED + Examples::FOCUSED

FOCUSABLE_SELECTORS = focusable.node_pattern_union
FOCUSING_SELECTORS = focused.node_pattern_union

FOCUS_SYMBOL = s(:sym, :focus)
FOCUS_TRUE = s(:pair, FOCUS_SYMBOL, s(:true))
Expand All @@ -41,9 +40,7 @@ class Focus < Cop
(send nil #{FOCUSABLE_SELECTORS} $...)}
PATTERN

def_node_matcher :focused_block?, <<-PATTERN
(send nil #{FOCUSING_SELECTORS} ...)
PATTERN
def_node_matcher :focused_block?, focused.send_pattern

def on_send(node)
focus_metadata(node) do |focus|
Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop/cop/rspec/instance_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ class InstanceVariable < Cop

EXAMPLE_GROUP_METHODS = ExampleGroups::ALL + SharedGroups::ALL

def_node_matcher :spec_group?, <<-PATTERN
(block (send _ #{EXAMPLE_GROUP_METHODS.node_pattern_union} ...) ...)
PATTERN
def_node_matcher :spec_group?, EXAMPLE_GROUP_METHODS.block_pattern

def_node_search :ivar_usage, '$(ivar $_)'

Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop/cop/rspec/multiple_expectations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ class MultipleExpectations < Cop

MSG = 'Too many expectations.'.freeze

def_node_matcher :example?, <<-PATTERN
(block (send _ #{Examples::ALL.node_pattern_union} ...) ...)
PATTERN
def_node_matcher :example?, Examples::ALL.block_pattern

def_node_search :expect, '(send _ :expect ...)'

Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop/cop/rspec/nested_groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ class NestedGroups < Cop

MSG = 'Maximum example group nesting exceeded'.freeze

def_node_search :find_contexts, <<-PATTERN
(block (send nil #{ExampleGroups::ALL.node_pattern_union} ...) (args) ...)
PATTERN
def_node_search :find_contexts, ExampleGroups::ALL.block_pattern

def on_block(node)
describe, = described_constant(node)
Expand Down
12 changes: 10 additions & 2 deletions lib/rubocop/rspec/language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@ def include?(selector)
selectors.include?(selector)
end

def node_pattern
selectors.map(&:inspect).join(' ')
def block_pattern
"(block #{send_pattern} ...)"
end

def send_pattern
"(send _ #{node_pattern_union} ...)"
end

def node_pattern_union
"{#{node_pattern}}"
end

def node_pattern
selectors.map(&:inspect).join(' ')
end

protected

attr_reader :selectors
Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop/rspec/language/node_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ module Language
module NodePattern
extend RuboCop::NodePattern::Macros

def_node_matcher :example_group?, <<-PATTERN
(block (send _ #{ExampleGroups::ALL.node_pattern_union} ...) ...)
PATTERN
def_node_matcher :example_group?, ExampleGroups::ALL.block_pattern
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions spec/rubocop/rspec/language/selector_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,18 @@
expect(selector_set.node_pattern_union).to eql('{:foo :bar}')
end
end

context '#send_pattern' do
it 'builds a send matching pattern' do
expect(selector_set.send_pattern).to eql('(send _ {:foo :bar} ...)')
end
end

context '#block_pattern' do
it 'builds a block matching pattern' do
expect(selector_set.block_pattern).to eql(
'(block (send _ {:foo :bar} ...) ...)'
)
end
end
end

0 comments on commit bd88c07

Please sign in to comment.