Class: Spoom::Cli::Run

Inherits:
Thor
  • Object
show all
Includes:
Helper
Defined in:
lib/spoom/cli/run.rb

Constant Summary collapse

SORT_CODE =
"code"
SORT_LOC =
"loc"
SORT_ENUM =
[SORT_CODE, SORT_LOC]
DEFAULT_FORMAT =
"%C - %F:%L: %M"

Constants included from Helper

Helper::HIGHLIGHT_COLOR

Instance Method Summary collapse

Methods included from Helper

#blue, #color?, #colorize, #context, #context_requiring_sorbet!, #cyan, #exec_path, #gray, #green, #highlight, #red, #say, #say_error, #yellow

Methods included from Spoom::Colorize

#set_color

Instance Method Details

#tc(*paths_to_select) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/spoom/cli/run.rb', line 26

def tc(*paths_to_select)
  context = context_requiring_sorbet!
  limit = options[:limit]
  sort = options[:sort]
  code = options[:code]
  uniq = options[:uniq]
  format = options[:format]
  count = options[:count]
  sorbet = options[:sorbet]

  unless limit || code || sort
    result = T.unsafe(context).srb_tc(
      *options[:sorbet_options].split(" "),
      capture_err: false,
      sorbet_bin: sorbet,
    )

    say_error(result.err, status: nil, nl: false)
    exit(result.status)
  end

  error_url_base = Spoom::Sorbet::Errors::DEFAULT_ERROR_URL_BASE
  result = T.unsafe(context).srb_tc(
    *options[:sorbet_options].split(" "),
    "--error-url-base=#{error_url_base}",
    capture_err: true,
    sorbet_bin: sorbet,
  )

  if result.status
    say_error(result.err, status: nil, nl: false)
    exit(0)
  end

  unless result.exit_code == 100
    # Sorbet will return exit code 100 if there are type checking errors.
    # If Sorbet returned something else, it means it didn't terminate normally.
    say_error(result.err, status: nil, nl: false)
    exit(1)
  end

  errors = Spoom::Sorbet::Errors::Parser.parse_string(result.err, error_url_base: error_url_base)
  errors_count = errors.size

  errors = errors.select { |e| e.code == code } if code

  unless paths_to_select.empty?
    errors.select! do |error|
      paths_to_select.any? { |path_to_select| error.file&.start_with?(path_to_select) }
    end
  end

  errors = case sort
  when SORT_CODE
    Spoom::Sorbet::Errors.sort_errors_by_code(errors)
  when SORT_LOC
    errors.sort
  else
    errors # preserve natural sort
  end

  errors = T.must(errors.slice(0, limit)) if limit

  lines = errors.map { |e| format_error(e, format || DEFAULT_FORMAT) }
  lines = lines.uniq if uniq

  lines.each do |line|
    say_error(line, status: nil)
  end

  if count
    if errors_count == errors.size
      say_error("Errors: #{errors_count}", status: nil)
    else
      say_error("Errors: #{errors.size} shown, #{errors_count} total", status: nil)
    end
  end

  exit(1)
rescue Spoom::Sorbet::Error::Segfault => error
  say_error(<<~ERR, status: nil)
    #{red("!!! Sorbet exited with code #{error.result.exit_code} - SEGFAULT !!!")}

    This is most likely related to a bug in Sorbet.
  ERR

  exit(error.result.exit_code)
rescue Spoom::Sorbet::Error::Killed => error
  say_error(<<~ERR, status: nil)
    #{red("!!! Sorbet exited with code #{error.result.exit_code} - KILLED !!!")}
  ERR

  exit(error.result.exit_code)
end