Class: Shirobai::Cop::Layout::BlockAlignment

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

Overview

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

Rust walks the AST once, reproducing stock’s ‘on_block` (and `on_numblock` / `on_itblock`): it picks the block’s alignment target by walking the lineage (‘block_end_align_target`), unwraps op_asgn / masgn LHS (`find_lhs_node`), and decides whether the closing token (`end` / `}`) is misaligned under the configured `EnforcedStyleAlignWith`. Each misaligned block returns its closing-token range, the formatted message (including the `either`-only “ or …” alternative), and the autocorrect target column (`compute_start_col`).

The autocorrect mirrors ‘BlockAlignment#autocorrect`: the delta between the target column and the closing token’s column is applied as inserted spaces before the token (positive delta) or removed leading whitespace (negative delta).

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

Constant Summary collapse

STYLE_TO_U8 =
{ either: 0, start_of_block: 1, start_of_line: 2 }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



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

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

.bundle_args(config) ⇒ Object

Packed args for the bundled run: ‘[style]`. `EnforcedStyleAlignWith` defaults to `either` (0) when the config does not mention this cop.



36
37
38
39
# File 'lib/shirobai/cop/layout/block_alignment.rb', line 36

def self.bundle_args(config)
  align = config.for_badge(badge)["EnforcedStyleAlignWith"]
  [STYLE_TO_U8.fetch((align || "either").to_sym, 0)]
end

.cop_nameObject



31
# File 'lib/shirobai/cop/layout/block_alignment.rb', line 31

def self.cop_name = "Layout/BlockAlignment"

Instance Method Details

#on_new_investigationObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/shirobai/cop/layout/block_alignment.rb', line 41

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

  records_for_source.each do |end_start, end_end, message, align_column|
    end_range = Parser::Source::Range.new(buffer, off[end_start], off[end_end])
    add_offense(end_range, message: message) do |corrector|
      autocorrect(corrector, buffer, end_range, align_column)
    end
  end
end

#style_parameter_nameObject

‘EnforcedStyleAlignWith` keys the style.



54
55
56
# File 'lib/shirobai/cop/layout/block_alignment.rb', line 54

def style_parameter_name
  "EnforcedStyleAlignWith"
end