Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rubocop-rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require_relative 'rubocop/rspec/wording'
require_relative 'rubocop/rspec/language'
require_relative 'rubocop/rspec/language/node_pattern'
require_relative 'rubocop/rspec/config_shortcuts'
require_relative 'rubocop/rspec/concept'
require_relative 'rubocop/rspec/example_group'
require_relative 'rubocop/rspec/example'
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/cop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module RSpec
class Cop < WorkaroundCop
include RuboCop::RSpec::Language
include RuboCop::RSpec::Language::NodePattern
include RuboCop::RSpec::ConfigShortcuts

DEFAULT_CONFIGURATION =
RuboCop::RSpec::CONFIG.fetch('AllCops').fetch('RSpec')
Expand Down
8 changes: 5 additions & 3 deletions lib/rubocop/cop/rspec/empty_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ class EmptyHook < Cop

MSG = 'Empty hook detected.'

def_node_matcher :empty_hook?, <<~PATTERN
(block $#{Hooks::ALL.send_pattern} _ nil?)
def_node_matcher :empty_block?, <<~PATTERN
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need a matcher to tell that a block is empty?
add_offense(hook) is node.body.nil? should do the job

(block $#{Patterns::SEND_PATTERN} _ nil?)
PATTERN

def on_block(node)
empty_hook?(node) do |hook|
return unless hook?(node)

empty_block?(node) do |hook|
add_offense(hook)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/scattered_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def on_block(node)
end

def repeated_hooks(node)
hooks = RuboCop::RSpec::ExampleGroup.new(node)
hooks = RuboCop::RSpec::ExampleGroup.new(node, config)
.hooks
.select(&:knowable_scope?)
.group_by { |hook| [hook.name, hook.scope, hook.metadata] }
Expand Down
5 changes: 4 additions & 1 deletion lib/rubocop/rspec/concept.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ module RSpec
class Concept
include Language
include Language::NodePattern
include ConfigShortcuts
extend NodePattern::Macros

def initialize(node)
def initialize(node, config = nil)
@node = node
@config = config
end

def eql?(other)
Expand All @@ -29,6 +31,7 @@ def to_node
protected

attr_reader :node
attr_reader :config
end
end
end
18 changes: 18 additions & 0 deletions lib/rubocop/rspec/config_shortcuts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module RuboCop
module RSpec
# Config shortcuts for RuboCop::Cop::Rspec::Cop
# and Rubocop::RSpec::ExampleGroup
module ConfigShortcuts
def rspec_aliases(setting)
config
.for_all_cops
.fetch('RSpec', {})
.fetch('Aliases', {})
.fetch(setting, [])
.map(&:to_sym)
end
end
end
end
32 changes: 21 additions & 11 deletions lib/rubocop/rspec/language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ module RSpec
module Language
RSPEC = '{(const {nil? cbase} :RSpec) nil?}'

module Patterns
BLOCK_PATTERN = <<-PATTERN
(block (send {(const {nil? cbase} :RSpec) nil?} $_ ...) ...)
PATTERN

SEND_PATTERN = <<-PATTERN
(send {(const {nil? cbase} :RSpec) nil?} $_ ...)
PATTERN
end

# Set of method selectors
class SelectorSet
def initialize(selectors)
Expand Down Expand Up @@ -91,17 +101,17 @@ module Examples
end

module Hooks
ALL = SelectorSet.new(
%i[
prepend_before
before
append_before
around
prepend_after
after
append_after
]
)
NAMES = %i[
prepend_before
before
append_before
around
prepend_after
after
append_after
].freeze

ALL = SelectorSet.new(NAMES)

module Scopes
ALL = SelectorSet.new(
Expand Down
9 changes: 8 additions & 1 deletion lib/rubocop/rspec/language/node_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ module NodePattern

def_node_matcher :example?, Examples::ALL.block_pattern

def_node_matcher :hook?, Hooks::ALL.block_pattern
def hook?(node)
block_pattern(node) do |hook|
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you need a pattern for this.

return false unless node.block_type?
hooks.include?(node.method_name)

hooks = Hooks::NAMES + rspec_aliases('Hooks')
hooks.include?(hook)
end
end

def_node_matcher :let?, Helpers::ALL.block_or_block_pass_pattern

def_node_matcher :subject?, Subject::ALL.block_pattern

def_node_matcher :block_pattern, Patterns::BLOCK_PATTERN
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/rubocop/cop/rspec/empty_hook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,30 @@
RUBY
end
end

# TODO: These tests just demonstrate that hooks aliases work in rewritten cops
context 'with `custom_hook` hook' do
it 'does NOT detect offense for empty NOT configured hook' do
expect_no_offenses(<<~RUBY)
custom_hook {}
RUBY
end

context 'when `custom_hook` is configured in AllCops.RSpec.Aliases.Hooks' do
include_context 'config'

let(:all_cops_config) do
{ 'RSpec' => { 'Aliases' => { 'Hooks' => %w[custom_hook] } } }
end

it 'detects offense for empty configured hook' do
expect_offense(<<~RUBY)
custom_hook {}
^^^^^^^^^^^ Empty hook detected.
RUBY

expect_correction('')
end
end
end
end
26 changes: 26 additions & 0 deletions spec/rubocop/cop/rspec/expect_in_hook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,30 @@
end
RUBY
end

# TODO: These tests just demonstrate that hooks aliases work in rewritten cops
it 'does NOT add an offense for `expect` with block in `custom_hook` hook' do
expect_no_offenses(<<-RUBY)
setup do
expect { something }.to eq('foo')
end
RUBY
end

context 'when `custom_hook` is configured in AllCops.RSpec.Aliases.Hooks' do
include_context 'config'

let(:all_cops_config) do
{ 'RSpec' => { 'Aliases' => { 'Hooks' => %w[custom_hook] } } }
end

it 'adds an offense for `expect` with block in `custom_hook` hook' do
expect_offense(<<-RUBY)
custom_hook do
expect { something }.to eq('foo')
^^^^^^ Do not use `expect` in `custom_hook` hook
end
RUBY
end
end
end