Skip to content

Commit

Permalink
Deprecate EnsureNode#body in favour of EnsureNode#branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvandersluis authored and marcandre committed Nov 13, 2024
1 parent bab7f45 commit ec4cfc0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/change_deprecate_ensurenodebody_in_favour_of.md
Original file line number Diff line number Diff line change
@@ -0,0 +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][])
21 changes: 21 additions & 0 deletions lib/rubocop/ast/node/ensure_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,31 @@ module AST
# node when the builder constructs the AST, making its methods available
# to all `ensure` nodes within RuboCop.
class EnsureNode < Node
DEPRECATION_WARNING_LOCATION_CACHE = [] # rubocop:disable Style/MutableConstant
private_constant :DEPRECATION_WARNING_LOCATION_CACHE

# Returns the body of the `ensure` clause.
#
# @return [Node, nil] The body of the `ensure`.
# @deprecated Use `EnsureNode#branch`
def body
first_caller = caller(1..1).first

unless DEPRECATION_WARNING_LOCATION_CACHE.include?(first_caller)
warn '`EnsureNode#body` is deprecated and will be changed in the next major version of ' \
'rubocop-ast. Use `EnsureNode#branch` instead to get the body of the `ensure` branch.'
warn "Called from:\n#{caller.join("\n")}\n\n"

DEPRECATION_WARNING_LOCATION_CACHE << first_caller
end

branch
end

# Returns an the ensure branch in the exception handling statement.
#
# @return [Node, nil] the body of the ensure branch.
def branch
node_parts[1]
end

Expand Down
10 changes: 6 additions & 4 deletions spec/rubocop/ast/ensure_node_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# frozen_string_literal: true

RSpec.describe RuboCop::AST::EnsureNode do
let(:ensure_node) { parse_source(source).ast.children.first }
let(:parsed_source) { parse_source(source) }
let(:ensure_node) { parsed_source.ast.children.first }
let(:node) { parsed_source.node }

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

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

describe '#body' do
let(:source) { 'begin; beginbody; ensure; :ensurebody; end' }
describe '#branch' do
let(:source) { 'begin; beginbody; ensure; >>ensurebody<<; end' }

it { expect(ensure_node.body).to be_sym_type }
it { expect(ensure_node.branch).to eq(node) }
end

describe '#rescue_node' do
Expand Down

0 comments on commit ec4cfc0

Please sign in to comment.