Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions lib/tapioca/helpers/sorbet_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ module SorbetHelper
# Represents the `sorbet/config` file, and provides access to its options. https://sorbet.org/docs/cli-ref
# If the file doesn't exist, this object will still exist, but will return default values for all options.
class SorbetConfig
BOOLEAN_OPTIONS = [
"--enable-experimental-rbs-signatures",
"--enable-experimental-rbs-assertions",
"--enable-experimental-rbs-comments",
].freeze #: Array[String]

class << self
#: (Spoom::Context spoom_context) -> SorbetConfig
def parse_from(spoom_context)
Expand All @@ -35,6 +41,16 @@ def parse(content)
key, value = line.split("=", 2)
key = key #: as !nil

if BOOLEAN_OPTIONS.include?(key)
# treat `--bool_option` like `--bool_option=true`
value ||= true
end

case value
when "true" then value = true
when "false" then value = false
end

[key, value]
end.to_h #: Hash[String, String | bool | nil]

Expand All @@ -45,14 +61,19 @@ def parse(content)
end,

parser: options["--parser"] == "prism" ? :prism : :original,

use_rbs: !!(options["--enable-experimental-rbs-comments"] ||
options["--enable-experimental-rbs-signatures"] ||
options["--enable-experimental-rbs-assertions"]),
)
end
end

#: (cache_dir: String?, parser: Symbol) -> void
def initialize(cache_dir:, parser:)
#: (cache_dir: String?, parser: Symbol, use_rbs: bool) -> void
def initialize(cache_dir:, parser:, use_rbs:)
@cache_dir = cache_dir #: String?
@parser = parser #: Symbol
@use_rbs = use_rbs #: bool
end

#: String?
Expand All @@ -63,6 +84,9 @@ def initialize(cache_dir:, parser:)

#: -> bool
def parse_with_prism? = @parser == :prism

#: -> bool
def use_rbs? = @use_rbs
end

FEATURE_REQUIREMENTS = {
Expand All @@ -81,6 +105,10 @@ def sorbet(*sorbet_args)
sorbet_args << "--parser=prism"
end

if sorbet_config.use_rbs?
sorbet_args << "--enable-experimental-rbs-comments"
end

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

Expand Down
47 changes: 47 additions & 0 deletions spec/tapioca/helpers/sorbet_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,53 @@ class Tapioca::SorbetHelperSpec < Minitest::Spec
end
end

describe "--enable-experimental-rbs-*" do
it "enables use_rbs? when --enable-experimental-rbs-comments is set" do
config = parse(<<~CONFIG)
.
--enable-experimental-rbs-comments
CONFIG
assert_predicate(config, :use_rbs?)
end

it "enables use_rbs? when --enable-experimental-rbs-signatures is set" do
config = parse(<<~CONFIG)
.
--enable-experimental-rbs-signatures
CONFIG
assert_predicate(config, :use_rbs?)
end

it "enables use_rbs? when --enable-experimental-rbs-assertions is set" do
config = parse(<<~CONFIG)
.
--enable-experimental-rbs-assertions
CONFIG
assert_predicate(config, :use_rbs?)
end

it "defaults use_rbs? to false" do
config = parse("")
refute_predicate(config, :use_rbs?)
end

it "handles explicit true value" do
config = parse(<<~CONFIG)
.
--enable-experimental-rbs-comments=true
CONFIG
assert_predicate(config, :use_rbs?)
end

it "handles explicit false value" do
config = parse(<<~CONFIG)
.
--enable-experimental-rbs-comments=false
CONFIG
refute_predicate(config, :use_rbs?)
end
end

private

#: (String content) -> Tapioca::SorbetHelper::SorbetConfig
Expand Down
Loading