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

Introduce Type builder interface #331

Merged
merged 2 commits into from
Jul 31, 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
1 change: 1 addition & 0 deletions lib/rbi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Error < StandardError

require "rbi/loc"
require "rbi/model"
require "rbi/type"
require "rbi/visitor"
require "rbi/index"
require "rbi/rewriters/add_sig_templates"
Expand Down
38 changes: 23 additions & 15 deletions lib/rbi/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ def add_block_param(name)
sig do
params(
params: T::Array[SigParam],
return_type: T.nilable(String),
return_type: T.any(String, Type),
is_abstract: T::Boolean,
is_override: T::Boolean,
is_overridable: T::Boolean,
Expand All @@ -586,7 +586,7 @@ def add_block_param(name)
end
def add_sig(
params: [],
return_type: nil,
return_type: "void",
Copy link
Member

Choose a reason for hiding this comment

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

Should this also be Type.void like below?

is_abstract: false,
is_override: false,
is_overridable: false,
Expand Down Expand Up @@ -928,8 +928,10 @@ def initialize(visibility, loc: nil, comments: [])
@visibility = visibility
end

sig { params(other: Visibility).returns(T::Boolean) }
sig { params(other: T.nilable(Object)).returns(T::Boolean) }
def ==(other)
return false unless other.is_a?(Visibility)

visibility == other.visibility
end

Expand Down Expand Up @@ -1105,7 +1107,7 @@ class Sig < Node
sig { returns(T::Array[SigParam]) }
attr_reader :params

sig { returns(T.nilable(String)) }
sig { returns(T.any(Type, String)) }
attr_accessor :return_type

sig { returns(T::Boolean) }
Expand All @@ -1120,7 +1122,7 @@ class Sig < Node
sig do
params(
params: T::Array[SigParam],
return_type: T.nilable(String),
return_type: T.any(Type, String),
is_abstract: T::Boolean,
is_override: T::Boolean,
is_overridable: T::Boolean,
Expand All @@ -1133,7 +1135,7 @@ class Sig < Node
end
def initialize(
params: [],
return_type: nil,
return_type: "void",
is_abstract: false,
is_override: false,
is_overridable: false,
Expand All @@ -1160,7 +1162,7 @@ def <<(param)
@params << param
end

sig { params(name: String, type: String).void }
sig { params(name: String, type: T.any(Type, String)).void }
def add_param(name, type)
@params << SigParam.new(name, type)
end
Expand All @@ -1169,7 +1171,7 @@ def add_param(name, type)
def ==(other)
return false unless other.is_a?(Sig)

params == other.params && return_type == other.return_type && is_abstract == other.is_abstract &&
params == other.params && return_type.to_s == other.return_type.to_s && is_abstract == other.is_abstract &&
is_override == other.is_override && is_overridable == other.is_overridable && is_final == other.is_final &&
type_params == other.type_params && checked == other.checked
end
Expand All @@ -1179,12 +1181,15 @@ class SigParam < NodeWithComments
extend T::Sig

sig { returns(String) }
attr_reader :name, :type
attr_reader :name

sig { returns(T.any(Type, String)) }
attr_reader :type

sig do
params(
name: String,
type: String,
type: T.any(Type, String),
loc: T.nilable(Loc),
comments: T::Array[Comment],
block: T.nilable(T.proc.params(node: SigParam).void),
Expand All @@ -1199,7 +1204,7 @@ def initialize(name, type, loc: nil, comments: [], &block)

sig { params(other: Object).returns(T::Boolean) }
def ==(other)
other.is_a?(SigParam) && name == other.name && type == other.type
other.is_a?(SigParam) && name == other.name && type.to_s == other.type.to_s
end
end

Expand Down Expand Up @@ -1229,15 +1234,18 @@ class TStructField < NodeWithComments
abstract!

sig { returns(String) }
attr_accessor :name, :type
attr_accessor :name

sig { returns(T.any(Type, String)) }
attr_accessor :type

sig { returns(T.nilable(String)) }
attr_accessor :default

sig do
params(
name: String,
type: String,
type: T.any(Type, String),
default: T.nilable(String),
loc: T.nilable(Loc),
comments: T::Array[Comment],
Expand All @@ -1260,7 +1268,7 @@ class TStructConst < TStructField
sig do
params(
name: String,
type: String,
type: T.any(Type, String),
default: T.nilable(String),
loc: T.nilable(Loc),
comments: T::Array[Comment],
Expand Down Expand Up @@ -1290,7 +1298,7 @@ class TStructProp < TStructField
sig do
params(
name: String,
type: String,
type: T.any(Type, String),
default: T.nilable(String),
loc: T.nilable(Loc),
comments: T::Array[Comment],
Expand Down
2 changes: 1 addition & 1 deletion lib/rbi/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ def visit_call_node(node)
end
end
when "void"
@current.return_type = nil
@current.return_type = "void"
Copy link
Member

Choose a reason for hiding this comment

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

Again, maybe this should be Type.void? Or do want to wait for the Type parser to make that change?

Copy link
Collaborator Author

@Morriar Morriar Jul 31, 2024

Choose a reason for hiding this comment

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

Yeah I should have changed all of them to "void" for now

end

visit(node.receiver)
Expand Down
14 changes: 7 additions & 7 deletions lib/rbi/printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ def print_param_comment_leading_space(node, last:)
def print_sig_param_comment_leading_space(node, last:)
printn
printt
print(" " * (node.name.size + node.type.size + 3))
print(" " * (node.name.size + node.type.to_s.size + 3))
print(" ") unless last
end

Expand Down Expand Up @@ -654,10 +654,10 @@ def print_sig_as_line(node)
print(").")
end
return_type = node.return_type
if node.return_type && node.return_type != "void"
print("returns(#{return_type})")
else
if node.return_type.to_s == "void"
print("void")
else
print("returns(#{return_type})")
end
printn(" }")
end
Expand Down Expand Up @@ -707,10 +707,10 @@ def print_sig_as_block(node)
print(".") if modifiers.any? || params.any?

return_type = node.return_type
if return_type && return_type != "void"
print("returns(#{return_type})")
else
if return_type.to_s == "void"
print("void")
else
print("returns(#{return_type})")
end
printn
dedent
Expand Down
4 changes: 2 additions & 2 deletions lib/rbi/rewriters/attr_to_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def convert_to_methods; end

private

sig(:final) { returns([T.nilable(Sig), T.nilable(String)]) }
sig(:final) { returns([T.nilable(Sig), T.nilable(T.any(Type, String))]) }
def parse_sig
raise UnexpectedMultipleSigsError, self if 1 < sigs.count

Expand Down Expand Up @@ -101,7 +101,7 @@ def create_getter_method(name, sig, visibility, loc, comments)
params(
name: String,
sig: T.nilable(Sig),
attribute_type: T.nilable(String),
attribute_type: T.nilable(T.any(Type, String)),
visibility: Visibility,
loc: T.nilable(Loc),
comments: T::Array[Comment],
Expand Down
Loading
Loading