Class: RuboCop::OptionsValidator Private

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.



397
398
399
# File 'lib/rubocop/options.rb', line 397

def initialize(options)
  @options = 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.



366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/rubocop/options.rb', line 366

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, format_message_from(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.

Returns:

  • (Boolean)


540
541
542
# File 'lib/rubocop/options.rb', line 540

def boolean_or_empty_cache?
  !@options.key?(:cache) || %w[true false].include?(@options[:cache])
end

#disable_parallel_when_invalid_option_comboObject

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.



511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/rubocop/options.rb', line 511

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

#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.

Returns:

  • (Boolean)


536
537
538
# File 'lib/rubocop/options.rb', line 536

def except_syntax?
  @options.key?(:except) && (@options[:except] & %w[Lint/Syntax Syntax]).any?
end

#incompatible_optionsObject

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.



544
545
546
# File 'lib/rubocop/options.rb', line 544

def incompatible_options
  @incompatible_options ||= @options.keys & Options::EXITING_OPTIONS
end

#invalid_arguments_for_parallelObject

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.



524
525
526
527
528
529
# File 'lib/rubocop/options.rb', line 524

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.

Returns:

  • (Boolean)


531
532
533
534
# File 'lib/rubocop/options.rb', line 531

def only_includes_redundant_disable?
  @options.key?(:only) &&
    (@options[:only] & %w[Lint/RedundantCopDisableDirective RedundantCopDisableDirective]).any?
end

#validate_auto_gen_configObject

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.



431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/rubocop/options.rb', line 431

def validate_auto_gen_config
  return if @options.key?(:auto_gen_config)

  message = '--%<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(message, flag: option.to_s.tr('_', '-'))
    end
  end
end

#validate_autocorrectObject

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.



497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'lib/rubocop/options.rb', line 497

def validate_autocorrect
  if @options.key?(:safe_autocorrect) && @options.key?(:autocorrect_all)
    message = Rainbow(<<~MESSAGE).red
      Error: Both safe and unsafe autocorrect options are specified, use only one.
    MESSAGE
    raise OptionArgumentError, message
  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_rootObject

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.



567
568
569
570
571
# File 'lib/rubocop/options.rb', line 567

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_stdinObject

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.



478
479
480
481
482
# File 'lib/rubocop/options.rb', line 478

def validate_changed_and_stdin
  return unless @options.key?(:changed) && @options.key?(:stdin)

  raise OptionArgumentError, '--changed cannot be used with --stdin.'
end

#validate_changed_revision(revision) ⇒ 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.

OptionParser hands --changed lib/ the path as the revision, which is a natural thing to type, so point at the fix rather than at git's error.



550
551
552
553
554
555
556
557
# File 'lib/rubocop/options.rb', line 550

def validate_changed_revision(revision)
  return unless revision && File.exist?(revision)

  raise OptionArgumentError,
        "--changed takes a git revision, but `#{revision}` is a path. Write the revision " \
        "as `--changed=#{revision}` if that is really what you meant, or drop it to " \
        'compare against HEAD.'
end

#validate_compatibilityObject

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



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/rubocop/options.rb', line 406

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 incompatible_options.size <= 1

  raise OptionArgumentError, "Incompatible cli options: #{incompatible_options.inspect}"
end

#validate_cop_optionsObject

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.



401
402
403
# File 'lib/rubocop/options.rb', line 401

def validate_cop_options
  %i[only except].each { |opt| OptionsValidator.validate_cop_list(@options[opt]) }
end

#validate_diffObject

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.



472
473
474
475
476
# File 'lib/rubocop/options.rb', line 472

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_autocorrectObject

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.



452
453
454
455
456
457
458
459
# File 'lib/rubocop/options.rb', line 452

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_failedObject

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.



444
445
446
447
448
449
450
# File 'lib/rubocop/options.rb', line 444

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_correctableObject

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.



461
462
463
464
465
466
467
468
# File 'lib/rubocop/options.rb', line 461

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_copsObject

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
493
494
495
# File 'lib/rubocop/options.rb', line 490

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_optionObject

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.

Raises:

  • (OptionParser::MissingArgument)


559
560
561
562
563
564
565
# File 'lib/rubocop/options.rb', line 559

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_modeObject

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.



484
485
486
487
488
# File 'lib/rubocop/options.rb', line 484

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