Class: Shirobai::Cop::Layout::ElseAlignment

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

Overview

Drop-in Rust reimplementation of Layout/ElseAlignment.

Rust walks the AST once, reproducing stock's callbacks: on_if / on_case / on_case_match / on_rescue and the CheckAssignment family. Each else / elsif keyword (and the else of a case / begin / def / block rescue chain) is checked against the construct it belongs to; elsif chains carry the same base down the recursion, and an if on an assignment RHS aligns with the assignment node (variable) or the if (keyword) per Layout/EndAlignment's EnforcedStyleAlignWith. Per misaligned keyword Rust returns the keyword range, the formatted message, and the signed column delta.

The autocorrect mirrors AlignmentCorrector.correct for the single-line keyword range: shift the keyword's line by the column delta (insert spaces at the line start for a positive delta, remove leading whitespace for a negative one). As in stock, the correction is skipped (offense still registered) when Layout/IndentationStyle is tabs.

Offenses come from the per-file bundled run (Shirobai::Dispatch); the behaviour is purely config-driven, so this cop is always bundle eligible.

Constant Summary collapse

STYLE_TO_U8 =
{ keyword: 0, variable: 1, start_of_line: 2 }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



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

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

.bundle_args(config) ⇒ Object

Packed args for the bundled run: [style]. ElseAlignment reads Layout/EndAlignment's EnforcedStyleAlignWith (default keyword).



37
38
39
40
# File 'lib/shirobai/cop/layout/else_alignment.rb', line 37

def self.bundle_args(config)
  align = config.for_cop("Layout/EndAlignment")["EnforcedStyleAlignWith"]
  [STYLE_TO_U8.fetch((align || "keyword").to_sym, 0)]
end

.cop_nameObject



32
# File 'lib/shirobai/cop/layout/else_alignment.rb', line 32

def self.cop_name = "Layout/ElseAlignment"

Instance Method Details

#on_new_investigationObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/shirobai/cop/layout/else_alignment.rb', line 42

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

  records_for_source.each do |else_start, else_end, message, column_delta|
    else_range = Parser::Source::Range.new(buffer, off[else_start], off[else_end])
    add_offense(else_range, message: message) do |corrector|
      next if tabs

      align_keyword(corrector, buffer, else_range, column_delta)
    end
  end
end