class RuboCop::Cop::Style::MultilineInPatternThen

Checks uses of the `then` keyword in multi-line `in` statement.

@example

# bad
case expression
in pattern then
end

# good
case expression
in pattern
end

# good
case expression
in pattern then do_something
end

# good
case expression
in pattern then do_something(arg1,
                             arg2)
end

Constants

MSG

Public Instance Methods

on_in_pattern(node) click to toggle source
# File lib/rubocop/cop/style/multiline_in_pattern_then.rb, line 39
def on_in_pattern(node)
  return if !node.then? || require_then?(node)

  range = node.loc.begin
  add_offense(range) do |corrector|
    corrector.remove(range_with_surrounding_space(range, side: :left, newlines: false))
  end
end

Private Instance Methods

require_then?(in_pattern_node) click to toggle source

Requires `then` for write `in` and its body on the same line.

# File lib/rubocop/cop/style/multiline_in_pattern_then.rb, line 51
def require_then?(in_pattern_node)
  return true unless in_pattern_node.pattern.single_line?
  return false unless in_pattern_node.body

  same_line?(in_pattern_node, in_pattern_node.body)
end