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

Add enumerable_method? for MethodIdentifierPredicates #37

Merged
merged 1 commit into from Jun 14, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a small chance this might cause an issue due to a mismatch between Ruby version RuboCop is run with versus the version of the code inspected, but I think it's a small enough concern that this is still okay.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. Might be best to list them explicitly.


# 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