Class: RuboCop::ConfigValidator

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

Overview

Handles validation of configuration, for example cop names, parameter names, and Ruby versions. rubocop:disable-next Metrics/ClassLength

Constant Summary collapse

COMMON_PARAMS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w[Exclude Include Severity inherit_mode AutoCorrect StyleGuide Details
Enabled Reference References Safe SafeAutoCorrect].freeze
INTERNAL_PARAMS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w[Description StyleGuide Preview
VersionAdded VersionChanged VersionRemoved
Reference References Safe SafeAutoCorrect].freeze
NEW_COPS_VALUES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w[pending disable enable].freeze
NEW_COPS_VERSION_PATTERN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/\A\d+(\.\d+)*\z/.freeze
CONFIG_CHECK_AUTOCORRECTS =
%w[always contextual disabled].freeze

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ConfigValidator

Returns a new instance of ConfigValidator.



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

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

Instance Method Details

#target_ruby_version ⇒ Object



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

def target_ruby_version
  target_ruby.version
end

#validate ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubocop/config_validator.rb', line 39

def validate
  check_cop_config_value(@config)
  reject_conflicting_safe_settings

  # Don't validate RuboCop's own files further. Avoids infinite recursion.
  return if @config.internal?

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

  validate_parameter_shape(valid_cop_names)

  check_obsoletions
  alert_about_unrecognized_cops(invalid_cop_names)
  validate_all_cops_parameters
  validate_parameter_names(valid_cop_names)
  validate_enforced_styles(valid_cop_names)
  validate_syntax_cop
  reject_mutually_exclusive_defaults
end

#validate_after_resolution ⇒ Object

Validations that should only be run after all config resolving has taken place:

  • The target ruby version is only checked once the entire inheritance chain has been loaded so that only the final value is validated, and any obsolete but overridden values are ignored.


66
67
68
# File 'lib/rubocop/config_validator.rb', line 66

def validate_after_resolution
  check_target_ruby
end

#validate_parser_engine ⇒ Object

Checks that the parser engine can parse the target Ruby version. This is for the runner to call before it parses a file, because a configuration that no file is inspected with is free to name any engine.



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

def validate_parser_engine
  return if @parser_engine_validated

  check_parser_engine
  @parser_engine_validated = true
end