Skip to content

Commit

Permalink
Merge pull request #8 from thatch-health/olivier-rescue-from-better-t…
Browse files Browse the repository at this point in the history
…ypes

Improve types for `rescue_from`
  • Loading branch information
olivier-thatch authored Jul 12, 2024
2 parents bf82075 + e45584e commit acc5896
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
28 changes: 21 additions & 7 deletions lib/tapioca/dsl/compilers/grape_endpoints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,30 @@ def create_callbacks_methods

sig { void }
def create_request_response_methods
request_response_methods_module.create_method(
sigs = [
request_response_methods_module.create_sig(
parameters: {
args: "Symbol",
block: "T.nilable(T.proc.bind(#{EndpointClassName}).params(e: Exception).void)",
},
return_type: "void",
),
request_response_methods_module.create_sig(
type_parameters: ["E"],
parameters: {
args: "T::Class[T.all(::Exception, T.type_parameter(:E))]",
block: "T.nilable(T.proc.bind(#{EndpointClassName}).params(e: T.type_parameter(:E)).void)",
},
return_type: "void",
),
]
request_response_methods_module.create_method_with_sigs(
"rescue_from",
sigs: sigs,
parameters: [
create_rest_param("args", type: "T.untyped"),
create_block_param(
"block",
type: "T.nilable(T.proc.bind(#{EndpointClassName}).params(e: Exception).void)",
),
RBI::RestParam.new("args"),
RBI::BlockParam.new("block"),
],
return_type: "void",
)
end

Expand Down
10 changes: 9 additions & 1 deletion rbi/grape.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ module Grape

module RequestResponse
module ClassMethods
sig { params(args: T.untyped, block: T.proc.bind(Grape::Endpoint).params(e: Exception).void).void }
sig { params(args: Symbol, block: T.nilable(T.proc.bind(Grape::Endpoint).params(e: Exception).void)).void }
sig do
type_parameters(:E)
.params(
args: T::Class[T.all(Exception, T.type_parameter(:E))],
block: T.proc.bind(Grape::Endpoint).params(e: T.type_parameter(:E)).void,
)
.void
end
def rescue_from(*args, &block); end
end
end
Expand Down
6 changes: 4 additions & 2 deletions spec/tapioca/dsl/compilers/grape_endpoints_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def finally(&block); end
end
module GeneratedRequestResponseMethods
sig { params(args: T.untyped, block: T.nilable(T.proc.bind(PrivateEndpoint).params(e: Exception).void)).void }
sig { params(args: Symbol, block: T.nilable(T.proc.bind(PrivateEndpoint).params(e: Exception).void)).void }
sig { type_parameters(:E).params(args: T::Class[T.all(::Exception, T.type_parameter(:E))], block: T.nilable(T.proc.bind(PrivateEndpoint).params(e: T.type_parameter(:E)).void)).void }
def rescue_from(*args, &block); end
end
Expand Down Expand Up @@ -177,7 +178,8 @@ def finally(&block); end
end
module GeneratedRequestResponseMethods
sig { params(args: T.untyped, block: T.nilable(T.proc.bind(PrivateEndpoint).params(e: Exception).void)).void }
sig { params(args: Symbol, block: T.nilable(T.proc.bind(PrivateEndpoint).params(e: Exception).void)).void }
sig { type_parameters(:E).params(args: T::Class[T.all(::Exception, T.type_parameter(:E))], block: T.nilable(T.proc.bind(PrivateEndpoint).params(e: T.type_parameter(:E)).void)).void }
def rescue_from(*args, &block); end
end
Expand Down

0 comments on commit acc5896

Please sign in to comment.