docscribe 1.4.2 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +601 -139
- data/exe/docscribe-client +105 -0
- data/lib/docscribe/cli/check_for_comments.rb +183 -0
- data/lib/docscribe/cli/config_builder.rb +107 -53
- data/lib/docscribe/cli/formatters/json.rb +294 -0
- data/lib/docscribe/cli/formatters/sarif.rb +235 -0
- data/lib/docscribe/cli/formatters/text.rb +208 -0
- data/lib/docscribe/cli/formatters.rb +26 -0
- data/lib/docscribe/cli/generate.rb +56 -62
- data/lib/docscribe/cli/init.rb +14 -6
- data/lib/docscribe/cli/options.rb +206 -89
- data/lib/docscribe/cli/rbs_gen.rb +529 -0
- data/lib/docscribe/cli/run.rb +433 -154
- data/lib/docscribe/cli/server.rb +135 -0
- data/lib/docscribe/cli/sigs.rb +366 -0
- data/lib/docscribe/cli/update_types.rb +103 -0
- data/lib/docscribe/cli.rb +21 -24
- data/lib/docscribe/config/defaults.rb +7 -2
- data/lib/docscribe/config/emit.rb +17 -0
- data/lib/docscribe/config/filtering.rb +17 -24
- data/lib/docscribe/config/loader.rb +19 -17
- data/lib/docscribe/config/plugin.rb +1 -1
- data/lib/docscribe/config/rbs.rb +39 -7
- data/lib/docscribe/config/sorbet.rb +22 -16
- data/lib/docscribe/config/sorting.rb +1 -1
- data/lib/docscribe/config/template.rb +10 -1
- data/lib/docscribe/config/utils.rb +11 -9
- data/lib/docscribe/config.rb +10 -6
- data/lib/docscribe/infer/ast_walk.rb +1 -1
- data/lib/docscribe/infer/literals.rb +6 -11
- data/lib/docscribe/infer/names.rb +2 -3
- data/lib/docscribe/infer/params.rb +14 -16
- data/lib/docscribe/infer/raises.rb +3 -5
- data/lib/docscribe/infer/returns.rb +615 -151
- data/lib/docscribe/infer.rb +29 -26
- data/lib/docscribe/inline_rewriter/collector.rb +159 -164
- data/lib/docscribe/inline_rewriter/doc_block.rb +145 -115
- data/lib/docscribe/inline_rewriter/doc_builder.rb +1032 -723
- data/lib/docscribe/inline_rewriter/source_helpers.rb +48 -48
- data/lib/docscribe/inline_rewriter/tag_sorter.rb +82 -85
- data/lib/docscribe/inline_rewriter.rb +485 -488
- data/lib/docscribe/lru_cache.rb +49 -0
- data/lib/docscribe/parsing.rb +28 -9
- data/lib/docscribe/plugin/base/collector_plugin.rb +2 -1
- data/lib/docscribe/plugin/base/tag_plugin.rb +0 -1
- data/lib/docscribe/plugin/context.rb +28 -18
- data/lib/docscribe/plugin/registry.rb +25 -26
- data/lib/docscribe/plugin/tag.rb +9 -14
- data/lib/docscribe/plugin.rb +17 -16
- data/lib/docscribe/server.rb +608 -0
- data/lib/docscribe/types/provider_chain.rb +4 -2
- data/lib/docscribe/types/rbs/collection_loader.rb +2 -2
- data/lib/docscribe/types/rbs/provider.rb +177 -51
- data/lib/docscribe/types/rbs/type_formatter.rb +224 -83
- data/lib/docscribe/types/signature.rb +22 -42
- data/lib/docscribe/types/sorbet/base_provider.rb +29 -21
- data/lib/docscribe/types/sorbet/rbi_provider.rb +6 -5
- data/lib/docscribe/types/sorbet/source_provider.rb +6 -4
- data/lib/docscribe/types/yard/formatter.rb +100 -0
- data/lib/docscribe/types/yard/parser.rb +240 -0
- data/lib/docscribe/types/yard/types.rb +52 -0
- data/lib/docscribe/version.rb +1 -1
- metadata +38 -1
|
@@ -10,28 +10,107 @@ module Docscribe
|
|
|
10
10
|
module TypeFormatter
|
|
11
11
|
module_function
|
|
12
12
|
|
|
13
|
-
#
|
|
13
|
+
# Dispatch an RBS type object to the appropriate YARD formatter.
|
|
14
|
+
#
|
|
15
|
+
# @note module_function: defines #to_yard (visibility: private)
|
|
16
|
+
# @param [Docscribe::Types::RBS::TypeFormatter::rbs_type, nil] type the RBS type object to convert
|
|
17
|
+
# @param [Boolean] collapse_generics whether to omit generic type arguments
|
|
18
|
+
# @param [Boolean] collapse_object_generics whether to collapse generics when all inner types are Object
|
|
19
|
+
# @return [String]
|
|
20
|
+
def to_yard(type, collapse_generics: false, collapse_object_generics: false)
|
|
21
|
+
return 'Object' unless type
|
|
22
|
+
|
|
23
|
+
handler = to_yard_formatters.find { |klass, _| type.is_a?(klass) }
|
|
24
|
+
return handler.last.call(type, cg: collapse_generics, cog: collapse_object_generics) if handler
|
|
25
|
+
|
|
26
|
+
if named_type?(type)
|
|
27
|
+
return format_named(type, # steep:ignore
|
|
28
|
+
collapse_generics: collapse_generics,
|
|
29
|
+
collapse_object_generics: collapse_object_generics)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
fallback_string(type)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Check if the given type object is a named RBS type (class, singleton, interface, or alias).
|
|
36
|
+
#
|
|
37
|
+
# @note module_function: defines #named_type? (visibility: private)
|
|
38
|
+
# @param [Docscribe::Types::RBS::TypeFormatter::rbs_type] type the RBS type object to check
|
|
39
|
+
# @return [Boolean]
|
|
40
|
+
def named_type?(type)
|
|
41
|
+
named_type_classes.any? { |klass| type.is_a?(klass) }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Return or memoize the list of RBS type classes considered named types.
|
|
45
|
+
#
|
|
46
|
+
# @note module_function: defines #named_type_classes (visibility: private)
|
|
47
|
+
# @return [Array<Class>]
|
|
48
|
+
def named_type_classes
|
|
49
|
+
@named_type_classes ||= [
|
|
50
|
+
::RBS::Types::ClassInstance,
|
|
51
|
+
::RBS::Types::ClassSingleton,
|
|
52
|
+
::RBS::Types::Interface,
|
|
53
|
+
::RBS::Types::Alias
|
|
54
|
+
].freeze
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Fallback conversion of an unrecognized RBS type to a cleaned string representation.
|
|
58
|
+
#
|
|
59
|
+
# @note module_function: defines #fallback_string (visibility: private)
|
|
60
|
+
# @param [Docscribe::Types::RBS::TypeFormatter::rbs_type] type the unrecognized RBS type object
|
|
61
|
+
# @return [String]
|
|
62
|
+
def fallback_string(type)
|
|
63
|
+
type.to_s
|
|
64
|
+
.gsub(/\A::/, '')
|
|
65
|
+
.gsub(/\bbool\b/, 'Boolean')
|
|
66
|
+
.gsub(/\buntyped\b/, 'Object')
|
|
67
|
+
end
|
|
68
|
+
|
|
14
69
|
# Return or memoize the dispatch hash mapping RBS type classes to formatter lambdas.
|
|
15
70
|
#
|
|
16
|
-
# @note module_function:
|
|
17
|
-
# @return [Hash
|
|
71
|
+
# @note module_function: defines #to_yard_formatters (visibility: private)
|
|
72
|
+
# @return [Hash<Class, Docscribe::Types::RBS::TypeFormatter::formatter_fn>]
|
|
18
73
|
def to_yard_formatters
|
|
19
|
-
@to_yard_formatters ||=
|
|
74
|
+
@to_yard_formatters ||= formatter_pairs.to_h.freeze
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Hash of RBS type classes and their YARD formatter lambdas.
|
|
78
|
+
#
|
|
79
|
+
# @note module_function: defines #formatter_pairs (visibility: private)
|
|
80
|
+
# @return [Hash<Class, Docscribe::Types::RBS::TypeFormatter::formatter_fn>]
|
|
81
|
+
def formatter_pairs # steep:ignore # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
|
82
|
+
@formatter_pairs ||= {
|
|
20
83
|
::RBS::Types::Bases::Any => ->(_, **) { format_any },
|
|
21
84
|
::RBS::Types::Bases::Bool => ->(_, **) { format_bool },
|
|
22
85
|
::RBS::Types::Bases::Void => ->(_, **) { format_void },
|
|
23
86
|
::RBS::Types::Bases::Nil => ->(_, **) { format_nil },
|
|
24
|
-
::RBS::Types::Optional =>
|
|
25
|
-
|
|
87
|
+
::RBS::Types::Optional => lambda { |t, cg:, cog:|
|
|
88
|
+
format_optional(t, collapse_generics: cg, collapse_object_generics: cog)
|
|
89
|
+
},
|
|
90
|
+
::RBS::Types::Union => lambda { |t, cg:, cog:|
|
|
91
|
+
format_union(t, collapse_generics: cg, collapse_object_generics: cog)
|
|
92
|
+
},
|
|
26
93
|
::RBS::Types::Literal => ->(t, **) { format_literal(t.literal) },
|
|
27
|
-
::RBS::Types::Proc => ->(_, **) { format_proc }
|
|
94
|
+
::RBS::Types::Proc => ->(_, **) { format_proc },
|
|
95
|
+
::RBS::Types::Tuple => lambda { |t, cg:, cog:|
|
|
96
|
+
format_tuple(t, collapse_generics: cg, collapse_object_generics: cog)
|
|
97
|
+
},
|
|
98
|
+
::RBS::Types::Bases::Top => ->(_, **) { format_top },
|
|
99
|
+
::RBS::Types::Bases::Bottom => ->(_, **) { format_bottom },
|
|
100
|
+
::RBS::Types::Bases::Self => ->(_, **) { format_self },
|
|
101
|
+
::RBS::Types::Bases::Instance => ->(_, **) { format_instance },
|
|
102
|
+
::RBS::Types::Bases::Class => ->(_, **) { format_class_type },
|
|
103
|
+
::RBS::Types::Variable => ->(t, **) { format_variable(t) },
|
|
104
|
+
::RBS::Types::Record => lambda { |t, cg:, cog:|
|
|
105
|
+
format_record(t, collapse_generics: cg, collapse_object_generics: cog)
|
|
106
|
+
},
|
|
107
|
+
::RBS::Types::Intersection => ->(t, cg:, cog:) { format_intersection(t, collapse_generics: cg, collapse_object_generics: cog) }
|
|
28
108
|
}.freeze
|
|
29
109
|
end
|
|
30
|
-
# rubocop:enable Metrics/AbcSize, Layout/LineLength
|
|
31
110
|
|
|
32
111
|
# Format RBS `any` type as the YARD-equivalent `Object`.
|
|
33
112
|
#
|
|
34
|
-
# @note module_function:
|
|
113
|
+
# @note module_function: defines #format_any (visibility: private)
|
|
35
114
|
# @return [String]
|
|
36
115
|
def format_any
|
|
37
116
|
'Object'
|
|
@@ -39,7 +118,7 @@ module Docscribe
|
|
|
39
118
|
|
|
40
119
|
# Format RBS `bool` type as the YARD `Boolean`.
|
|
41
120
|
#
|
|
42
|
-
# @note module_function:
|
|
121
|
+
# @note module_function: defines #format_bool (visibility: private)
|
|
43
122
|
# @return [String]
|
|
44
123
|
def format_bool
|
|
45
124
|
'Boolean'
|
|
@@ -47,7 +126,7 @@ module Docscribe
|
|
|
47
126
|
|
|
48
127
|
# Format RBS `void` type as the YARD `void`.
|
|
49
128
|
#
|
|
50
|
-
# @note module_function:
|
|
129
|
+
# @note module_function: defines #format_void (visibility: private)
|
|
51
130
|
# @return [String]
|
|
52
131
|
def format_void
|
|
53
132
|
'void'
|
|
@@ -55,7 +134,7 @@ module Docscribe
|
|
|
55
134
|
|
|
56
135
|
# Format RBS `nil` type as the YARD `nil`.
|
|
57
136
|
#
|
|
58
|
-
# @note module_function:
|
|
137
|
+
# @note module_function: defines #format_nil (visibility: private)
|
|
59
138
|
# @return [String]
|
|
60
139
|
def format_nil
|
|
61
140
|
'nil'
|
|
@@ -63,18 +142,20 @@ module Docscribe
|
|
|
63
142
|
|
|
64
143
|
# Format an RBS Optional type as a YARD optional type with `?` suffix.
|
|
65
144
|
#
|
|
66
|
-
# @note module_function:
|
|
67
|
-
# @param [
|
|
145
|
+
# @note module_function: defines #format_optional (visibility: private)
|
|
146
|
+
# @param [RBS::Types::Optional] type the optional type to format
|
|
68
147
|
# @param [Boolean] collapse_generics whether to omit generic type arguments
|
|
148
|
+
# @param [Boolean] collapse_object_generics collapse Object generics flag
|
|
69
149
|
# @return [String]
|
|
70
|
-
def format_optional(type, collapse_generics:)
|
|
71
|
-
"#{to_yard(type.type, collapse_generics: collapse_generics
|
|
150
|
+
def format_optional(type, collapse_generics:, collapse_object_generics:)
|
|
151
|
+
"#{to_yard(type.type, collapse_generics: collapse_generics,
|
|
152
|
+
collapse_object_generics: collapse_object_generics)}?"
|
|
72
153
|
end
|
|
73
154
|
|
|
74
155
|
# Map a Ruby literal value to its corresponding YARD type name.
|
|
75
156
|
#
|
|
76
|
-
# @note module_function:
|
|
77
|
-
# @param [
|
|
157
|
+
# @note module_function: defines #format_literal (visibility: private)
|
|
158
|
+
# @param [RBS::Types::Literal] lit a Ruby literal value
|
|
78
159
|
# @return [String]
|
|
79
160
|
def format_literal(lit)
|
|
80
161
|
case lit
|
|
@@ -90,46 +171,157 @@ module Docscribe
|
|
|
90
171
|
|
|
91
172
|
# Format RBS Proc type as the YARD `Proc`.
|
|
92
173
|
#
|
|
93
|
-
# @note module_function:
|
|
174
|
+
# @note module_function: defines #format_proc (visibility: private)
|
|
94
175
|
# @return [String]
|
|
95
176
|
def format_proc
|
|
96
177
|
'Proc'
|
|
97
178
|
end
|
|
98
179
|
|
|
180
|
+
# Format an RBS Tuple type as a parenthesized list of YARD types.
|
|
181
|
+
#
|
|
182
|
+
# @note module_function: defines #format_tuple (visibility: private)
|
|
183
|
+
# @param [RBS::Types::Tuple] type the tuple type to format
|
|
184
|
+
# @param [Boolean] collapse_generics whether to omit generic type arguments
|
|
185
|
+
# @param [Boolean] collapse_object_generics collapse Object generics flag
|
|
186
|
+
# @return [String]
|
|
187
|
+
def format_tuple(type, collapse_generics:, collapse_object_generics:)
|
|
188
|
+
"(#{type.types.map do |t|
|
|
189
|
+
to_yard(t, collapse_generics: collapse_generics, collapse_object_generics: collapse_object_generics)
|
|
190
|
+
end.join(', ')})"
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# Format RBS top type as YARD `Object`.
|
|
194
|
+
#
|
|
195
|
+
# @note module_function: defines #format_top (visibility: private)
|
|
196
|
+
# @return [String]
|
|
197
|
+
def format_top
|
|
198
|
+
'Object'
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# Format RBS bottom type as YARD `Object`.
|
|
202
|
+
#
|
|
203
|
+
# @note module_function: defines #format_bottom (visibility: private)
|
|
204
|
+
# @return [String]
|
|
205
|
+
def format_bottom
|
|
206
|
+
'Object'
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# Format RBS self type as YARD `self`.
|
|
210
|
+
#
|
|
211
|
+
# @note module_function: defines #format_self (visibility: private)
|
|
212
|
+
# @return [String]
|
|
213
|
+
def format_self
|
|
214
|
+
'self'
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# Format RBS instance type as YARD `Object`.
|
|
218
|
+
#
|
|
219
|
+
# @note module_function: defines #format_instance (visibility: private)
|
|
220
|
+
# @return [String]
|
|
221
|
+
def format_instance
|
|
222
|
+
'Object'
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# Format RBS class type as YARD `Class`.
|
|
226
|
+
#
|
|
227
|
+
# @note module_function: defines #format_class_type (visibility: private)
|
|
228
|
+
# @return [String]
|
|
229
|
+
def format_class_type
|
|
230
|
+
'Class'
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# Format an RBS type variable as its name string.
|
|
234
|
+
#
|
|
235
|
+
# @note module_function: defines #format_variable (visibility: private)
|
|
236
|
+
# @param [RBS::Types::Variable] type the variable type
|
|
237
|
+
# @return [String]
|
|
238
|
+
def format_variable(type)
|
|
239
|
+
type.name.to_s
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# Format an RBS Record type as a YARD `Hash<Symbol, ValueType>`.
|
|
243
|
+
#
|
|
244
|
+
# @note module_function: defines #format_record (visibility: private)
|
|
245
|
+
# @param [RBS::Types::Record] type the record type
|
|
246
|
+
# @param [Boolean] collapse_generics whether to omit generic type arguments
|
|
247
|
+
# @param [Boolean] collapse_object_generics collapse Object generics flag
|
|
248
|
+
# @return [String]
|
|
249
|
+
def format_record(type, collapse_generics:, collapse_object_generics:)
|
|
250
|
+
value_types = type.all_fields.values.map do |(ty, _)|
|
|
251
|
+
to_yard(ty, collapse_generics: collapse_generics, collapse_object_generics: collapse_object_generics)
|
|
252
|
+
end.uniq
|
|
253
|
+
"Hash<Symbol, #{value_types.join(', ')}>"
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# Format an RBS Intersection type as `Type & Type` list.
|
|
257
|
+
#
|
|
258
|
+
# @note module_function: defines #format_intersection (visibility: private)
|
|
259
|
+
# @param [RBS::Types::Intersection] type the intersection type
|
|
260
|
+
# @param [Boolean] collapse_generics whether to omit generic type arguments
|
|
261
|
+
# @param [Boolean] collapse_object_generics collapse Object generics flag
|
|
262
|
+
# @return [String]
|
|
263
|
+
def format_intersection(type, collapse_generics:, collapse_object_generics:)
|
|
264
|
+
type.types.map do |t|
|
|
265
|
+
to_yard(t, collapse_generics: collapse_generics, collapse_object_generics: collapse_object_generics)
|
|
266
|
+
end.join(' & ')
|
|
267
|
+
end
|
|
268
|
+
|
|
99
269
|
# Format an RBS Union type as a comma-separated list of YARD types.
|
|
100
270
|
#
|
|
101
|
-
# @note module_function:
|
|
102
|
-
# @param [
|
|
271
|
+
# @note module_function: defines #format_union (visibility: private)
|
|
272
|
+
# @param [RBS::Types::Union] type the union type to format
|
|
103
273
|
# @param [Boolean] collapse_generics whether to omit generic type arguments
|
|
274
|
+
# @param [Boolean] collapse_object_generics collapse Object generics flag
|
|
104
275
|
# @return [String]
|
|
105
|
-
def format_union(type, collapse_generics:)
|
|
106
|
-
type.types.map
|
|
107
|
-
|
|
108
|
-
|
|
276
|
+
def format_union(type, collapse_generics:, collapse_object_generics:)
|
|
277
|
+
type.types.map do |t|
|
|
278
|
+
to_yard(t, collapse_generics: collapse_generics, collapse_object_generics: collapse_object_generics)
|
|
279
|
+
end
|
|
280
|
+
.uniq
|
|
281
|
+
.join(', ')
|
|
109
282
|
end
|
|
110
283
|
|
|
111
284
|
# Format an RBS named type (class, interface, alias) with optional generic arguments.
|
|
112
285
|
#
|
|
113
|
-
# @note module_function:
|
|
114
|
-
# @param [::
|
|
286
|
+
# @note module_function: defines #format_named (visibility: private)
|
|
287
|
+
# @param [Docscribe::Types::RBS::TypeFormatter::named_rbs_type] type the unrecognized RBS type object
|
|
115
288
|
# @param [Boolean] collapse_generics whether to omit generic type arguments
|
|
289
|
+
# @param [Boolean] collapse_object_generics whether to collapse generics when all inner types are Object
|
|
116
290
|
# @return [String]
|
|
117
|
-
def format_named(type, collapse_generics:)
|
|
291
|
+
def format_named(type, collapse_generics:, collapse_object_generics:)
|
|
118
292
|
name = type.name.to_s.delete_prefix('::')
|
|
119
293
|
args = type.respond_to?(:args) ? type.args : [] #: Array[untyped]
|
|
120
294
|
|
|
121
295
|
if args && !args.empty?
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
"#{name}<#{args.map { |a| to_yard(a, collapse_generics: collapse_generics) }.join(', ')}>"
|
|
296
|
+
format_generic_args(name, args, collapse_generics: collapse_generics,
|
|
297
|
+
collapse_object_generics: collapse_object_generics)
|
|
125
298
|
else
|
|
126
299
|
name
|
|
127
300
|
end
|
|
128
301
|
end
|
|
129
302
|
|
|
303
|
+
# Format generic type arguments for a named type.
|
|
304
|
+
#
|
|
305
|
+
# @note module_function: defines #format_generic_args (visibility: private)
|
|
306
|
+
# @param [String] name the type name
|
|
307
|
+
# @param [Array<Docscribe::Types::RBS::TypeFormatter::rbs_type>] args the generic type arguments
|
|
308
|
+
# @param [Boolean] collapse_generics whether to omit generic type arguments
|
|
309
|
+
# @param [Boolean] collapse_object_generics whether to collapse generics when all inner types are Object
|
|
310
|
+
# @return [String]
|
|
311
|
+
def format_generic_args(name, args, collapse_generics:, collapse_object_generics:)
|
|
312
|
+
return name if collapse_generics
|
|
313
|
+
|
|
314
|
+
formatted = args.map do |a|
|
|
315
|
+
to_yard(a, collapse_generics: collapse_generics, collapse_object_generics: collapse_object_generics)
|
|
316
|
+
end
|
|
317
|
+
return name if collapse_object_generics && formatted.all? { |s| s == 'Object' }
|
|
318
|
+
|
|
319
|
+
"#{name}<#{formatted.join(', ')}>"
|
|
320
|
+
end
|
|
321
|
+
|
|
130
322
|
# Convert a Ruby literal value to its YARD type name string.
|
|
131
323
|
#
|
|
132
|
-
# @note module_function:
|
|
324
|
+
# @note module_function: defines #literal_to_yard (visibility: private)
|
|
133
325
|
# @param [Object] lit a Ruby literal value
|
|
134
326
|
# @return [String]
|
|
135
327
|
def literal_to_yard(lit)
|
|
@@ -143,57 +335,6 @@ module Docscribe
|
|
|
143
335
|
else 'Object'
|
|
144
336
|
end
|
|
145
337
|
end
|
|
146
|
-
|
|
147
|
-
# Dispatch an RBS type object to the appropriate YARD formatter.
|
|
148
|
-
#
|
|
149
|
-
# @note module_function: when included, also defines #to_yard (instance visibility: private)
|
|
150
|
-
# @param [::RBS::Type] type the RBS type object to convert
|
|
151
|
-
# @param [Boolean] collapse_generics whether to omit generic type arguments
|
|
152
|
-
# @return [String]
|
|
153
|
-
def to_yard(type, collapse_generics: false)
|
|
154
|
-
return 'Object' unless type
|
|
155
|
-
|
|
156
|
-
handler = to_yard_formatters.find { |klass, _| type.is_a?(klass) }
|
|
157
|
-
return handler.last.call(type, collapse_generics: collapse_generics) if handler
|
|
158
|
-
|
|
159
|
-
return format_named(type, collapse_generics: collapse_generics) if named_type?(type)
|
|
160
|
-
|
|
161
|
-
fallback_string(type)
|
|
162
|
-
end
|
|
163
|
-
|
|
164
|
-
# Check if the given type object is a named RBS type (class, singleton, interface, or alias).
|
|
165
|
-
#
|
|
166
|
-
# @note module_function: when included, also defines #named_type? (instance visibility: private)
|
|
167
|
-
# @param [::RBS::Type] type the RBS type object to check
|
|
168
|
-
# @return [Boolean]
|
|
169
|
-
def named_type?(type)
|
|
170
|
-
named_type_classes.any? { |klass| type.is_a?(klass) }
|
|
171
|
-
end
|
|
172
|
-
|
|
173
|
-
# Return or memoize the list of RBS type classes considered named types.
|
|
174
|
-
#
|
|
175
|
-
# @note module_function: when included, also defines #named_type_classes (instance visibility: private)
|
|
176
|
-
# @return [Array<Class>]
|
|
177
|
-
def named_type_classes
|
|
178
|
-
@named_type_classes ||= [
|
|
179
|
-
::RBS::Types::ClassInstance,
|
|
180
|
-
::RBS::Types::ClassSingleton,
|
|
181
|
-
::RBS::Types::Interface,
|
|
182
|
-
::RBS::Types::Alias
|
|
183
|
-
].freeze
|
|
184
|
-
end
|
|
185
|
-
|
|
186
|
-
# Fallback conversion of an unrecognized RBS type to a cleaned string representation.
|
|
187
|
-
#
|
|
188
|
-
# @note module_function: when included, also defines #fallback_string (instance visibility: private)
|
|
189
|
-
# @param [::RBS::Type] type the unrecognized RBS type object
|
|
190
|
-
# @return [String]
|
|
191
|
-
def fallback_string(type)
|
|
192
|
-
type.to_s
|
|
193
|
-
.gsub(/\A::/, '')
|
|
194
|
-
.gsub(/\bbool\b/, 'Boolean')
|
|
195
|
-
.gsub(/\buntyped\b/, 'Object')
|
|
196
|
-
end
|
|
197
338
|
end
|
|
198
339
|
end
|
|
199
340
|
end
|
|
@@ -2,64 +2,44 @@
|
|
|
2
2
|
|
|
3
3
|
module Docscribe
|
|
4
4
|
module Types
|
|
5
|
-
# Simplified view of an RBS method signature for Docscribe.
|
|
6
|
-
#
|
|
7
|
-
# @!attribute return_type
|
|
8
|
-
# @return [String] formatted return type for YARD output
|
|
9
|
-
# @!attribute param_types
|
|
10
|
-
# @return [Hash{String=>String}] mapping of parameter name to formatted type
|
|
11
|
-
# @!attribute rest_positional
|
|
12
|
-
# @return [RestPositional, nil] info for `*args`
|
|
13
|
-
# @!attribute rest_keywords
|
|
14
|
-
# @return [RestKeywords, nil] info for `**kwargs`
|
|
15
|
-
#
|
|
16
5
|
# @!attribute [rw] return_type
|
|
17
|
-
# @return [
|
|
18
|
-
# @param [
|
|
6
|
+
# @return [String]
|
|
7
|
+
# @param [String] value
|
|
19
8
|
#
|
|
20
9
|
# @!attribute [rw] param_types
|
|
21
|
-
# @return [
|
|
22
|
-
# @param [
|
|
10
|
+
# @return [Hash<String, String>]
|
|
11
|
+
# @param [Hash<String, String>] value
|
|
12
|
+
#
|
|
13
|
+
# @!attribute [rw] positional_types
|
|
14
|
+
# @return [Array<String>]
|
|
15
|
+
# @param [Array<String>] value
|
|
23
16
|
#
|
|
24
17
|
# @!attribute [rw] rest_positional
|
|
25
|
-
# @return [
|
|
26
|
-
# @param [
|
|
18
|
+
# @return [Docscribe::Types::RestPositional, nil]
|
|
19
|
+
# @param [Docscribe::Types::RestPositional, nil] value
|
|
27
20
|
#
|
|
28
21
|
# @!attribute [rw] rest_keywords
|
|
29
|
-
# @return [
|
|
30
|
-
# @param [
|
|
31
|
-
MethodSignature = Struct.new(:return_type, :param_types, :rest_positional, :rest_keywords,
|
|
22
|
+
# @return [Docscribe::Types::RestKeywords, nil]
|
|
23
|
+
# @param [Docscribe::Types::RestKeywords, nil] value
|
|
24
|
+
MethodSignature = Struct.new(:return_type, :param_types, :positional_types, :rest_positional, :rest_keywords,
|
|
25
|
+
keyword_init: true)
|
|
32
26
|
|
|
33
|
-
# Simplified representation of an RBS rest-positional parameter.
|
|
34
|
-
#
|
|
35
|
-
# @!attribute name
|
|
36
|
-
# @return [String, nil] parameter name in RBS, if present
|
|
37
|
-
# @!attribute element_type
|
|
38
|
-
# @return [String] formatted element type
|
|
39
|
-
#
|
|
40
27
|
# @!attribute [rw] name
|
|
41
|
-
# @return [
|
|
42
|
-
# @param [
|
|
28
|
+
# @return [String, nil]
|
|
29
|
+
# @param [String, nil] value
|
|
43
30
|
#
|
|
44
31
|
# @!attribute [rw] element_type
|
|
45
|
-
# @return [
|
|
46
|
-
# @param [
|
|
32
|
+
# @return [String]
|
|
33
|
+
# @param [String] value
|
|
47
34
|
RestPositional = Struct.new(:name, :element_type, keyword_init: true)
|
|
48
35
|
|
|
49
|
-
# Simplified representation of an RBS rest-keyword parameter.
|
|
50
|
-
#
|
|
51
|
-
# @!attribute name
|
|
52
|
-
# @return [String, nil] parameter name in RBS, if present
|
|
53
|
-
# @!attribute type
|
|
54
|
-
# @return [String] formatted kwargs type
|
|
55
|
-
#
|
|
56
36
|
# @!attribute [rw] name
|
|
57
|
-
# @return [
|
|
58
|
-
# @param [
|
|
37
|
+
# @return [String, nil]
|
|
38
|
+
# @param [String, nil] value
|
|
59
39
|
#
|
|
60
40
|
# @!attribute [rw] type
|
|
61
|
-
# @return [
|
|
62
|
-
# @param [
|
|
41
|
+
# @return [String]
|
|
42
|
+
# @param [String] value
|
|
63
43
|
RestKeywords = Struct.new(:name, :type, keyword_init: true)
|
|
64
44
|
end
|
|
65
45
|
end
|
|
@@ -15,12 +15,15 @@ module Docscribe
|
|
|
15
15
|
# - SourceProvider => inline `sig` declarations in the current Ruby file
|
|
16
16
|
# - RBIProvider => project RBI files
|
|
17
17
|
class BaseProvider
|
|
18
|
+
# Initialize
|
|
19
|
+
#
|
|
18
20
|
# @param [Boolean] collapse_generics whether generic container details
|
|
19
|
-
#
|
|
20
|
-
# @return [
|
|
21
|
-
def initialize(collapse_generics: false)
|
|
21
|
+
# @param [Boolean] collapse_object_generics collapse Object generics
|
|
22
|
+
# @return [void]
|
|
23
|
+
def initialize(collapse_generics: false, collapse_object_generics: false)
|
|
22
24
|
require 'rbs'
|
|
23
25
|
@collapse_generics = !!collapse_generics
|
|
26
|
+
@collapse_object_generics = !!collapse_object_generics
|
|
24
27
|
@index = {}
|
|
25
28
|
@warned = false
|
|
26
29
|
end
|
|
@@ -50,6 +53,8 @@ module Docscribe
|
|
|
50
53
|
# @raise [SyntaxError]
|
|
51
54
|
# @raise [StandardError]
|
|
52
55
|
# @return [void]
|
|
56
|
+
# @return [nil] if LoadError
|
|
57
|
+
# @return [nil] if ::RBS::BaseError, SyntaxError, StandardError
|
|
53
58
|
def load_from_string(source, label:)
|
|
54
59
|
return unless defined?(RubyVM::AbstractSyntaxTree)
|
|
55
60
|
|
|
@@ -82,7 +87,7 @@ module Docscribe
|
|
|
82
87
|
#
|
|
83
88
|
# @private
|
|
84
89
|
# @param [String] container normalized container name
|
|
85
|
-
# @param [Object] member
|
|
90
|
+
# @param [Object] member RBS method definition member
|
|
86
91
|
# @return [void]
|
|
87
92
|
def process_method_member(container, member)
|
|
88
93
|
return unless method_definition_member?(member)
|
|
@@ -91,13 +96,15 @@ module Docscribe
|
|
|
91
96
|
overload = member.overloads&.first
|
|
92
97
|
return unless overload
|
|
93
98
|
|
|
94
|
-
func = overload.method_type.type
|
|
99
|
+
func = overload.method_type.type #: ::RBS::Types::Function
|
|
95
100
|
@index[[container, scope, member.name.to_s.to_sym]] = build_signature(func)
|
|
96
101
|
end
|
|
97
102
|
|
|
103
|
+
# Method definition member
|
|
104
|
+
#
|
|
98
105
|
# @private
|
|
99
|
-
# @param [Object] member
|
|
100
|
-
# @return [Boolean]
|
|
106
|
+
# @param [Object] member member to check for method def
|
|
107
|
+
# @return [Boolean, nil]
|
|
101
108
|
def method_definition_member?(member)
|
|
102
109
|
defined?(::RBS::AST::Members::MethodDefinition) &&
|
|
103
110
|
member.is_a?(::RBS::AST::Members::MethodDefinition)
|
|
@@ -106,7 +113,7 @@ module Docscribe
|
|
|
106
113
|
# Convert an RBS function type into Docscribe's simplified signature model.
|
|
107
114
|
#
|
|
108
115
|
# @private
|
|
109
|
-
# @param [
|
|
116
|
+
# @param [RBS::Types::Function] func RBS function type to convert
|
|
110
117
|
# @return [Docscribe::Types::MethodSignature]
|
|
111
118
|
def build_signature(func)
|
|
112
119
|
MethodSignature.new(
|
|
@@ -120,8 +127,8 @@ module Docscribe
|
|
|
120
127
|
# Build a name => type map for ordinary positional/keyword parameters.
|
|
121
128
|
#
|
|
122
129
|
# @private
|
|
123
|
-
# @param [
|
|
124
|
-
# @return [Hash
|
|
130
|
+
# @param [RBS::Types::Function] func RBS function to extract params
|
|
131
|
+
# @return [Hash<String, String>]
|
|
125
132
|
def build_param_types(func)
|
|
126
133
|
param_types = {} #: Hash[String, String]
|
|
127
134
|
|
|
@@ -138,8 +145,8 @@ module Docscribe
|
|
|
138
145
|
# Add keyword parameters to the normalized parameter map.
|
|
139
146
|
#
|
|
140
147
|
# @private
|
|
141
|
-
# @param [Hash
|
|
142
|
-
# @param [Hash
|
|
148
|
+
# @param [Hash<String, String>] param_types normalized param type map
|
|
149
|
+
# @param [Hash<Symbol, RBS::Types::Function::Param>] keywords keyword parameter entries
|
|
143
150
|
# @return [void]
|
|
144
151
|
def add_keywords!(param_types, keywords)
|
|
145
152
|
keywords.each do |kw, p|
|
|
@@ -150,8 +157,8 @@ module Docscribe
|
|
|
150
157
|
# Add positional parameters with names to the normalized param map.
|
|
151
158
|
#
|
|
152
159
|
# @private
|
|
153
|
-
# @param [Hash
|
|
154
|
-
# @param [Array<
|
|
160
|
+
# @param [Hash<String, String>] param_types normalized param type map
|
|
161
|
+
# @param [Array<RBS::Types::Function::Param>] list positional parameter objects
|
|
155
162
|
# @return [void]
|
|
156
163
|
def add_positionals!(param_types, list)
|
|
157
164
|
list.each do |p|
|
|
@@ -164,7 +171,7 @@ module Docscribe
|
|
|
164
171
|
# Build normalized `*args` metadata.
|
|
165
172
|
#
|
|
166
173
|
# @private
|
|
167
|
-
# @param [
|
|
174
|
+
# @param [RBS::Types::Function] func RBS function for rest params
|
|
168
175
|
# @return [Docscribe::Types::RestPositional, nil]
|
|
169
176
|
def build_rest_positional(func)
|
|
170
177
|
rp = func.rest_positionals
|
|
@@ -182,7 +189,7 @@ module Docscribe
|
|
|
182
189
|
# YARD output, we expose that as a Hash keyed by Symbol.
|
|
183
190
|
#
|
|
184
191
|
# @private
|
|
185
|
-
# @param [
|
|
192
|
+
# @param [RBS::Types::Function] func RBS function for rest keywords
|
|
186
193
|
# @return [Docscribe::Types::RestKeywords, nil]
|
|
187
194
|
def build_rest_keywords(func)
|
|
188
195
|
rk = func.rest_keywords
|
|
@@ -200,28 +207,29 @@ module Docscribe
|
|
|
200
207
|
# generated comments.
|
|
201
208
|
#
|
|
202
209
|
# @private
|
|
203
|
-
# @param [
|
|
210
|
+
# @param [Docscribe::Types::RBS::TypeFormatter::rbs_type] type RBS type object to format
|
|
204
211
|
# @return [String]
|
|
205
212
|
def format_type(type)
|
|
206
213
|
Docscribe::Types::RBS::TypeFormatter.to_yard(
|
|
207
214
|
type,
|
|
208
|
-
collapse_generics: @collapse_generics
|
|
215
|
+
collapse_generics: @collapse_generics,
|
|
216
|
+
collapse_object_generics: @collapse_object_generics
|
|
209
217
|
)
|
|
210
218
|
end
|
|
211
219
|
|
|
212
220
|
# Normalize container names so lookups are consistent.
|
|
213
221
|
#
|
|
214
222
|
# @private
|
|
215
|
-
# @param [String] name
|
|
223
|
+
# @param [String] name method name
|
|
216
224
|
# @return [String]
|
|
217
225
|
def normalize_container(name)
|
|
218
|
-
name.to_s.delete_prefix('::')
|
|
226
|
+
name.to_s.delete_prefix('::').sub(/\[.*\]/, '').sub(/<.*>/, '')
|
|
219
227
|
end
|
|
220
228
|
|
|
221
229
|
# Print one debug warning per provider instance when debugging is enabled.
|
|
222
230
|
#
|
|
223
231
|
# @private
|
|
224
|
-
# @param [String] msg
|
|
232
|
+
# @param [String] msg warning message text
|
|
225
233
|
# @return [void]
|
|
226
234
|
def warn_once(msg)
|
|
227
235
|
return unless ENV['DOCSCRIBE_RBS_DEBUG'] == '1'
|
|
@@ -12,13 +12,14 @@ module Docscribe
|
|
|
12
12
|
# any signatures that can be parsed are indexed into Docscribe's normalized
|
|
13
13
|
# signature model.
|
|
14
14
|
class RBIProvider < BaseProvider
|
|
15
|
+
# Initialize
|
|
16
|
+
#
|
|
15
17
|
# @param [Array<String>] rbi_dirs directories scanned recursively for
|
|
16
|
-
# `.rbi` files
|
|
17
18
|
# @param [Boolean] collapse_generics whether generic container types
|
|
18
|
-
#
|
|
19
|
-
# @return [
|
|
20
|
-
def initialize(rbi_dirs:, collapse_generics: false)
|
|
21
|
-
super(collapse_generics: collapse_generics)
|
|
19
|
+
# @param [Boolean] collapse_object_generics collapse Object generics
|
|
20
|
+
# @return [void]
|
|
21
|
+
def initialize(rbi_dirs:, collapse_generics: false, collapse_object_generics: false)
|
|
22
|
+
super(collapse_generics: collapse_generics, collapse_object_generics: collapse_object_generics)
|
|
22
23
|
|
|
23
24
|
Array(rbi_dirs).each do |dir|
|
|
24
25
|
path = Pathname(dir)
|
|
@@ -10,13 +10,15 @@ module Docscribe
|
|
|
10
10
|
# This provider parses the source being rewritten and indexes any leading
|
|
11
11
|
# `sig` declarations it can resolve through the RBS RBI prototype bridge.
|
|
12
12
|
class SourceProvider < BaseProvider
|
|
13
|
+
# Initialize
|
|
14
|
+
#
|
|
13
15
|
# @param [String] source Ruby source containing inline `sig` declarations
|
|
14
16
|
# @param [String] file source label used in diagnostics/debug warnings
|
|
15
17
|
# @param [Boolean] collapse_generics whether generic container types
|
|
16
|
-
#
|
|
17
|
-
# @return [
|
|
18
|
-
def initialize(source:, file:, collapse_generics: false)
|
|
19
|
-
super(collapse_generics: collapse_generics)
|
|
18
|
+
# @param [Boolean] collapse_object_generics collapse Object generics
|
|
19
|
+
# @return [void]
|
|
20
|
+
def initialize(source:, file:, collapse_generics: false, collapse_object_generics: false)
|
|
21
|
+
super(collapse_generics: collapse_generics, collapse_object_generics: collapse_object_generics)
|
|
20
22
|
load_from_string(source, label: file)
|
|
21
23
|
end
|
|
22
24
|
end
|