Skip to content

Commit

Permalink
Introduce RuboCop::Ast::MethodDispatchNode#selector
Browse files Browse the repository at this point in the history
If you need to manipulate the call-site of a method dispatch, you have
to use node.loc.selector for regular methods and node.loc.keyword for
syntax nodes that behave like method invocations, but are not. Super and
yield nodes are an example of that.

This forces us to check if node.loc responds to #keyword in a few places
in rubocop proper and we can make it easier if we expose a method, here
I have called it #selector, to all method dispatch nodes and work with
it.
  • Loading branch information
gsamokovarov authored and marcandre committed Jun 1, 2023
1 parent 0677018 commit 53c69cd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master (unreleased)

* [#262](https://github.com/rubocop/rubocop-ast/pull/267): Introduce RuboCop::Ast::MethodDispatchNode#selector. ([@gsamokovarov][])

## 1.28.1 (2023-05-01)

### Bug fixes
Expand Down Expand Up @@ -416,4 +418,4 @@
[@gsamokovarov]: https://github.com/gsamokovarov
[@nobuyo]: https://github.com/nobuyo
[@tdeo]: https://github.com/tdeo
[@ydah]: https://github.com/ydah
[@ydah]: https://github.com/ydah
23 changes: 17 additions & 6 deletions lib/rubocop/ast/node/mixin/method_dispatch_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module AST
# Common functionality for nodes that are a kind of method dispatch:
# `send`, `csend`, `super`, `zsuper`, `yield`, `defined?`,
# and (modern only): `index`, `indexasgn`, `lambda`
module MethodDispatchNode
module MethodDispatchNode # rubocop:disable Metrics/ModuleLength
extend NodePattern::Macros
include MethodIdentifierPredicates

Expand All @@ -28,6 +28,17 @@ def method_name
node_parts[1]
end

# The source range for the method name or keyword that dispatches this call.
#
# @return [Parser::Source::Range] the source range for the method name or keyword
def selector
if loc.respond_to? :keyword
loc.keyword
else
loc.selector
end
end

# The `block` or `numblock` node associated with this method dispatch, if any.
#
# @return [BlockNode, nil] the `block` or `numblock` node associated with this method
Expand Down Expand Up @@ -147,7 +158,7 @@ def const_receiver?
#
# @return [Boolean] whether the method is the implicit form of `#call`
def implicit_call?
method?(:call) && !loc.selector
method?(:call) && !selector
end

# Whether this method dispatch has an explicit block.
Expand Down Expand Up @@ -222,9 +233,9 @@ def lambda_literal?
#
# @return [Boolean] whether this method is a unary operation
def unary_operation?
return false unless loc.selector
return false unless selector

operator_method? && loc.expression.begin_pos == loc.selector.begin_pos
operator_method? && loc.expression.begin_pos == selector.begin_pos
end

# Checks whether this is a binary operation.
Expand All @@ -235,9 +246,9 @@ def unary_operation?
#
# @return [Boolean] whether this method is a binary operation
def binary_operation?
return false unless loc.selector
return false unless selector

operator_method? && loc.expression.begin_pos != loc.selector.begin_pos
operator_method? && loc.expression.begin_pos != selector.begin_pos
end

private
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/ast/super_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
it { expect(super_node.method_name).to eq(:super) }
end

describe '#selector' do
let(:source) { 'super(foo)' }

it { expect(super_node.selector.source).to eq('super') }
end

describe '#method?' do
context 'when message matches' do
context 'when argument is a symbol' do
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/ast/yield_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
it { expect(yield_node.method_name).to eq(:yield) }
end

describe '#selector' do
let(:source) { 'yield :foo, :bar' }

it { expect(yield_node.selector.source).to eq('yield') }
end

describe '#method?' do
context 'when message matches' do
context 'when argument is a symbol' do
Expand Down

0 comments on commit 53c69cd

Please sign in to comment.