Skip to content

Commit 7f67381

Browse files
authored
Merge pull request #219 from manuelpuyol/master
Add JSON formatter
2 parents c79ef70 + 10701ac commit 7f67381

3 files changed

Lines changed: 172 additions & 1 deletion

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# frozen_string_literal: true
2+
3+
require "json"
4+
5+
module ERBLint
6+
module Reporters
7+
class JsonReporter < Reporter
8+
def preview; end
9+
10+
def show
11+
puts formatted_data
12+
end
13+
14+
private
15+
16+
def formatted_data
17+
{
18+
metadata: metadata,
19+
files: formatted_files,
20+
summary: summary,
21+
}.to_json
22+
end
23+
24+
def metadata
25+
{
26+
erb_lint_version: ERBLint::VERSION,
27+
ruby_engine: RUBY_ENGINE,
28+
ruby_version: RUBY_VERSION,
29+
ruby_patchlevel: RUBY_PATCHLEVEL.to_s,
30+
ruby_platform: RUBY_PLATFORM,
31+
}
32+
end
33+
34+
def summary
35+
{
36+
offenses: stats.found,
37+
inspected_files: stats.processed_files.size,
38+
corrected: stats.corrected,
39+
}
40+
end
41+
42+
def formatted_files
43+
processed_files.map do |filename, offenses|
44+
{
45+
path: filename,
46+
offenses: formatted_offenses(offenses),
47+
}
48+
end
49+
end
50+
51+
def formatted_offenses(offenses)
52+
offenses.map do |offense|
53+
format_offense(offense)
54+
end
55+
end
56+
57+
def format_offense(offense)
58+
{
59+
linter: offense.linter.class.simple_name,
60+
message: offense.message.to_s,
61+
location: {
62+
start_line: offense.line_number,
63+
start_column: offense.column,
64+
last_line: offense.source_range.last_line,
65+
last_column: offense.source_range.last_column,
66+
length: offense.source_range.length,
67+
},
68+
}
69+
end
70+
end
71+
end
72+
end

spec/erb_lint/cli_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def run(_processed_source)
7676

7777
it 'shows format instructions' do
7878
expect { subject }.to(
79-
output(/Report offenses in the given format: \(compact, multiline\) \(default: multiline\)/).to_stdout
79+
output(/Report offenses in the given format: \(compact, json, multiline\) \(default: multiline\)/).to_stdout
8080
)
8181
end
8282

@@ -319,6 +319,7 @@ def run(_processed_source)
319319
expect { subject }.to(output(Regexp.new(Regexp.escape(<<~EOF.strip))).to_stderr)
320320
nonexistentformat: is not a valid format. Available formats:
321321
- compact
322+
- json
322323
- multiline
323324
EOF
324325
end
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe ERBLint::Reporters::JsonReporter do
6+
describe '.show' do
7+
subject { described_class.new(stats, false).show }
8+
9+
let(:stats) do
10+
ERBLint::Stats.new(
11+
found: 2,
12+
processed_files: {
13+
'app/views/subscriptions/_loader.html.erb' => offenses,
14+
},
15+
corrected: 1
16+
)
17+
end
18+
19+
let(:offenses) do
20+
[
21+
instance_double(
22+
ERBLint::Offense,
23+
message: 'Extra space detected where there should be no space.',
24+
line_number: 1,
25+
column: 7,
26+
source_range: instance_double(
27+
BetterHtml::Tokenizer::Location,
28+
last_line: 1,
29+
last_column: 9,
30+
length: 2,
31+
),
32+
linter: ERBLint::Linters::SpaceInHtmlTag.new(nil, ERBLint::LinterConfig.new),
33+
),
34+
instance_double(
35+
ERBLint::Offense,
36+
message: 'Remove newline before `%>` to match start of tag.',
37+
line_number: 52,
38+
column: 10,
39+
source_range: instance_double(
40+
BetterHtml::Tokenizer::Location,
41+
last_line: 54,
42+
last_column: 10,
43+
length: 10,
44+
),
45+
linter: ERBLint::Linters::ClosingErbTagIndent.new(nil, ERBLint::LinterConfig.new),
46+
),
47+
]
48+
end
49+
50+
let(:expected_hash) do
51+
{
52+
metadata: {
53+
erb_lint_version: ERBLint::VERSION,
54+
ruby_engine: RUBY_ENGINE,
55+
ruby_version: RUBY_VERSION,
56+
ruby_patchlevel: RUBY_PATCHLEVEL.to_s,
57+
ruby_platform: RUBY_PLATFORM,
58+
},
59+
files: [{
60+
path: 'app/views/subscriptions/_loader.html.erb',
61+
offenses: [
62+
{
63+
linter: 'SpaceInHtmlTag',
64+
message: 'Extra space detected where there should be no space.',
65+
location: {
66+
start_line: 1,
67+
start_column: 7,
68+
last_line: 1,
69+
last_column: 9,
70+
length: 2,
71+
},
72+
},
73+
{
74+
linter: 'ClosingErbTagIndent',
75+
message: 'Remove newline before `%>` to match start of tag.',
76+
location: {
77+
start_line: 52,
78+
start_column: 10,
79+
last_line: 54,
80+
last_column: 10,
81+
length: 10,
82+
},
83+
},
84+
],
85+
}],
86+
summary: {
87+
offenses: 2,
88+
inspected_files: 1,
89+
corrected: 1,
90+
},
91+
}
92+
end
93+
94+
it 'displays formatted offenses output' do
95+
expect { subject }.to(output(expected_hash.to_json + "\n").to_stdout)
96+
end
97+
end
98+
end

0 commit comments

Comments
 (0)