diff --git a/lib/erb_lint/cli.rb b/lib/erb_lint/cli.rb index ee95c75..3e6a6b0 100644 --- a/lib/erb_lint/cli.rb +++ b/lib/erb_lint/cli.rb @@ -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]) @@ -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 diff --git a/lib/erb_lint/reporter.rb b/lib/erb_lint/reporter.rb index edccbd6..1371524 100644 --- a/lib/erb_lint/reporter.rb +++ b/lib/erb_lint/reporter.rb @@ -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 @@ -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 diff --git a/lib/erb_lint/reporters/compact_reporter.rb b/lib/erb_lint/reporters/compact_reporter.rb index d4c42e0..f7f33cf 100644 --- a/lib/erb_lint/reporters/compact_reporter.rb +++ b/lib/erb_lint/reporters/compact_reporter.rb @@ -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 diff --git a/lib/erb_lint/reporters/multiline_reporter.rb b/lib/erb_lint/reporters/multiline_reporter.rb index 47952da..b8c6886 100644 --- a/lib/erb_lint/reporters/multiline_reporter.rb +++ b/lib/erb_lint/reporters/multiline_reporter.rb @@ -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 diff --git a/spec/erb_lint/reporters/compact_reporter_spec.rb b/spec/erb_lint/reporters/compact_reporter_spec.rb index 5abbdf0..8f98bf8 100644 --- a/spec/erb_lint/reporters/compact_reporter_spec.rb +++ b/spec/erb_lint/reporters/compact_reporter_spec.rb @@ -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( @@ -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, @@ -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, @@ -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} diff --git a/spec/erb_lint/reporters/multiline_reporter_spec.rb b/spec/erb_lint/reporters/multiline_reporter_spec.rb index 21bd63b..23b3d06 100644 --- a/spec/erb_lint/reporters/multiline_reporter_spec.rb +++ b/spec/erb_lint/reporters/multiline_reporter_spec.rb @@ -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( @@ -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, @@ -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) @@ -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)