Skip to content

Commit

Permalink
Better sanitize Sorbet types when compiling signatures
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
  • Loading branch information
Morriar committed May 5, 2021
1 parent 2bb2a7e commit 86b2982
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/tapioca/compilers/symbol_table/symbol_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -592,18 +592,12 @@ def compile_signature(signature, parameters)
sig = RBI::Sig.new

parameters.each do |_, name|
type = parameter_types[name.to_sym].to_s
.gsub(".returns(<VOID>)", ".void")
.gsub("<NOT-TYPED>", "T.untyped")
.gsub(".params()", "")

type = sanitize_signature_types(parameter_types[name.to_sym].to_s)
sig << RBI::SigParam.new(name, type)
end

sig.return_type = type_of(signature.return_type)
.gsub("<VOID>", "void")
.gsub("<NOT-TYPED>", "T.untyped")
.gsub(".params()", "")
return_type = type_of(signature.return_type)
sig.return_type = sanitize_signature_types(return_type)

parameter_types.values.join(", ").scan(TYPE_PARAMETER_MATCHER).flatten.uniq.each do |k, _|
sig.type_params << k
Expand Down Expand Up @@ -832,6 +826,15 @@ def signature_of(method)
nil
end

sig { params(sig_string: String).returns(String) }
def sanitize_signature_types(sig_string)
sig_string
.gsub(".returns(<VOID>)", ".void")
.gsub("<VOID>", "void")
.gsub("<NOT-TYPED>", "T.untyped")
.gsub(".params()", "")
end

sig { params(constant: T::Types::Base).returns(String) }
def type_of(constant)
constant.to_s.gsub(/\bAttachedClass\b/, "T.attached_class")
Expand Down
6 changes: 6 additions & 0 deletions spec/tapioca/compilers/symbol_table_compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2050,6 +2050,9 @@ def baz(a)
def many_kinds_of_args(*a, b, c, d:, e: 42, **f, &blk)
end
sig { returns(T.proc.params(x: String).void) }
attr_reader :some_attribute
class << self
extend(T::Sig)
Expand Down Expand Up @@ -2236,6 +2239,9 @@ def foo(a, b:); end
sig { params(a: Integer, b: Integer, c: Integer, d: Integer, e: Integer, f: Integer, blk: T.proc.void).void }
def many_kinds_of_args(*a, b, c, d:, e: T.unsafe(nil), **f, &blk); end
sig { returns(T.proc.params(x: String).void) }
def some_attribute; end
class << self
sig { void }
def quux; end
Expand Down

0 comments on commit 86b2982

Please sign in to comment.