Skip to content

Commit

Permalink
Fix T::Struct constructor object creation (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
dduugg authored Jul 18, 2021
1 parent 054fb63 commit 3480c2a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## main (unreleased)

### Bug fixes

* [#71](https://github.com/dduugg/yard-sorbet/pull/71) Fix `T::Struct` constructor object creation

## 0.5.2 (2021-07-01)

### Bug fixes
Expand Down
6 changes: 3 additions & 3 deletions lib/yard-sorbet/handlers/struct_class_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def process_t_struct_props(props, class_ns)
# There is a chance that there is a custom initializer, so make sure we steal the existing docstring
# and source
docstring, directives = Directives.extract_directives(object.docstring)
# These should probably check for existing tags, and merge with any existing documentation:
props.each do |prop|
docstring.add_tag(YARD::Tags::Tag.new(:param, prop.doc, prop.types, prop.prop_name))
end
docstring.add_tag(YARD::Tags::Tag.new(:return, '', class_ns))
docstring.add_tag(YARD::Tags::Tag.new(:return, '', 'void'))
decorate_t_struct_init(object, props, docstring, directives)
register(object)
end

sig do
Expand All @@ -54,7 +54,7 @@ def decorate_t_struct_init(object, props, docstring, directives)
object.parameters = to_object_parameters(props)
# The "source" of our constructor is the field declarations
object.source ||= props.map(&:source).join("\n")
object.docstring = docstring.to_raw
object.docstring = docstring
Directives.add_directives(object.docstring, directives)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/yard-sorbet/tag_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def self.upsert_tag(docstring, tag_name, types = nil, name = nil)
params(docstring: YARD::Docstring, tag_name: String, name: T.nilable(String))
.returns(T.nilable(YARD::Tags::Tag))
end
private_class_method def self.find_tag(docstring, tag_name, name)
def self.find_tag(docstring, tag_name, name)
docstring.tags.find { |t| t.tag_name == tag_name && t.name == name }
end
end
Expand Down
29 changes: 23 additions & 6 deletions spec/yard_sorbet/handlers/struct_class_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,38 @@
end

describe 'constructor' do
it 'has the appropriate parameters' do
it 'has the parameter tag type' do
node = YARD::Registry.at('PersonStruct#initialize')
expect(node.parameters).to eq(
[['name:', nil], ['age:', nil], ['optional:', 'nil'], ['writable:', nil], ['mystery:', nil]]
)
tag = YARDSorbet::TagUtils.find_tag(node.docstring, 'param', 'name')
expect(tag&.types).to eq(['String'])
end

it 'has the nilable parameter tag type' do
node = YARD::Registry.at('PersonStruct#initialize')
tag = YARDSorbet::TagUtils.find_tag(node.docstring, 'param', 'optional')
expect(tag&.types).to eq(%w[String nil])
end

it 'return tag has type annotation' do
node = YARD::Registry.at('PersonStruct#initialize')
expect(node.tag(:return).type).to eq('void')
end

it 'uses the docstring from an explicit initializer' do
node = YARD::Registry.at('SpecializedPersonStruct#initialize')
expect(node.docstring).to eq('This is a special intializer')
end

it 'handles exceptional prop names' do
it 'handles keyword node prop names' do
node = YARD::Registry.at('ExceptionalPersonStruct#initialize')
tag = YARDSorbet::TagUtils.find_tag(node.docstring, 'param', 'end')
expect(tag&.types).to eq(['String'])
end

it 'handles const node prop names' do
node = YARD::Registry.at('ExceptionalPersonStruct#initialize')
expect(node.parameters).to eq([['end:', nil], ['Foo:', nil]])
tag = YARDSorbet::TagUtils.find_tag(node.docstring, 'param', 'Foo')
expect(tag&.types).to eq(['String'])
end
end
end

0 comments on commit 3480c2a

Please sign in to comment.