Skip to content

Commit ec4cfc0

Browse files
dvandersluismarcandre
authored andcommitted
Deprecate EnsureNode#body in favour of EnsureNode#branch.
1 parent bab7f45 commit ec4cfc0

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#337](https://github.com/rubocop/rubocop-ast/pull/337): Deprecate `EnsureNode#body` in favour of `EnsureNode#branch`. `EnsureNode#body` will be redefined in the next major version of rubocop-ast. ([@dvandersluis][])

lib/rubocop/ast/node/ensure_node.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,31 @@ 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+
DEPRECATION_WARNING_LOCATION_CACHE = [] # rubocop:disable Style/MutableConstant
10+
private_constant :DEPRECATION_WARNING_LOCATION_CACHE
11+
912
# Returns the body of the `ensure` clause.
1013
#
1114
# @return [Node, nil] The body of the `ensure`.
15+
# @deprecated Use `EnsureNode#branch`
1216
def body
17+
first_caller = caller(1..1).first
18+
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
28+
end
29+
30+
# Returns an the ensure branch in the exception handling statement.
31+
#
32+
# @return [Node, nil] the body of the ensure branch.
33+
def branch
1334
node_parts[1]
1435
end
1536

spec/rubocop/ast/ensure_node_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::AST::EnsureNode do
4-
let(:ensure_node) { parse_source(source).ast.children.first }
4+
let(:parsed_source) { parse_source(source) }
5+
let(:ensure_node) { parsed_source.ast.children.first }
6+
let(:node) { parsed_source.node }
57

68
describe '.new' do
79
let(:source) { 'begin; beginbody; ensure; ensurebody; end' }
810

911
it { expect(ensure_node).to be_a(described_class) }
1012
end
1113

12-
describe '#body' do
13-
let(:source) { 'begin; beginbody; ensure; :ensurebody; end' }
14+
describe '#branch' do
15+
let(:source) { 'begin; beginbody; ensure; >>ensurebody<<; end' }
1416

15-
it { expect(ensure_node.body).to be_sym_type }
17+
it { expect(ensure_node.branch).to eq(node) }
1618
end
1719

1820
describe '#rescue_node' do

0 commit comments

Comments
 (0)