shard-ameba/src/ameba/source.cr
2017-10-31 22:24:02 +02:00

37 lines
896 B
Crystal

require "compiler/crystal/syntax/*"
module Ameba
# An entity that represents a Crystal source file.
# Has path, lines of code and errors reported by rules.
class Source
# Represents an error caught by Ameba.
#
# Each error has the rule that created this error,
# position of the error and a message.
record Error,
rule : Rule,
pos : Int32?,
message : String
getter lines : Array(String)
getter errors = [] of Error
getter path : String?
getter content : String
def initialize(@content : String, @path = nil)
@lines = @content.split("\n")
end
def error(rule : Rule, line_number : Int32?, message : String)
errors << Error.new rule, line_number, message
end
def valid?
errors.empty?
end
def ast
Crystal::Parser.new(@content).tap { |p| p.filename = @path }.parse
end
end
end