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

COMMON_PARAMS =
%w(Exclude Include Severity)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Config.



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

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.



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

def loaded_path
  @loaded_path
end

Instance Method Details

#cop_enabled?(cop) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#file_to_exclude?(file) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/rubocop/config.rb', line 75

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)


68
69
70
71
72
73
# File 'lib/rubocop/config.rb', line 68

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



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

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

#patterns_to_excludeObject



84
85
86
# File 'lib/rubocop/config.rb', line 84

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

#patterns_to_includeObject



80
81
82
# File 'lib/rubocop/config.rb', line 80

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

#validateObject

TODO: This should be a private method



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

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 COMMON_PARAMS.include?(param) ||
             default_config[name].key?(param)
        fail ValidationError,
             "unrecognized parameter #{name}:#{param} found " \
             "in #{loaded_path || self}"
      end
    end
  end
end

#warn_unless_validObject



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

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