class Prism::ParseResult::Errors
An object to represent the set of errors on a parse result. This object can be used to format the errors in a human-readable way.
Attributes
The parse result that contains the errors.
Public Class Methods
(ParseResult parse_result) → void
Source
# File lib/prism/parse_result/errors.rb, line 19 def initialize(parse_result) @parse_result = parse_result end
Initialize a new set of errors from the given parse result.
Public Instance Methods
() → String
Source
# File lib/prism/parse_result/errors.rb, line 26 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 # Each character takes up one column, so the carets are measured # in characters rather than bytes. case line_number when location.start_line start_column = location.start_character_column io.print(" " * start_column + "^") if location.start_line == location.end_line end_column = location.end_character_column io.print("~" * (end_column - start_column - 1)) if start_column != end_column io.puts(" " + error.message) else io.puts("~" * (line.length - start_column)) end when location.end_line io.puts("~" * location.end_character_column + " " + error.message) else io.puts("~" * line.length) end end end io.puts io.string end
Formats the errors in a human-readable way and return them as a string.