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.



293
294
295
# File 'lib/rubocop/options.rb', line 293

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.



262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/rubocop/options.rb', line 262

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)


412
413
414
# File 'lib/rubocop/options.rb', line 412

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.



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/rubocop/options.rb', line 376

def disable_parallel_when_invalid_option_combo
  return unless @options.key?(:parallel)

  invalid_options = [
    { name: :auto_gen_config, value: true, flag: '--auto-gen-config' },
    { name: :fail_fast, value: true, flag: '-F/--fail-fast.' },
    { name: :auto_correct, value: true, flag: '--auto-correct.' },
    { name: :cache, value: 'false', flag: '--cache false' }
  ]

  invalid_flags = invalid_options.each_with_object([]) do |option, flags|
    # `>` rather than `>=` because `@options` will also contain `parallel: true`
    flags << option[:flag] if @options > { option[:name] => option[:value] }
  end

  return if invalid_flags.empty?

  @options.delete(:parallel)

  puts '-P/--parallel is being ignored because ' \
       "it is not compatible with #{invalid_flags.join(', ')}"
end

#display_only_fail_level_offenses_with_autocorrect?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)


404
405
406
# File 'lib/rubocop/options.rb', line 404

def display_only_fail_level_offenses_with_autocorrect?
  @options[:display_only_fail_level_offenses] && @options[:autocorrect]
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)


408
409
410
# File 'lib/rubocop/options.rb', line 408

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.



416
417
418
# File 'lib/rubocop/options.rb', line 416

def incompatible_options
  @incompatible_options ||= @options.keys & Options::EXITING_OPTIONS
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)


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

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

#validate_auto_correctObject

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.



368
369
370
371
372
373
374
# File 'lib/rubocop/options.rb', line 368

def validate_auto_correct
  return if @options.key?(:auto_correct)
  return unless @options.key?(:disable_uncorrectable)

  raise OptionArgumentError,
        format('--disable-uncorrectable can only be used together with --auto-correct.')
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.

rubocop:enable Metrics/AbcSize



329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/rubocop/options.rb', line 329

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



428
429
430
431
432
# File 'lib/rubocop/options.rb', line 428

def validate_cache_enabled_for_cache_root
  return unless @options[:cache] == 'false'

  raise OptionArgumentError, '--cache-root cannot be used with --cache false'
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 Metrics/AbcSize



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/rubocop/options.rb', line 302

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

  if display_only_fail_level_offenses_with_autocorrect?
    raise OptionArgumentError, '--autocorrect cannot be used with ' \
                               '--display-only-fail-level-offenses'
  end

  validate_auto_gen_config
  validate_auto_correct
  validate_display_only_failed
  validate_display_only_failed_and_display_only_correctable
  validate_display_only_correctable_and_auto_correct
  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.



297
298
299
# File 'lib/rubocop/options.rb', line 297

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

#validate_display_only_correctable_and_auto_correctObject

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.



350
351
352
353
354
355
356
357
# File 'lib/rubocop/options.rb', line 350

def validate_display_only_correctable_and_auto_correct
  return if !@options.key?(:safe_auto_correct) && !@options.key?(:auto_correct)
  return if !@options.key?(:display_only_correctable) &&
            !@options.key?(:display_only_safe_correctable)

  raise OptionArgumentError,
        '--auto-correct 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.



342
343
344
345
346
347
348
# File 'lib/rubocop/options.rb', line 342

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



359
360
361
362
363
364
365
366
# File 'lib/rubocop/options.rb', line 359

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_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)


420
421
422
423
424
425
426
# File 'lib/rubocop/options.rb', line 420

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