Class: Shirobai::Cop::Layout::IndentationConsistency

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

Overview

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

Rust walks the AST, reproduces the stock cop’s ‘on_begin` / `on_kwbegin` over every multi-statement group (a parser-gem `begin` / `kwbegin`), runs `check_alignment` (base column from the first child or `base_column_for_normal_style`, sections split by `private` / `protected` in `indented_internal_methods` style) and returns, per offending child, its range, the `column_delta` and whether it is autocorrectable (false when the child’s range is ‘within?` an already-registered offense in the same pass — `@current_offenses`). Ruby applies the realignment via `AlignmentCorrector`, the same division of labour as the other indentation cops. Offenses come from the per-file bundled run (`Shirobai::Dispatch`); the autocorrect re-passes go through the standalone entry point.

Constant Summary collapse

MSG =
"Inconsistent indentation detected."

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



28
# File 'lib/shirobai/cop/layout/indentation_consistency.rb', line 28

def self.badge = RuboCop::Cop::Badge.parse("Layout/IndentationConsistency")

.bundle_args(config) ⇒ Object

Packed args for the bundled run: ‘[indented_internal_methods]`.



31
32
33
34
# File 'lib/shirobai/cop/layout/indentation_consistency.rb', line 31

def self.bundle_args(config)
  cop_config = config.for_badge(badge)
  [(cop_config["EnforcedStyle"] || "normal") == "indented_internal_methods" ? 1 : 0]
end

.cop_nameObject



27
# File 'lib/shirobai/cop/layout/indentation_consistency.rb', line 27

def self.cop_name = "Layout/IndentationConsistency"

Instance Method Details

#on_new_investigationObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/shirobai/cop/layout/indentation_consistency.rb', line 36

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

  offenses_for_source.each do |start, fin, column_delta, autocorrect|
    range = Parser::Source::Range.new(buffer, off[start], off[fin])
    # Mirror the per-offense split (see argument_alignment): in lint mode
    # the block still runs and a non-empty corrector keeps the offense
    # correctable to match stock; `autocorrect` is false when the offense
    # is nested in an already-corrected range and must be reported only.
    unless autocorrect
      add_offense(range, message: MSG)
      next
    end

    add_offense(range, message: MSG) do |corrector|
      node = node_at(off[start], off[fin])
      target = node || range
      RuboCop::Cop::AlignmentCorrector.correct(
        corrector, processed_source, target, column_delta
      )
    end
  end
end