lib/rubocop/config_loader.rb in rubocop-0.35.1 vs lib/rubocop/config_loader.rb in rubocop-0.36.0
- old
+ new
@@ -1,6 +1,7 @@
# encoding: utf-8
+# frozen_string_literal: true
require 'yaml'
require 'pathname'
module RuboCop
@@ -8,23 +9,27 @@
# and all its cops. A Config is associated with a YAML configuration
# file from which it was read. Several different Configs can be used
# during a run of the rubocop program, if files in several
# directories are inspected.
class ConfigLoader
- DOTFILE = '.rubocop.yml'
+ DOTFILE = '.rubocop.yml'.freeze
RUBOCOP_HOME = File.realpath(File.join(File.dirname(__FILE__), '..', '..'))
DEFAULT_FILE = File.join(RUBOCOP_HOME, 'config', 'default.yml')
- AUTO_GENERATED_FILE = '.rubocop_todo.yml'
+ AUTO_GENERATED_FILE = '.rubocop_todo.yml'.freeze
class << self
- attr_accessor :debug, :auto_gen_config, :exclude_limit
+ attr_accessor :debug, :auto_gen_config
attr_writer :root_level # The upwards search is stopped at this level.
attr_writer :default_configuration
- alias_method :debug?, :debug
- alias_method :auto_gen_config?, :auto_gen_config
+ alias debug? debug
+ alias auto_gen_config? auto_gen_config
+ def clear_options
+ @debug = @auto_gen_config = @root_level = nil
+ end
+
def load_file(path)
path = File.absolute_path(path)
hash = load_yaml_configuration(path)
resolve_inheritance_from_gems(hash, hash.delete('inherit_gem'))
@@ -38,11 +43,11 @@
config.deprecation_check do |deprecation_message|
warn("#{path} - #{deprecation_message}")
end
config.add_missing_namespaces
- config.warn_unless_valid
+ config.validate
config.make_excludes_absolute
config
end
# Return a recursive merge of two hashes. That is, a normal hash merge,
@@ -58,24 +63,23 @@
result
end
def base_configs(path, inherit_from)
configs = Array(inherit_from).compact.map do |f|
- if f =~ URI.regexp
+ if f =~ /\A#{URI.regexp(%w(http https))}\z/
f = RemoteConfig.new(f).file
- load_file(f)
else
f = File.expand_path(f, File.dirname(path))
if auto_gen_config?
next if f.include?(AUTO_GENERATED_FILE)
old_auto_config_file_warning if f.include?('rubocop-todo.yml')
end
print 'Inheriting ' if debug?
- load_file(f)
end
+ load_file(f)
end
configs.compact
end
@@ -127,11 +131,11 @@
end
private
# Returns a new hash where the parameters of the given config hash have
- # been replaced by parmeters returned by the given block.
+ # been replaced by parameters returned by the given block.
def transform(config)
Hash[config.map { |cop, params| [cop, yield(params)] }]
end
def load_yaml_configuration(absolute_path)
@@ -172,12 +176,18 @@
end
end
def resolve_inheritance_from_gems(hash, gems)
(gems || {}).each_pair do |gem_name, config_path|
+ if gem_name == 'rubocop'
+ fail ArgumentError,
+ "can't inherit configuration from the rubocop gem"
+ end
+
hash['inherit_from'] = Array(hash['inherit_from'])
- hash['inherit_from'] << gem_config_path(gem_name, config_path)
+ # Put gem configuration first so local configuration overrides it.
+ hash['inherit_from'].unshift gem_config_path(gem_name, config_path)
end
end
def gem_config_path(gem_name, relative_config_path)
spec = Gem::Specification.find_by_name(gem_name)
@@ -202,12 +212,11 @@
end
dirs_to_search << Dir.home
end
def old_auto_config_file_warning
- warn Rainbow('Attention: rubocop-todo.yml has been renamed to ' \
- "#{AUTO_GENERATED_FILE}").red
- exit(1)
+ fail RuboCop::Error, 'rubocop-todo.yml is obsolete; it must be called' \
+ " #{AUTO_GENERATED_FILE} instead"
end
end
end
end