Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile T.type_alias #291

Merged
merged 1 commit into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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