Class: RubocopAutoCorrector::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop_auto_corrector/cli.rb

Constant Summary collapse

DEFAULT_ORDER =
100

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



12
13
14
15
16
# File 'lib/rubocop_auto_corrector/cli.rb', line 12

def initialize
  data = YAML.load_file("#{__dir__}/data.yml")
  @cop_orders = data['cop_orders']
  @exclude_cops = data['exclude_cops']
end

Instance Method Details

#auto_correctable?(cop_name) ⇒ Boolean

Whether this cop is auto correctable

Parameters:

  • cop_name (String)

Returns:

  • (Boolean)


49
50
51
# File 'lib/rubocop_auto_corrector/cli.rb', line 49

def auto_correctable?(cop_name)
  RubocopAutoCorrector::CopFinder.new(cop_name).auto_correctable?
end

#collect_offense_cop_namesObject



34
35
36
37
38
39
40
41
42
# File 'lib/rubocop_auto_corrector/cli.rb', line 34

def collect_offense_cop_names
  rubocop_result = JSON.parse(run_rubocop_for_collect)

  cop_names = []
  rubocop_result['files'].each do |file|
    cop_names += file['offenses'].map { |offense| offense['cop_name'] }
  end
  cop_names.uniq
end

#perform(auto_collect_all) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rubocop_auto_corrector/cli.rb', line 18

def perform(auto_collect_all)
  rubocop_option = auto_collect_all ? '--autocorrect-all' : '--autocorrect'

  cop_names = collect_offense_cop_names.select { |cop_name| auto_correctable?(cop_name) }
                                       .sort_by { |cop_name| [cop_order(cop_name), cop_name] }

  cop_names.each do |cop_name|
    if (reason = exclude_reason(cop_name))
      puts reason
      next
    end

    run_and_commit "rubocop #{rubocop_option} --only #{cop_name}"
  end
end