diff --git a/CHANGELOG.md b/CHANGELOG.md index 66822baa3..d6c5ceaa4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][]) @@ -32,3 +33,4 @@ [@marcandre]: https://github.com/marcandre [@owst]: https://github.com/owst +[@fatkodima]: https://github.com/fatkodima diff --git a/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb b/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb index 7ddca670a..64be7ebbb 100644 --- a/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +++ b/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb @@ -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 @@ -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 diff --git a/spec/rubocop/ast/send_node_spec.rb b/spec/rubocop/ast/send_node_spec.rb index d597a8113..bc1458773 100644 --- a/spec/rubocop/ast/send_node_spec.rb +++ b/spec/rubocop/ast/send_node_spec.rb @@ -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' }