Module: Tapioca::SorbetHelper

Constant Summary collapse

SORBET_GEM_SPEC =

: ::Gem::Specification

::Gem::Specification.find_by_name("sorbet-static")
SORBET_BIN =

: Pathname

Pathname.new(SORBET_GEM_SPEC.full_gem_path) / "libexec" / "sorbet"
SORBET_EXE_PATH_ENV_VAR =
"TAPIOCA_SORBET_EXE"
SORBET_PAYLOAD_URL =
"https://github.com/sorbet/sorbet/tree/master/rbi"
SPOOM_CONTEXT =

: Spoom::Context

Spoom::Context.new(".")
FEATURE_REQUIREMENTS =
{
  # feature_name: ::Gem::Requirement.new(">= ___"), # https://github.com/sorbet/sorbet/pull/___
}.freeze

Instance Method Summary collapse

Instance Method Details

#sorbet(*sorbet_args) ⇒ Object

: (*String sorbet_args) -> Spoom::ExecResult



21
22
23
# File 'lib/tapioca/helpers/sorbet_helper.rb', line 21

def sorbet(*sorbet_args)
  SPOOM_CONTEXT.srb(sorbet_args.join(" "), sorbet_bin: sorbet_path)
end

#sorbet_pathObject

: -> String



45
46
47
48
49
# File 'lib/tapioca/helpers/sorbet_helper.rb', line 45

def sorbet_path
  sorbet_path = ENV.fetch(SORBET_EXE_PATH_ENV_VAR, SORBET_BIN)
  sorbet_path = SORBET_BIN if sorbet_path.empty?
  sorbet_path.to_s.shellescape
end

#sorbet_supports?(feature, version: nil) ⇒ Boolean

: (Symbol feature, ?version: ::Gem::Version?) -> bool

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
# File 'lib/tapioca/helpers/sorbet_helper.rb', line 52

def sorbet_supports?(feature, version: nil)
  version = SORBET_GEM_SPEC.version unless version
  requirement = FEATURE_REQUIREMENTS[feature]

  Kernel.raise "Invalid Sorbet feature #{feature}" unless requirement

  requirement.satisfied_by?(version)
end

#sorbet_syntax_check!(source, rbi_mode:, &on_failure) ⇒ Object

: (String, rbi_mode: bool) { (String stderr) -> void } -> void



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tapioca/helpers/sorbet_helper.rb', line 26

def sorbet_syntax_check!(source, rbi_mode:, &on_failure)
  quoted_source = "\"#{source}\""

  result = if rbi_mode
    # --e-rbi cannot be used on its own, so we pass a dummy value like `-e ""`
    sorbet("--no-config", "--stop-after=parser", "-e", '""', "--e-rbi", quoted_source)
  else
    sorbet("--no-config", "--stop-after=parser", "-e", quoted_source)
  end

  unless result.status
    stderr = result.err #: as !nil
    on_failure.call(stderr)
  end

  nil
end