diff --git a/CHANGELOG.md b/CHANGELOG.md index 425e5d131c8..6621b6cb810 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ * [#8276](https://github.com/rubocop-hq/rubocop/issues/8276): Cop `Metrics/CyclomaticComplexity` not longer counts `&.` when repeated on the same variable. ([@marcandre][]) * [#8204](https://github.com/rubocop-hq/rubocop/pull/8204): **(Breaking)** Cop `Metrics/PerceivedComplexity` now counts `else` in `case` statements, `&.`, `||=`, `&&=` and blocks known to iterate. Default bumped from 7 to 8. Consider using `rubocop -a --disable-uncorrectable` to ease transition. ([@marcandre][]) * [#8416](https://github.com/rubocop-hq/rubocop/issues/8416): Cop `Lint/InterpolationCheck` marked as unsafe. ([@marcandre][]) +* [#8442](https://github.com/rubocop-hq/rubocop/pull/8442): Remove `RuboCop::Cop::ParserDiagnostic` mixin module. ([@koic][]) ## 0.88.0 (2020-07-13) diff --git a/lib/rubocop.rb b/lib/rubocop.rb index eebfd2e36c1..aa05076f8f3 100644 --- a/lib/rubocop.rb +++ b/lib/rubocop.rb @@ -95,7 +95,6 @@ require_relative 'rubocop/cop/mixin/on_normal_if_unless' require_relative 'rubocop/cop/mixin/ordered_gem_node' require_relative 'rubocop/cop/mixin/parentheses' -require_relative 'rubocop/cop/mixin/parser_diagnostic' require_relative 'rubocop/cop/mixin/percent_array' require_relative 'rubocop/cop/mixin/percent_literal' require_relative 'rubocop/cop/mixin/preceding_following_alignment' diff --git a/lib/rubocop/cop/lint/ambiguous_operator.rb b/lib/rubocop/cop/lint/ambiguous_operator.rb index 50e2c55b112..2df1285a861 100644 --- a/lib/rubocop/cop/lint/ambiguous_operator.rb +++ b/lib/rubocop/cop/lint/ambiguous_operator.rb @@ -21,7 +21,6 @@ module Lint # # With parentheses, there's no ambiguity. # do_something(*some_array) class AmbiguousOperator < Base - include ParserDiagnostic extend AutoCorrector AMBIGUITIES = { @@ -39,16 +38,23 @@ class AmbiguousOperator < Base 'a whitespace to the right of the `%s` if it ' \ 'should be a %s.' - private + def on_new_investigation + processed_source.diagnostics.each do |diagnostic| + next unless diagnostic.reason == :ambiguous_prefix - def autocorrect(corrector, node) - add_parentheses(node, corrector) - end + offense_node = find_offense_node_by(diagnostic) + message = message(diagnostic) - def relevant_diagnostic?(diagnostic) - diagnostic.reason == :ambiguous_prefix + add_offense( + diagnostic.location, message: message, severity: diagnostic.level + ) do |corrector| + add_parentheses(offense_node, corrector) + end + end end + private + def find_offense_node_by(diagnostic) ast = processed_source.ast ast.each_node(:splat, :block_pass, :kwsplat) do |node| @@ -67,7 +73,7 @@ def find_offense_node_by(diagnostic) end end - def alternative_message(diagnostic) + def message(diagnostic) operator = diagnostic.location.source hash = AMBIGUITIES[operator] format(MSG_FORMAT, hash) diff --git a/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb b/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb index e9786768455..87a70ff284a 100644 --- a/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +++ b/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb @@ -22,23 +22,26 @@ module Lint # # With parentheses, there's no ambiguity. # do_something(/pattern/i) class AmbiguousRegexpLiteral < Base - include ParserDiagnostic extend AutoCorrector MSG = 'Ambiguous regexp literal. Parenthesize the method arguments ' \ "if it's surely a regexp literal, or add a whitespace to the " \ 'right of the `/` if it should be a division.' - private + def on_new_investigation + processed_source.diagnostics.each do |diagnostic| + next unless diagnostic.reason == :ambiguous_literal - def autocorrect(corrector, node) - add_parentheses(node, corrector) - end + offense_node = find_offense_node_by(diagnostic) - def relevant_diagnostic?(diagnostic) - diagnostic.reason == :ambiguous_literal + add_offense(diagnostic.location, severity: diagnostic.level) do |corrector| + add_parentheses(offense_node, corrector) + end + end end + private + def find_offense_node_by(diagnostic) node = processed_source.ast.each_node(:regexp).find do |regexp_node| regexp_node.source_range.begin_pos == diagnostic.location.begin_pos @@ -46,10 +49,6 @@ def find_offense_node_by(diagnostic) node.parent end - - def alternative_message(_diagnostic) - MSG - end end end end diff --git a/lib/rubocop/cop/lint/useless_else_without_rescue.rb b/lib/rubocop/cop/lint/useless_else_without_rescue.rb index 068bd5bad26..222b351ede9 100644 --- a/lib/rubocop/cop/lint/useless_else_without_rescue.rb +++ b/lib/rubocop/cop/lint/useless_else_without_rescue.rb @@ -30,23 +30,14 @@ module Lint # do_something_else # end class UselessElseWithoutRescue < Base - include ParserDiagnostic - MSG = '`else` without `rescue` is useless.' - private - - def relevant_diagnostic?(diagnostic) - diagnostic.reason == :useless_else - end - - def find_offense_node_by(diagnostic) - # TODO: When implementing auto-correction, this method should return - # an offense node passed as first argument of `add_offense` method. - end + def on_new_investigation + processed_source.diagnostics.each do |diagnostic| + next unless diagnostic.reason == :useless_else - def alternative_message(_diagnostic) - MSG + add_offense(diagnostic.location, severity: diagnostic.level) + end end end end diff --git a/lib/rubocop/cop/mixin/parser_diagnostic.rb b/lib/rubocop/cop/mixin/parser_diagnostic.rb deleted file mode 100644 index 430c6eac0fd..00000000000 --- a/lib/rubocop/cop/mixin/parser_diagnostic.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -module RuboCop - module Cop - # Common functionality for cops which processes Parser's diagnostics. - # This mixin requires its user class to define `#relevant_diagnostic?`. - # - # def relevant_diagnostic?(diagnostic) - # diagnostic.reason == :my_interested_diagnostic_type - # end - # - # If you want to use an alternative offense message rather than the one in - # Parser's diagnostic, define `#alternative_message`. - # - # def alternative_message(diagnostic) - # 'My custom message' - # end - module ParserDiagnostic - def on_new_investigation - processed_source.diagnostics.each do |d| - next unless relevant_diagnostic?(d) - - message = if respond_to?(:alternative_message, true) - alternative_message(d) - else - d.message.capitalize - end - offense_node = find_offense_node_by(d) - - add_offense(d.location, message: message, severity: d.level) do |corrector| - autocorrect(corrector, offense_node) - end - end - end - - private - - # If a mixed-in subclass has auto-correction, implement it in the mixed-in subclass. - def autocorrect(corrector, node); end - end - end -end