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

Returns a new instance of OptionsValidator.



229
230
231
# File 'lib/rubocop/options.rb', line 229

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.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/rubocop/options.rb', line 195

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

Returns:

  • (Boolean)


295
296
297
# File 'lib/rubocop/options.rb', line 295

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

#except_syntax?Boolean

Returns:

  • (Boolean)


290
291
292
293
# File 'lib/rubocop/options.rb', line 290

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

#incompatible_optionsObject



299
300
301
# File 'lib/rubocop/options.rb', line 299

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

#only_includes_unneeded_disable?Boolean

Returns:

  • (Boolean)


284
285
286
287
288
# File 'lib/rubocop/options.rb', line 284

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

#validate_auto_gen_configObject



252
253
254
255
256
257
258
259
260
261
262
# File 'lib/rubocop/options.rb', line 252

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].each do |option|
    if @options.key?(option)
      raise ArgumentError, format(message, flag: option.to_s.tr('_', '-'))
    end
  end
end

#validate_compatibilityObject

rubocop:disable Metrics/MethodLength

Raises:

  • (ArgumentError)


233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/rubocop/options.rb', line 233

def validate_compatibility # rubocop:disable Metrics/MethodLength
  if only_includes_unneeded_disable?
    raise ArgumentError, 'Lint/UnneededCopDisableDirective can not ' \
                         'be used with --only.'
  end
  if except_syntax?
    raise ArgumentError, 'Syntax checking can not be turned off.'
  end
  unless boolean_or_empty_cache?
    raise ArgumentError, '-C/--cache argument must be true or false'
  end
  validate_auto_gen_config
  validate_parallel

  return if incompatible_options.size <= 1
  raise ArgumentError, 'Incompatible cli options: ' \
                       "#{incompatible_options.inspect}"
end

#validate_exclude_limit_optionObject

Raises:

  • (OptionParser::MissingArgument)


303
304
305
306
307
308
# File 'lib/rubocop/options.rb', line 303

def validate_exclude_limit_option
  return if @options[:exclude_limit] =~ /^\d+$/
  # Emulate OptionParser's behavior to make failures consistent regardless
  # of option order.
  raise OptionParser::MissingArgument
end

#validate_parallelObject



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/rubocop/options.rb', line 264

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

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

  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 can not be combined with -F/--fail-fast.',
    auto_correct: '-P/--parallel can not be combined with --auto-correct.'
  }

  combos.each { |key, msg| raise ArgumentError, msg if @options.key?(key) }
end