diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 00000000..f2a971aa --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +ruby 3.2.2 diff --git a/lib/erb_lint/cli.rb b/lib/erb_lint/cli.rb index 932a2276..d68ff769 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]) @@ -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 diff --git a/lib/erb_lint/reporter.rb b/lib/erb_lint/reporter.rb index edccbd6a..13715245 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 11d997ac..bef46a41 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 47952da6..b8c68864 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 4e74c37c..679181d7 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( @@ -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, @@ -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, @@ -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} diff --git a/spec/erb_lint/reporters/multiline_reporter_spec.rb b/spec/erb_lint/reporters/multiline_reporter_spec.rb index 9727c62e..47b8ab49 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( @@ -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), @@ -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) @@ -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)