Class: HamlLint::RubocopConfigStore

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

Overview

To handle our need to force some configurations on RuboCop, while still allowing users to customize most of RuboCop using their own rubocop.yml config(s), we need to detect the effective RuboCop configuration for a specific file, and generate a new configuration containing our own “forced configuration” with a ‘inherit_from` that points on the user’s configuration.

This class handles all of this logic.

Instance Method Summary collapse

Constructor Details

#initializeRubocopConfigStore

Returns a new instance of RubocopConfigStore.



379
380
381
382
# File 'lib/haml_lint/linter/rubocop.rb', line 379

def initialize
  @dir_path_to_user_config_path = {}
  @user_config_path_to_config_object = {}
end

Instance Method Details

#config_object_pointing_to(user_config_path) ⇒ Object

Build a RuboCop::Config from config/forced_rubocop_config.yml which inherits from the given user_config_path and return it’s path.



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/haml_lint/linter/rubocop.rb', line 386

def config_object_pointing_to(user_config_path)
  if @user_config_path_to_config_object[user_config_path]
    return @user_config_path_to_config_object[user_config_path]
  end

  final_config_hash = forced_rubocop_config_hash.dup

  if user_config_path != ::RuboCop::ConfigLoader::DEFAULT_FILE
    # If we manually inherit from the default RuboCop config, we may get warnings
    # for deprecated stuff that is in it. We don't when we automatically
    # inherit from it (which always happens)
    final_config_hash['inherit_from'] = user_config_path
  end

  config_object = Tempfile.create(['.haml-lint-rubocop', '.yml']) do |tempfile|
    tempfile.write(final_config_hash.to_yaml)
    tempfile.close
    ::RuboCop::ConfigLoader.configuration_from_file(tempfile.path)
  end

  @user_config_path_to_config_object[user_config_path] = config_object
end

#forced_rubocop_config_hashObject

Returns the content (Hash) of config/forced_rubocop_config.yml after processing it’s ERB content. Cached since it doesn’t change between files



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/haml_lint/linter/rubocop.rb', line 422

def forced_rubocop_config_hash
  return @forced_rubocop_config_hash if @forced_rubocop_config_hash

  content = File.read(File.join(HamlLint::HOME, 'config', 'forced_rubocop_config.yml'))
  processed_content = HamlLint::Utils.process_erb(content)
  hash = YAML.safe_load(processed_content)

  if ENV['HAML_LINT_TESTING']
    # In newer RuboCop versions, new cops are not enabled by default, and instead
    # show a message until they are used. We just want a default for them
    # to avoid spamming STDOUT. Making it "disable" reduces the chances of having
    # the test suite start failing after a new cop gets added.
    hash['AllCops'] ||= {}
    if Gem::Version.new(::RuboCop::Version::STRING) >= Gem::Version.new('1')
      hash['AllCops']['NewCops'] = 'disable'
    end
  end

  @forced_rubocop_config_hash = hash.freeze
end

#user_rubocop_config_path_for(path) ⇒ Object

Find the path to the effective RuboCop configuration for a path (file or dir)



410
411
412
413
414
415
416
417
418
# File 'lib/haml_lint/linter/rubocop.rb', line 410

def user_rubocop_config_path_for(path)
  dir = if File.directory?(path)
          path
        else
          File.dirname(path)
        end

  @dir_path_to_user_config_path[dir] ||= ::RuboCop::ConfigLoader.configuration_file_for(dir)
end