class Prism::ParseResult::Errors
一个用来表示解析结果上的错误集合的对象。此对象可用于以人类可读的方式格式化错误。
属性
包含错误的解析结果。
公共类方法
源代码
# File lib/prism/parse_result/errors.rb, line 14 def initialize(parse_result) @parse_result = parse_result end
从给定的解析结果初始化一组新的错误。
公共实例方法
源代码
# File lib/prism/parse_result/errors.rb, line 19 def format error_lines = {} #: Hash[Integer, Array[ParseError]] parse_result.errors.each do |error| location = error.location (location.start_line..location.end_line).each do |line| error_lines[line] ||= [] error_lines[line] << error end end source_lines = parse_result.source.source.lines source_lines << "" if error_lines.key?(source_lines.size + 1) io = StringIO.new source_lines.each.with_index(1) do |line, line_number| io.puts(line) (error_lines.delete(line_number) || []).each do |error| location = error.location case line_number when location.start_line io.print(" " * location.start_column + "^") if location.start_line == location.end_line if location.start_column != location.end_column io.print("~" * (location.end_column - location.start_column - 1)) end io.puts(" " + error.message) else io.puts("~" * (line.bytesize - location.start_column)) end when location.end_line io.puts("~" * location.end_column + " " + error.message) else io.puts("~" * line.bytesize) end end end io.puts io.string end
以人类可读的方式格式化错误,并将其作为字符串返回。