Class: RuboCop::OptionsValidator Private
- Inherits:
-
Object
- Object
- RuboCop::OptionsValidator
- Defined in:
- lib/rubocop/options.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Validates option arguments and the options’ compatibility with each other.
Class Method Summary collapse
-
.validate_cop_list(names) ⇒ Object
private
Cop name validation must be done later than option parsing, so it’s not called from within Options.
Instance Method Summary collapse
- #boolean_or_empty_cache? ⇒ Boolean private
- #disable_parallel_when_invalid_option_combo ⇒ Object private
- #except_syntax? ⇒ Boolean private
- #incompatible_options ⇒ Object private
-
#initialize(options) ⇒ OptionsValidator
constructor
private
A new instance of OptionsValidator.
- #invalid_arguments_for_parallel ⇒ Object private
- #only_includes_redundant_disable? ⇒ Boolean private
-
#validate_auto_gen_config ⇒ Object
private
rubocop:enable Metrics/AbcSize.
- #validate_autocorrect ⇒ Object private
- #validate_cache_enabled_for_cache_root ⇒ Object private
-
#validate_compatibility ⇒ Object
private
rubocop:disable Metrics/AbcSize.
- #validate_cop_options ⇒ Object private
- #validate_display_only_correctable_and_autocorrect ⇒ Object private
- #validate_display_only_failed ⇒ Object private
- #validate_display_only_failed_and_display_only_correctable ⇒ Object private
- #validate_exclude_limit_option ⇒ Object private
- #validate_lsp_and_editor_mode ⇒ Object private
Constructor Details
permalink #initialize(options) ⇒ OptionsValidator
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of OptionsValidator.
365 366 367 |
# File 'lib/rubocop/options.rb', line 365 def initialize() @options = end |
Class Method Details
permalink .validate_cop_list(names) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Cop name validation must be done later than option parsing, so it’s not called from within Options.
334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/rubocop/options.rb', line 334 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 Method Details
permalink #boolean_or_empty_cache? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
486 487 488 |
# File 'lib/rubocop/options.rb', line 486 def boolean_or_empty_cache? !@options.key?(:cache) || %w[true false].include?(@options[:cache]) end |
permalink #disable_parallel_when_invalid_option_combo ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/rubocop/options.rb', line 456 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 |
permalink #except_syntax? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
482 483 484 |
# File 'lib/rubocop/options.rb', line 482 def except_syntax? @options.key?(:except) && (@options[:except] & %w[Lint/Syntax Syntax]).any? end |
permalink #incompatible_options ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
490 491 492 |
# File 'lib/rubocop/options.rb', line 490 def @incompatible_options ||= @options.keys & Options::EXITING_OPTIONS end |
permalink #invalid_arguments_for_parallel ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
469 470 471 472 473 474 475 |
# File 'lib/rubocop/options.rb', line 469 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 |
permalink #only_includes_redundant_disable? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
477 478 479 480 |
# File 'lib/rubocop/options.rb', line 477 def only_includes_redundant_disable? @options.key?(:only) && (@options[:only] & %w[Lint/RedundantCopDisableDirective RedundantCopDisableDirective]).any? end |
permalink #validate_auto_gen_config ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
rubocop:enable Metrics/AbcSize
397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/rubocop/options.rb', line 397 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 |
permalink #validate_autocorrect ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
442 443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/rubocop/options.rb', line 442 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, '--disable-uncorrectable can only be used together with --autocorrect.' end |
permalink #validate_cache_enabled_for_cache_root ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
502 503 504 505 506 |
# File 'lib/rubocop/options.rb', line 502 def validate_cache_enabled_for_cache_root return unless @options[:cache] == 'false' raise OptionArgumentError, '--cache-root cannot be used with --cache false' end |
permalink #validate_compatibility ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
rubocop:disable Metrics/AbcSize
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/rubocop/options.rb', line 374 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 |
permalink #validate_cop_options ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
369 370 371 |
# File 'lib/rubocop/options.rb', line 369 def %i[only except].each { |opt| OptionsValidator.validate_cop_list(@options[opt]) } end |
permalink #validate_display_only_correctable_and_autocorrect ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
418 419 420 421 422 423 424 425 |
# File 'lib/rubocop/options.rb', line 418 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 |
permalink #validate_display_only_failed ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
410 411 412 413 414 415 416 |
# File 'lib/rubocop/options.rb', line 410 def validate_display_only_failed return unless @options.key?(:display_only_failed) return if @options[:format] == 'junit' raise OptionArgumentError, '--display-only-failed can only be used together with --format junit.' end |
permalink #validate_display_only_failed_and_display_only_correctable ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
427 428 429 430 431 432 433 434 |
# File 'lib/rubocop/options.rb', line 427 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, '--display-only-failed cannot be used together with other display options.' end |
permalink #validate_exclude_limit_option ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
494 495 496 497 498 499 500 |
# File 'lib/rubocop/options.rb', line 494 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 |
permalink #validate_lsp_and_editor_mode ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
436 437 438 439 440 |
# File 'lib/rubocop/options.rb', line 436 def validate_lsp_and_editor_mode return if !@options.key?(:lsp) || !@options.key?(:editor_mode) raise OptionArgumentError, 'Do not specify `--editor-mode` as it is redundant in `--lsp`.' end |