Class: Shirobai::Cop::Layout::DefEndAlignment

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

Overview

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

Rust walks the AST once, reproducing stock’s callback dispatch: ‘on_def` / `on_defs` check a bare definition’s ‘end` against the configured style (the `def` keyword), while `on_send` handles a `def_modifier?` send (`private def foo`) — there both styles are available (`def` = the def keyword, `start_of_line` = the `private def` prefix) and the inner def is `ignore_node`d so its own callback is a no-op. 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. The alignment column comes from Rust (it already encodes the `node` vs `node.parent` anchor choice for `start_of_line`).

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



38
# File 'lib/shirobai/cop/layout/def_end_alignment.rb', line 38

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

.bundle_args(config) ⇒ Object

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



42
43
44
45
# File 'lib/shirobai/cop/layout/def_end_alignment.rb', line 42

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

.cop_nameObject



37
# File 'lib/shirobai/cop/layout/def_end_alignment.rb', line 37

def self.cop_name = "Layout/DefEndAlignment"

Instance Method Details

#on_new_investigationObject



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

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`.



67
68
69
# File 'lib/shirobai/cop/layout/def_end_alignment.rb', line 67

def style_parameter_name
  "EnforcedStyleAlignWith"
end