lib/tapioca/dsl/compilers/shale.rb in shale-builder-0.2.1 vs lib/tapioca/dsl/compilers/shale.rb in shale-builder-0.2.2

- old
+ new

@@ -46,27 +46,11 @@ if shale_builder_defined? && attribute.doc comments << RBI::Comment.new(T.must(attribute.doc)) end if has_shale_builder && attribute.type < ::Shale::Mapper - sigs = T.let([], T::Array[RBI::Sig]) - # simple getter - sigs << klass.create_sig( - parameters: { block: 'NilClass' }, - return_type: type, - ) - # getter with block - sigs << klass.create_sig( - parameters: { block: "T.proc.params(arg0: #{non_nilable_type}).void" }, - return_type: non_nilable_type - ) - mod.create_method_with_sigs( - attribute.name, - sigs: sigs, - comments: comments, - parameters: [RBI::BlockParam.new('block')], - ) + generate_mapper_getter(mod, attribute.name, type, non_nilable_type, comments) else mod.create_method(attribute.name, return_type: type, comments: comments) end non_nilable_type, nilable_type = shale_type_to_sorbet_setter_type(attribute) @@ -83,9 +67,56 @@ comments: comments, ) end end + end + + sig do + params( + mod: RBI::Scope, + method_name: String, + type: String, + non_nilable_type: String, + comments: T::Array[RBI::Comment], + ).void + end + def generate_mapper_getter(mod, method_name, type, non_nilable_type, comments) + if mod.respond_to?(:create_sig) + # for tapioca < 0.16.0 + sigs = T.let([], T::Array[RBI::Sig]) + # simple getter + sigs << mod.create_sig( + parameters: { block: 'NilClass' }, + return_type: type, + ) + # getter with block + sigs << mod.create_sig( + parameters: { block: "T.proc.params(arg0: #{non_nilable_type}).void" }, + return_type: non_nilable_type + ) + mod.create_method_with_sigs( + method_name, + sigs: sigs, + comments: comments, + parameters: [RBI::BlockParam.new('block')], + ) + else + # for tapioca >= 0.16.0 + mod.create_method(method_name, comments: comments) do |method| + method.add_block_param('block') + + method.add_sig do |sig| + sig.add_param('block', 'NilClass') + sig.return_type = type + end + + method.add_sig do |sig| + sig.add_param('block', "T.proc.params(arg0: #{non_nilable_type}).void") + sig.return_type = non_nilable_type + end + end + end end private sig { params(klass: Class).returns(T.nilable(T::Boolean)) }