Class: RuboCop::Cop::Elegant::IndentationLadder

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/elegant/indentation_ladder.rb

Overview

Enforces the “indentation ladder” rule: when a line is indented further to the right than the previous non-empty line, the extra indentation must be exactly two spaces. Larger jumps (or odd ones, such as a single space or three spaces) break the visual rhythm of the code and make nesting harder to follow. Lines that match the previous indentation, or de-indent by any amount, are not affected. Lines that belong to the body of a heredoc are ignored, because their whitespace is part of the literal value rather than program structure.

Constant Summary collapse

MSG =
'Indentation step of %<step>d spaces is not allowed; use 2 spaces'

Instance Method Summary collapse

Instance Method Details

#on_new_investigationObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rubocop/cop/elegant/indentation_ladder.rb', line 18

def on_new_investigation
  super
  skip = heredocs
  prev = nil
  processed_source.lines.each_with_index do |line, idx|
    num = idx + 1
    next if skip.include?(num)
    next if line.strip.empty?
    indent = line[/\A */].length
    register(num, indent - prev) if prev && indent > prev && (indent - prev) != 2
    prev = indent
  end
end