Class: Rubocop::Config

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

Overview

This class represents the configuration of the RuboCop application 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.

Defined Under Namespace

Classes: ValidationError

Constant Summary collapse

DOTFILE =
'.rubocop.yml'
RUBOCOP_HOME =
File.realpath(File.join(File.dirname(__FILE__), '..', '..'))
DEFAULT_FILE =
File.join(RUBOCOP_HOME, 'config', 'default.yml')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}, loaded_path = nil) ⇒ Config

Returns a new instance of Config.



157
158
159
160
161
# File 'lib/rubocop/config.rb', line 157

def initialize(hash = {}, loaded_path = nil)
  @hash = hash
  @loaded_path = loaded_path
  super(@hash)
end

Instance Attribute Details

#loaded_pathObject (readonly)

Returns the value of attribute loaded_path.



20
21
22
# File 'lib/rubocop/config.rb', line 20

def loaded_path
  @loaded_path
end

Class Method Details

.add_excludes_from_higher_level(config, highest_config) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rubocop/config.rb', line 113

def add_excludes_from_higher_level(config, highest_config)
  if highest_config['AllCops'] && highest_config['AllCops']['Excludes']
    config['AllCops'] ||= {}
    config['AllCops']['Excludes'] ||= []
    highest_config['AllCops']['Excludes'].each do |path|
      unless path.is_a?(Regexp) || path.start_with?('/')
        diff_in_level = config.loaded_path.count('/') -
          highest_config.loaded_path.count('/')
        path = '../' * diff_in_level + path
      end
      config['AllCops']['Excludes'] << path
    end
    config['AllCops']['Excludes'].uniq!
  end
end

.base_configs(path, inherit_from) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rubocop/config.rb', line 83

def base_configs(path, inherit_from)
  base_files = case inherit_from
               when nil then []
               when String then [inherit_from]
               when Array then inherit_from
               end
  base_files.map do |f|
    f = File.join(File.dirname(path), f) unless f.start_with?('/')
    load_file(f)
  end
end

.configuration_file_for(target_dir) ⇒ Object

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 user's home directory is checked. If there's no .rubocop.yml there either, the path to the default file is returned.



100
101
102
# File 'lib/rubocop/config.rb', line 100

def configuration_file_for(target_dir)
  config_files_in_path(target_dir).first || DEFAULT_FILE
end

.configuration_from_file(config_file) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/rubocop/config.rb', line 104

def configuration_from_file(config_file)
  config = load_file(config_file)
  found_files = config_files_in_path(config_file)
  if found_files.any? && found_files.last != config_file
    add_excludes_from_higher_level(config, load_file(found_files.last))
  end
  merge_with_default(config, config_file)
end

.correct_relative_excludes(all_cops, base_config, path) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rubocop/config.rb', line 44

def correct_relative_excludes(all_cops, base_config, path)
  all_cops['Excludes'].map! do |exclude_elem|
    if exclude_elem.is_a?(String) && exclude_elem =~ %r([^/].*/)
      rel_path = relative_path(base_config.loaded_path,
                               File.dirname(path))
      rel_path.to_s.sub(/#{DOTFILE}$/, '') + exclude_elem
    else
      exclude_elem
    end
  end
end

.default_configurationObject



129
130
131
# File 'lib/rubocop/config.rb', line 129

def default_configuration
  @default_configuration ||= load_file(DEFAULT_FILE)
end

.load_file(path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/config.rb', line 23

def load_file(path)
  path = File.absolute_path(path)
  hash = YAML.load_file(path)

  base_configs(path, hash['inherit_from']).reverse.each do |base_config|
    base_config.each do |key, value|
      if value.is_a?(Hash)
        if key == 'AllCops' && value['Excludes']
          correct_relative_excludes(value, base_config, path)
        end
        hash[key] = hash.has_key?(key) ? merge(value, hash[key]) : value
      end
    end
  end

  hash.delete('inherit_from')
  config = new(hash, path)
  config.warn_unless_valid
  config
end

.merge(base_hash, derived_hash) ⇒ 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.



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

def merge(base_hash, derived_hash)
  result = {}
  base_hash.each do |key, value|
    result[key] = if derived_hash.has_key?(key)
                    if value.is_a?(Hash)
                      merge(value, derived_hash[key])
                    else
                      derived_hash[key]
                    end
                  else
                    base_hash[key]
                  end
  end
  derived_hash.each do |key, value|
    result[key] = value unless base_hash.has_key?(key)
  end
  result
end

.merge_with_default(config, config_file) ⇒ Object



133
134
135
# File 'lib/rubocop/config.rb', line 133

def merge_with_default(config, config_file)
  new(merge(default_configuration, config), config_file)
end

.relative_path(path, base) ⇒ Object



56
57
58
59
# File 'lib/rubocop/config.rb', line 56

def relative_path(path, base)
  path_name = Pathname.new(File.expand_path(path))
  path_name.relative_path_from(Pathname.new(base)).to_s
end

Instance Method Details

#cop_enabled?(cop) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/rubocop/config.rb', line 167

def cop_enabled?(cop)
  self[cop].nil? || self[cop]['Enabled']
end

#file_to_exclude?(file) ⇒ Boolean

Returns:

  • (Boolean)


211
212
213
214
215
216
# File 'lib/rubocop/config.rb', line 211

def file_to_exclude?(file)
  relative_file_path = relative_path_to_loaded_dir(file)
  patterns_to_exclude.any? do |pattern|
    match_path?(pattern, relative_file_path)
  end
end

#file_to_include?(file) ⇒ Boolean

Returns:

  • (Boolean)


204
205
206
207
208
209
# File 'lib/rubocop/config.rb', line 204

def file_to_include?(file)
  relative_file_path = relative_path_to_loaded_dir(file)
  patterns_to_include.any? do |pattern|
    match_path?(pattern, relative_file_path)
  end
end

#for_cop(cop) ⇒ Object



163
164
165
# File 'lib/rubocop/config.rb', line 163

def for_cop(cop)
  self[cop]
end

#patterns_to_excludeObject



222
223
224
# File 'lib/rubocop/config.rb', line 222

def patterns_to_exclude
  @hash['AllCops']['Excludes']
end

#patterns_to_includeObject



218
219
220
# File 'lib/rubocop/config.rb', line 218

def patterns_to_include
  @hash['AllCops']['Includes']
end

#validateObject

TODO: This should be a private method



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rubocop/config.rb', line 178

def validate
  # Don't validate RuboCop's own files. Avoids inifinite recursion.
  return if @loaded_path.start_with?(RUBOCOP_HOME)

  default_config = self.class.default_configuration

  valid_cop_names, invalid_cop_names = @hash.keys.partition do |key|
    default_config.has_key?(key)
  end

  invalid_cop_names.each do |name|
    fail ValidationError,
         "unrecognized cop #{name} found in #{loaded_path || self}"
  end

  valid_cop_names.each do |name|
    @hash[name].each_key do |param|
      unless default_config[name].has_key?(param)
        fail ValidationError,
             "unrecognized parameter #{name}:#{param} found " +
             "in #{loaded_path || self}"
      end
    end
  end
end

#warn_unless_validObject



171
172
173
174
175
# File 'lib/rubocop/config.rb', line 171

def warn_unless_valid
  validate
rescue Config::ValidationError => e
  puts "Warning: #{e.message}".color(:red)
end