2017-10-26 17:47:42 +00:00
|
|
|
module Ameba
|
2017-10-30 20:00:01 +00:00
|
|
|
# An entity that represents a Crystal source file.
|
|
|
|
# Has path, lines of code and errors reported by rules.
|
2017-10-26 17:47:42 +00:00
|
|
|
class Source
|
2017-10-30 20:00:01 +00:00
|
|
|
# Represents an error caught by Ameba.
|
|
|
|
#
|
|
|
|
# Each error has the rule that created this error,
|
2017-11-07 21:50:25 +00:00
|
|
|
# location of the issue and a message.
|
2017-10-26 21:01:23 +00:00
|
|
|
record Error,
|
2017-11-07 21:50:25 +00:00
|
|
|
rule : Rule::Base,
|
2017-11-07 20:02:51 +00:00
|
|
|
location : Crystal::Location?,
|
2017-10-26 21:01:23 +00:00
|
|
|
message : String
|
|
|
|
|
2017-10-31 22:47:29 +00:00
|
|
|
getter lines : Array(String)?
|
2017-10-26 21:01:23 +00:00
|
|
|
getter errors = [] of Error
|
2017-10-31 18:29:30 +00:00
|
|
|
getter path : String?
|
2017-11-07 20:02:51 +00:00
|
|
|
getter code : String
|
2017-11-06 18:54:58 +00:00
|
|
|
getter ast : Crystal::ASTNode?
|
2017-10-26 17:47:42 +00:00
|
|
|
|
2017-11-07 20:02:51 +00:00
|
|
|
def initialize(@code : String, @path = nil)
|
2017-10-30 20:00:01 +00:00
|
|
|
end
|
|
|
|
|
2017-11-07 21:50:25 +00:00
|
|
|
def error(rule : Rule::Base, location, message : String)
|
2017-11-07 20:02:51 +00:00
|
|
|
errors << Error.new rule, location, message
|
2017-10-30 20:00:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
|
|
|
errors.empty?
|
2017-10-26 17:47:42 +00:00
|
|
|
end
|
2017-10-31 18:29:30 +00:00
|
|
|
|
2017-10-31 22:47:29 +00:00
|
|
|
def lines
|
2017-11-07 20:02:51 +00:00
|
|
|
@lines ||= @code.split("\n")
|
2017-10-31 22:47:29 +00:00
|
|
|
end
|
2017-11-06 18:54:58 +00:00
|
|
|
|
|
|
|
def ast
|
|
|
|
@ast ||=
|
2017-11-07 20:02:51 +00:00
|
|
|
Crystal::Parser.new(code)
|
2017-11-06 18:54:58 +00:00
|
|
|
.tap { |parser| parser.filename = @path }
|
|
|
|
.parse
|
|
|
|
end
|
2017-11-07 20:02:51 +00:00
|
|
|
|
|
|
|
def location(l, c)
|
|
|
|
Crystal::Location.new path, l, c
|
|
|
|
end
|
2017-10-26 17:47:42 +00:00
|
|
|
end
|
|
|
|
end
|