Class: RuboCop::ConfigLoaderResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/config_loader_resolver.rb

Overview

A help class for ConfigLoader that handles configuration resolution.

Instance Method Summary collapse

Instance Method Details

#merge(base_hash, derived_hash) ⇒ Object

Returns a new hash where the parameters of the given config hash have been replaced by parameters returned by the given block. Return a recursive merge of two hashes. That is, a normal hash merge, with the addition that any value that is a hash, and occurs in both arguments, will also be merged. And so on.



73
74
75
76
77
78
79
80
81
# File 'lib/rubocop/config_loader_resolver.rb', line 73

def merge(base_hash, derived_hash)
  result = base_hash.merge(derived_hash)
  keys_appearing_in_both = base_hash.keys & derived_hash.keys
  keys_appearing_in_both.each do |key|
    next unless base_hash[key].is_a?(Hash)
    result[key] = merge(base_hash[key], derived_hash[key])
  end
  result
end

#merge_with_default(config, config_file) ⇒ Object

Merges the given configuration with the default one. If AllCops:DisabledByDefault is true, it changes the Enabled params so that only cops from user configuration are enabled. If AllCops::EnabledByDefault is true, it changes the Enabled params so that only cops explicitly disabled in user configuration are disabled.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rubocop/config_loader_resolver.rb', line 49

def merge_with_default(config, config_file)
  default_configuration = ConfigLoader.default_configuration

  disabled_by_default = config.for_all_cops['DisabledByDefault']
  enabled_by_default = config.for_all_cops['EnabledByDefault']

  if disabled_by_default || enabled_by_default
    default_configuration = transform(default_configuration) do |params|
      params.merge('Enabled' => !disabled_by_default)
    end
  end

  if disabled_by_default
    config = handle_disabled_by_default(config, default_configuration)
  end

  Config.new(merge(default_configuration, config), config_file)
end

#resolve_inheritance(path, hash, file) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/rubocop/config_loader_resolver.rb', line 20

def resolve_inheritance(path, hash, file)
  base_configs(path, hash['inherit_from'], file)
    .reverse_each do |base_config|
    base_config.each do |k, v|
      hash[k] = hash.key?(k) ? merge(v, hash[k]) : v if v.is_a?(Hash)
    end
  end
end

#resolve_inheritance_from_gems(hash, gems) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/config_loader_resolver.rb', line 29

def resolve_inheritance_from_gems(hash, gems)
  (gems || {}).each_pair do |gem_name, config_path|
    if gem_name == 'rubocop'
      raise ArgumentError,
            "can't inherit configuration from the rubocop gem"
    end

    hash['inherit_from'] = Array(hash['inherit_from'])
    Array(config_path).reverse.each do |path|
      # Put gem configuration first so local configuration overrides it.
      hash['inherit_from'].unshift gem_config_path(gem_name, path)
    end
  end
end

#resolve_requires(path, hash) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/rubocop/config_loader_resolver.rb', line 9

def resolve_requires(path, hash)
  config_dir = File.dirname(path)
  Array(hash.delete('require')).each do |r|
    if r.start_with?('.')
      require(File.join(config_dir, r))
    else
      require(r)
    end
  end
end