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..0433c19 100644 --- a/test/rubocop/socketry/layout/consistent_blank_line_indentation.rb +++ b/test/rubocop/socketry/layout/consistent_blank_line_indentation.rb @@ -842,4 +842,34 @@ 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 "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 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