lib/rubocop/cop/lint/ambiguous_operator.rb in rubocop-0.88.0 vs lib/rubocop/cop/lint/ambiguous_operator.rb in rubocop-0.89.0

- old
+ new

@@ -18,12 +18,12 @@ # # # good # # # With parentheses, there's no ambiguity. # do_something(*some_array) - class AmbiguousOperator < Cop - include ParserDiagnostic + class AmbiguousOperator < Base + extend AutoCorrector AMBIGUITIES = { '+' => { actual: 'positive number', possible: 'addition' }, '-' => { actual: 'negative number', possible: 'subtraction' }, '*' => { actual: 'splat', possible: 'multiplication' }, @@ -36,22 +36,27 @@ MSG_FORMAT = 'Ambiguous %<actual>s operator. Parenthesize the method ' \ "arguments if it's surely a %<actual>s operator, or add " \ 'a whitespace to the right of the `%<operator>s` if it ' \ 'should be a %<possible>s.' - def autocorrect(node) - lambda do |corrector| - add_parentheses(node, corrector) + def on_new_investigation + processed_source.diagnostics.each do |diagnostic| + next unless diagnostic.reason == :ambiguous_prefix + + offense_node = find_offense_node_by(diagnostic) + message = message(diagnostic) + + add_offense( + diagnostic.location, message: message, severity: diagnostic.level + ) do |corrector| + add_parentheses(offense_node, corrector) + end end end private - def relevant_diagnostic?(diagnostic) - diagnostic.reason == :ambiguous_prefix - end - def find_offense_node_by(diagnostic) ast = processed_source.ast ast.each_node(:splat, :block_pass, :kwsplat) do |node| next unless offense_position?(node, diagnostic) @@ -66,10 +71,10 @@ offense_position?(first_argument, diagnostic) && unary_operator?(first_argument, diagnostic) end end - def alternative_message(diagnostic) + def message(diagnostic) operator = diagnostic.location.source hash = AMBIGUITIES[operator] format(MSG_FORMAT, hash) end