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

Singleton class syntax support #8

Merged
merged 1 commit into from
Nov 21, 2020
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
46 changes: 31 additions & 15 deletions lib/yard-sorbet/sig_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,43 @@ def process
class_def = statement.children.find { |c| c.type == :list }
class_contents = class_def.children

process_class_contents(class_contents)
end

private def process_class_contents(class_contents)
class_contents.each_with_index do |child, i|
if child.type == :sclass && child.children.size == 2 && child.children[1].type == :list
singleton_class_contents = child.children[1]
process_class_contents(singleton_class_contents)
end
next unless type_signature?(child)

next_statement = class_contents[i + 1]
if %i[def defs command].include?(next_statement&.type) && !next_statement.docstring
# Swap the method definition docstring and the sig docstring.
# Parse relevant parts of the `sig` and include them as well.
docstring, directives = YARDSorbet::Directives.extract_directives(child.docstring)
parsed_sig = parse_sig(child)
enhance_tag(docstring, :abstract, parsed_sig)
enhance_tag(docstring, :return, parsed_sig)
if next_statement.type != :command
parsed_sig[:params]&.each do |name, types|
enhance_param(docstring, name, types)
end
end
next_statement.docstring = docstring.to_raw
YARDSorbet::Directives.add_directives(next_statement.docstring, directives)
child.docstring = nil
next unless processable_method?(next_statement)

process_method_definition(next_statement, child)
end
end

private def processable_method?(next_statement)
%i[def defs command].include?(next_statement&.type) && !next_statement.docstring
end

private def process_method_definition(method_node, sig_node)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also did a quick refactoring of the naming here to better represent the variable.

next_statement => method_node
child => sig_node

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😗👌

# Swap the method definition docstring and the sig docstring.
# Parse relevant parts of the `sig` and include them as well.
docstring, directives = YARDSorbet::Directives.extract_directives(sig_node.docstring)
parsed_sig = parse_sig(sig_node)
enhance_tag(docstring, :abstract, parsed_sig)
enhance_tag(docstring, :return, parsed_sig)
if method_node.type != :command
parsed_sig[:params]&.each do |name, types|
enhance_param(docstring, name, types)
end
end
method_node.docstring = docstring.to_raw
YARDSorbet::Directives.add_directives(method_node.docstring, directives)
sig_node.docstring = nil
end

private def enhance_param(docstring, name, types)
Expand Down
1 change: 0 additions & 1 deletion spec/yard_sorbet/sig_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
end

it 'handles singleton class syntax' do
skip('TODO')
node = YARD::Registry.at('Signatures.reopening')
expect(node.docstring).to eq('comment reopening')
end
Expand Down