class RuboCop::Cop::Style::TernaryParentheses

This cop checks for the presence of parentheses around ternary conditions. It is configurable to enforce inclusion or omission of parentheses using `EnforcedStyle`. Omission is only enforced when removing the parentheses won't cause a different behavior.

@example EnforcedStyle: require_no_parentheses (default)

# bad
foo = (bar?) ? a : b
foo = (bar.baz?) ? a : b
foo = (bar && baz) ? a : b

# good
foo = bar? ? a : b
foo = bar.baz? ? a : b
foo = bar && baz ? a : b

@example EnforcedStyle: require_parentheses

# bad
foo = bar? ? a : b
foo = bar.baz? ? a : b
foo = bar && baz ? a : b

# good
foo = (bar?) ? a : b
foo = (bar.baz?) ? a : b
foo = (bar && baz) ? a : b

@example EnforcedStyle: require_parentheses_when_complex

# bad
foo = (bar?) ? a : b
foo = (bar.baz?) ? a : b
foo = bar && baz ? a : b

# good
foo = bar? ? a : b
foo = bar.baz? ? a : b
foo = (bar && baz) ? a : b

Constants

MSG
MSG_COMPLEX
NON_COMPLEX_TYPES
VARIABLE_TYPES

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/ternary_parentheses.rb, line 61
def autocorrect(node)
  condition = node.condition

  return nil if parenthesized?(condition) &&
                (safe_assignment?(condition) ||
                unsafe_autocorrect?(condition))

  if parenthesized?(condition)
    correct_parenthesized(condition)
  else
    correct_unparenthesized(condition)
  end
end
on_if(node) click to toggle source
# File lib/rubocop/cop/style/ternary_parentheses.rb, line 55
def on_if(node)
  return unless node.ternary? && !infinite_loop? && offense?(node)

  add_offense(node, location: node.source_range)
end

Private Instance Methods

below_ternary_precedence?(child) click to toggle source
# File lib/rubocop/cop/style/ternary_parentheses.rb, line 161
def below_ternary_precedence?(child)
  # Handle English "or", e.g. 'foo or bar ? a : b'
  (child.or_type? && child.semantic_operator?) ||
    # Handle English "and", e.g. 'foo and bar ? a : b'
    (child.and_type? && child.semantic_operator?) ||
    # Handle English "not", e.g. 'not foo ? a : b'
    (child.send_type? && child.prefix_not?)
end
complex_condition?(condition) click to toggle source

If the condition is parenthesized we recurse and check for any complex expressions within it.

# File lib/rubocop/cop/style/ternary_parentheses.rb, line 95
def complex_condition?(condition)
  if condition.begin_type?
    condition.to_a.any? { |x| complex_condition?(x) }
  else
    non_complex_expression?(condition) ? false : true
  end
end
correct_parenthesized(condition) click to toggle source
# File lib/rubocop/cop/style/ternary_parentheses.rb, line 175
def correct_parenthesized(condition)
  lambda do |corrector|
    corrector.remove(condition.loc.begin)
    corrector.remove(condition.loc.end)

    # Ruby allows no space between the question mark and parentheses.
    # If we remove the parentheses, we need to add a space or we'll
    # generate invalid code.
    unless whitespace_after?(condition)
      corrector.insert_after(condition.loc.end, ' ')
    end
  end
end
correct_unparenthesized(condition) click to toggle source
# File lib/rubocop/cop/style/ternary_parentheses.rb, line 189
def correct_unparenthesized(condition)
  lambda do |corrector|
    corrector.insert_before(condition.source_range, '(')
    corrector.insert_after(condition.source_range, ')')
  end
end
infinite_loop?() click to toggle source

When this cop is configured to enforce parentheses and the `RedundantParentheses` cop is enabled, it will cause an infinite loop as they compete to add and remove the parentheses respectively.

# File lib/rubocop/cop/style/ternary_parentheses.rb, line 145
def infinite_loop?
  require_parentheses? &&
    redundant_parentheses_enabled?
end
message(node) click to toggle source
# File lib/rubocop/cop/style/ternary_parentheses.rb, line 116
def message(node)
  if require_parentheses_when_complex?
    command = parenthesized?(node.condition) ? 'Only use' : 'Use'
    format(MSG_COMPLEX, command: command)
  else
    command = require_parentheses? ? 'Use' : 'Omit'
    format(MSG, command: command)
  end
end
non_complex_expression?(condition) click to toggle source

Anything that is not a variable, constant, or method/.method call will be counted as a complex expression.

# File lib/rubocop/cop/style/ternary_parentheses.rb, line 105
def non_complex_expression?(condition)
  NON_COMPLEX_TYPES.include?(condition.type) ||
    non_complex_send?(condition)
end
non_complex_send?(node) click to toggle source
# File lib/rubocop/cop/style/ternary_parentheses.rb, line 110
def non_complex_send?(node)
  return false unless node.call_type?

  !node.operator_method? || node.method?(:[])
end
offense?(node) click to toggle source
# File lib/rubocop/cop/style/ternary_parentheses.rb, line 77
def offense?(node)
  condition = node.condition

  if safe_assignment?(condition)
    !safe_assignment_allowed?
  else
    parens = parenthesized?(condition)
    case style
    when :require_parentheses_when_complex
      complex_condition?(condition) ? !parens : parens
    else
      require_parentheses? ? !parens : parens
    end
  end
end
parenthesized?(node) click to toggle source
# File lib/rubocop/cop/style/ternary_parentheses.rb, line 138
def parenthesized?(node)
  node.begin_type?
end
redundant_parentheses_enabled?() click to toggle source
# File lib/rubocop/cop/style/ternary_parentheses.rb, line 134
def redundant_parentheses_enabled?
  @config.for_cop('Style/RedundantParentheses').fetch('Enabled')
end
require_parentheses?() click to toggle source
# File lib/rubocop/cop/style/ternary_parentheses.rb, line 126
def require_parentheses?
  style == :require_parentheses
end
require_parentheses_when_complex?() click to toggle source
# File lib/rubocop/cop/style/ternary_parentheses.rb, line 130
def require_parentheses_when_complex?
  style == :require_parentheses_when_complex
end
unparenthesized_method_call?(child) click to toggle source
# File lib/rubocop/cop/style/ternary_parentheses.rb, line 157
def unparenthesized_method_call?(child)
  method_name(child) =~ /^[a-z]/i && !child.parenthesized?
end
unsafe_autocorrect?(condition) click to toggle source
# File lib/rubocop/cop/style/ternary_parentheses.rb, line 150
def unsafe_autocorrect?(condition)
  condition.children.any? do |child|
    unparenthesized_method_call?(child) ||
      below_ternary_precedence?(child)
  end
end
whitespace_after?(node) click to toggle source
# File lib/rubocop/cop/style/ternary_parentheses.rb, line 196
def whitespace_after?(node)
  index = index_of_last_token(node)
  last_token = processed_source.tokens[index]
  last_token.space_after?
end