Skip to content

Commit a3d5b4a

Browse files
committed
[#Fix 334] Make body consistent for RescueNode, EnsureNode and KeywordBeginNode.
1 parent dc102aa commit a3d5b4a

File tree

5 files changed

+85
-16
lines changed

5 files changed

+85
-16
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#335](https://github.com/rubocop/rubocop-ast/issues/334): **(Breaking change)** [#Fix 334] Make `body` consistent for `RescueNode`, `EnsureNode` and `KeywordBeginNode`. ([@dvandersluis][])

lib/rubocop/ast/node/ensure_node.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ module AST
66
# node when the builder constructs the AST, making its methods available
77
# to all `ensure` nodes within RuboCop.
88
class EnsureNode < Node
9-
# Returns the body of the `ensure` clause.
9+
# Returns the body of the `ensure` node.
1010
#
11-
# @return [Node, nil] The body of the `ensure`.
12-
# @deprecated Use `EnsureNode#branch`
11+
# @return [Node, nil] The body of the `ensure` node.
1312
def body
14-
branch
13+
return rescue_node.body if rescue_node
14+
15+
node_parts[0]
1516
end
1617

1718
# Returns an the ensure branch in the exception handling statement.
@@ -25,7 +26,7 @@ def branch
2526
#
2627
# @return [Node, nil] The `rescue` node.
2728
def rescue_node
28-
node_parts[0] if node_parts[0].rescue_type?
29+
node_parts[0] if node_parts[0]&.rescue_type?
2930
end
3031

3132
# Checks whether this node body is a void context.

lib/rubocop/ast/node/keyword_begin_node.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def body
1616
if rescue_node
1717
rescue_node.body
1818
elsif ensure_node
19-
ensure_node.node_parts[0]
19+
ensure_node.body
2020
elsif node_parts.one?
2121
node_parts[0]
2222
else

spec/rubocop/ast/ensure_node_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,54 @@
1111
it { expect(ensure_node).to be_a(described_class) }
1212
end
1313

14+
describe '#body' do
15+
subject(:body) { ensure_node.body }
16+
17+
context 'when there is no body' do
18+
let(:source) { 'begin; ensure; ensurebody; end' }
19+
20+
it { is_expected.to be_nil }
21+
end
22+
23+
context 'when the body is a single line' do
24+
let(:source) { 'begin; >>beginbody<<; ensure; ensurebody; end' }
25+
26+
it { is_expected.to eq(node) }
27+
end
28+
29+
context 'when the body is multiple lines' do
30+
let(:source) { 'begin; >>foo<<; bar; ensure; ensurebody; end' }
31+
32+
it 'returns a begin node' do
33+
expect(body).to be_begin_type
34+
expect(body.children).to include(node)
35+
end
36+
end
37+
38+
context 'with `rescue`' do
39+
context 'when there is no body' do
40+
let(:source) { 'begin; rescue; rescuebody; ensure; ensurebody; end' }
41+
42+
it { is_expected.to be_nil }
43+
end
44+
45+
context 'when the body is a single line' do
46+
let(:source) { 'begin; >>beginbody<<; rescue; rescuebody; ensure; ensurebody; end' }
47+
48+
it { is_expected.to eq(node) }
49+
end
50+
51+
context 'when the body is multiple lines' do
52+
let(:source) { 'begin; >>foo<<; bar; rescue; rescuebody; ensure; ensurebody; end' }
53+
54+
it 'returns a begin node' do
55+
expect(body).to be_begin_type
56+
expect(body.children).to include(node)
57+
end
58+
end
59+
end
60+
end
61+
1462
describe '#branch' do
1563
let(:source) { 'begin; beginbody; ensure; >>ensurebody<<; end' }
1664

spec/rubocop/ast/rescue_node_spec.rb

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::AST::RescueNode do
4-
subject(:ast) { parse_source(source).ast }
4+
subject(:ast) { parsed_source.ast }
55

6+
let(:parsed_source) { parse_source(source) }
7+
let(:node) { parsed_source.node }
68
let(:rescue_node) { ast.children.first }
79

810
describe '.new' do
@@ -16,26 +18,43 @@
1618
end
1719

1820
describe '#body' do
19-
let(:source) { <<~RUBY }
20-
begin
21-
foo
22-
rescue => e
23-
end
24-
RUBY
21+
subject(:body) { rescue_node.body }
2522

26-
it { expect(rescue_node.body).to be_send_type }
23+
context 'when the body is empty' do
24+
let(:source) { <<~RUBY }
25+
begin
26+
rescue => e
27+
end
28+
RUBY
29+
30+
it { is_expected.to be_nil }
31+
end
32+
33+
context 'when the body is a single line' do
34+
let(:source) { <<~RUBY }
35+
begin
36+
>>foo<<
37+
rescue => e
38+
end
39+
RUBY
40+
41+
it { is_expected.to eq(node) }
42+
end
2743

2844
context 'with multiple lines in body' do
2945
let(:source) { <<~RUBY }
3046
begin
31-
foo
47+
>>foo<<
3248
bar
3349
rescue => e
3450
baz
3551
end
3652
RUBY
3753

38-
it { expect(rescue_node.body).to be_begin_type }
54+
it 'returns a begin node' do
55+
expect(body).to be_begin_type
56+
expect(body.children).to include(node)
57+
end
3958
end
4059
end
4160

0 commit comments

Comments
 (0)