Skip to content

Commit cdd8459

Browse files
committed
Add signatures for RDoc code
1 parent 215a923 commit cdd8459

File tree

7 files changed

+257
-34
lines changed

7 files changed

+257
-34
lines changed

Steepfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ target :lib do
99
"lib/rbs/test.rb"
1010
)
1111

12-
library "set", "pathname", "json", "logger", "monitor", "tsort", "uri", 'yaml', 'dbm', 'pstore', 'singleton', 'shellwords', 'fileutils', 'find', 'digest'
12+
library "set", "pathname", "json", "logger", "monitor", "tsort", "uri", 'yaml', 'dbm', 'pstore', 'singleton', 'shellwords', 'fileutils', 'find', 'digest'#, 'rdoc'
1313
signature "stdlib/strscan/0/"
1414
signature "stdlib/rubygems/0/"
1515
signature "stdlib/optparse/0/"
16+
signature "stdlib/rdoc/0/"
1617

1718
configure_code_diagnostics do |config|
1819
config[D::Ruby::MethodDefinitionMissing] = :hint

lib/rdoc/parser/rbs.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ def parse_class_decl(decl:, context:, outer_name: nil)
5454
end
5555

5656
def parse_module_decl(decl:, context:, outer_name: nil)
57-
full_name = fully_qualified_name(outer_name: outer_name, decl: decl)
57+
full_name = fully_qualified_name(outer_name: outer_name, decl: _ = decl)
5858
kmodule = context.add_module(RDoc::NormalModule, full_name.to_s)
59-
kmodule.add_comment(construct_comment(context: context, comment: decl.comment.string), context) if decl.comment
59+
# @type var comment: RBS::AST::Comment
60+
kmodule.add_comment(construct_comment(context: context, comment: comment.string), context) if (comment = decl.comment)
6061
decl.members.each { |member| parse_member(decl: member, context: context, outer_name: full_name) }
6162
end
6263

@@ -130,7 +131,7 @@ def parse_extend_decl(decl:, context:, outer_name: nil)
130131
end
131132
end
132133
extend_decl = RDoc::Extend.new(name, nil)
133-
exclude_decl.comment = construct_comment(context: context, comment: decl.comment.string) if decl.comment
134+
extend_decl.comment = construct_comment(context: context, comment: decl.comment.string) if decl.comment
134135
context.add_extend(extend_decl)
135136
end
136137

sig/manifest.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ dependencies:
66
- name: optparse
77
- name: rubygems
88
- name: tsort
9+
- name: rdoc

sig/polyfill.rbs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ module RDoc
3939
end
4040
end
4141

42-
class CodeObject
43-
def comment: () -> RDoc::Markup::Document
44-
end
45-
4642
class Context < CodeObject
4743

4844
end
@@ -55,32 +51,6 @@ module RDoc
5551
def attributes: () -> Array[Attr]
5652
end
5753

58-
class Constant < CodeObject
59-
def name: () -> String
60-
end
61-
62-
class AnyMethod < MethodAttr
63-
def arglists: () -> String?
64-
65-
def callseq: () -> String?
66-
end
67-
68-
class MethodAttr < CodeObject
69-
attr_reader name: String
70-
71-
attr_reader singleton: bool
72-
73-
attr_reader is_alias_for: MethodAttr?
74-
75-
attr_reader call_seq: String
76-
77-
attr_reader arglists: String
78-
end
79-
80-
class Attr < MethodAttr
81-
attr_accessor rw: "RW" | "R" | "W"
82-
end
83-
8454
module Markup
8555
class Document
8656
include Enumerable[Document]

sig/rdoc/rbs.rbs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class RDoc::Parser::RBS < RDoc::Parser
2+
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
3+
4+
def initialize: (RDoc::TopLevel top_level, String filename, String content, Hash[untyped, untyped] options, RDoc::Stats stats) -> void
5+
6+
def scan: () -> RDoc::TopLevel
7+
8+
def parse_member: (decl: RBS::AST::Declarations::t | RBS::AST::Members::t, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
9+
10+
def parse_class_decl: (decl: RBS::AST::Declarations::Class, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
11+
12+
def parse_module_decl: (decl: RBS::AST::Declarations::Module | RBS::AST::Declarations::Interface, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
13+
14+
def parse_constant_decl: (decl: RBS::AST::Declarations::Constant, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
15+
16+
def parse_method_decl: (decl: RBS::AST::Members::MethodDefinition, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
17+
18+
def parse_method_alias_decl: (decl: RBS::AST::Members::Alias, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
19+
20+
def parse_attr_decl: (decl: RBS::AST::Members::AttrReader | RBS::AST::Members::AttrAccessor | RBS::AST::Members::AttrWriter, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
21+
22+
def parse_include_decl: (decl: RBS::AST::Members::Include, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
23+
24+
def parse_extend_decl: (decl: RBS::AST::Members::Extend, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
25+
26+
private
27+
28+
def construct_comment: (context: RDoc::Context, comment: String) -> RDoc::Comment
29+
30+
def fully_qualified_name: (outer_name: RBS::TypeName?, decl: allowed_decls) -> RBS::TypeName
31+
end

stdlib/rdoc/0/rdoc.rbs

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
module RDoc
2+
class Parser
3+
def self?.parse_files_matching: (Regexp path) -> void
4+
5+
def initialize: (RDoc::TopLevel top_level, String filename, String content, Hash[untyped, untyped] options, RDoc::Stats stats) -> void
6+
7+
def scan: () -> RDoc::TopLevel
8+
end
9+
10+
class CodeObject
11+
attr_reader comment: RDoc::Comment
12+
13+
def initialize: () -> void
14+
15+
def comment=: (RDoc::Comment | String) -> RDoc::Comment
16+
end
17+
18+
class Context < CodeObject
19+
include Comparable
20+
21+
TYPES: ::Array["class" | "instance"]
22+
23+
TOMDOC_TITLES: ::Array[nil | "Public" | "Internal" | "Deprecated"]
24+
25+
type class_types = singleton(RDoc::NormalClass) | singleton(RDoc::SingleClass)
26+
27+
def initialize: () -> void
28+
29+
def add_alias: (RDoc::Alias an_alias) -> RDoc::Alias
30+
31+
def add_attribute: (RDoc::Attr attribute) -> RDoc::Attr
32+
33+
def add_class: (class_types class_type, ::String given_name, ?::String superclass) -> (RDoc::NormalClass | RDoc::SingleClass)
34+
35+
def add_constant: (RDoc::Constant constant) -> RDoc::Constant
36+
37+
def add_include: (RDoc::Include `include`) -> RDoc::Include
38+
39+
def add_extend: (RDoc::Extend ext) -> RDoc::Extend
40+
41+
def add_method: (RDoc::AnyMethod method) -> RDoc::AnyMethod
42+
43+
def add_module: (singleton(RDoc::NormalModule) class_type, String name) -> RDoc::NormalModule
44+
45+
def find_module_named: (untyped name) -> (untyped | self)
46+
47+
def full_name: () -> "(unknown)"
48+
49+
def to_s: () -> ::String
50+
51+
def top_level: () -> RDoc::TopLevel
52+
end
53+
54+
class TopLevel < Context
55+
MARSHAL_VERSION: 0
56+
57+
def initialize: (String absolute_name, ?String relative_name) -> void
58+
59+
def ==: (untyped other) -> bool
60+
61+
alias eql? ==
62+
63+
def add_alias: (RDoc::Alias an_alias) -> RDoc::Alias
64+
65+
def add_constant: (RDoc::Constant constant) -> RDoc::Constant
66+
67+
def add_include: (RDoc::Include `include`) -> RDoc::Include
68+
69+
def add_method: (RDoc::AnyMethod method) -> RDoc::AnyMethod
70+
71+
def find_class_or_module: (::String name) -> RDoc::Context
72+
73+
def find_module_named: (String name) -> RDoc::Context
74+
75+
def full_name: () -> String
76+
77+
def to_s: () -> ::String
78+
end
79+
80+
module TokenStream
81+
def add_token: (Hash[untyped, untyped] token) -> void
82+
83+
def collect_tokens: () -> void
84+
85+
alias start_collecting_tokens collect_tokens
86+
end
87+
88+
class Comment
89+
attr_reader format: String
90+
91+
attr_accessor location: String
92+
93+
def initialize: (?String? text, ?RDoc::Context? location, ?String? language) -> void
94+
95+
def format=: (String format) -> void
96+
end
97+
98+
class ClassModule < Context
99+
def initialize: (String name, ?String superclass) -> void
100+
101+
def add_comment: (RDoc::Comment comment, RDoc::Context location) -> void
102+
end
103+
104+
class NormalClass < ClassModule
105+
def initialize: (String name, ?String superclass) -> void
106+
end
107+
108+
class SingleClass < ClassModule
109+
def initialize: (String name, ?String superclass) -> void
110+
end
111+
112+
class NormalModule < ClassModule
113+
end
114+
115+
class MethodAttr < CodeObject
116+
include Comparable
117+
118+
attr_reader is_alias_for: MethodAttr?
119+
120+
attr_reader arglists: String
121+
122+
attr_accessor name: String
123+
124+
attr_accessor visibility: untyped
125+
126+
attr_accessor `singleton`: bool
127+
128+
attr_reader text: String
129+
130+
attr_accessor call_seq: String
131+
132+
def initialize: (String text, String name) -> void
133+
134+
def pretty_name: () -> ::String
135+
136+
def type: () -> ("class" | "instance")
137+
138+
def path: () -> ::String
139+
140+
def to_s: () -> ::String
141+
end
142+
143+
class AnyMethod < MethodAttr
144+
include TokenStream
145+
146+
attr_accessor call_seq: ::String
147+
148+
attr_accessor params: ::String
149+
150+
attr_accessor line: Integer
151+
152+
def arglists: () -> String?
153+
154+
def callseq: () -> String?
155+
156+
def initialize: (String? text, String name) -> void
157+
end
158+
159+
class Attr < MethodAttr
160+
attr_accessor rw: "RW" | "R" | "W"
161+
162+
def initialize: (String? text, String name, String rw, RDoc::Comment? comment, ?bool `singleton`) -> void
163+
end
164+
165+
class Constant < CodeObject
166+
attr_writer is_alias_for: String
167+
168+
attr_accessor name: String
169+
170+
attr_accessor value: String
171+
172+
attr_accessor visibility: String
173+
174+
def initialize: (String name, String value, RDoc::Comment? comment) -> void
175+
end
176+
177+
class Mixin < CodeObject
178+
attr_accessor name: String
179+
180+
def initialize: (String name, RDoc::Comment? comment) -> void
181+
end
182+
183+
class Include < Mixin
184+
end
185+
186+
class Extend < Mixin
187+
end
188+
189+
class Alias < CodeObject
190+
attr_accessor name: String
191+
192+
attr_accessor old_name: String
193+
194+
def initialize: (String? text, String name, String old_name, RDoc::Comment? comment, ?bool `singleton`) -> void
195+
end
196+
197+
class Stats
198+
end
199+
end

test/rbs/rdoc/rbs_parser_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require "test_helper"
2+
3+
class RBSParserTest < Test::Unit::TestCase
4+
def parser(content)
5+
@tempfile = Tempfile.new self.class.name
6+
@filename = @tempfile.path
7+
8+
RDoc::TopLevel.reset
9+
@top_level = RDoc::TopLevel.new @filename
10+
11+
@options = RDoc::Options.new
12+
@options.quiet = true
13+
@stats = RDoc::Stats.new 0
14+
@parser = RDoc::Parser::RBS.new(@top_level, @filename, content, @options, @stats)
15+
end
16+
17+
def teardown
18+
@tempfile.close
19+
end
20+
end

0 commit comments

Comments
 (0)