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
Source
# File lib/prism/parse_result/errors.rb, line 14 def initialize(parse_result) @parse_result = parse_result end
Initialize a new set of errors from the given parse result.
Public Instance Methods
Source
# 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
Formats the errors in a human-readable way and return them as a string.