class RuboCop::Cop::Style::MultilineWhenThen
end
arg2)
when bar then do_something(arg1,
case foo
# good
end
when bar then do_something
case foo
# good
end
when bar
case foo
# good
end
when bar then
case foo
# bad
@example
in multi-line when statements.
This cop checks uses of the ‘then` keyword
def accept_node_type?(node)
def accept_node_type?(node) node&.array_type? || node&.hash_type? end
def on_when(node)
def on_when(node) # Without `then`, there's no offense return unless node.then? # Single line usage of `then` is not an offense return if !node.children.last.nil? && !node.multiline? # Requires `then` for write `when` and its body on the same line. return if require_then?(node) # For arrays and hashes there's no offense return if accept_node_type?(node.body) range = node.loc.begin add_offense(range) do |corrector| corrector.remove( range_with_surrounding_space(range: range, side: :left, newlines: false) ) end end
def require_then?(when_node)
def require_then?(when_node) unless when_node.conditions.first.first_line == when_node.conditions.last.last_line return true end return false unless when_node.body when_node.loc.line == when_node.body.loc.line end