Class: RuboCop::Cop::Yoshiki::Style::ItBlockParameter

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetRubyVersion
Includes:
ConfigurableEnforcedStyle
Defined in:
lib/rubocop/cop/yoshiki/style/it_block_parameter.rb

Overview

Checks for blocks with one argument where it block parameter can be used.

force_single_line requires a single-line one-parameter block to use it. Multi-line blocks may keep a named parameter, except |it| — never declare that name; it is the anonymous parameter.

Hash value omission keeps the original key (name: becomes name: it, not it:). Replacing the identifier would make it:, which calls a method named it instead of the parameter.

Examples:

EnforcedStyle: force_single_line

# bad
block { |named_param| do_something(named_param) }
block { do_something(_1) }
block { |name| remember!(name:) }
block { |it| do_something(it) }

# good
block { do_something(it) }
block { remember!(name: it) }
block do |named_param|
  do_something(named_param)
end

Constant Summary collapse

MSG_USE_IT_PARAMETER =
'Use `it` block parameter.'.freeze

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rubocop/cop/yoshiki/style/it_block_parameter.rb', line 39

def on_block node
  return correct_named_it(node) if named_it_parameter?(node)
  return unless force_single_line?
  return unless node.single_line?
  return unless node.arguments.one?
  return unless node.first_argument.arg_type?

  correct_to_it(node, node.first_argument.source) do |corrector|
    remove_block_arguments(corrector, node)
  end
end

#on_numblock(node) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/rubocop/cop/yoshiki/style/it_block_parameter.rb', line 51

def on_numblock node
  return unless force_single_line?
  return unless node.single_line?
  return unless node.children[1] == 1

  correct_to_it(node, '_1')
end