Class: RuboCop::Socketry::Layout::ConsistentBlankLineIndentation

Inherits:
Cop::Base
  • Object
show all
Extended by:
Cop::AutoCorrector
Includes:
Cop::Alignment, Cop::RangeHelp
Defined in:
lib/rubocop/socketry/layout/consistent_blank_line_indentation.rb

Overview

A RuboCop cop that enforces consistent blank line indentation based on AST structure. This cop ensures that blank lines are indented correctly according to their context in the code, using a two-pass algorithm that analyzes the AST to determine proper indentation levels.

Constant Summary collapse

MESSAGE =
"Blank lines must have the correct indentation."

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#The message displayed when a blank line has incorrect indentation.(messagedisplayed) ⇒ Object (readonly)



20
# File 'lib/rubocop/socketry/layout/consistent_blank_line_indentation.rb', line 20

MESSAGE = "Blank lines must have the correct indentation."

Instance Method Details

#configured_indentation_styleObject

Get the configured indentation style from cop configuration or fallback to default.



30
31
32
# File 'lib/rubocop/socketry/layout/consistent_blank_line_indentation.rb', line 30

def configured_indentation_style
	cop_config["IndentationStyle"] || config.for_cop("Layout/IndentationStyle")["Style"] || "tab"
end

#configured_indentation_widthObject

Get the configured indentation width from cop configuration or fallback to default.



24
25
26
# File 'lib/rubocop/socketry/layout/consistent_blank_line_indentation.rb', line 24

def configured_indentation_width
	cop_config["IndentationWidth"] || config.for_cop("Layout/IndentationWidth")["Width"] || 1
end

#indentation(width) ⇒ Object

Generate indentation string based on the current level and configured style.



37
38
39
40
41
42
43
44
# File 'lib/rubocop/socketry/layout/consistent_blank_line_indentation.rb', line 37

def indentation(width)
	case configured_indentation_style
	when "tab"
		"\t" * (width * configured_indentation_width)
	when "space"
		" " * (width * configured_indentation_width)
	end
end

#on_new_investigationObject

Main investigation method that processes the source code and checks blank line indentation. This method implements a two-pass algorithm: first building indentation deltas from the AST, then processing each line to check blank lines against expected indentation.



49
50
51
52
53
54
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/rubocop/socketry/layout/consistent_blank_line_indentation.rb', line 49

def on_new_investigation
	indentation_deltas = build_indentation_deltas
	current_level = 0
	
	processed_source.lines.each_with_index do |line, index|
		line_number = index + 1
		
		unless delta = indentation_deltas[line_number]
			# Skip this line (e.g., non-squiggly heredoc content):
			next
		end
		
		# Check blank lines for correct indentation:
		if line.strip.empty?
			expected_indentation = indentation(current_level)
			
			if line != expected_indentation
				add_offense(
					source_range(processed_source.buffer, line_number, 0, line.length),
					message: MESSAGE
				) do |corrector|
					corrector.replace(
						source_range(processed_source.buffer, line_number, 0, line.length),
						expected_indentation
					)
				end
			end
		end
		
		current_level += delta
	end
end