Skip to content

Commit 0938ae7

Browse files
committed
[#Fix 334] Make body consistent for RescueNode, EnsureNode and KeywordBeginNode.
1 parent 940b710 commit 0938ae7

File tree

5 files changed

+87
-16
lines changed

5 files changed

+87
-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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,56 @@
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+
describe '#branch' do
26+
let(:source) { 'begin; beginbody; ensure; >>ensurebody<<; end' }
27+
28+
it { is_expected.to eq(node) }
29+
end
30+
31+
context 'when the body is multiple lines' do
32+
let(:source) { 'begin; >>foo<<; bar; ensure; ensurebody; end' }
33+
34+
it 'returns a begin node' do
35+
expect(body).to be_begin_type
36+
expect(body.children).to include(node)
37+
end
38+
end
39+
40+
context 'with `rescue`' do
41+
context 'when there is no body' do
42+
let(:source) { 'begin; rescue; rescuebody; ensure; ensurebody; end' }
43+
44+
it { is_expected.to be_nil }
45+
end
46+
47+
context 'when the body is a single line' do
48+
let(:source) { 'begin; >>beginbody<<; rescue; rescuebody; ensure; ensurebody; end' }
49+
50+
it { is_expected.to eq(node) }
51+
end
52+
53+
context 'when the body is multiple lines' do
54+
let(:source) { 'begin; >>foo<<; bar; rescue; rescuebody; ensure; ensurebody; end' }
55+
56+
it 'returns a begin node' do
57+
expect(body).to be_begin_type
58+
expect(body.children).to include(node)
59+
end
60+
end
61+
end
62+
end
63+
1464
describe '#branch' do
1565
let(:source) { 'begin; beginbody; ensure; >>ensurebody<<; end' }
1666

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)