Skip to content

Commit

Permalink
Add enumerable_method? for MethodIdentifierPredicates
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima authored and marcandre committed Jun 14, 2020
1 parent 2a5908f commit 8b95869
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

### New features

* [#37](https://github.com/rubocop-hq/rubocop-ast/pull/37): Add `enumerable_method?` for `MethodIdentifierPredicates`. ([@fatkodima][])
* [#4](https://github.com/rubocop-hq/rubocop-ast/issues/4): Add `interpolation?` for `RegexpNode`. ([@tejasbubane][])
* [#20](https://github.com/rubocop-hq/rubocop-ast/pull/20): Add option predicates for `RegexpNode`. ([@owst][])
* [#11](https://github.com/rubocop-hq/rubocop-ast/issues/11): Add `argument_type?` method to make it easy to recognize argument nodes. ([@tejasbubane][])
Expand Down Expand Up @@ -32,3 +33,4 @@

[@marcandre]: https://github.com/marcandre
[@owst]: https://github.com/owst
[@fatkodima]: https://github.com/fatkodima
9 changes: 9 additions & 0 deletions lib/rubocop/ast/node/mixin/method_identifier_predicates.rb
Expand Up @@ -12,6 +12,8 @@ module MethodIdentifierPredicates
map reduce reject reject! reverse_each select
select! times upto].freeze

ENUMERABLE_METHODS = (Enumerable.instance_methods + [:each]).freeze

# http://phrogz.net/programmingruby/language.html#table_18.4
OPERATOR_METHODS = %i[| ^ & <=> == === =~ > >= < <= << >> + - * /
% ** ~ +@ -@ !@ ~@ [] []= ! != !~ `].freeze
Expand Down Expand Up @@ -53,6 +55,13 @@ def enumerator_method?
method_name.to_s.start_with?('each_')
end

# Checks whether the method is an Enumerable method.
#
# @return [Boolean] whether the method is an Enumerable method
def enumerable_method?
ENUMERABLE_METHODS.include?(method_name)
end

# Checks whether the method is a predicate method.
#
# @return [Boolean] whether the method is a predicate method
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/ast/send_node_spec.rb
Expand Up @@ -782,6 +782,21 @@ module Foo
end
end

describe '#enumerable_method?' do
context 'with an enumerable method' do
let(:send_node) { parse_source(source).ast.send_node }
let(:source) { 'foo.all? { |e| bar?(e) }' }

it { expect(send_node.enumerable_method?).to be_truthy }
end

context 'with a regular method' do
let(:source) { 'foo.bar(:baz)' }

it { expect(send_node.enumerable_method?).to be_falsey }
end
end

describe '#attribute_accessor?' do
context 'with an accessor' do
let(:source) { 'attr_reader :foo, bar, *baz' }
Expand Down

0 comments on commit 8b95869

Please sign in to comment.