Skip to content

Commit

Permalink
Merge pull request #340 from andyw8/andyw8/add-linter-names-option
Browse files Browse the repository at this point in the history
Add `--show-linter-names` option
  • Loading branch information
andyw8 authored May 24, 2024
2 parents a9c1e64 + ba09d0b commit f82b3f7
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 7 deletions.
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 @@ -375,6 +375,10 @@ def option_parser
@options[:autocorrect] = config
end

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

opts.on("--allow-no-files", "When no matching files found, exit successfully (default: false)") do |config|
@options[:allow_no_files] = config
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 @@ -21,18 +23,21 @@
[
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 @@ -45,18 +50,21 @@
[
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 @@ -76,6 +84,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 @@ -19,12 +19,14 @@
[
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 @@ -34,6 +36,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 @@ -48,8 +51,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 f82b3f7

Please sign in to comment.