Class: Shirobai::Cop::Layout::AssignmentIndentation

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

Overview

Drop-in Rust reimplementation of ‘Layout/AssignmentIndentation`.

Rust walks the AST once and, for each assignment / setter call whose operator is on a different line from the RHS, computes the RHS’s expected display column (‘leftmost_multiple_assignment.display_column + IndentationWidth`). Misaligned RHSes that begin their own line emit a `[rhs_start, rhs_end, column_delta]` record, which the wrapper turns into an offense at the RHS range. Autocorrect re-locates the matching `Parser::AST::Node` by `rhs_start` and hands it to stock’s ‘AlignmentCorrector#correct` with the same `column_delta` (this keeps the heredoc / string-literal taboo handling identical to stock).

Constant Summary collapse

MSG =
"Indent the first line of the right-hand-side of a multi-line assignment."

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



23
# File 'lib/shirobai/cop/layout/assignment_indentation.rb', line 23

def self.badge = RuboCop::Cop::Badge.parse(cop_name)

.bundle_args(config) ⇒ Object

‘IndentationWidth` falls back to `Layout/IndentationWidth.Width`, then to 2 — exactly stock’s ‘configured_indentation_width`.



27
28
29
30
31
# File 'lib/shirobai/cop/layout/assignment_indentation.rb', line 27

def self.bundle_args(config)
  own = config.for_badge(badge)["IndentationWidth"]
  width = own || config.for_cop("Layout/IndentationWidth")["Width"] || 2
  [width]
end

.cop_nameObject



22
# File 'lib/shirobai/cop/layout/assignment_indentation.rb', line 22

def self.cop_name = "Layout/AssignmentIndentation"

Instance Method Details

#on_new_investigationObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/shirobai/cop/layout/assignment_indentation.rb', line 33

def on_new_investigation
  buffer = processed_source.buffer
  off = SourceOffsets.for(processed_source.raw_source)

  records_for_source.each do |rhs_start, rhs_end, column_delta|
    range = Parser::Source::Range.new(buffer, off[rhs_start], off[rhs_end])
    node = locate_rhs_node(off[rhs_start])
    add_offense(range, message: MSG) do |corrector|
      # The corrector target is the Parser::AST::Node so stock's
      # `inside_string_ranges` / `block_comment_within?` checks work;
      # if relocation fails (defensive), fall back to the range.
      ::RuboCop::Cop::AlignmentCorrector.correct(
        corrector, processed_source, node || range, column_delta
      )
    end
  end
end