Skip to content

Commit

Permalink
Merge pull request #219 from manuelpuyol/master
Browse files Browse the repository at this point in the history
Add JSON formatter
  • Loading branch information
rafaelfranca authored Jul 26, 2021
2 parents c79ef70 + 10701ac commit 7f67381
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 1 deletion.
72 changes: 72 additions & 0 deletions lib/erb_lint/reporters/json_reporter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true

require "json"

module ERBLint
module Reporters
class JsonReporter < Reporter
def preview; end

def show
puts formatted_data
end

private

def formatted_data
{
metadata: metadata,
files: formatted_files,
summary: summary,
}.to_json
end

def metadata
{
erb_lint_version: ERBLint::VERSION,
ruby_engine: RUBY_ENGINE,
ruby_version: RUBY_VERSION,
ruby_patchlevel: RUBY_PATCHLEVEL.to_s,
ruby_platform: RUBY_PLATFORM,
}
end

def summary
{
offenses: stats.found,
inspected_files: stats.processed_files.size,
corrected: stats.corrected,
}
end

def formatted_files
processed_files.map do |filename, offenses|
{
path: filename,
offenses: formatted_offenses(offenses),
}
end
end

def formatted_offenses(offenses)
offenses.map do |offense|
format_offense(offense)
end
end

def format_offense(offense)
{
linter: offense.linter.class.simple_name,
message: offense.message.to_s,
location: {
start_line: offense.line_number,
start_column: offense.column,
last_line: offense.source_range.last_line,
last_column: offense.source_range.last_column,
length: offense.source_range.length,
},
}
end
end
end
end
3 changes: 2 additions & 1 deletion spec/erb_lint/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def run(_processed_source)

it 'shows format instructions' do
expect { subject }.to(
output(/Report offenses in the given format: \(compact, multiline\) \(default: multiline\)/).to_stdout
output(/Report offenses in the given format: \(compact, json, multiline\) \(default: multiline\)/).to_stdout
)
end

Expand Down Expand Up @@ -319,6 +319,7 @@ def run(_processed_source)
expect { subject }.to(output(Regexp.new(Regexp.escape(<<~EOF.strip))).to_stderr)
nonexistentformat: is not a valid format. Available formats:
- compact
- json
- multiline
EOF
end
Expand Down
98 changes: 98 additions & 0 deletions spec/erb_lint/reporters/json_reporter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# frozen_string_literal: true

require 'spec_helper'

describe ERBLint::Reporters::JsonReporter do
describe '.show' do
subject { described_class.new(stats, false).show }

let(:stats) do
ERBLint::Stats.new(
found: 2,
processed_files: {
'app/views/subscriptions/_loader.html.erb' => offenses,
},
corrected: 1
)
end

let(:offenses) do
[
instance_double(
ERBLint::Offense,
message: 'Extra space detected where there should be no space.',
line_number: 1,
column: 7,
source_range: instance_double(
BetterHtml::Tokenizer::Location,
last_line: 1,
last_column: 9,
length: 2,
),
linter: ERBLint::Linters::SpaceInHtmlTag.new(nil, ERBLint::LinterConfig.new),
),
instance_double(
ERBLint::Offense,
message: 'Remove newline before `%>` to match start of tag.',
line_number: 52,
column: 10,
source_range: instance_double(
BetterHtml::Tokenizer::Location,
last_line: 54,
last_column: 10,
length: 10,
),
linter: ERBLint::Linters::ClosingErbTagIndent.new(nil, ERBLint::LinterConfig.new),
),
]
end

let(:expected_hash) do
{
metadata: {
erb_lint_version: ERBLint::VERSION,
ruby_engine: RUBY_ENGINE,
ruby_version: RUBY_VERSION,
ruby_patchlevel: RUBY_PATCHLEVEL.to_s,
ruby_platform: RUBY_PLATFORM,
},
files: [{
path: 'app/views/subscriptions/_loader.html.erb',
offenses: [
{
linter: 'SpaceInHtmlTag',
message: 'Extra space detected where there should be no space.',
location: {
start_line: 1,
start_column: 7,
last_line: 1,
last_column: 9,
length: 2,
},
},
{
linter: 'ClosingErbTagIndent',
message: 'Remove newline before `%>` to match start of tag.',
location: {
start_line: 52,
start_column: 10,
last_line: 54,
last_column: 10,
length: 10,
},
},
],
}],
summary: {
offenses: 2,
inspected_files: 1,
corrected: 1,
},
}
end

it 'displays formatted offenses output' do
expect { subject }.to(output(expected_hash.to_json + "\n").to_stdout)
end
end
end

0 comments on commit 7f67381

Please sign in to comment.