Skip to content

Commit 914dfc7

Browse files
committed
Type check lib/rdoc/parser/rbs.rb
1 parent 287390a commit 914dfc7

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

lib/rdoc/parser/rbs.rb

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,19 @@ def parse_member(decl:, context:, outer_name: nil)
6565
def parse_class_decl(decl:, context:, outer_name: nil)
6666
full_name = fully_qualified_name(outer_name: outer_name, decl: decl)
6767
klass = context.add_class(RDoc::NormalClass, full_name.to_s, decl.super_class&.name&.to_s || "::Object")
68-
klass.add_comment(construct_comment(context: context, comment: decl.comment.string), context) if decl.comment
68+
klass.add_comment(construct_comment(context: context, comment: comment_string(decl)), context) if decl.comment
6969
decl.members.each { |member| parse_member(decl: member, context: context, outer_name: full_name) }
7070
end
7171

7272
def parse_module_decl(decl:, context:, outer_name: nil)
7373
full_name = fully_qualified_name(outer_name: outer_name, decl: _ = decl)
7474
kmodule = context.add_module(RDoc::NormalModule, full_name.to_s)
75-
kmodule.add_comment(construct_comment(context: context, comment: decl.comment.string), context) if decl.comment
75+
kmodule.add_comment(construct_comment(context: context, comment: comment_string(decl)), context) if decl.comment
7676
decl.members.each { |member| parse_member(decl: member, context: context, outer_name: full_name) }
7777
end
7878

7979
def parse_constant_decl(decl:, context:, outer_name: nil)
80-
comment = decl.comment ? construct_comment(context: context, comment: decl.comment.string) : nil
80+
comment = decl.comment ? construct_comment(context: context, comment: comment_string(decl)) : nil
8181
constant = RDoc::Constant.new(decl.name.to_s, decl.type.to_s, comment)
8282
context.add_constant(constant)
8383
end
@@ -87,19 +87,19 @@ def parse_method_decl(decl:, context:, outer_name: nil)
8787
method.singleton = decl.singleton?
8888
method.visibility = decl.visibility
8989
method.call_seq = decl.types.map { |type| "#{decl.name.to_s}#{type.to_s}" }.join("\n")
90-
if decl.location
90+
if loc = decl.location
9191
method.start_collecting_tokens
92-
method.add_token({ line_no: 1, char_no: 1, kind: :on_comment, text: "# File #{@top_level.relative_name}, line(s) #{decl.location.start_line}:#{decl.location.end_line}\n" })
93-
method.add_token({ line_no: 1, char_no: 1, text: decl.location.source })
94-
method.line = decl.location.start_line if decl.location
92+
method.add_token({ line_no: 1, char_no: 1, kind: :on_comment, text: "# File #{@top_level.relative_name}, line(s) #{loc.start_line}:#{loc.end_line}\n" })
93+
method.add_token({ line_no: 1, char_no: 1, text: loc.source })
94+
method.line = loc.start_line
9595
end
96-
method.comment = construct_comment(context: context, comment: decl.comment.string) if decl.comment
96+
method.comment = construct_comment(context: context, comment: comment_string(decl)) if decl.comment
9797
context.add_method(method)
9898
end
9999

100100
def parse_method_alias_decl(decl:, context:, outer_name: nil)
101101
alias_def = RDoc::Alias.new(nil, decl.old_name.to_s, decl.new_name.to_s, nil, decl.kind == :singleton)
102-
alias_def.comment = construct_comment(context: context, comment: decl.comment.string) if decl.comment
102+
alias_def.comment = construct_comment(context: context, comment: comment_string(decl)) if decl.comment
103103
context.add_alias(alias_def)
104104
end
105105

@@ -114,7 +114,7 @@ def parse_attr_decl(decl:, context:, outer_name: nil)
114114
end
115115
attribute = RDoc::Attr.new(nil, decl.name.to_s, rw, nil, decl.kind == :singleton)
116116
attribute.visibility = decl.visibility
117-
attribute.comment = construct_comment(context: context, comment: decl.comment.string) if decl.comment
117+
attribute.comment = construct_comment(context: context, comment: comment_string(decl)) if decl.comment
118118
context.add_attribute(attribute)
119119
end
120120

@@ -130,7 +130,7 @@ def parse_include_decl(decl:, context:, outer_name: nil)
130130
end
131131
end
132132
include_decl = RDoc::Include.new(name, nil)
133-
include_decl.comment = construct_comment(context: context, comment: decl.comment.string) if decl.comment
133+
include_decl.comment = construct_comment(context: context, comment: comment_string(decl)) if decl.comment
134134
context.add_include(include_decl)
135135
end
136136

@@ -146,7 +146,7 @@ def parse_extend_decl(decl:, context:, outer_name: nil)
146146
end
147147
end
148148
extend_decl = RDoc::Extend.new(name, nil)
149-
extend_decl.comment = construct_comment(context: context, comment: decl.comment.string) if decl.comment
149+
extend_decl.comment = construct_comment(context: context, comment: comment_string(decl)) if decl.comment
150150
context.add_extend(extend_decl)
151151
end
152152

@@ -158,6 +158,11 @@ def construct_comment(context:, comment:)
158158
comment
159159
end
160160

161+
def comment_string(with_comment)
162+
comment = with_comment.comment or raise "Object with `#comment` returning a object is expected"
163+
comment.string
164+
end
165+
161166
def fully_qualified_name(outer_name:, decl:)
162167
if outer_name
163168
(outer_name + decl.name)

sig/rdoc/rbs.rbs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
module RBS
22
module RDocPlugin
33
class Parser
4-
type allowed_decls = RBS::AST::Declarations::Class | RBS::AST::Declarations::Module | RBS::AST::Declarations::Constant | RBS::AST::Members::MethodDefinition | RBS::AST::Members::AttrReader | RBS::AST::Members::AttrAccessor | RBS::AST::Members::AttrWriter | RBS::AST::Members::Include | RBS::AST::Members::Extend
4+
type allowed_decls = RBS::AST::Declarations::Class
5+
| RBS::AST::Declarations::Module
6+
| RBS::AST::Declarations::Constant
57

68
def initialize: (RDoc::TopLevel top_level, String content) -> void
79

@@ -29,6 +31,25 @@ module RBS
2931

3032
def construct_comment: (context: RDoc::Context, comment: String) -> RDoc::Comment
3133

34+
# _DeclWithComment is a utility interface that has `#comment`
35+
#
36+
interface _DeclWithComment
37+
def comment: () -> ::RBS::AST::Comment?
38+
end
39+
40+
# Extract comment string from a declaration with comment
41+
#
42+
# * If `#comment" returns a Comment object, it returns its `#string` value
43+
# * If `#comment` returns `nil`, it raises an error
44+
#
45+
# Note that you have to confirm if the `#comment` of given declartion exists.
46+
#
47+
# ```ruby
48+
# comment = construct_comment(context: context, comment: comment_string(decl)) if decl.comment
49+
# ```
50+
#
51+
def comment_string: (_DeclWithComment) -> String
52+
3253
def fully_qualified_name: (outer_name: RBS::TypeName?, decl: allowed_decls) -> RBS::TypeName
3354
end
3455
end

0 commit comments

Comments
 (0)