From cb28d03f3c3501e3c9fbfb8a48e1120daf01be4b Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Mon, 8 Mar 2021 14:42:34 -0500 Subject: [PATCH] Simplify. Check is included in `adjacent_def_modifier?` --- ..._have_sendnodedef_modifier_return_the_def.md | 1 + .../ast/node/mixin/method_dispatch_node.rb | 17 ++++++++++++----- spec/rubocop/ast/send_node_spec.rb | 12 +++++++++--- 3 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 changelog/change_have_sendnodedef_modifier_return_the_def.md diff --git a/changelog/change_have_sendnodedef_modifier_return_the_def.md b/changelog/change_have_sendnodedef_modifier_return_the_def.md new file mode 100644 index 000000000..dc095bde4 --- /dev/null +++ b/changelog/change_have_sendnodedef_modifier_return_the_def.md @@ -0,0 +1 @@ +* [#x](https://github.com/rubocop-hq/rubocop-ast/pull/x): Have `SendNode#def_modifier?` return the `def` node it modifies, or `nil` instead of boolean. ([@marcandre][]) diff --git a/lib/rubocop/ast/node/mixin/method_dispatch_node.rb b/lib/rubocop/ast/node/mixin/method_dispatch_node.rb index 9a29d0d91..c232c6a79 100644 --- a/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +++ b/lib/rubocop/ast/node/mixin/method_dispatch_node.rb @@ -165,16 +165,23 @@ def arithmetic_operation? ARITHMETIC_OPERATORS.include?(method_name) end - # Checks if this node is part of a chain of `def` modifiers. + # @!method def_modifier?(node = self) + # Checks if this node is part of a chain of `def` or `defs` modifiers. # # @example # # private def foo; end # - # @return [Boolean] whether the dispatched method is a `def` modifier - def def_modifier? - send_type? && - adjacent_def_modifier? || each_child_node(:send).any?(&:def_modifier?) + # @return [Node | nil] returns the `def|defs` node this is a modifier for, + # or `nil` if it isn't a def modifier + def def_modifier?(node = self) + arg = node.children[2] + + return unless node.send_type? && node.receiver.nil? && arg.is_a?(::AST::Node) + + return arg if arg.def_type? || arg.defs_type? + + def_modifier?(arg) end # Checks whether this is a lambda. Some versions of parser parses diff --git a/spec/rubocop/ast/send_node_spec.rb b/spec/rubocop/ast/send_node_spec.rb index c8076e066..03e7de5c7 100644 --- a/spec/rubocop/ast/send_node_spec.rb +++ b/spec/rubocop/ast/send_node_spec.rb @@ -1106,19 +1106,25 @@ def bar context 'with a prefixed def modifier' do let(:source) { 'foo def bar; end' } - it { expect(send_node).to be_def_modifier } + it { expect(send_node.def_modifier?.method_name).to eq(:bar) } end context 'with several prefixed def modifiers' do let(:source) { 'foo bar baz def qux; end' } - it { expect(send_node).to be_def_modifier } + it { expect(send_node.def_modifier?.method_name).to eq(:qux) } end context 'with a block containing a method definition' do let(:source) { 'foo bar { baz def qux; end }' } - it { expect(send_node).not_to be_def_modifier } + it { expect(send_node.def_modifier?).to be_nil } + end + + context 'with call with no argument' do + let(:source) { 'foo' } + + it { expect(send_node.def_modifier?).to be_nil } end end