Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a quiet reporter that directs all reporting data to stderr #357

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/erb_lint/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
75 changes: 75 additions & 0 deletions lib/erb_lint/reporters/quiet_reporter.rb
Original file line number Diff line number Diff line change
@@ -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
Loading