Skip to content

Commit

Permalink
Support nested structs of the form A::B = Struct.new(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
marshall-lee committed Jan 16, 2025
1 parent efb33b5 commit f26f250
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/yard/handlers/ruby/constant_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def process_constant(statement)
end

def process_structclass(statement)
lhs = statement[0][0]
if lhs.type == :const
klass = create_class(lhs[0], P(:Struct))
lhs = statement[0]
if (lhs.type == :var_field && lhs[0].type == :const) || lhs.type == :const_path_field
klass = create_class(lhs.source, P(:Struct))
create_attributes(klass, extract_parameters(statement[1]))
parse_block(statement[1].block[1], :namespace => klass) unless statement[1].block.nil?
else
Expand Down
11 changes: 11 additions & 0 deletions spec/handlers/constant_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@
expect(obj.attributes[:instance]).to be_empty
end

it "turns A::Const = Struct.new(:sym) into class A::Const with attr :sym" do
obj = Registry.at("A::NestedCompactClass")
expect(obj).to be_kind_of(CodeObjects::ClassObject)
attrs = obj.attributes[:instance]
[:b, :c].each do |key|
expect(attrs).to have_key(key)
expect(attrs[key][:read]).not_to be nil
expect(attrs[key][:write]).not_to be nil
end
end

it "maintains docstrings on structs defined via constants" do
obj = Registry.at("DocstringStruct")
expect(obj).not_to be nil
Expand Down
1 change: 1 addition & 0 deletions spec/handlers/examples/constant_handler_001.rb.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ end
MyClass = Struct.new(:a, :b, :c)
NotMyClass = Struct.new("NotMyClass2", :b, :c)
MyEmptyStruct = Struct.new
A::NestedCompactClass = Struct.new(:b, :c)

MyStructWithConstant = Struct.new do
# A constant.
Expand Down

0 comments on commit f26f250

Please sign in to comment.