-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from andersonkrs/add-custom-formatters
Add --format option
- Loading branch information
Showing
11 changed files
with
385 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
require 'active_support/core_ext/class' | ||
|
||
module ERBLint | ||
class Reporter | ||
def self.create_reporter(format, *args) | ||
reporter_klass = "#{ERBLint::Reporters}::#{format.to_s.camelize}Reporter".constantize | ||
reporter_klass.new(*args) | ||
end | ||
|
||
def self.available_format?(format) | ||
available_formats.include?(format.to_s) | ||
end | ||
|
||
def self.available_formats | ||
descendants | ||
.map(&:to_s) | ||
.map(&:demodulize) | ||
.map(&:underscore) | ||
.map { |klass_name| klass_name.sub("_reporter", "") } | ||
.sort | ||
end | ||
|
||
def initialize(stats, autocorrect) | ||
@stats = stats | ||
@autocorrect = autocorrect | ||
end | ||
|
||
def preview; end | ||
|
||
def show; end | ||
|
||
private | ||
|
||
attr_reader :stats, :autocorrect | ||
delegate :processed_files, to: :stats | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
module ERBLint | ||
module Reporters | ||
class CompactReporter < Reporter | ||
def preview | ||
puts "Linting #{stats.files} files with "\ | ||
"#{stats.linters} #{'autocorrectable ' if autocorrect}linters..." | ||
end | ||
|
||
def show | ||
processed_files.each do |filename, offenses| | ||
offenses.each do |offense| | ||
puts format_offense(filename, offense) | ||
end | ||
end | ||
|
||
footer | ||
summary | ||
end | ||
|
||
private | ||
|
||
def format_offense(filename, offense) | ||
[ | ||
"#{filename}:", | ||
"#{offense.line_number}:", | ||
"#{offense.column}: ", | ||
offense.message.to_s, | ||
].join | ||
end | ||
|
||
def footer; end | ||
|
||
def summary | ||
if stats.corrected > 0 | ||
report_corrected_offenses | ||
elsif stats.found > 0 | ||
warn(Rainbow("#{stats.found} error(s) were found in ERB files").red) | ||
else | ||
puts 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 | ||
puts Rainbow("#{stats.corrected} error(s) corrected in ERB files").green | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
require_relative "compact_reporter" | ||
|
||
module ERBLint | ||
module Reporters | ||
class MultilineReporter < CompactReporter | ||
private | ||
|
||
def format_offense(filename, offense) | ||
<<~EOF | ||
#{offense.message}#{Rainbow(' (not autocorrected)').red if autocorrect} | ||
In file: #{filename}:#{offense.line_number} | ||
EOF | ||
end | ||
|
||
def footer | ||
puts | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
module ERBLint | ||
class Stats | ||
attr_accessor :found, | ||
:corrected, | ||
:exceptions, | ||
:linters, | ||
:files, | ||
:processed_files | ||
|
||
def initialize( | ||
found: 0, | ||
corrected: 0, | ||
exceptions: 0, | ||
linters: 0, | ||
files: 0, | ||
processed_files: {} | ||
) | ||
@found = found | ||
@corrected = corrected | ||
@exceptions = exceptions | ||
@linters = linters | ||
@files = files | ||
@processed_files = processed_files | ||
end | ||
end | ||
end |
Oops, something went wrong.