Class: Shirobai::Cop::Layout::ArrayAlignment

Inherits:
RuboCop::Cop::Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector
Includes:
RuboCop::Cop::Alignment
Defined in:
lib/shirobai/cop/layout/array_alignment.rb

Overview

Drop-in Rust reimplementation of Layout/ArrayAlignment.

Rust parses the source, walks every 2+-element array literal (plus the bracket-less arrays parser-gem synthesizes: single-assignment RHS lists and rescue exception lists; masgn RHS lists are skipped like stock), picks the alignment base for the configured EnforcedStyle (with_first_element / with_fixed_indentation) and returns each misaligned element as an offense range plus its column_delta. Ruby supplies the flattened config and applies the realignment via AlignmentCorrector (the same division of labour as Layout/ArgumentAlignment). The corrector receives the parser NODE for the offense range (resolved like Layout/IndentationConsistency), so heredoc bodies and multi-line string interiors inside a shifted element stay untouched, matching stock's taboo-range protection. Offenses come from the per-file bundled run (Shirobai::Dispatch); the config derivation is purely config-driven, so this cop is always bundle-eligible.

Constant Summary collapse

ALIGN_ELEMENTS_MSG =
"Align the elements of an array literal " \
"if they span more than one line."
FIXED_INDENT_MSG =
"Use one level of indentation for elements " \
"following the first line of a multi-line array."

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



34
# File 'lib/shirobai/cop/layout/array_alignment.rb', line 34

def self.badge = RuboCop::Cop::Badge.parse("Layout/ArrayAlignment")

.bundle_args(config) ⇒ Object

Packed args for the bundled run: [style, indentation_width].



37
38
39
40
41
42
43
# File 'lib/shirobai/cop/layout/array_alignment.rb', line 37

def self.bundle_args(config)
  cop_config = config.for_badge(badge)
  [
    cop_config["EnforcedStyle"] == "with_fixed_indentation" ? 1 : 0,
    cop_config["IndentationWidth"] || config.for_cop("Layout/IndentationWidth")["Width"] || 2
  ]
end

.cop_nameObject



33
# File 'lib/shirobai/cop/layout/array_alignment.rb', line 33

def self.cop_name = "Layout/ArrayAlignment"

Instance Method Details

#on_new_investigationObject



45
46
47
48
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/shirobai/cop/layout/array_alignment.rb', line 45

def on_new_investigation
  buffer = processed_source.buffer
  message = fixed_indentation? ? FIXED_INDENT_MSG : ALIGN_ELEMENTS_MSG

  offenses = Dispatch.offenses_for(processed_source, config, :array_alignment)
  off = SourceOffsets.for(processed_source.raw_source)
  offenses.each do |start, fin, column_delta, autocorrect|
    range = Parser::Source::Range.new(buffer, off[start], off[fin])
    # Split on the per-offense correctability flag (the mixin's
    # `within?` rule maps to `autocorrect: false`), exactly like
    # argument_alignment: the block must run for correctable offenses
    # even in lint mode so a non-empty corrector marks the offense
    # `:uncorrected` (correctable) to match stock.
    unless autocorrect
      add_offense(range, message: message)
      next
    end

    add_offense(range, message: message) do |corrector|
      # Stock passes the element NODE, whose string/heredoc interiors
      # `AlignmentCorrector` marks taboo. A bare range would realign
      # heredoc bodies inside the element.
      target = node_at(off[start], off[fin]) || range
      RuboCop::Cop::AlignmentCorrector.correct(
        corrector, processed_source, target, column_delta
      )
    end
  end
end