Class: RuboCop::OptionsValidator

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

Overview

Validates option arguments and the options' compatibilty with each other.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ OptionsValidator

Returns a new instance of OptionsValidator.


160
161
162
# File 'lib/rubocop/options.rb', line 160

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.


166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rubocop/options.rb', line 166

def self.validate_cop_list(names)
  return unless names

  namespaces = Cop::Cop.all.types.map { |t| t.to_s.capitalize }
  names.each do |name|
    next if Cop::Cop.all.any? { |c| c.cop_name == name }
    next if namespaces.include?(name)
    next if %w(Syntax Lint/Syntax).include?(name)

    fail ArgumentError, "Unrecognized cop or namespace: #{name}."
  end
end

Instance Method Details

#validate_compatibilityObject


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rubocop/options.rb', line 179

def validate_compatibility
  if @options.key?(:only) &&
     (@options[:only] & %w(Lint/UnneededDisable UnneededDisable)).any?
    fail ArgumentError, 'Lint/UnneededDisable can not be used with --only.'
  end
  if @options.key?(:except) &&
     (@options[:except] & %w(Lint/Syntax Syntax)).any?
    fail ArgumentError, 'Syntax checking can not be turned off.'
  end
  if @options.key?(:cache) && !%w(true false).include?(@options[:cache])
    fail ArgumentError, '-C/--cache argument must be true or false'
  end
  return if (incompat = @options.keys & Options::EXITING_OPTIONS).size <= 1
  fail ArgumentError, "Incompatible cli options: #{incompat.inspect}"
end

#validate_exclude_limit_option(args) ⇒ Object


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

def validate_exclude_limit_option(args)
  if @options[:exclude_limit] !~ /^\d+$/
    # Emulate OptionParser's behavior to make failures consistent regardless
    # of option order.
    fail 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')

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