lib/rubocop/cop/cop.rb in rubocop-0.72.0 vs lib/rubocop/cop/cop.rb in rubocop-0.73.0

- old
+ new

@@ -29,10 +29,20 @@ include RuboCop::AST::Sexp include Util include IgnoredNode include AutocorrectLogic + Correction = Struct.new(:lambda, :node, :cop) do + def call(corrector) + lambda.call(corrector) + rescue StandardError => e + raise ErrorWithAnalyzedFileLocation.new( + cause: e, node: node, cop: cop + ) + end + end + attr_reader :config, :offenses, :corrections attr_accessor :processed_source # TODO: Bad design. @registry = Registry.new @@ -139,19 +149,39 @@ def duplicate_location?(location) @offenses.any? { |o| o.location == location } end def correct(node) - return :unsupported unless support_autocorrect? - return :uncorrected unless autocorrect? - return :already_corrected if @corrected_nodes.key?(node) + reason = reason_to_not_correct(node) + return reason if reason @corrected_nodes[node] = true - correction = autocorrect(node) - return :uncorrected unless correction + if support_autocorrect? + correction = autocorrect(node) + return :uncorrected unless correction - @corrections << correction + @corrections << Correction.new(correction, node, self) + elsif disable_uncorrectable? + disable_uncorrectable(node) + end :corrected + end + + def reason_to_not_correct(node) + return :unsupported unless correctable? + return :uncorrected unless autocorrect? + return :already_corrected if @corrected_nodes.key?(node) + + nil + end + + def disable_uncorrectable(node) + @disabled_lines ||= {} + line = node.location.line + return if @disabled_lines.key?(line) + + @disabled_lines[line] = true + @corrections << Correction.new(disable_offense(node), node, self) end def config_to_allow_offenses Formatter::DisabledConfigFormatter .config_to_allow_offenses[cop_name] ||= {}