Skip to content

Commit

Permalink
Merge pull request #291 from jeffcarbs/compile-type-alias
Browse files Browse the repository at this point in the history
Compile T.type_alias
  • Loading branch information
paracycle committed May 12, 2021
2 parents 84bf565 + a31e49b commit a075b6c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/tapioca/compilers/symbol_table/symbol_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ def compile_object(tree, name, value)
klass = class_of(value)
klass_name = name_of(klass)

if klass_name == "T::Private::Types::TypeAlias"
tree << RBI::Const.new(name, "T.type_alias { #{T.unsafe(value).aliased_type} }")
return
end

return if klass_name&.start_with?("T::Types::", "T::Private::")

type_name = public_module?(klass) && klass_name || "T.untyped"
Expand Down
22 changes: 22 additions & 0 deletions spec/tapioca/compilers/symbol_table_compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ module Bar
end
Bar::Arr = T.let(T.unsafe(nil), Array)
Bar::Foo = T.type_alias { T.any(String, Symbol) }
RBI

assert_equal(output, compile)
end

it("compiles complex type aliases") do
add_ruby_file("bar.rb", <<~RUBY)
module Bar
module A
class B; end
Foo = ::T.type_alias { T.any(String, Symbol, A, B) }
end
end
RUBY

output = template(<<~RBI)
module Bar; end
module Bar::A; end
class Bar::A::B; end
Bar::A::Foo = T.type_alias { T.any(Bar::A, Bar::A::B, String, Symbol) }
RBI

assert_equal(output, compile)
Expand Down

0 comments on commit a075b6c

Please sign in to comment.