-
Notifications
You must be signed in to change notification settings - Fork 461
Parse RBS files as RDoc input #1728
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ab88253
Add native RBS parser support
st0012 6438b5f
Clarify RBS live reload state
st0012 802138b
Consolidate live reload file change state
st0012 4a74a38
Simplify live reload change handling
st0012 4c855be
Document current RBS parser limitations
st0012 0b096ce
Map RBS constructors to new
st0012 ea51f18
Document remaining RBS parser follow-ups
st0012 08f6a0b
Refine RBS namespace parsing
st0012 21e9e35
Address RBS parser review feedback
st0012 13804b7
Document native RBS parser support
st0012 4481644
Refine native RBS parser review feedback
st0012 eda206e
Tighten native RBS parser tests
st0012 a97735b
Address RBS parser review feedback
st0012 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,265 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rbs' | ||
|
|
||
| ## | ||
| # Parse RBS signature files as first-class RDoc input. | ||
|
|
||
| class RDoc::Parser::RBS < RDoc::Parser | ||
| RBS_FILE_EXTENSION = /\.rbs$/ | ||
|
|
||
| parse_files_matching RBS_FILE_EXTENSION | ||
|
|
||
| def scan | ||
| _, _, decls = ::RBS::Parser.parse_signature(@content) | ||
| decls.each do |decl| | ||
| parse_decl decl, @top_level | ||
| end | ||
| @top_level | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def record_object_location(object, location) | ||
| object.line = location.start_line if location | ||
|
|
||
| if RDoc::ClassModule === object | ||
| @top_level.add_to_classes_or_modules object unless | ||
| @top_level.classes_or_modules.include? object | ||
| object.record_location @top_level | ||
| else | ||
| object.record_location @top_level | ||
| end | ||
|
|
||
| object | ||
| end | ||
|
|
||
| def rdoc_comment_for(decl, context) | ||
| rbs_comment = decl.comment if decl.respond_to?(:comment) | ||
| return unless rbs_comment | ||
|
|
||
| # TODO: Run RBS comments through RDoc's directive preprocessor so | ||
| # directives like :nodoc: affect the documented object. | ||
| comment = RDoc::Comment.new rbs_comment.string, context | ||
| comment.format = 'markdown' | ||
| comment | ||
| end | ||
|
|
||
| def local_module_name(type_name, namespace) | ||
| name = type_name.to_s | ||
| return name if name.start_with?('::') | ||
|
|
||
| namespace_names = namespace ? namespace.to_s.split('::') : [] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about: def local_module_name(type_name, context)
...
namespace_names = TopLevel === context ? context.full_name.split('::') : []
...
endPassing |
||
|
|
||
| namespace_names.length.downto(1) do |length| | ||
| qualified_name = namespace_names.take(length).join('::') | ||
| if module_name = @top_level.find_module_named("#{qualified_name}::#{name}") | ||
| return module_name.full_name | ||
| end | ||
| end | ||
|
|
||
| name | ||
| end | ||
|
|
||
| def merge_attr_rw(existing_rw, new_rw) | ||
| rw = +'' | ||
| rw << 'R' if existing_rw.include?('R') || new_rw.include?('R') | ||
| rw << 'W' if existing_rw.include?('W') || new_rw.include?('W') | ||
| rw | ||
| end | ||
|
|
||
| def merge_documentation(object, comment, type_signature_lines) | ||
| if comment | ||
| object.comment = if object.comment.empty? | ||
| comment | ||
| else | ||
|
st0012 marked this conversation as resolved.
|
||
| "#{object.comment}\n---\n#{comment}" | ||
| end | ||
| end | ||
|
|
||
| # TODO: Track RBS-owned documentation overlays so incremental reparsing can | ||
| # replace stale comments and signatures from the previous RBS parse. | ||
| object.type_signature_lines ||= type_signature_lines | ||
|
st0012 marked this conversation as resolved.
|
||
| end | ||
|
st0012 marked this conversation as resolved.
|
||
|
|
||
| def merge_attribute_methods(context, name, rw, singleton, comment, type_signature_lines) | ||
| method_names = [] | ||
| method_names << name if rw.include?('R') | ||
| method_names << "#{name}=" if rw.include?('W') | ||
|
|
||
| methods = method_names.map { |method_name| context.find_method(method_name, singleton) } | ||
| methods.compact.each do |method| | ||
| merge_documentation method, comment, type_signature_lines | ||
| end | ||
|
|
||
| methods.all? | ||
| end | ||
|
|
||
| def rdoc_method_name(decl) | ||
| rbs_constructor_decl?(decl) ? 'new' : decl.name.to_s | ||
| end | ||
|
|
||
| def rdoc_method_singleton?(decl) | ||
| # TODO: RBS `self?` methods are :singleton_instance and should add both a | ||
| # singleton method and a private instance method. | ||
| rbs_constructor_decl?(decl) || decl.singleton? | ||
| end | ||
|
|
||
| def rdoc_method_visibility(decl) | ||
| rbs_constructor_decl?(decl) ? :public : decl.visibility | ||
| end | ||
|
|
||
| def rbs_constructor_decl?(decl) | ||
| decl.kind == :instance && decl.name == :initialize | ||
| end | ||
|
|
||
| def parse_attr_decl(decl, context) | ||
| rw = case decl | ||
| when ::RBS::AST::Members::AttrReader | ||
| 'R' | ||
| when ::RBS::AST::Members::AttrWriter | ||
| 'W' | ||
| when ::RBS::AST::Members::AttrAccessor | ||
| 'RW' | ||
| end | ||
|
|
||
| comment = rdoc_comment_for(decl, context) | ||
| type_signature_lines = [decl.type.to_s] | ||
| name = decl.name.to_s | ||
| singleton = decl.kind == :singleton | ||
| if attribute = context.find_attribute(name, singleton) | ||
| merge_documentation attribute, comment, type_signature_lines | ||
| attribute.rw = merge_attr_rw attribute.rw, rw | ||
| return | ||
| end | ||
|
|
||
| if merge_attribute_methods(context, name, rw, singleton, comment, type_signature_lines) | ||
| return | ||
| end | ||
|
|
||
| attribute = RDoc::Attr.new( | ||
| name, | ||
| rw, | ||
| comment, | ||
| singleton: singleton | ||
| ) | ||
| record_object_location attribute, decl.location | ||
| attribute.type_signature_lines = type_signature_lines | ||
| context.add_attribute attribute | ||
| attribute.visibility = decl.visibility if decl.visibility | ||
| end | ||
|
|
||
| def parse_class_decl(decl, context, _namespace) | ||
| owner, name = context.find_or_create_constant_owner_name decl.name | ||
| superclass = decl.super_class&.name&.to_s || '::Object' | ||
| klass = owner.add_class RDoc::NormalClass, name, superclass | ||
| record_object_location klass, decl.location | ||
| klass.add_comment rdoc_comment_for(decl, @top_level), @top_level if decl.comment | ||
|
|
||
| decl.members.each { |member| parse_decl member, klass, klass.full_name } | ||
| end | ||
|
|
||
| def parse_constant_decl(decl, context) | ||
| constant = RDoc::Constant.new decl.name.to_s, decl.type.to_s, | ||
| rdoc_comment_for(decl, context) | ||
| record_object_location constant, decl.location | ||
| context.add_constant constant | ||
| end | ||
|
|
||
| def parse_decl(decl, context, namespace = nil) | ||
| case decl | ||
| when ::RBS::AST::Declarations::Class | ||
| parse_class_decl decl, context, namespace | ||
| when ::RBS::AST::Declarations::Module, ::RBS::AST::Declarations::Interface | ||
| parse_module_decl decl, context, namespace | ||
| when ::RBS::AST::Declarations::ClassAlias, | ||
| ::RBS::AST::Declarations::ModuleAlias | ||
| # TODO: Add RBS class and module aliases to the RDoc store. | ||
| nil | ||
| else | ||
| parse_member_decl decl, context, namespace | ||
| end | ||
| end | ||
|
|
||
| def parse_extend_decl(decl, context, namespace) | ||
| extend_decl = RDoc::Extend.new local_module_name(decl.name, namespace), | ||
| rdoc_comment_for(decl, context) | ||
| record_object_location extend_decl, decl.location | ||
| context.add_extend extend_decl | ||
| end | ||
|
|
||
| def parse_include_decl(decl, context, namespace) | ||
| include_decl = RDoc::Include.new local_module_name(decl.name, namespace), | ||
| rdoc_comment_for(decl, context) | ||
| record_object_location include_decl, decl.location | ||
| context.add_include include_decl | ||
| end | ||
|
|
||
| def parse_member_decl(decl, context, namespace) | ||
| case decl | ||
| when ::RBS::AST::Declarations::Constant | ||
| parse_constant_decl decl, context | ||
| when ::RBS::AST::Members::MethodDefinition | ||
| parse_method_decl decl, context | ||
| when ::RBS::AST::Members::Alias | ||
| parse_method_alias_decl decl, context | ||
| when ::RBS::AST::Members::AttrReader, | ||
| ::RBS::AST::Members::AttrWriter, | ||
| ::RBS::AST::Members::AttrAccessor | ||
| parse_attr_decl decl, context | ||
| when ::RBS::AST::Members::Include | ||
| parse_include_decl decl, context, namespace | ||
| when ::RBS::AST::Members::Extend | ||
| parse_extend_decl decl, context, namespace | ||
| when ::RBS::AST::Members::Private, | ||
| ::RBS::AST::Members::Public | ||
| # TODO: Track standalone RBS visibility members. | ||
| nil | ||
| end | ||
| end | ||
|
|
||
| def parse_method_alias_decl(decl, context) | ||
| alias_def = RDoc::Alias.new( | ||
| decl.old_name.to_s, | ||
| decl.new_name.to_s, | ||
| rdoc_comment_for(decl, context), | ||
| singleton: decl.kind == :singleton | ||
| ) | ||
| record_object_location alias_def, decl.location | ||
| context.add_alias alias_def | ||
| end | ||
|
|
||
| def parse_method_decl(decl, context) | ||
| comment = rdoc_comment_for(decl, context) | ||
| type_signature_lines = decl.overloads.map { |overload| overload.method_type.to_s } | ||
| method_name = rdoc_method_name(decl) | ||
| singleton = rdoc_method_singleton?(decl) | ||
| visibility = rdoc_method_visibility(decl) | ||
|
|
||
| if method = context.find_method(method_name, singleton) | ||
| merge_documentation method, comment, type_signature_lines | ||
| return | ||
| end | ||
|
|
||
| method = RDoc::AnyMethod.new method_name, singleton: singleton | ||
| record_object_location method, decl.location | ||
| method.type_signature_lines = type_signature_lines | ||
|
|
||
| if loc = decl.location | ||
| method.start_collecting_tokens :ruby | ||
| method.add_token line_no: loc.start_line, char_no: 1, text: loc.source | ||
| end | ||
|
|
||
| method.comment = comment if decl.comment | ||
| context.add_method method | ||
| method.visibility = visibility if visibility | ||
| end | ||
|
|
||
| def parse_module_decl(decl, context, _namespace) | ||
| mod = context.add_module RDoc::NormalModule, decl.name.to_s | ||
| record_object_location mod, decl.location | ||
| mod.add_comment rdoc_comment_for(decl, @top_level), @top_level if decl.comment | ||
|
|
||
| decl.members.each { |member| parse_decl member, mod, mod.full_name } | ||
| end | ||
| end | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.