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

Set the generics of the struct based on the type of its attributes #179

Merged
merged 1 commit into from
Jan 23, 2025
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
7 changes: 6 additions & 1 deletion lib/rbs/inline/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,12 @@ def translate_struct_assign_decl(decl, rbs) #: void
members: [*attributes, new],
super_class: RBS::AST::Declarations::Class::Super.new(
name: RBS::TypeName.new(name: :Struct, namespace: RBS::Namespace.empty),
args: [RBS::Types::Bases::Any.new(location: nil)],
args: [
RBS::Types::Union.new(
types: decl.each_attribute.map { |_, attr| attr&.type || RBS::Types::Bases::Any.new(location: nil) }.uniq,
location: nil
)
],
location: nil
),
annotations: decl.class_annotations,
Expand Down
6 changes: 3 additions & 3 deletions test/rbs/inline/writer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ class Account

assert_equal <<~RBS, output
# Account record
class Account < Struct[untyped]
class Account < Struct[Integer | String]
attr_accessor id(): Integer

attr_accessor email(): String
Expand All @@ -1029,7 +1029,7 @@ def self.new: (?name: untyped) -> instance
end
end

class Item < Struct[untyped]
class Item < Struct[String | Integer]
attr_accessor sku(): String

attr_accessor price(): Integer
Expand All @@ -1041,7 +1041,7 @@ def self.new: (?String sku, ?Integer price) -> instance
# @rbs %a{rbs-inline:readonly-attributes=true}
%a{rbs-inline:new-args=required}
%a{rbs-inline:readonly-attributes=true}
class User < Struct[untyped]
class User < Struct[String]
attr_reader name(): String

def self.new: (String name) -> instance
Expand Down
Loading