lib/rubocop/cop/style/ternary_parentheses.rb in rubocop-0.49.1 vs lib/rubocop/cop/style/ternary_parentheses.rb in rubocop-0.50.0

- old
+ new

@@ -52,18 +52,21 @@ class TernaryParentheses < Cop include SafeAssignment include ConfigurableEnforcedStyle include SurroundingSpace + VARIABLE_TYPES = AST::Node::VARIABLES + NON_COMPLEX_TYPES = [*VARIABLE_TYPES, :const, :defined?, :yield].freeze + MSG = '%s parentheses for ternary conditions.'.freeze MSG_COMPLEX = '%s parentheses for ternary expressions with' \ ' complex conditions.'.freeze def on_if(node) return unless node.ternary? && !infinite_loop? && offense?(node) - add_offense(node, node.source_range, message(node)) + add_offense(node, node.source_range) end private def offense?(node) @@ -100,23 +103,27 @@ # complex expressions within it. def complex_condition?(condition) if condition.begin_type? condition.to_a.any? { |x| complex_condition?(x) } else - non_complex_type?(condition) ? false : true + non_complex_expression?(condition) ? false : true end end # Anything that is not a variable, constant, or method/.method call # will be counted as a complex expression. - def non_complex_type?(condition) - condition.variable? || condition.const_type? || - (condition.send_type? && !operator?(condition.method_name)) || - condition.defined_type? || condition.yield_type? || - square_brackets?(condition) + def non_complex_expression?(condition) + NON_COMPLEX_TYPES.include?(condition.type) || + non_complex_send?(condition) end + def non_complex_send?(node) + return false unless node.send_type? + + !node.operator_method? || node.method?(:[]) + end + def message(node) if require_parentheses_when_complex? omit = parenthesized?(node.condition) ? 'Only use' : 'Use' format(MSG_COMPLEX, omit) else @@ -162,12 +169,9 @@ def_node_matcher :method_call_argument, <<-PATTERN {(:defined? $...) (send {_ nil} _ $(send nil _)...)} PATTERN - - def_node_matcher :square_brackets?, - '(send {(send _recv _msg) str array hash} :[] ...)' def correct_parenthesized(condition) lambda do |corrector| corrector.remove(condition.loc.begin) corrector.remove(condition.loc.end)