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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Config.



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

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

Instance Attribute Details

#contains_auto_generated_configObject

Returns the value of attribute contains_auto_generated_config.



16
17
18
# File 'lib/rubocop/config.rb', line 16

def contains_auto_generated_config
  @contains_auto_generated_config
end

#loaded_pathObject (readonly)

Returns the value of attribute loaded_path.



15
16
17
# File 'lib/rubocop/config.rb', line 15

def loaded_path
  @loaded_path
end

Instance Method Details

#cop_enabled?(cop) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/rubocop/config.rb', line 29

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

#file_to_exclude?(file) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/rubocop/config.rb', line 73

def file_to_exclude?(file)
  file = File.join(Dir.pwd, file) unless file.start_with?('/')
  patterns_to_exclude.any? { |pattern| match_path?(pattern, file) }
end

#file_to_include?(file) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
# File 'lib/rubocop/config.rb', line 66

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



24
25
26
27
# File 'lib/rubocop/config.rb', line 24

def for_cop(cop)
  cop = cop.cop_name if cop.respond_to?(:cop_name)
  self[cop]
end

#patterns_to_excludeObject



82
83
84
# File 'lib/rubocop/config.rb', line 82

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

#patterns_to_includeObject



78
79
80
# File 'lib/rubocop/config.rb', line 78

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

#validateObject

TODO: This should be a private method



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rubocop/config.rb', line 40

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

  default_config = ConfigLoader.default_configuration

  valid_cop_names, invalid_cop_names = @hash.keys.partition do |key|
    default_config.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].key?(param)
        fail ValidationError,
             "unrecognized parameter #{name}:#{param} found " +
             "in #{loaded_path || self}"
      end
    end
  end
end

#warn_unless_validObject



33
34
35
36
37
# File 'lib/rubocop/config.rb', line 33

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