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

cop_todo.rb « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cd1c6669ae2c54541b2e7140ecd2c8c0be366007 (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
# frozen_string_literal: true

require_relative 'formatter/graceful_formatter'

module RuboCop
  class CopTodo
    attr_accessor :previously_disabled, :grace_period

    attr_reader :cop_name, :files, :offense_count

    def initialize(cop_name)
      @cop_name = cop_name
      @files = Set.new
      @offense_count = 0
      @cop_class = self.class.find_cop_by_name(cop_name)
      @previously_disabled = false
      @grace_period = false
    end

    def record(file, offense_count)
      @files << file
      @offense_count += offense_count
    end

    def add_files(files)
      @files.merge(files)
    end

    def autocorrectable?
      @cop_class&.support_autocorrect?
    end

    def generate?
      previously_disabled || grace_period || files.any?
    end

    def to_yaml
      yaml = []
      yaml << '---'
      yaml << '# Cop supports --autocorrect.' if autocorrectable?
      yaml << "#{cop_name}:"

      if previously_disabled
        yaml << "  # Offense count: #{offense_count}"
        yaml << '  # Temporarily disabled due to too many offenses'
        yaml << '  Enabled: false'
      end

      yaml << "  #{RuboCop::Formatter::GracefulFormatter.grace_period_key_value}" if grace_period

      if files.any?
        yaml << '  Exclude:'
        yaml.concat files.sort.map { |file| "    - '#{file}'" }
      end

      yaml << ''

      yaml.join("\n")
    end

    def self.find_cop_by_name(cop_name)
      RuboCop::Cop::Registry.global.find_by_cop_name(cop_name)
    end
  end
end