Class: RuboCop::ConfigLoaderResolver Private
- Inherits:
-
Object
- Object
- RuboCop::ConfigLoaderResolver
- Defined in:
- lib/rubocop/config_loader_resolver.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
A help class for ConfigLoader that handles configuration resolution.
Instance Method Summary collapse
-
#apply_preview_defaults(default_configuration, preview) ⇒ Object
private
A cop's entry in the default configuration may carry a
Previewsection with the defaults it is expected to adopt in the next major release. -
#fix_include_paths(base_config_path, hash, path, key, value) ⇒ Object
private
When one .rubocop.yml file inherits from another .rubocop.yml file, the Include paths in the base configuration are relative to the directory where the base configuration file is.
-
#merge(base_hash, derived_hash, **opts) ⇒ Object
private
Return a recursive merge of two hashes.
-
#merge_with_default(config, config_file, unset_nil:, resolve_preview: true) ⇒ Object
private
Merges the given configuration with the default one.
-
#override_department_setting_for_cops(base_hash, derived_hash) ⇒ Object
private
An
Enabled: truesetting in user configuration for a cop overrides anEnabled: falsesetting for its department. -
#override_enabled_for_disabled_departments(base_hash, derived_hash) ⇒ Object
private
If a cop was previously explicitly enabled, but then superseded by the department being disabled, disable it.
-
#resolve_inheritance(path, hash, file, debug) ⇒ Object
private
rubocop:disable Metrics/MethodLength, Metrics/AbcSize.
- #resolve_inheritance_from_gems(hash) ⇒ Object private
-
#resolve_plugins(rubocop_config, plugins) ⇒ Object
private
rubocop:disable Metrics/ClassLength.
- #resolve_requires(path, hash) ⇒ Object private
Instance Method Details
#apply_preview_defaults(default_configuration, preview) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
A cop's entry in the default configuration may carry a Preview section
with the defaults it is expected to adopt in the next major release. Under
Preview those replace the current defaults. The section is dropped either
way, so the resolved configuration only ever shows what is in effect.
182 183 184 185 186 187 188 189 190 |
# File 'lib/rubocop/config_loader_resolver.rb', line 182 def apply_preview_defaults(default_configuration, preview) transform(default_configuration) do |params| next params unless params['Preview'].is_a?(Hash) params = params.dup preview_params = params.delete('Preview') preview ? params.merge(preview_params) : params end end |
#fix_include_paths(base_config_path, hash, path, key, value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
When one .rubocop.yml file inherits from another .rubocop.yml file, the Include paths in the base configuration are relative to the directory where the base configuration file is. For the derived configuration, we need to make those paths relative to where the derived configuration file is.
66 67 68 69 70 71 72 73 74 |
# File 'lib/rubocop/config_loader_resolver.rb', line 66 def fix_include_paths(base_config_path, hash, path, key, value) return unless File.basename(base_config_path).start_with?('.rubocop') base_dir = File.dirname(base_config_path) derived_dir = File.dirname(path) hash[key]['Include'] = value['Include'].map do |include_path| PathUtil.relative_path(File.join(base_dir, include_path), derived_dir) end end |
#merge(base_hash, derived_hash, **opts) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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-next Metrics/AbcSize
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/rubocop/config_loader_resolver.rb', line 128 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 merge_hashes?(base_hash, derived_hash, key) result[key] = merge(base_hash[key], derived_hash[key], **opts) elsif should_union?(derived_hash, base_hash, opts[:inherit_mode], key) result[key] = Array(base_hash[key]) | Array(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:, resolve_preview: true) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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.
When the --disable-all-cops or --enable-all-cops CLI option is given,
it takes precedence over the configuration values.
With resolve_preview: false the Preview sections of the default
configuration are left in place and preview is treated as off. That is
what extending the default configuration itself needs: preview gets
applied later, when a project configuration is resolved against it.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rubocop/config_loader_resolver.rb', line 103 def merge_with_default(config, config_file, unset_nil:, resolve_preview: true) base_defaults, preview = defaults_with_preview(config, resolve_preview) default_configuration = base_defaults disabled_by_default, enabled_by_default = resolve_default_overrides(config) if disabled_by_default || enabled_by_default default_configuration = transform(base_defaults) do |params| params.merge('Enabled' => !disabled_by_default) end end if disabled_by_default config = handle_disabled_by_default(config, default_configuration, base_defaults) end override_enabled_for_disabled_departments(default_configuration, config) opts = { inherit_mode: inherit_mode_for_default(config, preview), unset_nil: unset_nil } Config.new(merge(default_configuration, config, **opts), config_file) end |
#override_department_setting_for_cops(base_hash, derived_hash) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
An Enabled: true setting in user configuration for a cop overrides an
Enabled: false setting for its department.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/rubocop/config_loader_resolver.rb', line 147 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. derived_hash[key]['Enabled'] = 'override_department' if derived_hash[key]['Enabled'] end end |
#override_enabled_for_disabled_departments(base_hash, derived_hash) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
If a cop was previously explicitly enabled, but then superseded by the department being disabled, disable it.
164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/rubocop/config_loader_resolver.rb', line 164 def override_enabled_for_disabled_departments(base_hash, derived_hash) cops_to_disable = derived_hash.each_key.with_object([]) do |key, cops| next unless disabled?(derived_hash, key) cops.concat(base_hash.keys.grep(Regexp.new("^#{key}/"))) end cops_to_disable.each do |cop_name| next unless base_hash.dig(cop_name, 'Enabled') == true derived_hash.replace(merge({ cop_name => { 'Enabled' => false } }, derived_hash)) end end |
#resolve_inheritance(path, hash, file, debug) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
rubocop:disable Metrics/MethodLength, Metrics/AbcSize
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rubocop/config_loader_resolver.rb', line 38 def resolve_inheritance(path, hash, file, debug) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize inherited_files = Array(hash['inherit_from']) base_configs = base_configs(path, inherited_files, file) preview = inherited_preview?(hash, base_configs) base_configs.each_with_index.reverse_each do |base_config, index| override_department_setting_for_cops(base_config, hash) override_enabled_for_disabled_departments(base_config, hash) base_config.each do |k, v| next unless v.is_a?(Hash) only_base_has_include = v.key?('Include') && !hash.dig(k, 'Include') 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, preview)) end hash[k] = v fix_include_paths(base_config.loaded_path, hash, path, k, v) if only_base_has_include end end end |
#resolve_inheritance_from_gems(hash) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rubocop/config_loader_resolver.rb', line 76 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_plugins(rubocop_config, plugins) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
rubocop:disable Metrics/ClassLength
11 12 13 14 15 16 |
# File 'lib/rubocop/config_loader_resolver.rb', line 11 def resolve_plugins(rubocop_config, plugins) plugins = Array(plugins) - ConfigLoader.loaded_plugins.map { |plugin| plugin.about.name } return if plugins.empty? Plugin.integrate_plugins(rubocop_config, plugins) end |
#resolve_requires(path, hash) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rubocop/config_loader_resolver.rb', line 18 def resolve_requires(path, hash) config_dir = File.dirname(path) hash.delete('require').tap do |loaded_features| Array(loaded_features).each do |feature| if Plugin.plugin_capable?(feature) # NOTE: Compatibility for before plugins style. warn Rainbow(<<~MESSAGE).yellow #{feature} extension supports plugin, specify `plugins: #{feature}` instead of `require: #{feature}` in #{path}. For more information, see https://docs.rubocop.org/rubocop/plugin_migration_guide.html. MESSAGE rubocop_config = Config.create(hash, path, check: false) resolve_plugins(rubocop_config, feature) else FeatureLoader.load(config_directory_path: config_dir, feature: feature) end end end end |