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
-
#path_given_to_changed?(revision) ⇒ Boolean
private
OptionParser hands
--changed lib/the path as the revision, which is a natural thing to type, so an argument that names an existing path is taken as one. - #validate_auto_gen_config ⇒ Object private
- #validate_autocorrect ⇒ Object private
- #validate_cache_enabled_for_cache_root ⇒ Object private
- #validate_changed_and_stdin ⇒ Object private
-
#validate_compatibility ⇒ Object
private
rubocop:disable-next Metrics/AbcSize.
- #validate_cop_options ⇒ Object private
-
#validate_diff ⇒ Object
private
--diffpromises not to write anything, and--auto-gen-configexists to write a file. - #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_enable_all_cops_and_disable_all_cops ⇒ Object private
- #validate_exclude_limit_option ⇒ Object private
- #validate_lsp_and_editor_mode ⇒ Object private
- #validate_max_offenses_per_cop_option ⇒ Object private
Constructor Details
#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.
413 414 415 |
# File 'lib/rubocop/options.rb', line 413 def initialize() @options = end |
Class Method Details
.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.
382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/rubocop/options.rb', line 382 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
#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.
558 559 560 |
# File 'lib/rubocop/options.rb', line 558 def boolean_or_empty_cache? !@options.key?(:cache) || %w[true false].include?(@options[:cache]) end |
#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.
527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
# File 'lib/rubocop/options.rb', line 527 def disable_parallel_when_invalid_option_combo return unless @options[:parallel] invalid_flags = invalid_arguments_for_parallel return if invalid_flags.empty? @options.delete(:parallel) # On stderr, so that it can't end up in the middle of a formatter's # output, such as `--format json`. warn '-P/--parallel is being ignored because ' \ "it is not compatible with #{invalid_flags.join(', ')}." end |
#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.
554 555 556 |
# File 'lib/rubocop/options.rb', line 554 def except_syntax? @options.key?(:except) && (@options[:except] & %w[Lint/Syntax Syntax]).any? end |
#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.
562 563 564 |
# File 'lib/rubocop/options.rb', line 562 def @incompatible_options ||= @options.keys & Options::EXITING_OPTIONS end |
#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.
542 543 544 545 546 547 |
# File 'lib/rubocop/options.rb', line 542 def invalid_arguments_for_parallel [('-F/--fail-fast' if @options.key?(:fail_fast)), ('--profile' if @options[:profile]), ('--memory' if @options[:memory]), ('--cache false' if @options > { cache: 'false' })].compact end |
#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.
549 550 551 552 |
# File 'lib/rubocop/options.rb', line 549 def only_includes_redundant_disable? @options.key?(:only) && (@options[:only] & %w[Lint/RedundantCopDisableDirective RedundantCopDisableDirective]).any? end |
#path_given_to_changed?(revision) ⇒ 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.
OptionParser hands --changed lib/ the path as the revision, which is a
natural thing to type, so an argument that names an existing path is
taken as one. When it also names a revision there is no telling which
was meant, and git itself refuses such an argument as ambiguous.
570 571 572 573 574 575 576 577 |
# File 'lib/rubocop/options.rb', line 570 def path_given_to_changed?(revision) return false unless revision && File.exist?(revision) return true unless ChangedFiles.revision?(revision) raise OptionArgumentError, "--changed got `#{revision}`, which is both a path and a git revision. Write " \ "`./#{revision}` for the path, or `--changed=#{revision}^0` for the revision." end |
#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.
447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/rubocop/options.rb', line 447 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 ⇒ 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.
513 514 515 516 517 518 519 520 521 522 523 524 525 |
# File 'lib/rubocop/options.rb', line 513 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 |
#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.
593 594 595 596 597 |
# File 'lib/rubocop/options.rb', line 593 def validate_cache_enabled_for_cache_root return unless @options[:cache] == 'false' raise OptionArgumentError, '--cache-root cannot be used with --cache false' end |
#validate_changed_and_stdin ⇒ 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 |
# File 'lib/rubocop/options.rb', line 494 def validate_changed_and_stdin return unless @options.key?(:changed) && @options.key?(:stdin) raise OptionArgumentError, '--changed cannot be used with --stdin.' end |
#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-next Metrics/AbcSize
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/rubocop/options.rb', line 422 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 validate_diff validate_changed_and_stdin validate_enable_all_cops_and_disable_all_cops disable_parallel_when_invalid_option_combo return if .size <= 1 raise OptionArgumentError, "Incompatible cli options: #{.inspect}" end |
#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.
417 418 419 |
# File 'lib/rubocop/options.rb', line 417 def %i[only except].each { |opt| OptionsValidator.validate_cop_list(@options[opt]) } end |
#validate_diff ⇒ 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.
--diff promises not to write anything, and --auto-gen-config exists to
write a file.
488 489 490 491 492 |
# File 'lib/rubocop/options.rb', line 488 def validate_diff return unless @options.key?(:diff) && @options.key?(:auto_gen_config) raise OptionArgumentError, '--diff cannot be used with --auto-gen-config.' end |
#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.
468 469 470 471 472 473 474 475 |
# File 'lib/rubocop/options.rb', line 468 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 ⇒ 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.
460 461 462 463 464 465 466 |
# File 'lib/rubocop/options.rb', line 460 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 |
#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.
477 478 479 480 481 482 483 484 |
# File 'lib/rubocop/options.rb', line 477 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 |
#validate_enable_all_cops_and_disable_all_cops ⇒ 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.
506 507 508 509 510 511 |
# File 'lib/rubocop/options.rb', line 506 def validate_enable_all_cops_and_disable_all_cops return if !@options.key?(:enable_all_cops) || !@options.key?(:disable_all_cops) raise OptionArgumentError, '--enable-all-cops cannot be used together with --disable-all-cops.' end |
#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.
585 586 587 588 589 590 591 |
# File 'lib/rubocop/options.rb', line 585 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 ⇒ 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.
500 501 502 503 504 |
# File 'lib/rubocop/options.rb', line 500 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 |
#validate_max_offenses_per_cop_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.
579 580 581 582 583 |
# File 'lib/rubocop/options.rb', line 579 def validate_max_offenses_per_cop_option return if /^[1-9]\d*$/.match?(@options[:max_offenses_per_cop]) raise OptionParser::InvalidArgument, '--max-offenses-per-cop must be a positive integer' end |