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, **opts) ⇒ Object

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.

rubocop:disable Metrics/AbcSize



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rubocop/config_loader_resolver.rb', line 88

def merge(base_hash, derived_hash, **opts)
  result = base_hash.merge(derived_hash)
  keys_appearing_in_both = base_hash.keys & derived_hash.keys
  keys_appearing_in_both.each do |key|
    if opts[:unset_nil] && derived_hash[key].nil?
      result.delete(key)
    elsif base_hash[key].is_a?(Hash)
      result[key] = merge(base_hash[key], derived_hash[key], **opts)
    elsif should_union?(base_hash, key, opts[:inherit_mode])
      result[key] = base_hash[key] | derived_hash[key]
    elsif opts[:debug]
      warn_on_duplicate_setting(base_hash, derived_hash, key, **opts)
    end
  end
  result
end

#merge_with_default(config, config_file, unset_nil:) ⇒ 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.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rubocop/config_loader_resolver.rb', line 62

def merge_with_default(config, config_file, unset_nil:)
  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

  opts = { inherit_mode: config['inherit_mode'] || {},
           unset_nil: unset_nil }
  Config.new(merge(default_configuration, config, **opts), config_file)
end

#override_department_setting_for_cops(base_hash, derived_hash) ⇒ Object

An ‘Enabled: true` setting in user configuration for a cop overrides an `Enabled: false` setting for its department.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rubocop/config_loader_resolver.rb', line 108

def override_department_setting_for_cops(base_hash, derived_hash)
  derived_hash.each_key do |key|
    next unless key =~ %r{(.*)/.*}

    department = Regexp.last_match(1)
    next unless disabled?(derived_hash, department) ||
                disabled?(base_hash, department)

    # The `override_department` setting for the `Enabled` parameter is an
    # internal setting that's not documented in the manual. It will cause a
    # cop to be enabled later, when logic surrounding enabled/disabled it
    # run, even though its department is disabled.
    if derived_hash[key]['Enabled']
      derived_hash[key]['Enabled'] = 'override_department'
    end
  end
end

#resolve_inheritance(path, hash, file, debug) ⇒ Object

rubocop:disable Metrics/MethodLength



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/config_loader_resolver.rb', line 21

def resolve_inheritance(path, hash, file, debug)
  inherited_files = Array(hash['inherit_from'])
  base_configs(path, inherited_files, file)
    .reverse.each_with_index do |base_config, index|
    override_department_setting_for_cops(base_config, hash)
    base_config.each do |k, v|
      next unless v.is_a?(Hash)

      if hash.key?(k)
        v = merge(v, hash[k],
                  cop_name: k, file: file, debug: debug,
                  inherited_file: inherited_files[index],
                  inherit_mode: determine_inherit_mode(hash, k))
      end
      hash[k] = v
    end
  end
end

#resolve_inheritance_from_gems(hash) ⇒ Object

rubocop:enable Metrics/MethodLength



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubocop/config_loader_resolver.rb', line 41

def resolve_inheritance_from_gems(hash)
  gems = hash.delete('inherit_gem')
  (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