Skip to content

Commit

Permalink
Use children matching in Pending cop
Browse files Browse the repository at this point in the history
  • Loading branch information
pirj committed Aug 11, 2019
1 parent 4e17b25 commit ce421f0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
45 changes: 21 additions & 24 deletions lib/rubocop/cop/rspec/pending.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ module RSpec
# end
#
# describe MyClass do
# it "should be true", skip: true do
# expect(1).to eq(2)
# end
# end
#
# describe MyClass do
# it "should be true" do
# pending
# end
Expand All @@ -28,43 +34,34 @@ module RSpec
class Pending < Cop
MSG = 'Pending spec found.'

PENDING_EXAMPLES = Examples::PENDING + Examples::SKIPPED \
+ ExampleGroups::SKIPPED
SKIPPABLE_EXAMPLES = ExampleGroups::GROUPS + Examples::EXAMPLES
SKIPPABLE_SELECTORS = SKIPPABLE_EXAMPLES.node_pattern_union
PENDING = Examples::PENDING + Examples::SKIPPED + ExampleGroups::SKIPPED
SKIPPABLE = ExampleGroups::GROUPS + Examples::EXAMPLES

SKIP_SYMBOL = s(:sym, :skip)
PENDING_SYMBOL = s(:sym, :pending)
def_node_matcher :skippable?, SKIPPABLE.send_pattern

def_node_matcher :metadata, <<-PATTERN
{(send #{RSPEC} #{SKIPPABLE_SELECTORS} ... (hash $...))
(send #{RSPEC} #{SKIPPABLE_SELECTORS} $...)}
def_node_matcher :skipped_in_symbol_metadata?, <<-PATTERN
(send _ _ <#skip_or_pending? ...>)
PATTERN

def_node_matcher :pending_block?, PENDING_EXAMPLES.send_pattern
def_node_matcher :skipped_in_hash_metadata?, <<-PATTERN
(send _ _ ... (hash <(pair #skip_or_pending? true) ...>))
PATTERN

def_node_matcher :skip_or_pending?, '{(sym :skip) (sym :pending)}'
def_node_matcher :pending_block?, PENDING.send_pattern

def on_send(node)
return unless pending_block?(node) || skipped_from_metadata?(node)
return unless pending_block?(node) || skipped_in_metadata?(node)

add_offense(node)
end

private

def skipped_from_metadata?(node)
(metadata(node) || []).any? { |n| skip_node?(n) }
end

def skip_node?(node)
if node.respond_to?(:key)
skip_symbol?(node.key) && node.value.truthy_literal?
else
skip_symbol?(node)
end
end
def skipped_in_metadata?(node)
return unless skippable?(node)

def skip_symbol?(symbol_node)
[SKIP_SYMBOL, PENDING_SYMBOL].include?(symbol_node)
skipped_in_hash_metadata?(node) || skipped_in_symbol_metadata?(node)
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions manual/cops_rspec.md
Original file line number Diff line number Diff line change
Expand Up @@ -2199,6 +2199,12 @@ describe MyClass do
it "should be true"
end

describe MyClass do
it "should be true", skip: true do
expect(1).to eq(2)
end
end

describe MyClass do
it "should be true" do
pending
Expand Down
22 changes: 11 additions & 11 deletions spec/rubocop/cop/rspec/pending_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,61 +124,61 @@
RUBY
end

it 'does not flag describe' do
it 'ignores describe' do
expect_no_offenses(<<-RUBY)
describe 'test' do; end
RUBY
end

it 'does not flag example' do
it 'ignores example' do
expect_no_offenses(<<-RUBY)
example 'test' do; end
RUBY
end

it 'does not flag scenario' do
it 'ignores scenario' do
expect_no_offenses(<<-RUBY)
scenario 'test' do; end
RUBY
end

it 'does not flag specify' do
it 'ignores specify' do
expect_no_offenses(<<-RUBY)
specify 'test' do; end
specify do; end
RUBY
end

it 'does not flag feature' do
it 'ignores feature' do
expect_no_offenses(<<-RUBY)
feature 'test' do; end
RUBY
end

it 'does not flag context' do
it 'ignores context' do
expect_no_offenses(<<-RUBY)
context 'test' do; end
RUBY
end

it 'does not flag it' do
it 'ignores it' do
expect_no_offenses(<<-RUBY)
it 'test' do; end
RUBY
end

it 'does not flag it with skip: false metadata' do
it 'ignores it with skip: false metadata' do
expect_no_offenses(<<-RUBY)
it 'test', skip: false do; end
RUBY
end

it 'does not flag example_group' do
it 'ignores example_group' do
expect_no_offenses(<<-RUBY)
example_group 'test' do; end
RUBY
end

it 'does not flag method called pending' do
it 'ignores method called pending' do
expect_no_offenses(<<-RUBY)
subject { Project.pending }
RUBY
Expand Down

0 comments on commit ce421f0

Please sign in to comment.