Sha256: a38ffcae04ef62fc4650a4cdfb62f7cf9b45fb5a63ea32731b2badc5790d56c7

Contents?: true

Size: 1.76 KB

Versions: 154

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # 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 < Base
        include RangeHelp

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

        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
        alias on_csend on_send

        private

        def check_ternary(ternary, node)
          if node.method?(:[]) || node.assignment_method? || !ternary.condition.operator_keyword?
            return
          end

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

          add_offense(range)
        end

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

          add_offense(node)
        end
      end
    end
  end
end

Version data entries

154 entries across 153 versions & 17 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/require_parentheses.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.65.0 lib/rubocop/cop/lint/require_parentheses.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.64.1 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.63.4 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.63.3 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.63.2 lib/rubocop/cop/lint/require_parentheses.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.63.1 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.63.0 lib/rubocop/cop/lint/require_parentheses.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.62.1 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.62.0 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.61.0 lib/rubocop/cop/lint/require_parentheses.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.60.2 lib/rubocop/cop/lint/require_parentheses.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/require_parentheses.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/require_parentheses.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/require_parentheses.rb