Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/rubocop/socketry/layout/consistent_blank_line_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def indentation(width)
def on_new_investigation
indentation_deltas = build_indentation_deltas
current_level = 0
semantic_level = 0

processed_source.lines.each_with_index do |line, index|
line_number = index + 1
Expand All @@ -75,7 +76,16 @@ def on_new_investigation
end
end

current_level += delta
if delta > 0
current_level += 1
semantic_level += delta
elsif delta < 0
semantic_level += delta
current_level = [current_level, semantic_level].min
end

current_level = [current_level, 0].max
semantic_level = [semantic_level, 0].max
end
end

Expand All @@ -87,10 +97,6 @@ def on_new_investigation
def build_indentation_deltas
Hash.new(0).tap do |deltas|
walk_ast_for_indentation(processed_source.ast, deltas)

# Cap deltas to a maximum of +1 or -1 per line to handle cases where
# multiple structures open/close on the same line (e.g., `[..., {`)
deltas.transform_values!{|delta| delta.nil? ? nil : delta.clamp(-1, 1)}
end
end

Expand Down
4 changes: 4 additions & 0 deletions releases.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Releases

## Unreleased

- Fixed `Layout/ConsistentBlankLineIndentation` to preserve semantic indentation depth for same-line nested structures, avoiding negative indentation levels when later closing lines are processed.

## v0.10.0

- Fixed `Layout/BlockDelimiterSpacing` to correctly treat a block as a statement (requiring space before `{`) when it is the sole body of a multi-line outer block or method/class/module definition. Previously, the absence of a `:begin` wrapper in the AST caused such blocks (e.g. `Async {foo}` or `let(:bar) {baz}` inside a `describe`/`context` block) to be misclassified as expression-context and have their space incorrectly removed.
Expand Down
12 changes: 12 additions & 0 deletions test/rubocop/socketry/layout/consistent_blank_line_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -830,4 +830,16 @@ def foo
expect(offenses.first.message).to be(:include?, message)
end
end

with "a top-level blank line after nested array and hash" do
let(:source) {"CONFIG = [[Foo, {\n\tbar: true\n}]\n]\n\nputs CONFIG\n"}

it "does not crash when clamped deltas would otherwise make indentation negative" 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
Loading