class HamlLint::RubocopConfigStore
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.
Public Class Methods
# File lib/haml_lint/linter/rubocop.rb, line 379 def initialize @dir_path_to_user_config_path = {} @user_config_path_to_config_object = {} end
Public Instance Methods
Build a RuboCop::Config from config/forced_rubocop_config.yml which inherits from the given user_config_path and return it’s path.
# 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
Returns the content (Hash) of config/forced_rubocop_config.yml after processing it’s ERB content. Cached since it doesn’t change between files
# 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
Find the path to the effective RuboCop configuration for a path (file or dir)
# 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