Module: Gry::RubocopAdapter

Defined in:
lib/gry/rubocop_adapter.rb

Class Method Summary collapse

Class Method Details

.config_baseObject



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/gry/rubocop_adapter.rb', line 64

def config_base
  base = {
    'AllCops' => {
      'TargetRubyVersion' => RubocopAdapter.target_ruby_version,
    },
  }
  return base unless rails?
  base.merge({
    'Rails' => {
      'Enabled' => true,
    }
  })
end

.config_specified_by_userObject



55
56
57
58
59
60
61
62
# File 'lib/gry/rubocop_adapter.rb', line 55

def config_specified_by_user
  path = RuboCop::ConfigLoader.configuration_file_for(Dir.pwd)
  if path == RuboCop::ConfigLoader::DEFAULT_FILE
    RuboCop::Config.new
  else
    RuboCop::ConfigLoader.load_file(path)
  end
end

.configurable_copsObject



9
10
11
12
13
14
15
16
# File 'lib/gry/rubocop_adapter.rb', line 9

def configurable_cops
  conf = RuboCop::ConfigLoader.default_configuration.to_h
  conf
    .select{|key, cop_conf| !enforced_styles(cop_conf).empty? || metrics_cop?(key) }
    .reject{|key, _cop_conf| !rails? && key.start_with?('Rails/')}
    .select{|key, _cop_conf| conf = config_specified_by_user.for_cop(key); conf.empty? || conf.keys == ['Enabled']} # Ignore always configured cops
    .keys
end

.current_configObject



51
52
53
# File 'lib/gry/rubocop_adapter.rb', line 51

def current_config
  RuboCop::ConfigStore.new.for(Dir.pwd)
end

.default_configObject



5
6
7
# File 'lib/gry/rubocop_adapter.rb', line 5

def default_config
  RuboCop::ConfigLoader.default_configuration
end

.enforced_styles(cop_conf) ⇒ Object

Parameters:

  • cop_conf (Hash)


19
20
21
22
23
# File 'lib/gry/rubocop_adapter.rb', line 19

def enforced_styles(cop_conf)
  cop_conf.keys.select do |key|
    key.start_with?('Enforced')
  end
end

.find_target_filesObject



78
79
80
# File 'lib/gry/rubocop_adapter.rb', line 78

def find_target_files
  `rubocop --list-target-files`.each_line
end

.metrics_cop?(cop_name) ⇒ Boolean

Parameters:

  • cop_name (String)

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
# File 'lib/gry/rubocop_adapter.rb', line 27

def metrics_cop?(cop_name)
  return false unless cop_name.start_with?('Metrics')
  # https://github.com/bbatsov/rubocop/pull/4055
  exclude_cops = Gem::Version.new(RuboCop::Version.version) >= Gem::Version.new('0.48.0') ?
    %w[Metrics/BlockNesting] :
    %w[Metrics/ParameterLists Metrics/BlockNesting]
  !exclude_cops.include?(cop_name)
end

.rails?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/gry/rubocop_adapter.rb', line 44

def rails?
  return @rails if defined?(@rails)
  @rails = current_config.to_h.dig('Rails', 'Enabled') ||
    File.exist?('./bin/rails') ||
    File.exist?('./script/rails')
end

.target_ruby_versionObject



40
41
42
# File 'lib/gry/rubocop_adapter.rb', line 40

def target_ruby_version
  current_config.target_ruby_version
end

.to_supported_styles(enforced_style) ⇒ Object



36
37
38
# File 'lib/gry/rubocop_adapter.rb', line 36

def to_supported_styles(enforced_style)
  RuboCop::Cop::Util.to_supported_styles(enforced_style)
end