Class: Shirobai::Cop::Layout::EndAlignment

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/end_alignment.rb

Overview

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

Rust walks the AST once, reproducing stock’s callback dispatch: the ‘CheckAssignment` `on_lvasgn` / `on_send` family resolves a conditional on the RHS of an assignment (following the call chain and unwrapping `begin`/`||`/`&&`) and checks it against the assignment LHS (`variable`), while `on_if` / `on_while` / `on_case` / `on_class` / … check a bare construct against its own keyword. Per checked `end` it returns the matched styles (for `config_to_allow_offenses` parity) and, when the configured style is not matched, the offense message and the autocorrect target column.

This wrapper replays stock’s ‘check_end_kw_alignment` decisions in walk order: `correct_style_detected` when the configured style already matches, otherwise `add_offense` plus `style_detected(matched styles)`. The autocorrect mirrors `AlignmentCorrector#align_end`: replace the whitespace before `end` with the target indentation, or (when something non-space precedes `end`) insert a newline + indentation after it.

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 =
{ keyword: 0, variable: 1, start_of_line: 2 }.freeze
U8_TO_STYLE =
STYLE_TO_U8.invert.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



36
# File 'lib/shirobai/cop/layout/end_alignment.rb', line 36

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

.bundle_args(config) ⇒ Object

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



40
41
42
43
# File 'lib/shirobai/cop/layout/end_alignment.rb', line 40

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

.cop_nameObject



35
# File 'lib/shirobai/cop/layout/end_alignment.rb', line 35

def self.cop_name = "Layout/EndAlignment"

Instance Method Details

#on_new_investigationObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/shirobai/cop/layout/end_alignment.rb', line 45

def on_new_investigation
  buffer = processed_source.buffer
  off = SourceOffsets.for(processed_source.raw_source)
  style_bit = STYLE_TO_U8.fetch(style)

  records_for_source.each do |end_start, end_end, matching, message, align_column|
    if matching.include?(style_bit)
      correct_style_detected
      next
    end

    end_range = Parser::Source::Range.new(buffer, off[end_start], off[end_end])
    add_offense(end_range, message: message) do |corrector|
      align_end(corrector, buffer, off, end_range, align_column)
    end
    style_detected(matching.map { |bit| U8_TO_STYLE.fetch(bit) })
  end
end

#style_parameter_nameObject

‘EndKeywordAlignment` keys its style off `EnforcedStyleAlignWith`.



65
66
67
# File 'lib/shirobai/cop/layout/end_alignment.rb', line 65

def style_parameter_name
  "EnforcedStyleAlignWith"
end