Class: RuboCop::OptionsValidator Private
Do not use. This class is for internal use only.
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/rubocop/options.rb |
Overview
Validates option arguments and the options' compatibility with each other.
Constant Summary
-
SYNTAX_DEPARTMENTS =
private
# File 'lib/rubocop/options.rb', line 314%w[Syntax Lint/Syntax].freeze
Class Method Summary
- .new(options) ⇒ OptionsValidator constructor Internal use only
-
.validate_cop_list(names)
Internal use only
Cop name validation must be done later than option parsing, so it’s not called from within Options.
- .format_message_from(name, cop_names) private Internal use only
Instance Attribute Summary
- #boolean_or_empty_cache? ⇒ Boolean readonly Internal use only
- #except_syntax? ⇒ Boolean readonly Internal use only
- #only_includes_redundant_disable? ⇒ Boolean readonly Internal use only
Instance Method Summary
- #disable_parallel_when_invalid_option_combo Internal use only
- #incompatible_options Internal use only
- #invalid_arguments_for_parallel Internal use only
- #validate_auto_gen_config Internal use only
- #validate_autocorrect Internal use only
- #validate_cache_enabled_for_cache_root Internal use only
- #validate_compatibility Internal use only
- #validate_cop_options Internal use only
- #validate_display_only_correctable_and_autocorrect Internal use only
- #validate_display_only_failed Internal use only
- #validate_display_only_failed_and_display_only_correctable Internal use only
- #validate_exclude_limit_option Internal use only
- #validate_lsp_and_editor_mode Internal use only
Class Method Details
.format_message_from(name, cop_names) (private)
[ GitHub ]# File 'lib/rubocop/options.rb', line 336
def (name, cop_names) = 'Unrecognized cop or department: %<name>s.' = "%<message>s\nDid you mean? %<candidate>s" corrections = NameSimilarity.find_similar_names(name, cop_names) if corrections.empty? format(, name: name) else format(, message: format(, name: name), candidate: corrections.join(', ')) end end
.validate_cop_list(names)
Cop name validation must be done later than option parsing, so it’s not called from within Options.
# File 'lib/rubocop/options.rb', line 319
def validate_cop_list(names) return unless names cop_names = Cop::Registry.global.names departments = Cop::Registry.global.departments.map(&:to_s) names.each do |name| next if cop_names.include?(name) next if departments.include?(name) next if SYNTAX_DEPARTMENTS.include?(name) raise IncorrectCopNameError, (name, cop_names) end end
Instance Attribute Details
#boolean_or_empty_cache? ⇒ Boolean
(readonly)
# File 'lib/rubocop/options.rb', line 472
def boolean_or_empty_cache? !@options.key?(:cache) || %w[true false].include?(@options[:cache]) end
#except_syntax? ⇒ Boolean
(readonly)
# File 'lib/rubocop/options.rb', line 468
def except_syntax? @options.key?(:except) && (@options[:except] & %w[Lint/Syntax Syntax]).any? end
#only_includes_redundant_disable? ⇒ Boolean
(readonly)
# File 'lib/rubocop/options.rb', line 463
def only_includes_redundant_disable? @options.key?(:only) && (@options[:only] & %w[Lint/RedundantCopDisableDirective RedundantCopDisableDirective]).any? end
Instance Method Details
#disable_parallel_when_invalid_option_combo
[ GitHub ]# File 'lib/rubocop/options.rb', line 442
def disable_parallel_when_invalid_option_combo return unless @options.key?(:parallel) invalid_flags = invalid_arguments_for_parallel return if invalid_flags.empty? @options.delete(:parallel) puts '-P/--parallel is being ignored because ' \ "it is not compatible with #{invalid_flags.join(', ')}." end
#incompatible_options
[ GitHub ]# File 'lib/rubocop/options.rb', line 476
def @incompatible_options ||= @options.keys & Options::EXITING_OPTIONS end
#invalid_arguments_for_parallel
[ GitHub ]# File 'lib/rubocop/options.rb', line 455
def invalid_arguments_for_parallel [('--auto-gen-config' if @options.key?(:auto_gen_config)), ('-F/--fail-fast' if @options.key?(:fail_fast)), ('--profile' if @options[:profile]), ('--memory' if @options[:memory]), ('--cache false' if @options > { cache: 'false' })].compact end
#validate_auto_gen_config
[ GitHub ]# File 'lib/rubocop/options.rb', line 382
def validate_auto_gen_config return if @options.key?(:auto_gen_config) = '--%<flag>s can only be used together with --auto-gen-config.' %i[exclude_limit offense_counts auto_gen_timestamp auto_gen_only_exclude].each do |option| if @options.key?(option) raise OptionArgumentError, format(, flag: option.to_s.tr('_', '-')) end end end
#validate_autocorrect
# File 'lib/rubocop/options.rb', line 428
def validate_autocorrect if @options.key?(:safe_autocorrect) && @options.key?(:autocorrect_all) = Rainbow(<<~MESSAGE).red Error: Both safe and unsafe autocorrect options are specified, use only one. MESSAGE raise OptionArgumentError, end return if @options.key?(:autocorrect) return unless @options.key?(:disable_uncorrectable) raise OptionArgumentError, format('--disable-uncorrectable can only be used together with --autocorrect.') end
#validate_cache_enabled_for_cache_root
# File 'lib/rubocop/options.rb', line 488
def validate_cache_enabled_for_cache_root return unless @options[:cache] == 'false' raise OptionArgumentError, '--cache-root cannot be used with --cache false' end
#validate_compatibility
# File 'lib/rubocop/options.rb', line 359
def validate_compatibility # rubocop:disable Metrics/MethodLength if only_includes_redundant_disable? raise OptionArgumentError, 'Lint/RedundantCopDisableDirective cannot be used with --only.' end raise OptionArgumentError, 'Syntax checking cannot be turned off.' if except_syntax? unless boolean_or_empty_cache? raise OptionArgumentError, '-C/--cache argument must be true or false' end validate_auto_gen_config validate_autocorrect validate_display_only_failed validate_display_only_failed_and_display_only_correctable validate_display_only_correctable_and_autocorrect validate_lsp_and_editor_mode disable_parallel_when_invalid_option_combo return if .size <= 1 raise OptionArgumentError, "Incompatible cli options: #{ .inspect}" end
#validate_cop_options
[ GitHub ]# File 'lib/rubocop/options.rb', line 354
def %i[only except].each { |opt| OptionsValidator.validate_cop_list(@options[opt]) } end
#validate_display_only_correctable_and_autocorrect
# File 'lib/rubocop/options.rb', line 403
def validate_display_only_correctable_and_autocorrect return unless @options.key?(:autocorrect) return if !@options.key?(:display_only_correctable) && !@options.key?(:display_only_safe_correctable) raise OptionArgumentError, '--autocorrect cannot be used with --display-only-[safe-]correctable.' end
#validate_display_only_failed
# File 'lib/rubocop/options.rb', line 395
def validate_display_only_failed return unless @options.key?(:display_only_failed) return if @options[:format] == 'junit' raise OptionArgumentError, format('--display-only-failed can only be used together with --format junit.') end
#validate_display_only_failed_and_display_only_correctable
# File 'lib/rubocop/options.rb', line 412
def validate_display_only_failed_and_display_only_correctable return unless @options.key?(:display_only_failed) return if !@options.key?(:display_only_correctable) && !@options.key?(:display_only_safe_correctable) raise OptionArgumentError, format('--display-only-failed cannot be used together with other display options.') end
#validate_exclude_limit_option
# File 'lib/rubocop/options.rb', line 480
def validate_exclude_limit_option return if /^\d+$/.match?(@options[:exclude_limit]) # Emulate OptionParser's behavior to make failures consistent regardless # of option order. raise OptionParser::MissingArgument end
#validate_lsp_and_editor_mode
# File 'lib/rubocop/options.rb', line 421
def validate_lsp_and_editor_mode return if !@options.key?(:lsp) || !@options.key?(:editor_mode) raise OptionArgumentError, format('Do not specify `--editor-mode` as it is redundant in `--lsp`.') end