Welcome to mirror list, hosted at ThFree Co, Russian Federation.

graceful_formatter.rb « formatter « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b5db7c6c1924a88004a863bdd3f71eb9d5f78be0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# frozen_string_literal: true

require 'rubocop'

module RuboCop
  module Formatter
    class GracefulFormatter < ::RuboCop::Formatter::ProgressFormatter
      CONFIG_DETAILS_KEY = 'Details'
      CONFIG_DETAILS_VALUE = 'grace period'

      class << self
        attr_accessor :active_offenses
      end

      def started(...)
        super

        self.class.active_offenses = 0

        @silenced_offenses_for_files = {}
        @config = RuboCop::ConfigStore.new.for_pwd
      end

      def file_finished(file, offenses)
        silenced_offenses, active_offenses = offenses.partition { silenced?(_1) }

        @silenced_offenses_for_files[file] = silenced_offenses if silenced_offenses.any?

        super(file, active_offenses)
      end

      def finished(inspected_files)
        # See the note below why are using this ivar in the first place.
        unless defined?(@total_offense_count)
          raise <<~MESSAGE
            RuboCop has changed its internals and the instance variable
            `@total_offense_count` is no longer defined but we were relying on it.

            Please change the implementation.

            See https://github.com/rubocop/rubocop/blob/65a757b0f/lib/rubocop/formatter/simple_text_formatter.rb#L24
          MESSAGE
        end

        super

        # Internally, RuboCop has no notion of "silenced offenses". We cannot
        # override this meaning in a formatter that's why we track what we
        # consider to be an active offense.
        # This is needed for `adjusted_exit_status` method below.
        self.class.active_offenses = @total_offense_count

        report_silenced_offenses(inspected_files)
      end

      # We consider this run a success without any active offenses.
      def self.adjusted_exit_status(status)
        return status unless status == RuboCop::CLI::STATUS_OFFENSES
        return RuboCop::CLI::STATUS_SUCCESS if active_offenses == 0

        status
      end

      def self.grace_period?(cop_name, config)
        details = config[CONFIG_DETAILS_KEY]
        return false unless details
        return true if details == CONFIG_DETAILS_VALUE

        warn "#{cop_name}: Unhandled value #{details.inspect} for `Details` key."

        false
      end

      def self.grace_period_key_value
        "#{CONFIG_DETAILS_KEY}: #{CONFIG_DETAILS_VALUE}"
      end

      private

      def silenced?(offense)
        cop_config = @config.for_cop(offense.cop_name)

        self.class.grace_period?(offense.cop_name, cop_config)
      end

      def report_silenced_offenses(inspected_files)
        return if @silenced_offenses_for_files.empty?

        output.puts
        output.puts 'Silenced offenses:'
        output.puts

        @silenced_offenses_for_files.each do |file, offenses|
          report_file(file, offenses)
        end

        silenced_offense_count = @silenced_offenses_for_files.values.sum(&:size)
        silenced_text = colorize("#{silenced_offense_count} offenses", :yellow)

        output.puts
        output.puts "#{inspected_files.size} files inspected, #{silenced_text} silenced"
      end

      def report_file_as_mark(_offenses)
        # Skip progress bar. No dots. No C/Ws.
      end
    end
  end
end