lib/rubocop/cop/style/ternary_parentheses.rb in rubocop-0.51.0 vs lib/rubocop/cop/style/ternary_parentheses.rb in rubocop-0.52.0

- old
+ new

@@ -6,48 +6,39 @@ # 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 + # @example EnforcedStyle: require_no_parentheses (default) + # # bad # foo = (bar?) ? a : b # foo = (bar.baz?) ? a : b # foo = (bar && baz) ? a : b # - # @good + # # good # foo = bar? ? a : b # foo = bar.baz? ? a : b # foo = bar && baz ? a : b # - # @example - # - # EnforcedStyle: require_parentheses - # - # @bad + # @example EnforcedStyle: require_parentheses + # # bad # foo = bar? ? a : b # foo = bar.baz? ? a : b # foo = bar && baz ? a : b # - # @good + # # good # foo = (bar?) ? a : b # foo = (bar.baz?) ? a : b # foo = (bar && baz) ? a : b # - # @example - # - # EnforcedStyle: require_parentheses_when_complex - # - # @bad + # @example EnforcedStyle: require_parentheses_when_complex + # # bad # foo = (bar?) ? a : b # foo = (bar.baz?) ? a : b # foo = bar && baz ? a : b # - # @good + # # good # foo = bar? ? a : b # foo = bar.baz? ? a : b # foo = (bar && baz) ? a : b class TernaryParentheses < Cop include SafeAssignment @@ -55,20 +46,34 @@ 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' \ + MSG = '%<command>s parentheses for ternary conditions.'.freeze + MSG_COMPLEX = '%<command>s parentheses for ternary expressions with' \ ' complex conditions.'.freeze def on_if(node) return unless node.ternary? && !infinite_loop? && offense?(node) add_offense(node, location: node.source_range) end + 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 + private def offense?(node) condition = node.condition @@ -83,24 +88,10 @@ require_parentheses? ? !parens : parens end end end - 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 - # If the condition is parenthesized we recurse and check for any # complex expressions within it. def complex_condition?(condition) if condition.begin_type? condition.to_a.any? { |x| complex_condition?(x) } @@ -122,15 +113,15 @@ !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) + command = parenthesized?(node.condition) ? 'Only use' : 'Use' + format(MSG_COMPLEX, command: command) else - verb = require_parentheses? ? 'Use' : 'Omit' - format(MSG, verb) + command = require_parentheses? ? 'Use' : 'Omit' + format(MSG, command: command) end end def require_parentheses? style == :require_parentheses @@ -193,11 +184,11 @@ end end def whitespace_after?(node) index = index_of_last_token(node) - last_token, next_token = processed_source.tokens[index, 2] - space_between?(last_token, next_token) + last_token = processed_source.tokens[index] + last_token.space_after? end end end end end