Sha256: 120b016f1bf75f0f4b7657d9a1b10767af0dae3055dbad7512eb3ede6a27ccb6
Contents?: true
Size: 1.52 KB
Versions: 2
Compression:
Stored size: 1.52 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 if cond.type == :begin return if parens_required?(node) # allow safe assignment return if safe_assignment?(cond) && safe_assignment_allowed? add_offense(cond, :expression, message(node)) end 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) @corrections << lambda do |corrector| corrector.remove(node.loc.begin) corrector.remove(node.loc.end) end end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rubocop-0.19.1 | lib/rubocop/cop/style/parentheses_around_condition.rb |
rubocop-0.19.0 | lib/rubocop/cop/style/parentheses_around_condition.rb |