diff --git a/lib/tapioca/helpers/sorbet_helper.rb b/lib/tapioca/helpers/sorbet_helper.rb index 2e1d1c634..b0bfa55dc 100644 --- a/lib/tapioca/helpers/sorbet_helper.rb +++ b/lib/tapioca/helpers/sorbet_helper.rb @@ -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) @@ -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] @@ -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? @@ -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 = { @@ -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 diff --git a/spec/tapioca/helpers/sorbet_helper_spec.rb b/spec/tapioca/helpers/sorbet_helper_spec.rb index d1569f442..67cb1ca8c 100644 --- a/spec/tapioca/helpers/sorbet_helper_spec.rb +++ b/spec/tapioca/helpers/sorbet_helper_spec.rb @@ -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