Sha256: ea6c23045d5b02ad2579f617b584104f398c2b64e19026206b68834e03e9b700

Contents?: true

Size: 1.75 KB

Versions: 24

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for expressions where there is a call to a predicate
      # method with at least one argument, where no parentheses are used around
      # the parameter list, and a boolean operator, && or ||, is used in the
      # last argument.
      #
      # The idea behind warning for these constructs is that the user might
      # be under the impression that the return value from the method call is
      # an operand of &&/||.
      #
      # @example
      #
      #   # bad
      #
      #   if day.is? :tuesday && month == :jan
      #     # ...
      #   end
      #
      # @example
      #
      #   # good
      #
      #   if day.is?(:tuesday) && month == :jan
      #     # ...
      #   end
      class RequireParentheses < Cop
        include RangeHelp

        MSG = 'Use parentheses in the method call to avoid confusion about ' \
              'precedence.'.freeze

        def on_send(node)
          return if !node.arguments? || node.parenthesized?

          if node.first_argument.if_type? && node.first_argument.ternary?
            check_ternary(node.first_argument, node)
          elsif node.predicate_method?
            check_predicate(node.last_argument, node)
          end
        end

        private

        def check_ternary(ternary, node)
          return unless ternary.condition.operator_keyword?

          range = range_between(node.source_range.begin_pos,
                                ternary.condition.source_range.end_pos)

          add_offense(range, location: range)
        end

        def check_predicate(predicate, node)
          return unless predicate.operator_keyword?

          add_offense(node)
        end
      end
    end
  end
end

Version data entries

24 entries across 22 versions & 3 rubygems

Version Path
rubocop-0.63.1 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-0.63.0 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-0.62.0 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-0.61.1 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-0.61.0 lib/rubocop/cop/lint/require_parentheses.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/require_parentheses.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/require_parentheses.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/require_parentheses.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/require_parentheses.rb
rubocop-0.60.0 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-0.59.2 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-0.59.1 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-0.59.0 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-0.58.2 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-0.58.1 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-0.58.0 lib/rubocop/cop/lint/require_parentheses.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/lint/require_parentheses.rb
rubocop-0.57.2 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-0.57.1 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-0.57.0 lib/rubocop/cop/lint/require_parentheses.rb