Class: RuboCop::Cop::RSpec::HashInBlockStyle

Inherits:
RuboCop::Cop show all
Includes:
RuboCop::Cop::RangeHelp, SurroundingSpace
Defined in:
lib/rubocop/cop/rspec/hash_in_block_style.rb

Overview

Examples:


# bad
let(:foo) { { baz: true } }

# good
let(:foo) { Hash[baz: true] }

Constant Summary collapse

MSG =
"Please use Hash[] in inline block"

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/rspec/hash_in_block_style.rb', line 29

def autocorrect(node)
  left = tokens(node).find(&:left_brace?)
  right = tokens(node).select(&:right_curly_brace?).last
  lambda do |corrector|
    corrector.replace(left.pos, 'Hash[')
    corrector.replace(right.pos, ']')
  end
end

#on_block(node) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/rubocop/cop/rspec/hash_in_block_style.rb', line 19

def on_block(node)
  return unless %i[let let_it_be].include?(node.send_node.method_name)
  return unless inline_block?(node)

  hash = node.children.last
  return unless hash.is_a?(RuboCop::AST::HashNode)

  add_offense(hash, location: :expression)
end