From 559a47596a5a4964df75df7a7dfa25bfc5e3f1c8 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 29 Jul 2026 21:39:25 +1200 Subject: [PATCH 1/2] Fix blank line indentation for modifier conditionals --- .../consistent_blank_line_indentation.rb | 4 ++-- releases.md | 4 ++++ .../consistent_blank_line_indentation.rb | 24 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/rubocop/socketry/layout/consistent_blank_line_indentation.rb b/lib/rubocop/socketry/layout/consistent_blank_line_indentation.rb index 318de58..0f401f3 100644 --- a/lib/rubocop/socketry/layout/consistent_blank_line_indentation.rb +++ b/lib/rubocop/socketry/layout/consistent_blank_line_indentation.rb @@ -120,8 +120,8 @@ def walk_ast_for_indentation(node, deltas, parent = nil) end end when :if - # We don't want to add deltas for elsif, because it's handled by the if node: - if node.keyword == "if" || node.keyword == "unless" + # Modifier conditionals don't introduce indentation, and elsif is handled by the if node: + if !node.modifier_form? && (node.keyword == "if" || node.keyword == "unless") if location = node.location deltas[location.line] += 1 deltas[location.last_line] -= 1 diff --git a/releases.md b/releases.md index a1f20b4..6383193 100644 --- a/releases.md +++ b/releases.md @@ -1,5 +1,9 @@ # Releases +## Unreleased + + - Fixed `Layout/ConsistentBlankLineIndentation` to ignore modifier conditionals when calculating indentation depth. + ## v0.11.0 - Fixed `Layout/ConsistentBlankLineIndentation` to preserve semantic indentation depth for same-line nested structures, avoiding negative indentation levels when later closing lines are processed. diff --git a/test/rubocop/socketry/layout/consistent_blank_line_indentation.rb b/test/rubocop/socketry/layout/consistent_blank_line_indentation.rb index 581dd93..feb3702 100644 --- a/test/rubocop/socketry/layout/consistent_blank_line_indentation.rb +++ b/test/rubocop/socketry/layout/consistent_blank_line_indentation.rb @@ -842,4 +842,28 @@ def foo expect(offenses).to be(:empty?) end end + + with "a block guarded by a modifier conditional" do + let(:source) {"describe Foo do\n\tit \"works\" do\n\t\tfoo\n\tend\n\t\n\tit \"still works\" do\n\t\tbar\n\tend\nend if condition\n"} + + it "does not count the modifier conditional as an indentation level" do + processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION.to_f) + investigator = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true) + report = investigator.investigate(processed_source) + offenses = report.offenses + expect(offenses).to be(:empty?) + end + end + + with "a begin block guarded by a modifier loop" do + let(:source) {"begin\n\tfoo do\n\tend\n\t\n\tbar\nend while condition\n"} + + it "does not count the modifier loop as an indentation level" do + processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION.to_f) + investigator = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true) + report = investigator.investigate(processed_source) + offenses = report.offenses + expect(offenses).to be(:empty?) + end + end end From 061ea10dbe88caf99cd5bf2700b1230958370371 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 29 Jul 2026 21:40:59 +1200 Subject: [PATCH 2/2] Cover modifier post loops --- .../consistent_blank_line_indentation.rb | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/test/rubocop/socketry/layout/consistent_blank_line_indentation.rb b/test/rubocop/socketry/layout/consistent_blank_line_indentation.rb index feb3702..0433c19 100644 --- a/test/rubocop/socketry/layout/consistent_blank_line_indentation.rb +++ b/test/rubocop/socketry/layout/consistent_blank_line_indentation.rb @@ -855,15 +855,21 @@ def foo end end - with "a begin block guarded by a modifier loop" do - let(:source) {"begin\n\tfoo do\n\tend\n\t\n\tbar\nend while condition\n"} + with "begin blocks guarded by modifier loops" do + let(:sources) do + ["while", "until"].map do |keyword| + "begin\n\tfoo do\n\tend\n\t\n\tbar\nend #{keyword} condition\n" + end + end - it "does not count the modifier loop as an indentation level" do - processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION.to_f) - investigator = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true) - report = investigator.investigate(processed_source) - offenses = report.offenses - expect(offenses).to be(:empty?) + it "does not count modifier loops as indentation levels" do + sources.each do |source| + processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION.to_f) + investigator = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true) + report = investigator.investigate(processed_source) + offenses = report.offenses + expect(offenses).to be(:empty?) + end end end end