Class: RuboCop::ConfigValidator

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rubocop/config_validator.rb

Overview

Handles validation of configuration, for example cop names, parameter names, and Ruby versions.

Constant Summary collapse

COMMON_PARAMS =
%w[Exclude Include Severity inherit_mode
AutoCorrect StyleGuide Details].freeze
INTERNAL_PARAMS =
%w[Description StyleGuide VersionAdded
VersionChanged Reference Safe SafeAutoCorrect].freeze
DEFAULT_RUBY_VERSION =

2.3 is the oldest officially supported Ruby version.

2.3
KNOWN_RUBIES =
[2.3, 2.4, 2.5, 2.6, 2.7].freeze
OBSOLETE_RUBIES =
{
  1.9 => '0.50', 2.0 => '0.50', 2.1 => '0.58', 2.2 => '0.69'
}.freeze
RUBY_VERSION_FILENAME =
'.ruby-version'

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ConfigValidator

Returns a new instance of ConfigValidator.



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

def initialize(config)
  @config = config
  @config_obsoletion = ConfigObsoletion.new(config)
end

Instance Method Details

#target_ruby_versionObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubocop/config_validator.rb', line 54

def target_ruby_version
  @target_ruby_version ||= begin
    if for_all_cops['TargetRubyVersion']
      @target_ruby_version_source = :rubocop_yml

      for_all_cops['TargetRubyVersion'].to_f
    elsif target_ruby_version_from_version_file
      @target_ruby_version_source = :ruby_version_file

      target_ruby_version_from_version_file
    elsif target_ruby_version_from_bundler_lock_file
      @target_ruby_version_source = :bundler_lock_file

      target_ruby_version_from_bundler_lock_file
    else
      DEFAULT_RUBY_VERSION
    end
  end
end

#validateObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rubocop/config_validator.rb', line 33

def validate
  # Don't validate RuboCop's own files. Avoids infinite recursion.
  base_config_path = File.expand_path(File.join(ConfigLoader::RUBOCOP_HOME,
                                                'config'))
  return if File.expand_path(@config.loaded_path)
                .start_with?(base_config_path)

  valid_cop_names, invalid_cop_names = @config.keys.partition do |key|
    ConfigLoader.default_configuration.key?(key)
  end

  @config_obsoletion.reject_obsolete_cops_and_parameters

  warn_about_unrecognized_cops(invalid_cop_names)
  check_target_ruby
  validate_parameter_names(valid_cop_names)
  validate_enforced_styles(valid_cop_names)
  validate_syntax_cop
  reject_mutually_exclusive_defaults
end

#validate_section_presence(name) ⇒ Object

Raises:



74
75
76
77
78
79
# File 'lib/rubocop/config_validator.rb', line 74

def validate_section_presence(name)
  return unless @config.key?(name) && @config[name].nil?

  raise ValidationError,
        "empty section #{name} found in #{smart_loaded_path}"
end