Module: RuboCop::Cop::CheckLineBreakable

Included in:
Layout::LineLength
Defined in:
lib/rubocop/cop/mixin/check_line_breakable.rb

Overview

This mixin detects collections that are safe to “break” by inserting new lines. This is useful for breaking up long lines.

Let’s look at hashes as an example:

We know hash keys are safe to break across lines. We can add linebreaks into hashes on lines longer than the specified maximum. Then in further passes cops can clean up the multi-line hash. For example, say the maximum line length is as indicated below:

|
v

“0000000000”, bar: “0000000000”, baz: “0000000000”

In a LineLength autocorrection pass, a line is added before the first key that exceeds the column limit:

“0000000000”, bar: “0000000000”, baz: “0000000000”

In a MultilineHashKeyLineBreaks pass, lines are inserted before all keys:

“0000000000”, bar: “0000000000”, baz: “0000000000”

Then in future passes FirstHashElementLineBreak, MultilineHashBraceLayout, and TrailingCommaInHashLiteral will manipulate as well until we get:

foo: "0000000000",
bar: "0000000000",
baz: "0000000000",

(Note: Passes may not happen exactly in this sequence.)

Instance Method Summary collapse

Instance Method Details

#extract_breakable_node(node, max) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rubocop/cop/mixin/check_line_breakable.rb', line 45

def extract_breakable_node(node, max)
  if node.send_type?
    return if chained_to_heredoc?(node)

    args = process_args(node.arguments)
    return extract_breakable_node_from_elements(node, args, max)
  elsif node.def_type?
    return extract_breakable_node_from_elements(node, node.arguments, max)
  elsif node.array_type? || node.hash_type?
    return extract_breakable_node_from_elements(node, node.children, max)
  end
  nil
end