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, #check_sorbet_segfault, #color?, #colorize, #cyan, #exec_path, #gray, #green, #highlight, #in_sorbet_project!, #in_sorbet_project?, #red, #say, #say_error, #sorbet_config, #sorbet_config_file, #yellow

Methods included from Spoom::Colorize

#set_color

Instance Method Details

#tc(*arg) ⇒ Object


25
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
# File 'lib/spoom/cli/run.rb', line 25

def tc(*arg)
  in_sorbet_project!

  path = exec_path
  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
    output, status, exit_code = T.unsafe(Spoom::Sorbet).srb_tc(
      *arg,
      path: path,
      capture_err: false,
      sorbet_bin: sorbet
    )

    check_sorbet_segfault(exit_code)
    say_error(output, status: nil, nl: false)
    exit(status)
  end

  output, status, exit_code = T.unsafe(Spoom::Sorbet).srb_tc(
    *arg,
    path: path,
    capture_err: true,
    sorbet_bin: sorbet
  )

  check_sorbet_segfault(exit_code)

  if status
    say_error(output, status: nil, nl: false)
    exit(0)
  end

  errors = Spoom::Sorbet::Errors::Parser.parse_string(output)
  errors_count = errors.size

  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 = errors.select { |e| e.code == code } if code
  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)
end