Class: Spoom::Sorbet::Errors::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/spoom/sorbet/errors.rb

Overview

Parse errors from Sorbet output

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

HEADER =
[
  "👋 Hey there! Heads up that this is not a release build of sorbet.",
  "Release builds are faster and more well-supported by the Sorbet team.",
  "Check out the README to learn how to build Sorbet in release mode.",
  "To forcibly silence this error, either pass --silence-dev-message,",
  "or set SORBET_SILENCE_DEV_MESSAGE=1 in your shell environment.",
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error_url_base: DEFAULT_ERROR_URL_BASE) ⇒ Parser

Returns a new instance of Parser.

Signature:

  • (?error_url_base: String) -> void



74
75
76
77
78
79
# File 'lib/spoom/sorbet/errors.rb', line 74

def initialize(error_url_base: DEFAULT_ERROR_URL_BASE)
  @errors = [] #: Array[Error]
  @warnings = [] #: Array[String]
  @error_line_match_regex = error_line_match_regexp(error_url_base) #: Regexp
  @current_error = nil #: Error?
end

Class Method Details

.parse_result(output, error_url_base: DEFAULT_ERROR_URL_BASE) ⇒ Object

Used when callers need both parsed Sorbet errors and leading stderr warnings.

Signature:

  • (String output, ?error_url_base: String) -> ParseResult



67
68
69
70
# File 'lib/spoom/sorbet/errors.rb', line 67

def parse_result(output, error_url_base: DEFAULT_ERROR_URL_BASE)
  parser = Spoom::Sorbet::Errors::Parser.new(error_url_base: error_url_base)
  parser.parse_result(output)
end

.parse_string(output, error_url_base: DEFAULT_ERROR_URL_BASE) ⇒ Object

Used when only Sorbet errors are needed and leading stderr warnings can be ignored.

Signature:

  • (String output, ?error_url_base: String) -> Array[Error]



61
62
63
# File 'lib/spoom/sorbet/errors.rb', line 61

def parse_string(output, error_url_base: DEFAULT_ERROR_URL_BASE)
  parse_result(output, error_url_base: error_url_base).errors
end

Instance Method Details

#parse(output) ⇒ Object

Signature:

  • (String output) -> Array[Error]



82
83
84
# File 'lib/spoom/sorbet/errors.rb', line 82

def parse(output)
  parse_result(output).errors
end

#parse_result(output) ⇒ Object

Signature:

  • (String output) -> ParseResult



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/spoom/sorbet/errors.rb', line 87

def parse_result(output)
  output.each_line do |line|
    break if /^No errors! Great job\./.match?(line)
    break if /^Errors: /.match?(line)
    next if HEADER.include?(line.strip)

    next if line == "\n"

    if (error = match_error_line(line))
      close_error if @current_error
      open_error(error)
      next
    end

    if @current_error
      append_error(line)
    else
      append_warning(line)
    end
  end
  close_error if @current_error
  ParseResult.new(@errors, @warnings)
end