Skip to content

Commit 4bc884d

Browse files
Fix block delimiter spacing in do...end blocks.
1 parent a043022 commit 4bc884d

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

lib/rubocop/socketry/layout/block_delimiter_spacing.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,28 @@ def lambda_or_proc?(send_node)
8181

8282
# Check if the block is part of an expression (not a top-level statement)
8383
# Top-level statements are directly inside a :begin node (file/method body)
84-
# and should have space. Everything else (expressions, nested blocks) should not.
84+
# or inside do...end block bodies, and should have space.
85+
# Everything else (expressions, nested arguments, etc.) should not have space.
8586
def part_of_expression?(node)
8687
parent = node.parent
8788
return false unless parent
8889

8990
# If parent is a :begin node (sequence of statements), this is top-level
90-
# Otherwise, it's part of an expression or nested context
91-
parent.type != :begin
91+
return false if parent.type == :begin
92+
93+
# If parent is a :block node, check if it's a do...end block
94+
# do...end blocks contain statements (no space)
95+
# {...} blocks contain expressions (space required)
96+
if parent.type == :block
97+
# do...end blocks use keywords, {...} blocks use braces
98+
return false unless parent.braces?
99+
end
100+
101+
# Check if we're in a :kwbegin node (begin...end block body)
102+
return false if parent.type == :kwbegin
103+
104+
# Otherwise, it's part of an expression (assignment, argument, etc.)
105+
true
92106
end
93107

94108
# Check that there's no space before the opening brace for lambdas

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Fixed `Layout/BlockDelimiterSpacing` to correctly distinguish between statement and expression contexts for blocks inside `do...end` blocks.
6+
37
## v0.7.0
48

59
- Fixed `Layout/BlockDelimiterSpacing` to correctly handle blocks inside `do...end` blocks (statements should have space before braces).

test/rubocop/socketry/layout/block_delimiter_spacing.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,4 +425,42 @@
425425
expect(offenses.first.message).to be(:include?, "Remove space")
426426
end
427427
end
428+
429+
# Nested blocks inside do...end blocks
430+
with "a braces block inside a do...end block" do
431+
let(:source) do
432+
<<~RUBY
433+
foo do
434+
bar {baz}
435+
end
436+
RUBY
437+
end
438+
439+
it "does not register an offense for standalone method call inside do...end" do
440+
processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION.to_f)
441+
investigator = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true)
442+
report = investigator.investigate(processed_source)
443+
offenses = report.offenses
444+
expect(offenses).to be(:empty?)
445+
end
446+
end
447+
448+
with "a braces block inside a do...end block without space" do
449+
let(:source) do
450+
<<~RUBY
451+
foo do
452+
bar{baz}
453+
end
454+
RUBY
455+
end
456+
457+
it "registers an offense for missing space before brace" do
458+
processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION.to_f)
459+
investigator = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true)
460+
report = investigator.investigate(processed_source)
461+
offenses = report.offenses
462+
expect(offenses).not.to be(:empty?)
463+
expect(offenses.first.message).to be(:include?, "Add a space")
464+
end
465+
end
428466
end

0 commit comments

Comments
 (0)