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

Add an easier syntax to create methods and signatures #284

Merged
merged 1 commit into from
Apr 4, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions lib/rbi/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,79 @@ def <<(param)
@params << param
end

sig { params(name: String).void }
def add_param(name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these are all in the form add_..., perhaps it could be shortened to param, opt_param etc? No big deal though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is with add_sig, defining a method named sig in a Sorbet project is creating all kind of trouble. So I went with add_ prefix everywhere

@params << ReqParam.new(name)
end

sig { params(name: String, default_value: String).void }
def add_opt_param(name, default_value)
@params << OptParam.new(name, default_value)
end

sig { params(name: String).void }
def add_rest_param(name)
@params << RestParam.new(name)
end

sig { params(name: String).void }
def add_kw_param(name)
@params << KwParam.new(name)
end

sig { params(name: String, default_value: String).void }
def add_kw_opt_param(name, default_value)
@params << KwOptParam.new(name, default_value)
end

sig { params(name: String).void }
def add_kw_rest_param(name)
@params << KwRestParam.new(name)
end

sig { params(name: String).void }
def add_block_param(name)
@params << BlockParam.new(name)
end

sig do
params(
params: T::Array[SigParam],
return_type: T.nilable(String),
is_abstract: T::Boolean,
is_override: T::Boolean,
is_overridable: T::Boolean,
is_final: T::Boolean,
type_params: T::Array[String],
checked: T.nilable(Symbol),
block: T.proc.params(node: Sig).void,
).void
end
def add_sig(
params: [],
return_type: nil,
is_abstract: false,
is_override: false,
is_overridable: false,
is_final: false,
type_params: [],
checked: nil,
&block
)
sig = Sig.new(
params: params,
return_type: return_type,
is_abstract: is_abstract,
is_override: is_override,
is_overridable: is_overridable,
is_final: is_final,
type_params: type_params,
checked: checked,
&block
)
@sigs << sig
end

sig { returns(String) }
def fully_qualified_name
if is_singleton
Expand Down Expand Up @@ -1085,6 +1158,11 @@ def <<(param)
@params << param
end

sig { params(name: String, type: String).void }
Morriar marked this conversation as resolved.
Show resolved Hide resolved
def add_param(name, type)
@params << SigParam.new(name, type)
end

sig { params(other: Object).returns(T::Boolean) }
def ==(other)
return false unless other.is_a?(Sig)
Expand Down
42 changes: 42 additions & 0 deletions test/rbi/model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,48 @@ class Enum < ::T::Enum
RBI
end

def test_model_sig_builder
rbi = Tree.new do |tree|
tree << Method.new("foo") do |node|
node.add_param("p1")
node.add_opt_param("p2", "'value'")
node.add_rest_param("p3")
node.add_kw_param("p4")
node.add_kw_opt_param("p5", "'value'")
node.add_kw_rest_param("p6")
node.add_block_param("p7")

node.add_sig do |sig|
sig.add_param("p1", "T.untyped")
sig.add_param("p2", "String")
sig.return_type = "T.untyped"
end

node.add_sig do |sig|
sig.add_param("p3", "T.untyped")
sig.return_type = "void"
end

node.add_sig(type_params: ["T", "U"]) do |sig|
sig.is_abstract = true
sig.is_override = true
sig.is_overridable = true
sig.is_final = true
sig.checked = :never
sig.add_param("p4", "T.untyped")
sig.return_type = "void"
end
end
end

assert_equal(<<~RBI, rbi.string)
sig { params(p1: T.untyped, p2: String).returns(T.untyped) }
sig { params(p3: T.untyped).void }
sig(:final) { abstract.override.overridable.type_parameters(:T, :U).checked(:never).params(p4: T.untyped).void }
def foo(p1, p2 = 'value', *p3, p4:, p5: 'value', **p6, &p7); end
RBI
end

def test_model_fully_qualified_names
mod = Module.new("Foo")
assert_equal("::Foo", mod.fully_qualified_name)
Expand Down
Loading