Class: Shirobai::Cop::Layout::AccessModifierIndentation

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

Overview

Drop-in Rust reimplementation of Layout/AccessModifierIndentation.

Rust walks the AST once, reproducing stock's on_class / on_sclass / on_module / on_block alias chain: for every class-like / block node, look at the body and — when it is a parser-(begin ...) (>= 2 statements; prism's single-statement StatementsNode mirrors stock's bare body so we skip it) — inspect each direct child send that is a bare_access_modifier? (no receiver / no arguments / no block, name in {public, protected, private, module_function}). Same-line modifiers (same_line?(node, modifier)) are skipped, exactly like stock. For each kept modifier Rust returns column_delta = expected - actual (matching stock's @column_delta in column_offset_between(modifier.source_range, node.loc.end)); the wrapper either fires correct_style_detected (delta zero) or registers the offense and shifts the modifier's line by column_delta (mirroring stock's AlignmentCorrector.correct(corrector, processed_source, node, column_delta) for a one-line node, which simply inserts or removes leading whitespace).

Stock's AlignmentCorrector.correct reads processed_source.config (for using_tabs?); a faithful drop-in does the same — autocorrect is skipped entirely under Layout/IndentationStyle: tabs, just like stock.

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

Constant Summary collapse

STYLE_TO_U8 =
{ indent: 0, outdent: 1 }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



40
# File 'lib/shirobai/cop/layout/access_modifier_indentation.rb', line 40

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

.bundle_args(config) ⇒ Object

Packed args for the bundled run: [style, indentation_width]. indentation_width mirrors Alignment#configured_indentation_width — the cop's own IndentationWidth if set, else Layout/IndentationWidth's Width, else 2.



46
47
48
49
50
51
52
53
# File 'lib/shirobai/cop/layout/access_modifier_indentation.rb', line 46

def self.bundle_args(config)
  own = config.for_badge(badge)
  style = STYLE_TO_U8.fetch((own["EnforcedStyle"] || "indent").to_sym, 0)
  width = own["IndentationWidth"] ||
          config.for_cop("Layout/IndentationWidth")["Width"] ||
          2
  [style, width]
end

.cop_nameObject



39
# File 'lib/shirobai/cop/layout/access_modifier_indentation.rb', line 39

def self.cop_name = "Layout/AccessModifierIndentation"

Instance Method Details

#on_new_investigationObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/shirobai/cop/layout/access_modifier_indentation.rb', line 55

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

  records_for_source.each do |start, fin, message, column_delta|
    send_range = Parser::Source::Range.new(buffer, off[start], off[fin])
    if column_delta.zero?
      correct_style_detected
      next
    end
    add_offense(send_range, message: message) do |corrector|
      # Delegate to stock's `AlignmentCorrector.correct`. It reads
      # `processed_source.config` for tab-style detection and string /
      # block-comment range avoidance; for a single-line modifier
      # those checks fall through to a single `each_line` iteration
      # that just inserts or removes leading whitespace. Passing the
      # `Parser::Source::Range` directly avoids needing the actual
      # AST node — stock's implementation accepts either (`node
      # .respond_to?(:loc) ? node.source_range : node`).
      RuboCop::Cop::AlignmentCorrector.correct(
        corrector, processed_source, send_range, column_delta
      )
      opposite_or_unrecognized_style(column_delta)
    end
  end
end