Skip to content

Commit 95ec139

Browse files
committed
Add proper handling of if/elsif/else blocks.
1 parent f014035 commit 95ec139

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

lib/rubocop/socketry/layout/consistent_blank_line_indentation.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def on_new_investigation
5252

5353
processed_source.lines.each_with_index do |line, index|
5454
line_number = index + 1
55-
55+
5656
unless delta = indentation_deltas[line_number]
5757
# Skip this line (e.g., non-squiggly heredoc content):
5858
next
@@ -97,11 +97,20 @@ def walk_ast_for_indentation(node, deltas)
9797
return unless node.is_a?(Parser::AST::Node)
9898

9999
case node.type
100-
when :block, :hash, :array, :class, :module, :sclass, :def, :defs, :if, :case, :while, :until, :for, :kwbegin
100+
when :block, :hash, :array, :class, :module, :sclass, :def, :defs, :case, :while, :until, :for, :kwbegin
101+
101102
if location = node.location
102103
deltas[location.line] += 1
103104
deltas[location.last_line] -= 1
104105
end
106+
when :if
107+
# We don't want to add deltas for elsif, because it's handled by the if node:
108+
if node.keyword == "if"
109+
if location = node.location
110+
deltas[location.line] += 1
111+
deltas[location.last_line] -= 1
112+
end
113+
end
105114
when :dstr
106115
if location = node.location
107116
if location.is_a?(Parser::Source::Map::Heredoc) and body = location.heredoc_body

test/rubocop/socketry/layout/consistent_blank_line_indentation.rb

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@
377377
expect(offenses).to be(:empty?)
378378
end
379379
end
380-
380+
381381
with "a heredoc with correct body indentation" do
382382
let(:source) {"def foo\n\tputs <<~FOO\n\t\tHello\n\t\tWorld\n\tFOO\nend\n"}
383383
it "does not register an offense for properly indented heredoc body" do
@@ -388,7 +388,7 @@
388388
expect(offenses).to be(:empty?)
389389
end
390390
end
391-
391+
392392
with "a heredoc with incorrect body indentation" do
393393
let(:source) {"def foo\n\tputs <<~FOO\n\n\t\tHello World\n\tFOO\nend\n"}
394394
it "registers an offense for unindented heredoc body" do
@@ -399,7 +399,7 @@
399399
expect(offenses).not.to be(:empty?)
400400
end
401401
end
402-
402+
403403
with "a non-squiggly heredoc with any indentation" do
404404
let(:source) {"def foo\n\tputs <<-FOO\n\t\tHello\n\t\tWorld\n\tFOO\nend\n"}
405405
it "does not register an offense for non-squiggly heredoc content" do
@@ -410,7 +410,7 @@
410410
expect(offenses).to be(:empty?)
411411
end
412412
end
413-
413+
414414
with "a plain heredoc with any indentation" do
415415
let(:source) {"def foo\n\tputs <<FOO\n\t\tHello\n\t\tWorld\n\tFOO\nend\n"}
416416
it "does not register an offense for plain heredoc content" do
@@ -421,7 +421,7 @@
421421
expect(offenses).to be(:empty?)
422422
end
423423
end
424-
424+
425425
with "an interpolated string" do
426426
let(:source) {"def foo\n\tname = 'world'\n\tputs \"Hello \#{name}\"\n\t\n\tputs 'done'\nend\n"}
427427
it "does not interfere with blank line indentation for interpolated strings" do
@@ -432,4 +432,26 @@
432432
expect(offenses).to be(:empty?)
433433
end
434434
end
435+
436+
with "a blank line in if/elsif/else clauses with proper indentation" do
437+
let(:source) {"if foo\n\t\n\tputs 'foo'\nelsif bar\n\t\n\tputs 'bar'\nelse\n\t\n\tputs 'else'\nend\n"}
438+
it "does not register an offense when blank lines are properly indented" do
439+
processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION.to_f)
440+
investigator = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true)
441+
report = investigator.investigate(processed_source)
442+
offenses = report.offenses
443+
expect(offenses).to be(:empty?)
444+
end
445+
end
446+
447+
with "a blank line in if/elsif/else clauses with incorrect indentation" do
448+
let(:source) {"if foo\n\t\n\tputs 'foo'\nelsif bar\n\t\t\n\tputs 'bar'\nelse\n\t\n\tputs 'else'\nend\n"}
449+
it "registers an offense when blank line in elsif is over-indented" do
450+
processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION.to_f)
451+
investigator = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true)
452+
report = investigator.investigate(processed_source)
453+
offenses = report.offenses
454+
expect(offenses).not.to be(:empty?)
455+
end
456+
end
435457
end

0 commit comments

Comments
 (0)