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.



202
203
204
# File 'lib/rubocop/options.rb', line 202

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.



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/rubocop/options.rb', line 188

def self.validate_cop_list(names)
  return unless names

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

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

    raise ArgumentError, "Unrecognized cop or department: #{name}."
  end
end

Instance Method Details

#boolean_or_empty_cache?Boolean

Returns:

  • (Boolean)


257
258
259
# File 'lib/rubocop/options.rb', line 257

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

#except_syntax?Boolean

Returns:

  • (Boolean)


252
253
254
255
# File 'lib/rubocop/options.rb', line 252

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

#incompatible_optionsObject



265
266
267
# File 'lib/rubocop/options.rb', line 265

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

#no_offense_counts_without_auto_gen_config?Boolean

Returns:

  • (Boolean)


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

def no_offense_counts_without_auto_gen_config?
  @options.key?(:no_offense_counts) && !@options.key?(:auto_gen_config)
end

#only_includes_unneeded_disable?Boolean

Returns:

  • (Boolean)


247
248
249
250
# File 'lib/rubocop/options.rb', line 247

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

#validate_compatibilityObject

rubocop:disable Metrics/MethodLength

Raises:

  • (ArgumentError)


206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/rubocop/options.rb', line 206

def validate_compatibility # rubocop:disable Metrics/MethodLength
  if only_includes_unneeded_disable?
    raise ArgumentError, 'Lint/UnneededDisable 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
  if no_offense_counts_without_auto_gen_config?
    raise ArgumentError, '--no-offense-counts can only be used together ' \
                         'with --auto-gen-config.'
  end
  validate_parallel

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

#validate_exclude_limit_option(args) ⇒ Object

Raises:

  • (ArgumentError)


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

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

  # --exclude-limit is valid if there's a parsed or yet unparsed
  # --auto-gen-config.
  return if @options[:auto_gen_config] || args.include?('--auto-gen-config')

  raise ArgumentError,
        '--exclude-limit can only be used with --auto-gen-config.'
end

#validate_parallelObject



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

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