Skip to content

Commit

Permalink
Add linter names option
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Jan 13, 2024
1 parent 0122969 commit 804c838
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 7 deletions.
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 3.2.2
6 changes: 5 additions & 1 deletion lib/erb_lint/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def run(args = ARGV)
@stats.linters = enabled_linter_classes.size
@stats.autocorrectable_linters = enabled_linter_classes.count(&:support_autocorrect?)

reporter = Reporter.create_reporter(@options[:format], @stats, autocorrect?)
reporter = Reporter.create_reporter(@options[:format], @stats, autocorrect?, @options[:show_linter_names])
reporter.preview

runner = ERBLint::Runner.new(file_loader, @config, @options[:disable_inline_configs])
Expand Down Expand Up @@ -394,6 +394,10 @@ def option_parser
opts.on_tail("--version", "Show version") do
success!(ERBLint::VERSION)
end

opts.on("--show-linter-names", "Show linter names") do
@options[:show_linter_names] = true
end
end
end

Expand Down
5 changes: 3 additions & 2 deletions lib/erb_lint/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ def self.available_formats
.sort
end

def initialize(stats, autocorrect)
def initialize(stats, autocorrect, show_linter_names = false)
@stats = stats
@autocorrect = autocorrect
@show_linter_names = show_linter_names
end

def preview; end
Expand All @@ -34,7 +35,7 @@ def show; end

private

attr_reader :stats, :autocorrect
attr_reader :stats, :autocorrect, :show_linter_names

delegate :processed_files, to: :stats
end
Expand Down
3 changes: 2 additions & 1 deletion lib/erb_lint/reporters/compact_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def format_offense(filename, offense)
"#{filename}:",
"#{offense.line_number}:",
"#{offense.column}: ",
("[#{offense.simple_name}] " if show_linter_names),
offense.message.to_s,
].join
].compact.join
end

def footer; end
Expand Down
7 changes: 6 additions & 1 deletion lib/erb_lint/reporters/multiline_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ class MultilineReporter < CompactReporter
private

def format_offense(filename, offense)
details = "#{offense.message}#{Rainbow(" (not autocorrected)").red if autocorrect}"
if show_linter_names
details = "[#{offense.simple_name}] " + details
end

<<~EOF
#{offense.message}#{Rainbow(" (not autocorrected)").red if autocorrect}
#{details}
In file: #{filename}:#{offense.line_number}
EOF
end
Expand Down
25 changes: 24 additions & 1 deletion spec/erb_lint/reporters/compact_reporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

describe ERBLint::Reporters::CompactReporter do
describe ".show" do
subject { described_class.new(stats, false).show }
let(:autocorrect) { false }
let(:show_linter_names) { false }
subject { described_class.new(stats, autocorrect, show_linter_names).show }

let(:stats) do
ERBLint::Stats.new(
Expand All @@ -20,14 +22,17 @@
let(:show_file_offenses) do
[
instance_double(ERBLint::Offense,
simple_name: "SpaceInHtmlTag",
message: "Extra space detected where there should be no space.",
line_number: 61,
column: 10),
instance_double(ERBLint::Offense,
simple_name: "FinalNewline",
message: "Remove multiple trailing newline at the end of the file.",
line_number: 125,
column: 1),
instance_double(ERBLint::Offense,
simple_name: "TrailingComma",
message: "Trailing comma expected.",
line_number: 145,
column: 4,
Expand All @@ -38,14 +43,17 @@
let(:notification_file_offenses) do
[
instance_double(ERBLint::Offense,
simple_name: "SpaceIndentation",
message: "Indent with spaces instead of tabs.",
line_number: 3,
column: 1),
instance_double(ERBLint::Offense,
simple_name: "SpaceInHtmlTag",
message: "Extra space detected where there should be no space.",
line_number: 7,
column: 1),
instance_double(ERBLint::Offense,
simple_name: "TrailingComma",
message: "Trailing comma expected.",
line_number: 8,
column: 4,
Expand All @@ -64,6 +72,21 @@
MESSAGE
end

context "with show linter names enabled" do
let(:show_linter_names) { true }

it "displays formatted offenses output with linter names" do
expect { subject }.to(output(<<~MESSAGE).to_stdout)
app/views/users/show.html.erb:61:10: [SpaceInHtmlTag] Extra space detected where there should be no space.
app/views/users/show.html.erb:125:1: [FinalNewline] Remove multiple trailing newline at the end of the file.
app/views/users/show.html.erb:145:4: [TrailingComma] Trailing comma expected.
app/views/shared/_notifications.html.erb:3:1: [SpaceIndentation] Indent with spaces instead of tabs.
app/views/shared/_notifications.html.erb:7:1: [SpaceInHtmlTag] Extra space detected where there should be no space.
app/views/shared/_notifications.html.erb:8:4: [TrailingComma] Trailing comma expected.
MESSAGE
end
end

it "outputs offenses summary to stderr" do
expect { subject }.to(output(<<~MESSAGE).to_stderr)
#{Rainbow("2 error(s) were ignored in ERB files").yellow}
Expand Down
23 changes: 22 additions & 1 deletion spec/erb_lint/reporters/multiline_reporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

describe ERBLint::Reporters::MultilineReporter do
describe ".show" do
subject { described_class.new(stats, autocorrect).show }
subject { described_class.new(stats, autocorrect, show_linter_names).show }

let(:stats) do
ERBLint::Stats.new(
Expand All @@ -18,10 +18,12 @@
let(:offenses) do
[
instance_double(ERBLint::Offense,
simple_name: "SpaceInHtmlTag",
message: "Extra space detected where there should be no space.",
line_number: 1,
column: 7),
instance_double(ERBLint::Offense,
simple_name: "ClosingErbTagIndent",
message: "Remove newline before `%>` to match start of tag.",
line_number: 52,
column: 10),
Expand All @@ -30,6 +32,7 @@

context "when autocorrect is false" do
let(:autocorrect) { false }
let(:show_linter_names) { false }

it "displays formatted offenses output" do
expect { subject }.to(output(<<~MESSAGE).to_stdout)
Expand All @@ -44,8 +47,26 @@
end
end

context "when show_linter_names is true" do
let(:autocorrect) { false }
let(:show_linter_names) { true }

it "displays formatted offenses output" do
expect { subject }.to(output(<<~MESSAGE).to_stdout)
[SpaceInHtmlTag] Extra space detected where there should be no space.
In file: app/views/subscriptions/_loader.html.erb:1
[ClosingErbTagIndent] Remove newline before `%>` to match start of tag.
In file: app/views/subscriptions/_loader.html.erb:52
MESSAGE
end
end

context "when autocorrect is true" do
let(:autocorrect) { true }
let(:show_linter_names) { false }

it "displays not autocorrected warning" do
expect { subject }.to(output(/(not autocorrected)/).to_stdout)
Expand Down

0 comments on commit 804c838

Please sign in to comment.