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

Introduce RuboCop::Ast::MethodDispatchNode#selector #267

Merged
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
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