Class: RuboCop::Cop::Tablecop::CondenseIn
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Tablecop::CondenseIn
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/tablecop/condense_in.rb
Overview
Checks for multi-line ‘in` clauses (pattern matching) that could be condensed to a single line using the `then` keyword, with alignment.
This cop encourages a table-like, vertically-aligned style for pattern matching case statements where each in clause fits on one line.
When guard clauses are present, three-column alignment is used: patterns align, then ‘if` keywords align, then `then` keywords align.
Constant Summary collapse
- MSG =
"Condense `in` to single line with aligned `then`"
Instance Method Summary collapse
Instance Method Details
#on_case_match(node) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rubocop/cop/tablecop/condense_in.rb', line 49 def on_case_match(node) in_nodes = node.in_pattern_branches return if in_nodes.empty? # Analyze which in-patterns can be condensed condensable = in_nodes.map { |n| [n, condensable?(n)] } # If none can be condensed, nothing to do return unless condensable.any? { |_, can| can } # Calculate alignment widths from all condensable in-patterns max_pattern_width = calculate_max_pattern_width(condensable) max_guard_width = calculate_max_guard_width(condensable) # Check if alignment would exceed line length for any condensable in-pattern use_alignment = can_align_all?(condensable, max_pattern_width, max_guard_width, node) # Register offenses and corrections for each condensable in-pattern condensable.each do |in_node, can_condense| next unless can_condense next if in_node.single_line? # Already condensed register_offense(in_node, max_pattern_width, max_guard_width, use_alignment, node) end end |