class HamlLint::LinterSelector

Chooses the appropriate linters to run given the specified configuration.

Public Class Methods

new(config, options) click to toggle source

Creates a selector using the given configuration and additional options.

@param config [HamlLint::Configuration] @param options [Hash]

# File lib/haml_lint/linter_selector.rb, line 10
def initialize(config, options)
  @config = config
  @options = options
end

Public Instance Methods

linters_for_file(file) click to toggle source

Returns the set of linters to run against the given file.

@param file [String] @raise [HamlLint::Exceptions::NoLintersError] when no linters are enabled @return [Array<HamlLint::Linter>]

# File lib/haml_lint/linter_selector.rb, line 20
def linters_for_file(file)
  @linters ||= extract_enabled_linters(@config, @options)
  @linters.select { |linter| run_linter_on_file?(@config, linter, file) }
end

Private Instance Methods

extract_enabled_linters(config, options) click to toggle source

Returns a list of linters that are enabled given the specified configuration and additional options.

@param config [HamlLint::Configuration] @param options [Hash] @return [Array<HamlLint::Linter>]

# File lib/haml_lint/linter_selector.rb, line 33
def extract_enabled_linters(config, options)
  included_linters =
    LinterRegistry.extract_linters_from(options.fetch(:included_linters, []))

  included_linters = LinterRegistry.linters if included_linters.empty?

  excluded_linters =
    LinterRegistry.extract_linters_from(options.fetch(:excluded_linters, []))

  # After filtering out explicitly included/excluded linters, only include
  # linters which are enabled in the configuration
  linters = (included_linters - excluded_linters).map do |linter_class|
    linter_config = config.for_linter(linter_class)
    linter_class.new(linter_config) if linter_config['enabled']
  end.compact

  # Highlight condition where all linters were filtered out, as this was
  # likely a mistake on the user's part
  if linters.empty?
    raise HamlLint::Exceptions::NoLintersError, 'No linters specified'
  end

  linters
end
run_linter_on_file?(config, linter, file) click to toggle source

Whether to run the given linter against the specified file.

@param config [HamlLint::Configuration] @param linter [HamlLint::Linter] @param file [String] @return [Boolean]

# File lib/haml_lint/linter_selector.rb, line 64
def run_linter_on_file?(config, linter, file)
  linter_config = config.for_linter(linter)

  if linter_config['include'] &&
     !HamlLint::Utils.any_glob_matches?(linter_config['include'], file)
    return false
  end

  if HamlLint::Utils.any_glob_matches?(linter_config['exclude'], file)
    return false
  end

  true
end