Module: RuboCop::AST::MethodIdentifierPredicates

Included in:
DefNode, MethodDispatchNode
Defined in:
lib/rubocop/ast/node/mixin/method_identifier_predicates.rb

Overview

Note:

this mixin expects ‘#method_name` and `#receiver` to be implemented

Common predicates for nodes that reference method identifiers: ‘send`, `csend`, `def`, `defs`, `super`, `zsuper`

Constant Summary collapse

ENUMERATOR_METHODS =
%i[collect collect_concat detect downto each
find find_all find_index inject loop map!
map reduce reject reject! reverse_each select
select! times upto].freeze

Instance Method Summary collapse

Instance Method Details

#assignment_method?Boolean

Checks whether the method is an assignment method.

Returns:

  • (Boolean)

    whether the method is an assignment



40
41
42
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 40

def assignment_method?
  !comparison_method? && method_name.to_s.end_with?('=')
end

#bang_method?Boolean

Checks whether the method is a bang method.

Returns:

  • (Boolean)

    whether the method is a bang method



62
63
64
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 62

def bang_method?
  method_name.to_s.end_with?('!')
end

#camel_case_method?Boolean

Checks whether the method is a camel case method, e.g. ‘Integer()`.

Returns:

  • (Boolean)

    whether the method is a camel case method



70
71
72
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 70

def camel_case_method?
  method_name.to_s =~ /\A[A-Z]/
end

#comparison_method?Boolean

Checks whether the method is a comparison method.

Returns:

  • (Boolean)

    whether the method is a comparison



33
34
35
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 33

def comparison_method?
  Node::COMPARISON_OPERATORS.include?(method_name)
end

#const_receiver?Boolean

Checks whether the explicit receiver of node is a ‘const` node.

Returns:

  • (Boolean)

    whether the receiver of this node is a ‘const` node



84
85
86
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 84

def const_receiver?
  receiver && receiver.const_type?
end

#enumerator_method?Boolean

Checks whether the method is an enumerator method.

Returns:

  • (Boolean)

    whether the method is an enumerator



47
48
49
50
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 47

def enumerator_method?
  ENUMERATOR_METHODS.include?(method_name) ||
    method_name.to_s.start_with?('each_')
end

#method?(name) ⇒ Boolean

Checks whether the method name matches the argument.

Parameters:

  • name (Symbol, String)

    the method name to check for

Returns:

  • (Boolean)

    whether the method name matches the argument



19
20
21
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 19

def method?(name)
  method_name == name.to_sym
end

#operator_method?Boolean

Checks whether the method is an operator method.

Returns:

  • (Boolean)

    whether the method is an operator



26
27
28
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 26

def operator_method?
  RuboCop::Cop::Util::OPERATOR_METHODS.include?(method_name)
end

#predicate_method?Boolean

Checks whether the method is a predicate method.

Returns:

  • (Boolean)

    whether the method is a predicate method



55
56
57
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 55

def predicate_method?
  method_name.to_s.end_with?('?')
end

#self_receiver?Boolean

Checks whether the explicit receiver of this node is ‘self`.

Returns:

  • (Boolean)

    whether the receiver of this node is ‘self`



77
78
79
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 77

def self_receiver?
  receiver && receiver.self_type?
end