Skip to content

Commit

Permalink
Compile T.type_alias
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffcarbs committed Apr 20, 2021
1 parent 4955d93 commit dd33c31
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/tapioca/compilers/symbol_table/symbol_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ def compile_object(name, value)
klass = class_of(value)
klass_name = name_of(klass)

if klass_name == "T::Private::Types::TypeAlias"
return indented("#{name} = T.type_alias { #{T.unsafe(value).aliased_type} }")
end

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

type_name = public_module?(klass) && klass_name || "T.untyped"
Expand Down
29 changes: 29 additions & 0 deletions spec/tapioca/compilers/symbol_table_compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,35 @@ 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 dd33c31

Please sign in to comment.