Class: RuboCop::OptionsValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/options.rb

Overview

Validates option arguments and the options’ compatibility with each other.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ OptionsValidator



262
263
264
# File 'lib/rubocop/options.rb', line 262

def initialize(options)
  @options = options
end

Class Method Details

.validate_cop_list(names) ⇒ Object

Cop name validation must be done later than option parsing, so it’s not called from within Options.



231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/rubocop/options.rb', line 231

def validate_cop_list(names)
  return unless names

  cop_names = Cop::Cop.registry.names
  departments = Cop::Cop.registry.departments.map(&:to_s)

  names.each do |name|
    next if cop_names.include?(name)
    next if departments.include?(name)
    next if %w[Syntax Lint/Syntax].include?(name)

    raise IncorrectCopNameError, format_message_from(name, cop_names)
  end
end

Instance Method Details

#boolean_or_empty_cache?Boolean



364
365
366
# File 'lib/rubocop/options.rb', line 364

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

#display_only_fail_level_offenses_with_autocorrect?Boolean



355
356
357
# File 'lib/rubocop/options.rb', line 355

def display_only_fail_level_offenses_with_autocorrect?
  @options[:display_only_fail_level_offenses] && @options[:autocorrect]
end

#except_syntax?Boolean



359
360
361
362
# File 'lib/rubocop/options.rb', line 359

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

#incompatible_optionsObject



368
369
370
# File 'lib/rubocop/options.rb', line 368

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

#only_includes_redundant_disable?Boolean



349
350
351
352
353
# File 'lib/rubocop/options.rb', line 349

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

#validate_auto_correctObject



314
315
316
317
318
319
320
321
# File 'lib/rubocop/options.rb', line 314

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

  raise OptionArgumentError,
        format('--%<flag>s can only be used together with --auto-correct.',
               flag: '--disable-uncorrectable')
end

#validate_auto_gen_configObject

rubocop:enable Metrics/AbcSize



300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/rubocop/options.rb', line 300

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 no_offense_counts no_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_compatibilityObject

rubocop:disable Metrics/AbcSize



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/rubocop/options.rb', line 273

def validate_compatibility # rubocop:disable Metrics/MethodLength
  if only_includes_redundant_disable?
    raise OptionArgumentError, 'Lint/RedundantCopDisableDirective cannot ' \
                               'be used with --only.'
  end
  if except_syntax?
    raise OptionArgumentError, 'Syntax checking cannot be turned off.'
  end
  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_parallel

  return if incompatible_options.size <= 1

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

#validate_cop_optionsObject



266
267
268
269
270
# File 'lib/rubocop/options.rb', line 266

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

#validate_exclude_limit_optionObject

Raises:

  • (OptionParser::MissingArgument)


372
373
374
375
376
377
378
# File 'lib/rubocop/options.rb', line 372

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_parallelObject



323
324
325
326
327
328
329
330
331
332
333
# File 'lib/rubocop/options.rb', line 323

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

  if @options[:cache] == 'false'
    raise OptionArgumentError, '-P/--parallel uses caching to speed up ' \
                               'execution, so combining with --cache ' \
                               'false is not allowed.'
  end

  validate_parallel_with_combo_option
end

#validate_parallel_with_combo_optionObject



335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/rubocop/options.rb', line 335

def validate_parallel_with_combo_option
  combos = {
    auto_gen_config: '-P/--parallel uses caching to speed up execution, ' \
                     'while --auto-gen-config needs a non-cached run, ' \
                     'so they cannot be combined.',
    fail_fast: '-P/--parallel cannot be combined with -F/--fail-fast.',
    auto_correct: '-P/--parallel cannot be combined with --auto-correct.'
  }

  combos.each do |key, msg|
    raise OptionArgumentError, msg if @options.key?(key)
  end
end