class HamlLint::Reporter::DisabledConfigReporter

Outputs a YAML configuration file based on existing violations.

Constants

HEADING

Attributes

exclude_limit[R]

Number of offenses to allow before simply disabling the linter

@return [Integer] file exclude limit

linters_lint_count[R]

A hash of linters with the files that have that type of lint.

@return [Hash<String, Integer] a Hash with linter name keys and lint

count values
linters_with_lints[R]

A hash of linters with the files that have that type of lint.

@return [Hash<String, Array<String>>] a Hash with linter name keys and file

name list values

Public Class Methods

available?() click to toggle source

Disables this reporter on the CLI since it doesn't output anything.

@return [false]

# File lib/haml_lint/reporter/disabled_config_reporter.rb, line 20
def self.available?
  false
end
new(log, limit: 15) click to toggle source

Create the reporter that will display the report and write the config.

@param _log [HamlLint::Logger]

Calls superclass method HamlLint::Reporter.new
# File lib/haml_lint/reporter/disabled_config_reporter.rb, line 27
def initialize(log, limit: 15)
  super(log)
  @linters_with_lints = Hash.new { |hash, key| hash[key] = [] }
  @linters_lint_count = Hash.new(0)
  @exclude_limit = limit
end

Public Instance Methods

display_report(report) click to toggle source

Prints the standard progress reporter output and writes the new config file.

@param report [HamlLint::Report] @return [void]

# File lib/haml_lint/reporter/disabled_config_reporter.rb, line 55
def display_report(report)
  super

  File.write(ConfigurationLoader::AUTO_GENERATED_FILE, config_file_contents)
  log.log "Created #{ConfigurationLoader::AUTO_GENERATED_FILE}."
  log.log "Run `haml-lint --config #{ConfigurationLoader::AUTO_GENERATED_FILE}`" \
    ", or add `inherits_from: #{ConfigurationLoader::AUTO_GENERATED_FILE}` in a " \
    '.haml-lint.yml file.'
end
finished_file(file, lints) click to toggle source

Prints the standard progress report marks and tracks files with lint.

@param file [String] @param lints [Array<HamlLint::Lint>] @return [void]

# File lib/haml_lint/reporter/disabled_config_reporter.rb, line 70
def finished_file(file, lints)
  super

  if lints.any?
    lints.each do |lint|
      linters_with_lints[lint.linter.name] |= [lint.filename]
      linters_lint_count[lint.linter.name] += 1
    end
  end
end

Private Instance Methods

config_file_contents() click to toggle source

The contents of the generated configuration file based on captured lint.

@return [String] a Yaml-formatted configuration file's contents

# File lib/haml_lint/reporter/disabled_config_reporter.rb, line 86
def config_file_contents
  output = []
  output << HEADING
  output << 'linters:' if linters_with_lints.any?
  linters_with_lints.each do |linter, files|
    output << generate_config_for_linter(linter, files)
  end
  output.join("\n\n")
end
generate_config_for_linter(linter, files) click to toggle source

Constructs the configuration for excluding a linter in some files.

@param linter [String] the name of the linter to exclude @param files [Array<String>] the files in which the linter is excluded @return [String] a Yaml-formatted configuration

# File lib/haml_lint/reporter/disabled_config_reporter.rb, line 101
def generate_config_for_linter(linter, files)
  [].tap do |output|
    output << "  # Offense count: #{linters_lint_count[linter]}"
    output << "  #{linter}:"
    # disable the linter when there are many files with offenses.
    # exclude the affected files otherwise.
    if files.count > exclude_limit
      output << '    enabled: false'
    else
      output << '    exclude:'
      files.each do |filename|
        output << %{      - "#{filename}"}
      end
    end
  end.join("\n")
end