Sha256: 6e9ce32e1bb2785a3bd581d1ef89794abab5262ddf176695ddf84360e5f8a410

Contents?: true

Size: 1.9 KB

Versions: 9

Compression:

Stored size: 1.9 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # This cop checks for the presence of superfluous parentheses around the
      # condition of if/unless/while/until.
      class ParenthesesAroundCondition < Cop
        include IfNode
        include SafeAssignment

        def on_if(node)
          return if ternary_op?(node)
          process_control_op(node)
        end

        def on_while(node)
          process_control_op(node)
        end

        def on_until(node)
          process_control_op(node)
        end

        private

        def process_control_op(node)
          cond, _body = *node

          return unless cond.type == :begin
          # handle `if (something rescue something_else) ...`
          return if modifier_op?(cond.children.first)
          # check if there's any whitespace between the keyword and the cond
          return if parens_required?(node)
          # allow safe assignment
          return if safe_assignment?(cond) && safe_assignment_allowed?

          add_offense(cond, :expression, message(node))
        end

        def modifier_op?(node)
          return false if ternary_op?(node)
          return true if node.type == :rescue

          [:if, :while, :until].include?(node.type) &&
            node.loc.end.nil?
        end

        def parens_required?(node)
          exp = node.loc.expression
          kw = node.loc.keyword
          kw_offset = kw.begin_pos - exp.begin_pos

          exp.source[kw_offset..-1].start_with?(kw.source + '(')
        end

        def message(node)
          kw = node.loc.keyword.source
          article = kw == 'while' ? 'a' : 'an'
          "Don't use parentheses around the condition of #{article} `#{kw}`."
        end

        def autocorrect(node)
          lambda do |corrector|
            corrector.remove(node.loc.begin)
            corrector.remove(node.loc.end)
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/style/parentheses_around_condition.rb
rubocop-0.35.0 lib/rubocop/cop/style/parentheses_around_condition.rb
rubocop-0.34.2 lib/rubocop/cop/style/parentheses_around_condition.rb
rubocop-0.34.1 lib/rubocop/cop/style/parentheses_around_condition.rb
rubocop-0.34.0 lib/rubocop/cop/style/parentheses_around_condition.rb
rubocop-0.33.0 lib/rubocop/cop/style/parentheses_around_condition.rb
rubocop-0.32.1 lib/rubocop/cop/style/parentheses_around_condition.rb
rubocop-0.32.0 lib/rubocop/cop/style/parentheses_around_condition.rb
rubocop-0.31.0 lib/rubocop/cop/style/parentheses_around_condition.rb