From 40aad5f8e36bde7e6f847ad18c2b209b913060aa Mon Sep 17 00:00:00 2001 From: Nick Poorman Date: Wed, 26 Jun 2024 11:42:24 -0600 Subject: [PATCH] Added a quiet reporter that directs all reporting data to stderr --- lib/erb_lint/cli.rb | 14 +++-- lib/erb_lint/reporters/quiet_reporter.rb | 75 ++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 lib/erb_lint/reporters/quiet_reporter.rb diff --git a/lib/erb_lint/cli.rb b/lib/erb_lint/cli.rb index 3e6a6b0..c3dff0b 100644 --- a/lib/erb_lint/cli.rb +++ b/lib/erb_lint/cli.rb @@ -80,18 +80,20 @@ def run(args = ARGV) runner = ERBLint::Runner.new(file_loader, @config, @options[:disable_inline_configs]) file_content = nil + out = (@options[:format] == "quiet") ? method(:warn) : method(:puts) + lint_files.each do |filename| runner.clear_offenses begin file_content = run_on_file(runner, filename) rescue => e @stats.exceptions += 1 - puts "Exception occurred when processing: #{relative_filename(filename)}" - puts "If this file cannot be processed by erb-lint, " \ + out.call "Exception occurred when processing: #{relative_filename(filename)}" + out.call "If this file cannot be processed by erb-lint, " \ "you can exclude it in your configuration file." - puts e.message - puts Rainbow(e.backtrace.join("\n")).red - puts + out.call e.message + out.call Rainbow(e.backtrace.join("\n")).red + out.call end end @@ -101,7 +103,7 @@ def run(args = ARGV) if stdin? && autocorrect? # When running from stdin, we only lint a single file - puts "================ #{lint_files.first} ==================\n" + out.call "================ #{lint_files.first} ==================\n" puts file_content end diff --git a/lib/erb_lint/reporters/quiet_reporter.rb b/lib/erb_lint/reporters/quiet_reporter.rb new file mode 100644 index 0000000..d1572d2 --- /dev/null +++ b/lib/erb_lint/reporters/quiet_reporter.rb @@ -0,0 +1,75 @@ +# frozen_string_literal: true + +module ERBLint + module Reporters + class QuietReporter < Reporter + def preview + warn "#{linting} #{stats.files} files with #{linters}..." + end + + def show + processed_files.each do |filename, offenses| + offenses.each do |offense| + warn format_offense(filename, offense) + end + end + + footer + summary + end + + private + + def linting + "Linting" + (autocorrect ? " and autocorrecting" : "") + end + + def linters + "#{stats.linters} linters" + (autocorrect ? " (#{stats.autocorrectable_linters} autocorrectable)" : "") + end + + def format_offense(filename, offense) + [ + "#{filename}:", + "#{offense.line_number}:", + "#{offense.column}: ", + ("[#{offense.simple_name}] " if show_linter_names), + offense.message.to_s + ].compact.join + end + + def footer + end + + def summary + if stats.corrected > 0 + report_corrected_offenses + elsif stats.ignored > 0 || stats.found > 0 + if stats.ignored > 0 + warn(Rainbow("#{stats.ignored} error(s) were ignored in ERB files").yellow) + end + + if stats.found > 0 + warn(Rainbow("#{stats.found} error(s) were found in ERB files").red) + end + else + warn Rainbow("No errors were found in ERB files").green + end + end + + def report_corrected_offenses + corrected_found_diff = stats.found - stats.corrected + + if corrected_found_diff > 0 + message = Rainbow( + "#{stats.corrected} error(s) corrected and #{corrected_found_diff} error(s) remaining in ERB files" + ).red + + warn(message) + else + warn Rainbow("#{stats.corrected} error(s) corrected in ERB files").green + end + end + end + end +end