Class: RubocopAutoCorrector::CopFinder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cop_name) ⇒ CopFinder

Returns a new instance of CopFinder.

Parameters:

  • cop_name (String)

    e.g. Metrics/AbcSize



8
9
10
# File 'lib/rubocop_auto_corrector/cop_finder.rb', line 8

def initialize(cop_name)
  @cop_name = cop_name
end

Instance Attribute Details

#cop_nameObject (readonly)

Returns the value of attribute cop_name.



5
6
7
# File 'lib/rubocop_auto_corrector/cop_finder.rb', line 5

def cop_name
  @cop_name
end

Instance Method Details

#auto_correctable?Boolean

Whether this cop is auto correctable

Returns:

  • (Boolean)


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
# File 'lib/rubocop_auto_corrector/cop_finder.rb', line 14

def auto_correctable? # rubocop:disable Metrics/MethodLength
  cop_candidacies.any? do |cop_candidacy|
    gem_name = cop_candidacy[:gem_name]
    cop_class_name = cop_candidacy[:cop_class_name]

    Object.new.instance_eval <<-RUBY, __FILE__, __LINE__ + 1
      # begin
      #   require 'rubocop-rspec'
      # rescue LoadError
      # end
      #
      # return ::RuboCop::Cop::RSpec::AlignLeftLetBrace.support_autocorrect? if ::RuboCop::Cop::RSpec::AlignLeftLetBrace.respond_to?(:support_autocorrect?)
      # ::RuboCop::Cop::RSpec::AlignLeftLetBrace.new.respond_to?(:autocorrect)

      begin
        require '#{gem_name}'
      rescue LoadError
      end

      return #{cop_class_name}.support_autocorrect? if #{cop_class_name}.respond_to?(:support_autocorrect?)
      #{cop_class_name}.new.respond_to?(:autocorrect)
    RUBY
  rescue NameError
    false
  end
end

#cop_candidaciesArray<Hash<Symbol, String>>

rubocop:disable Metrics/MethodLength

Returns:

  • (Array<Hash<Symbol, String>>)


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
# File 'lib/rubocop_auto_corrector/cop_finder.rb', line 43

def cop_candidacies
  cop_class_suffix = cop_name.gsub('/', '::')

  case cop_name
  when %r{^RSpec/}
    [
      {
        gem_name: 'rubocop-rspec',
        cop_class_name: "::RuboCop::Cop::#{cop_class_suffix}"
      }
    ]
  when 'Rails/HttpStatus'
    [
      # for rubocop-rspec < 2.28.0
      {
        gem_name: 'rubocop-rspec',
        cop_class_name: "::RuboCop::Cop::RSpec::#{cop_class_suffix}"
      }
    ]
  when %r{^FactoryBot/}
    [
      # for rubocop-rspec < 2.0.0
      {
        gem_name: 'rubocop-rspec',
        cop_class_name: "::RuboCop::Cop::RSpec::#{cop_class_suffix}"
      },
      # for rubocop-rspec v3+
      {
        gem_name: 'rubocop-factory_bot',
        cop_class_name: "::RuboCop::Cop::#{cop_class_suffix}"
      }
    ]
  when %r{^Capybara/}
    [
      # for rubocop-rspec < 2.0.0
      {
        gem_name: 'rubocop-rspec',
        cop_class_name: "::RuboCop::Cop::RSpec::#{cop_class_suffix}"
      },
      # for rubocop-rspec v3+
      {
        gem_name: 'rubocop-capybara',
        cop_class_name: "::RuboCop::Cop::#{cop_class_suffix}"
      }
    ]
  when %r{^(Layout|Lint|Metrics|Naming|Security|Style|Bundler|Gemspec)/}
    # Official cops
    [
      {
        gem_name: 'rubocop',
        cop_class_name: "::RuboCop::Cop::#{cop_class_suffix}"
      }
    ]
  else
    # Unknown cops
    department_camel = cop_name.split('/').first
    department_snake = department_camel.gsub(/(?<=.)([A-Z])/) { |s| "_#{s}" }.downcase

    [
      {
        gem_name: "rubocop-#{department_snake}",
        cop_class_name: "::RuboCop::Cop::#{cop_class_suffix}"
      }
    ]
  end
end