This commit is contained in:
Vitalii Elenhaupt 2017-10-27 00:01:23 +03:00
parent ffd44dc77b
commit 5e10113055
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
5 changed files with 48 additions and 9 deletions

View file

@ -33,6 +33,21 @@ require "ameba"
Ameba.run
```
```sh
Inspecting 7 files.
..F...F
7 inspected, 2 failures.
src/ameba/formatter.cr:47
Ameba::Rule::LineLength: Line too long [122]
src/ameba.cr:18
Ameba::Rule::LineLength: Line too long [81]
```
## Contributing
1. Fork it ( https://github.com/veelenga/ameba/fork )

View file

@ -13,7 +13,7 @@ module Ameba
end
def run(files, formatter : Formatter)
sources = files.map { |path| Source.new(File.read path) }
sources = files.map { |path| Source.new(path) }
reporter = Reporter.new formatter
reporter.start sources

View file

@ -13,6 +13,7 @@ module Ameba
def start(sources)
puts formatter.before sources
puts "\n\n"
end
def report(source)
@ -20,7 +21,7 @@ module Ameba
end
def finish(sources)
puts
puts "\n\n"
puts formatter.after sources
end
end
@ -39,7 +40,20 @@ module Ameba
end
def after(sources)
"Done!"
String.build do |mes|
failures = sources.select { |s| s.errors.any? }
l = failures.map { |f| f.errors.size }.sum
mes << "#{sources.size} inspected, #{l} failure#{"s" if l != 1}.\n\n"
failures.each do |failure|
failure.errors.each do |error|
mes << "#{failure.path}:#{error.pos}"
mes << "\n"
mes << "#{error.rule}: #{error.message}\n\n"
end
end
end
end
end
end

View file

@ -1,8 +1,8 @@
struct Ameba::Rule::LineLength
def test(source)
source.lines.each do |line|
source.lines.each_with_index do |line, index|
if line.size > 79
source.errors << "Line too long"
source.error self, index + 1, "Line too long [#{line.size}]"
end
end
end

View file

@ -1,10 +1,20 @@
module Ameba
class Source
getter lines : Array(String)
getter errors = [] of String
record Error,
rule : String,
pos : Int32,
message : String
def initialize(content : String)
@lines = content.split "\n"
getter lines : Array(String)
getter errors = [] of Error
getter path : String
def initialize(@path : String)
@lines = File.read_lines(@path)
end
def error(rule, line_number : Int32, message : String)
errors << Error.new rule.class.name, line_number, message
end
end
end