shard-ameba/src/ameba/source.cr

38 lines
894 B
Crystal
Raw Normal View History

2017-10-31 18:29:30 +00:00
require "compiler/crystal/syntax/*"
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,
# position of the error and a message.
2017-10-26 21:01:23 +00:00
record Error,
2017-10-30 20:00:01 +00:00
rule : Rule,
2017-10-26 21:01:23 +00:00
pos : Int32,
message : String
2017-10-26 17:47:42 +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?
getter content : String
2017-10-26 17:47:42 +00:00
2017-10-31 18:29:30 +00:00
def initialize(@content : String, @path = nil)
@lines = @content.split("\n")
2017-10-30 20:00:01 +00:00
end
def error(rule : Rule, line_number : Int32, message : String)
errors << Error.new rule, line_number, message
end
def valid?
errors.empty?
2017-10-26 17:47:42 +00:00
end
2017-10-31 18:29:30 +00:00
def ast
Crystal::Parser.new(@content).tap { |p| p.filename = @path }.parse
end
2017-10-26 17:47:42 +00:00
end
end