class RuboCop::Cop::Style::SingleLineDoEndBlock
->(arg) { bar(arg) }
# good
->(arg) do bar(arg) end
# bad
end
bar(arg)
foo do |arg|
# good
foo do |arg| bar(arg) end
# bad
@example
`Layout/RedundantLineBreak` offense.
by not registering an offense if it would subsequently cause a
be autocorrected to be on a single line if possible. This cop respects that configuration
NOTE: If ‘InspectBlocks` is set to `true` for `Layout/RedundantLineBreak`, blocks will
`do` … `end` syntax to preserve semantics and does not change it to `{`…`}` block.
is configured for `Style/BlockDelimiters`. The autocorrection maintains the
In practice a single line `do`…`end` is autocorrected when `EnforcedStyle: semantic`
Checks for single-line `do`…`end` block.
def do_line(node)
def do_line(node) if node.numblock_type? || node.arguments.children.empty? || node.send_node.lambda_literal? node.loc.begin else node.arguments end end
def on_block(node)
def on_block(node) return if !node.single_line? || node.braces? return if single_line_blocks_preferred? && suitable_as_single_line?(node) add_offense(node) do |corrector| corrector.insert_after(do_line(node), "\n") node_body = node.body if node_body.respond_to?(:heredoc?) && node_body.heredoc? corrector.remove(node.loc.end) corrector.insert_after(node_body.loc.heredoc_end, "\nend") else corrector.insert_before(node.loc.end, "\n") end end end
def single_line_blocks_preferred?
def single_line_blocks_preferred? redundant_line_break_config = @config.for_cop('Layout/RedundantLineBreak') redundant_line_break_config['Enabled'] && redundant_line_break_config['InspectBlocks'] end