Skip to content

Commit

Permalink
Merge pull request #99 from euglena1215/support-struct-hash-initialize
Browse files Browse the repository at this point in the history
Add overload for `self.new` method when using `keyword_init: true` with `Struct.new`
  • Loading branch information
soutaro committed Sep 17, 2024
2 parents 152e8ab + 798cb39 commit 92bb8eb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/rbs/inline/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,29 @@ def translate_struct_assign_decl(decl, rbs) #: void
method_type: RBS::MethodType.new(type_params: [], type: method_type, block: nil, location: nil),
annotations: []
)

unless decl.positional_init?
new.overloads <<
RBS::AST::Members::MethodDefinition::Overload.new(
method_type: RBS::MethodType.new(
type_params: [],
type: Types::Function.empty(Types::Bases::Instance.new(location: nil)).then do |t|
t.update(required_positionals: [
RBS::Types::Function::Param.new(
type: RBS::Types::Record.new(all_fields: decl.each_attribute.map do |name, attr|
[name, attr&.type || Types::Bases::Any.new(location: nil)]
end.to_h, location: nil),
name: nil,
location: nil
)
])
end,
block: nil,
location: nil
),
annotations:[]
)
end
end

rbs << RBS::AST::Declarations::Class.new(
Expand Down
1 change: 1 addition & 0 deletions test/rbs/inline/writer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,7 @@ class Group < Struct[untyped]
attr_accessor name(): untyped
def self.new: (?name: untyped) -> instance
| ({ ?name: untyped }) -> instance
end
end
Expand Down

0 comments on commit 92bb8eb

Please sign in to comment.