Skip to content

Commit

Permalink
Have SendNode#def_modifier? return the def node it modifies, or `…
Browse files Browse the repository at this point in the history
…nil` instead of boolean

Stricter check (used to check any child, including the receiver of the `send`)
  • Loading branch information
marcandre committed Mar 9, 2021
1 parent bf6dbec commit 9d62f6c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
@@ -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][])
14 changes: 10 additions & 4 deletions lib/rubocop/ast/node/mixin/method_dispatch_node.rb
Expand Up @@ -165,15 +165,21 @@ 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?
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)
if node.send_type? && node.receiver.nil?
arg = node.children[2]
return arg if arg.def_type? || arg.defs_type?
def_modifier?(arg)
end
end

# Checks whether this is a lambda. Some versions of parser parses
Expand Down
6 changes: 3 additions & 3 deletions spec/rubocop/ast/send_node_spec.rb
Expand Up @@ -1106,19 +1106,19 @@ 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
end

Expand Down

0 comments on commit 9d62f6c

Please sign in to comment.