Show affected code while using a default formatter

This commit is contained in:
Vitalii Elenhaupt 2018-12-12 21:45:00 +02:00
parent 148044f479
commit f671d6f857
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
7 changed files with 89 additions and 11 deletions

View file

@ -33,15 +33,19 @@ Inspecting 107 files.
...............F.....................F.................................................................... ...............F.....................F....................................................................
src/ameba/rule/unneeded_disable_directive.cr:29:7 src/ameba/formatter/flycheck_formatter.cr:4:33
Lint/UselessAssign: Useless assignment to variable `s`
src/ameba/formatter/flycheck_formatter.cr:5:21
Lint/UnusedArgument: Unused argument `location` Lint/UnusedArgument: Unused argument `location`
> source.issues.each do |e, location|
^
Finished in 248.9 milliseconds src/ameba/formatter/base_formatter.cr:12:7
Lint/UselessAssign: Useless assignment to variable `s`
> return s += issues.size
^
107 inspected, 2 failures. Finished in 542.64 milliseconds
129 inspected, 2 failures.
``` ```

View file

@ -62,6 +62,11 @@ module Ameba::Cli
c.colors?.should be_false c.colors?.should be_false
end end
it "accepts --without-affected-code flag" do
c = Cli.parse_args %w(--without-affected-code)
c.without_affected_code?.should be_true
end
it "doesn't disable colors by default" do it "doesn't disable colors by default" do
c = Cli.parse_args %w(--all) c = Cli.parse_args %w(--all)
c.colors?.should be_true c.colors?.should be_true

View file

@ -50,6 +50,37 @@ module Ameba::Formatter
log.should contain "NamedRuleError" log.should contain "NamedRuleError"
end end
it "writes affected code by default" do
output.clear
s = Source.new(%(
a = 22
puts a
)).tap do |source|
source.add_issue(DummyRule.new, {1, 5}, "DummyRuleError")
end
subject.finished [s]
log = output.to_s
log.should contain "> a = 22"
log.should contain " \e[33m^\e[0m"
end
it "doesn't write affected code if it is disabled" do
output.clear
s = Source.new(%(
a = 22
puts a
)).tap do |source|
source.add_issue(DummyRule.new, {1, 5}, "DummyRuleError")
end
formatter = DotFormatter.new output
formatter.config[:without_affected_code] = true
formatter.finished [s]
log = output.to_s
log.should_not contain "> a = 22"
log.should_not contain " \e[33m^\e[0m"
end
it "does not write disabled issues" do it "does not write disabled issues" do
s = Source.new "" s = Source.new ""
s.add_issue(DummyRule.new, location: {1, 1}, s.add_issue(DummyRule.new, location: {1, 1},

View file

@ -27,6 +27,7 @@ module Ameba::Cli
property except : Array(String)? property except : Array(String)?
property? all = false property? all = false
property? colors = true property? colors = true
property? without_affected_code = false
end end
def parse_args(args, opts = Opts.new) def parse_args(args, opts = Opts.new)
@ -68,6 +69,11 @@ module Ameba::Cli
opts.config = "" opts.config = ""
end end
parser.on("--without-affected-code",
"Stop showing affected code while using a default formatter") do
opts.without_affected_code = true
end
parser.on("--no-color", "Disable colors") do parser.on("--no-color", "Disable colors") do
opts.colors = false opts.colors = false
end end
@ -91,6 +97,7 @@ module Ameba::Cli
if name = opts.formatter if name = opts.formatter
config.formatter = name config.formatter = name
end end
config.formatter.config[:without_affected_code] = opts.without_affected_code?
end end
private def print_version private def print_version

View file

@ -6,6 +6,7 @@ module Ameba::Formatter
class BaseFormatter class BaseFormatter
# TODO: allow other IOs # TODO: allow other IOs
getter output : IO::FileDescriptor | IO::Memory getter output : IO::FileDescriptor | IO::Memory
getter config = {} of Symbol => String | Bool
def initialize(@output = STDOUT) def initialize(@output = STDOUT)
end end

View file

@ -19,15 +19,25 @@ module Ameba::Formatter
end end
# Reports a message when inspection is finished. # Reports a message when inspection is finished.
def finished(sources) def finished(sources, break_line = "\n\n")
output << "\n\n" output << break_line
show_affected_code = !config[:without_affected_code]?
failed_sources = sources.reject &.valid? failed_sources = sources.reject &.valid?
failed_sources.each do |source| failed_sources.each do |source|
source.issues.each do |issue| source.issues.each do |issue|
next if issue.disabled? next if issue.disabled?
output << "#{issue.location}\n".colorize(:cyan) next if (location = issue.location).nil?
output << "#{issue.rule.name}: #{issue.message}\n\n".colorize(:red)
output << "#{location}\n".colorize(:cyan)
output << "#{issue.rule.name}: #{issue.message}\n".colorize(:red)
if show_affected_code && (code = affected_code(source, location))
output << code
end
output << "\n"
end end
end end
@ -77,5 +87,25 @@ module Ameba::Formatter
"#{total} inspected, #{failures} failure#{s}.\n".colorize color "#{total} inspected, #{failures} failure#{s}.\n".colorize color
end end
private def affected_code(source, location, max_length = 100, placeholder = " ...", prompt = "> ")
line, column = location.line_number, location.column_number
affected_line = source.lines[line - 1]?
return unless affected_line
if affected_line.size > max_length && column < max_length
affected_line = affected_line[0, max_length - placeholder.size - 1] + placeholder
end
stripped = affected_line.lstrip
position = column - (affected_line.size - stripped.size) + prompt.size
String.build do |str|
str << prompt << stripped << "\n"
str << " " * (position - 1)
str << "^".colorize(:yellow)
end
end
end end
end end

View file

@ -22,7 +22,7 @@ module Ameba::Rule::Layout
source.lines.each_with_index do |line, index| source.lines.each_with_index do |line, index|
next unless line.size > max_length next unless line.size > max_length
issue_for({index + 1, line.size}, MSG) issue_for({index + 1, max_length + 1}, MSG)
end end
end end
end end