Skip to content

Commit fbe975c

Browse files
authored
Merge pull request #179 from rhiroe/patch-hiroe-2
Set the generics of the struct based on the type of its attributes
2 parents 6b4d598 + df02b18 commit fbe975c

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

lib/rbs/inline/writer.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,12 @@ def translate_struct_assign_decl(decl, rbs) #: void
428428
members: [*attributes, new],
429429
super_class: RBS::AST::Declarations::Class::Super.new(
430430
name: RBS::TypeName.new(name: :Struct, namespace: RBS::Namespace.empty),
431-
args: [RBS::Types::Bases::Any.new(location: nil)],
431+
args: [
432+
RBS::Types::Union.new(
433+
types: decl.each_attribute.map { |_, attr| attr&.type || RBS::Types::Bases::Any.new(location: nil) }.uniq,
434+
location: nil
435+
)
436+
],
432437
location: nil
433438
),
434439
annotations: decl.class_annotations,

test/rbs/inline/writer_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ class Account
10111011

10121012
assert_equal <<~RBS, output
10131013
# Account record
1014-
class Account < Struct[untyped]
1014+
class Account < Struct[Integer | String]
10151015
attr_accessor id(): Integer
10161016
10171017
attr_accessor email(): String
@@ -1029,7 +1029,7 @@ def self.new: (?name: untyped) -> instance
10291029
end
10301030
end
10311031
1032-
class Item < Struct[untyped]
1032+
class Item < Struct[String | Integer]
10331033
attr_accessor sku(): String
10341034
10351035
attr_accessor price(): Integer
@@ -1041,7 +1041,7 @@ def self.new: (?String sku, ?Integer price) -> instance
10411041
# @rbs %a{rbs-inline:readonly-attributes=true}
10421042
%a{rbs-inline:new-args=required}
10431043
%a{rbs-inline:readonly-attributes=true}
1044-
class User < Struct[untyped]
1044+
class User < Struct[String]
10451045
attr_reader name(): String
10461046
10471047
def self.new: (String name) -> instance

0 commit comments

Comments
 (0)