Skip to content

Commit 244e347

Browse files
committed
RDoc: Add support for generics in RDoc
1 parent bc8c7ae commit 244e347

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

lib/rdoc_plugin/parser.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,27 @@ def parse_member(decl:, context:, outer_name: nil)
5252

5353
def parse_class_decl(decl:, context:, outer_name: nil)
5454
full_name = fully_qualified_name(outer_name: outer_name, decl: decl)
55-
klass = context.add_class(RDoc::NormalClass, full_name.to_s, decl.super_class&.name&.to_s || "::Object")
55+
klass = context.add_class(RDoc::NormalClass, full_name.to_s, decl.super_class&.name&.to_s || "::Object",
56+
decl.type_params.map do |type_param|
57+
RDoc::TypeParameter.new(type_param.name.to_s,
58+
type_param.variance,
59+
type_param.unchecked?,
60+
type_param.upper_bound&.name&.to_s)
61+
end
62+
)
5663
klass.add_comment(construct_comment(context: context, comment: comment_string(decl)), context) if decl.comment
5764
decl.members.each { |member| parse_member(decl: member, context: context, outer_name: full_name) }
5865
end
5966

6067
def parse_module_decl(decl:, context:, outer_name: nil)
6168
full_name = fully_qualified_name(outer_name: outer_name, decl: _ = decl)
62-
kmodule = context.add_module(RDoc::NormalModule, full_name.to_s)
69+
kmodule = context.add_module(RDoc::NormalModule, full_name.to_s,
70+
decl.type_params.map do |type_param|
71+
RDoc::TypeParameter.new(type_param.name.to_s,
72+
type_param.variance,
73+
type_param.unchecked?,
74+
type_param.upper_bound&.name&.to_s)
75+
end)
6376
kmodule.add_comment(construct_comment(context: context, comment: comment_string(decl)), context) if decl.comment
6477
decl.members.each { |member| parse_member(decl: member, context: context, outer_name: full_name) }
6578
end

stdlib/rdoc/0/rdoc.rbs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ module RDoc
228228
# later sees `class Container`. `add_class` automatically upgrades `given_name`
229229
# to a class in this case.
230230
#
231-
def add_class: (class_types class_type, ::String given_name, ?::String superclass) -> (RDoc::NormalClass | RDoc::SingleClass)
231+
def add_class: (class_types class_type, ::String given_name, ?::String superclass, ?Array[RDoc::TypeParameter]) -> (RDoc::NormalClass | RDoc::SingleClass)
232232

233233
# <!--
234234
# rdoc-file=lib/rdoc/context.rb
@@ -271,7 +271,7 @@ module RDoc
271271
# Adds a module named `name`. If RDoc already knows `name` is a class then that
272272
# class is returned instead. See also #add_class.
273273
#
274-
def add_module: (singleton(RDoc::NormalModule) class_type, String name) -> RDoc::NormalModule
274+
def add_module: (singleton(RDoc::NormalModule) class_type, String name, ?Array[RDoc::TypeParameter]) -> RDoc::NormalModule
275275

276276
# <!--
277277
# rdoc-file=lib/rdoc/context.rb
@@ -749,6 +749,25 @@ module RDoc
749749
def initialize: (String? text, String name, String old_name, RDoc::Comment? comment, ?bool `singleton`) -> void
750750
end
751751

752+
class TypeParameter < CodeObject
753+
type variance = :invariant | :covariant | :contravariant
754+
755+
attr_reader name: String
756+
attr_reader variance: variance
757+
attr_reader upper_bound: String?
758+
attr_reader `unchecked`: bool
759+
760+
def initialize: (String name, Symbol variance, ?bool `unchecked`, ?String? upper_bound) -> void
761+
762+
def ==: (untyped other) -> bool
763+
764+
alias eql? ==
765+
766+
def unchecked?: () -> bool
767+
768+
def to_s: () -> String
769+
end
770+
752771
# <!-- rdoc-file=lib/rdoc/stats.rb -->
753772
# RDoc statistics collector which prints a summary and report of a project's
754773
# documentation totals.

0 commit comments

Comments
 (0)