Skip to content

Commit c045d3d

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

File tree

5 files changed

+84
-25
lines changed

5 files changed

+84
-25
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: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,13 @@ class EnsureNode < Node
99
DEPRECATION_WARNING_LOCATION_CACHE = [] # rubocop:disable Style/MutableConstant
1010
private_constant :DEPRECATION_WARNING_LOCATION_CACHE
1111

12-
# Returns the body of the `ensure` clause.
12+
# Returns the body of the `ensure` node.
1313
#
14-
# @return [Node, nil] The body of the `ensure`.
15-
# @deprecated Use `EnsureNode#branch`
14+
# @return [Node, nil] The body of the `ensure` node.
1615
def body
17-
first_caller = caller(1..1).first
16+
return rescue_node.body if rescue_node
1817

19-
unless DEPRECATION_WARNING_LOCATION_CACHE.include?(first_caller)
20-
warn '`EnsureNode#body` is deprecated and will be changed in the next major version of ' \
21-
'rubocop-ast. Use `EnsureNode#branch` instead to get the body of the `ensure` branch.'
22-
warn "Called from:\n#{caller.join("\n")}\n\n"
23-
24-
DEPRECATION_WARNING_LOCATION_CACHE << first_caller
25-
end
26-
27-
branch
18+
node_parts[0]
2819
end
2920

3021
# Returns an the ensure branch in the exception handling statement.
@@ -38,7 +29,7 @@ def branch
3829
#
3930
# @return [Node, nil] The `rescue` node.
4031
def rescue_node
41-
node_parts[0] if node_parts[0].rescue_type?
32+
node_parts[0] if node_parts[0]&.rescue_type?
4233
end
4334

4435
# 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)