Add flycheck formatter

refs #26
This commit is contained in:
Vitalii Elenhaupt 2017-12-08 16:57:40 +02:00 committed by V. Elenhaupt
parent 202a73ae0e
commit e1fa8677b0
3 changed files with 118 additions and 43 deletions

View File

@ -0,0 +1,44 @@
require "../../spec_helper"
private def flycheck
output = IO::Memory.new
Ameba::Formatter::FlycheckFormatter.new output
end
module Ameba::Formatter
describe FlycheckFormatter do
context "problems not found" do
it "reports nothing" do
subject = flycheck
subject.source_finished Source.new ""
subject.output.to_s.empty?.should be_true
end
end
context "when problems found" do
it "reports an error" do
s = Source.new "a = 1", "source.cr"
s.error DummyRule.new, s.location(1, 2), "message"
subject = flycheck
subject.source_finished s
subject.output.to_s.should eq "source.cr:1:2: E: message\n"
end
it "properly reports multi-line message" do
s = Source.new "a = 1", "source.cr"
s.error DummyRule.new, s.location(1, 2), "multi\nline"
subject = flycheck
subject.source_finished s
subject.output.to_s.should eq "source.cr:1:2: E: multi line\n"
end
it "reports nothing if location was not set" do
s = Source.new "a = 1", "source.cr"
s.error DummyRule.new, nil, "message"
subject = flycheck
subject.source_finished s
subject.output.to_s.should eq ""
end
end
end
end

View File

@ -5,45 +5,15 @@ require "option_parser"
module Ameba::Cli
extend self
private class Opts
property config = Ameba::Config::PATH
property silent : Bool = false
property generate : Bool = false
property files : Array(String)?
property only : Array(String)?
property except : Array(String)?
end
AVAILABLE_FORMATTERS = {
progress: Formatter::DotFormatter,
todo: Formatter::TODOFormatter,
flycheck: Formatter::FlycheckFormatter,
silent: Formatter::BaseFormatter,
}
def run(args, opts = Opts.new)
OptionParser.parse(args) do |parser|
parser.banner = "Usage: ameba [options] [file1 file2 ...]"
parser.on("-v", "--version", "Print version") { print_version }
parser.on("-h", "--help", "Show this help") { show_help parser }
parser.on("-s", "--silent", "Disable output") { opts.silent = true }
parser.unknown_args { |f| opts.files = f if f.any? }
parser.on("-c PATH", "--config PATH", "Specify configuration file") do |f|
opts.config = f
end
parser.on("--only RULE1,RULE2,...", "Specify a list of rules") do |rules|
opts.only = rules.split ","
end
parser.on("--except RULE1,RULE2,...", "Disable the given rules") do |rules|
opts.except = rules.split ","
end
parser.on("--gen-config", "Generate a configuration file acting as a TODO list") do
opts.generate = true
end
end
run_ameba opts
end
def run_ameba(opts)
def run(args)
opts = parse_args args
config = Ameba::Config.load opts.config
config.files = opts.files
@ -56,6 +26,52 @@ module Ameba::Cli
exit 255
end
private class Opts
property config = Ameba::Config::PATH
property formatter : String | Symbol = :progress
property files : Array(String)?
property only : Array(String)?
property except : Array(String)?
end
def parse_args(args, opts = Opts.new)
OptionParser.parse(args) do |parser|
parser.banner = "Usage: ameba [options] [file1 file2 ...]"
parser.on("-v", "--version", "Print version") { print_version }
parser.on("-h", "--help", "Show this help") { show_help parser }
parser.on("-s", "--silent", "Disable output") { opts.formatter = :silent }
parser.unknown_args { |f| opts.files = f if f.any? }
parser.on("-c", "--config PATH",
"Specify a configuration file") do |path|
opts.config = path
end
parser.on("-f", "--format FORMATTER",
"Choose an output formatter: #{formatters}") do |formatter|
opts.formatter = formatter
end
parser.on("--only RULE1,RULE2,...",
"Run only given rules") do |rules|
opts.only = rules.split ","
end
parser.on("--except RULE1,RULE2,...",
"Disable the given rules") do |rules|
opts.except = rules.split ","
end
parser.on("--gen-config",
"Generate a configuration file acting as a TODO list") do
opts.formatter = "todo"
end
end
opts
end
private def configure_rules(config, opts)
if only = opts.only
config.rules.map! { |r| r.enabled = false; r }
@ -70,15 +86,17 @@ module Ameba::Cli
end
private def configure_formatter(config, opts)
if opts.silent
config.formatter = Ameba::Formatter::BaseFormatter.new
elsif opts.generate
config.formatter = Ameba::Formatter::TODOFormatter.new
if cls = AVAILABLE_FORMATTERS[opts.formatter]?
config.formatter = cls.new
else
config.formatter = Ameba::Formatter::DotFormatter.new
raise "Unknown formatter `#{opts.formatter}`. Use any of #{formatters}."
end
end
private def formatters
AVAILABLE_FORMATTERS.keys.join("|")
end
private def print_version
puts Ameba::VERSION
exit 0

View File

@ -0,0 +1,13 @@
module Ameba::Formatter
class FlycheckFormatter < BaseFormatter
def source_finished(source : Source)
source.errors.each do |e|
if loc = e.location
output.printf "%s:%d:%d: %s: %s\n",
source.path, loc.line_number, loc.column_number, "E",
e.message.gsub("\n", " ")
end
end
end
end
end