lib/rubocop/config_loader.rb in rubocop-0.19.1 vs lib/rubocop/config_loader.rb in rubocop-0.20.0
- old
+ new
@@ -29,56 +29,63 @@
resolve_inheritance(path, hash)
hash.delete('inherit_from')
config = Config.new(hash, path)
+ deprecation_check(config)
config.warn_unless_valid
+ make_excludes_absolute(config)
config
end
+ def deprecation_check(config)
+ return unless config['AllCops']
+ if config['AllCops']['Excludes']
+ warn('AllCops/Excludes was renamed to AllCops/Exclude')
+ exit(-1)
+ elsif config['AllCops']['Includes']
+ warn('AllCops/Includes was renamed to AllCops/Include')
+ exit(-1)
+ end
+ end
+
def make_excludes_absolute(config)
- if config['AllCops'] && config['AllCops']['Excludes']
- config['AllCops']['Excludes'].map! do |exclude_elem|
+ if config['AllCops'] && config['AllCops']['Exclude']
+ config['AllCops']['Exclude'].map! do |exclude_elem|
if exclude_elem.is_a?(String) && !exclude_elem.start_with?('/')
- File.join(File.dirname(config.loaded_path), exclude_elem)
+ File.join(config.base_dir_for_path_parameters, exclude_elem)
else
exclude_elem
end
end
end
end
- def relative_path(path, base)
- path_name = Pathname.new(File.expand_path(path))
- path_name.relative_path_from(Pathname.new(base)).to_s
- end
-
- # Return an extended merge of two hashes. That is, a normal hash merge,
+ # 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 (i.e., cop names), will also be merged.
+ # arguments, will also be merged. And so on.
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|
if base_hash[key].is_a?(Hash)
- result[key] = base_hash[key].merge(derived_hash[key])
+ result[key] = merge(base_hash[key], derived_hash[key])
end
end
result
end
def base_configs(path, inherit_from)
- Array(inherit_from).map do |f|
+ configs = Array(inherit_from).map do |f|
f = File.join(File.dirname(path), f) unless f.start_with?('/')
- if auto_gen_config? && f.include?(AUTO_GENERATED_FILE)
- warn "Remove #{AUTO_GENERATED_FILE} from the current " \
- 'configuration before generating it again.'
- exit(1)
+ unless auto_gen_config? && f.include?(AUTO_GENERATED_FILE)
+ print 'Inheriting ' if debug?
+ load_file(f)
end
- print 'Inheriting ' if debug?
- load_file(f)
end
+
+ configs.compact
end
# Returns the path of .rubocop.yml searching upwards in the
# directory structure starting at the given directory where the
# inspected file is. If no .rubocop.yml is found there, the
@@ -92,22 +99,21 @@
config = load_file(config_file)
return config if config_file == DEFAULT_FILE
found_files = config_files_in_path(config_file)
if found_files.any? && found_files.last != config_file
- print 'AllCops/Excludes ' if debug?
+ print 'AllCops/Exclude ' if debug?
add_excludes_from_higher_level(config, load_file(found_files.last))
end
- make_excludes_absolute(config)
merge_with_default(config, config_file)
end
def add_excludes_from_higher_level(config, highest_config)
- if highest_config['AllCops'] && highest_config['AllCops']['Excludes']
+ if highest_config['AllCops'] && highest_config['AllCops']['Exclude']
config['AllCops'] ||= {}
- excludes = config['AllCops']['Excludes'] ||= []
- highest_config['AllCops']['Excludes'].each do |path|
+ excludes = config['AllCops']['Exclude'] ||= []
+ highest_config['AllCops']['Exclude'].each do |path|
unless path.is_a?(Regexp) || path.start_with?('/')
path = File.join(File.dirname(highest_config.loaded_path), path)
end
excludes << path unless excludes.include?(path)
end
@@ -127,12 +133,9 @@
private
def resolve_inheritance(path, hash)
base_configs(path, hash['inherit_from']).reverse_each do |base_config|
- if File.basename(base_config.loaded_path) == DOTFILE
- make_excludes_absolute(base_config)
- end
base_config.each do |k, v|
hash[k] = hash.key?(k) ? merge(v, hash[k]) : v if v.is_a?(Hash)
end
end
end