Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate EnsureNode#body in favour of EnsureNode#branch #337

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +15 to +28
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is overkill?

I thought it'd be useful to output a warning given that the method will no longer work the same post #336 release. At the same time, I am going to update all the @rubocop org repos to use the new method name, so the risk is only to 3rd party extensions that might use EnsureNode#body.

I also wanted it to not spam deprecation warnings, hence the cache to make sure each warning is just output once.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Earlopain it looks like StrictWarnings is causing the rubocop master specs to fail because of the warning 🤦. Any ideas?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this is awkward. I didn't anticipate this interaction.

Currently there is nothing to disable this but it is easy enough to control through environment variable. It already does nothing if CI is not set but I don't think that would be appropriate here. I can make a PR tomorrow.

Rails sets STRICT_WARNINGS=1 in their CI instead of relying on something generic, once again I should just have followed what they do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is overkill?

I don't think so, I like it.


# 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