docscribe 1.1.0 → 1.2.0

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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +662 -187
  3. data/exe/docscribe +2 -126
  4. data/lib/docscribe/cli/config_builder.rb +62 -0
  5. data/lib/docscribe/cli/init.rb +58 -0
  6. data/lib/docscribe/cli/options.rb +204 -0
  7. data/lib/docscribe/cli/run.rb +415 -0
  8. data/lib/docscribe/cli.rb +31 -0
  9. data/lib/docscribe/config/defaults.rb +71 -0
  10. data/lib/docscribe/config/emit.rb +126 -0
  11. data/lib/docscribe/config/filtering.rb +160 -0
  12. data/lib/docscribe/config/loader.rb +59 -0
  13. data/lib/docscribe/config/rbs.rb +51 -0
  14. data/lib/docscribe/config/sorbet.rb +87 -0
  15. data/lib/docscribe/config/sorting.rb +23 -0
  16. data/lib/docscribe/config/template.rb +176 -0
  17. data/lib/docscribe/config/utils.rb +102 -0
  18. data/lib/docscribe/config.rb +20 -230
  19. data/lib/docscribe/infer/ast_walk.rb +28 -0
  20. data/lib/docscribe/infer/constants.rb +11 -0
  21. data/lib/docscribe/infer/literals.rb +55 -0
  22. data/lib/docscribe/infer/names.rb +43 -0
  23. data/lib/docscribe/infer/params.rb +62 -0
  24. data/lib/docscribe/infer/raises.rb +68 -0
  25. data/lib/docscribe/infer/returns.rb +171 -0
  26. data/lib/docscribe/infer.rb +104 -258
  27. data/lib/docscribe/inline_rewriter/collector.rb +845 -0
  28. data/lib/docscribe/inline_rewriter/doc_block.rb +383 -0
  29. data/lib/docscribe/inline_rewriter/doc_builder.rb +605 -0
  30. data/lib/docscribe/inline_rewriter/source_helpers.rb +228 -0
  31. data/lib/docscribe/inline_rewriter/tag_sorter.rb +244 -0
  32. data/lib/docscribe/inline_rewriter.rb +599 -428
  33. data/lib/docscribe/parsing.rb +55 -44
  34. data/lib/docscribe/types/provider_chain.rb +37 -0
  35. data/lib/docscribe/types/rbs/provider.rb +213 -0
  36. data/lib/docscribe/types/rbs/type_formatter.rb +132 -0
  37. data/lib/docscribe/types/signature.rb +65 -0
  38. data/lib/docscribe/types/sorbet/base_provider.rb +217 -0
  39. data/lib/docscribe/types/sorbet/rbi_provider.rb +35 -0
  40. data/lib/docscribe/types/sorbet/source_provider.rb +25 -0
  41. data/lib/docscribe/version.rb +1 -1
  42. metadata +37 -3
@@ -0,0 +1,217 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'docscribe/types/signature'
4
+ require 'docscribe/types/rbs/type_formatter'
5
+
6
+ module Docscribe
7
+ module Types
8
+ module Sorbet
9
+ # Shared base for Sorbet-backed signature providers.
10
+ #
11
+ # This class parses Sorbet-style signatures through the RBS RBI prototype
12
+ # API and indexes them into Docscribe's normalized signature model.
13
+ #
14
+ # Concrete subclasses decide where the Sorbet source comes from:
15
+ # - SourceProvider => inline `sig` declarations in the current Ruby file
16
+ # - RBIProvider => project RBI files
17
+ class BaseProvider
18
+ # @param [Boolean] collapse_generics whether generic container details
19
+ # should be simplified during formatting
20
+ # @return [Object]
21
+ def initialize(collapse_generics: false)
22
+ require 'rbs'
23
+ @collapse_generics = !!collapse_generics
24
+ @index = {}
25
+ @warned = false
26
+ end
27
+
28
+ # Look up a normalized method signature by container, scope, and name.
29
+ #
30
+ # @param [String] container e.g. "MyModule::MyClass"
31
+ # @param [Symbol] scope :instance or :class
32
+ # @param [Symbol, String] name method name
33
+ # @return [Docscribe::Types::MethodSignature, nil]
34
+ def signature_for(container:, scope:, name:)
35
+ @index[[normalize_container(container), scope.to_sym, name.to_sym]]
36
+ end
37
+
38
+ private
39
+
40
+ # Parse Sorbet-flavored Ruby/RBI source and index any signatures found.
41
+ #
42
+ # Parsing failures are treated as non-fatal so Docscribe can fall back to
43
+ # other providers or plain inference.
44
+ #
45
+ # @private
46
+ # @param [String] source source text to parse
47
+ # @param [String] label file label used in debug warnings
48
+ # @raise [LoadError]
49
+ # @raise [::RBS::BaseError]
50
+ # @raise [SyntaxError]
51
+ # @raise [StandardError]
52
+ # @return [void]
53
+ def load_from_string(source, label:)
54
+ return unless defined?(RubyVM::AbstractSyntaxTree)
55
+
56
+ parser = ::RBS::Prototype::RBI.new
57
+ parser.parse(source)
58
+ index_decls(parser.decls)
59
+ rescue LoadError
60
+ nil
61
+ rescue ::RBS::BaseError, SyntaxError, StandardError => e
62
+ warn_once("Docscribe: Sorbet signature load failed for #{label}: #{e.class}: #{e.message}")
63
+ nil
64
+ end
65
+
66
+ # Index parsed declarations into the provider lookup table.
67
+ #
68
+ # @private
69
+ # @param [Array<Object>] decls parsed RBS declarations
70
+ # @return [void]
71
+ def index_decls(decls)
72
+ Array(decls).each do |decl|
73
+ next unless decl.respond_to?(:name)
74
+ next unless decl.respond_to?(:members)
75
+
76
+ container = normalize_container(decl.name.to_s)
77
+
78
+ decl.members.each do |member|
79
+ next unless method_definition_member?(member)
80
+
81
+ scope = member.kind == :singleton ? :class : :instance
82
+ overload = member.overloads&.first
83
+ next unless overload
84
+
85
+ func = overload.method_type.type
86
+ @index[[container, scope, member.name.to_s.to_sym]] = build_signature(func)
87
+ end
88
+ end
89
+ end
90
+
91
+ # @private
92
+ # @param [Object] member
93
+ # @return [Boolean]
94
+ def method_definition_member?(member)
95
+ defined?(::RBS::AST::Members::MethodDefinition) &&
96
+ member.is_a?(::RBS::AST::Members::MethodDefinition)
97
+ end
98
+
99
+ # Convert an RBS function type into Docscribe's simplified signature model.
100
+ #
101
+ # @private
102
+ # @param [::RBS::Types::Function] func
103
+ # @return [Docscribe::Types::MethodSignature]
104
+ def build_signature(func)
105
+ MethodSignature.new(
106
+ return_type: format_type(func.return_type),
107
+ param_types: build_param_types(func),
108
+ rest_positional: build_rest_positional(func),
109
+ rest_keywords: build_rest_keywords(func)
110
+ )
111
+ end
112
+
113
+ # Build a name => type map for ordinary positional/keyword parameters.
114
+ #
115
+ # @private
116
+ # @param [::RBS::Types::Function] func
117
+ # @return [Hash{String => String}]
118
+ def build_param_types(func)
119
+ param_types = {}
120
+
121
+ add_positionals!(param_types, func.required_positionals)
122
+ add_positionals!(param_types, func.optional_positionals)
123
+ add_positionals!(param_types, func.trailing_positionals)
124
+
125
+ func.required_keywords.each { |kw, p| param_types[kw.to_s] = format_type(p.type) }
126
+ func.optional_keywords.each { |kw, p| param_types[kw.to_s] = format_type(p.type) }
127
+
128
+ param_types
129
+ end
130
+
131
+ # Add positional parameters with names to the normalized param map.
132
+ #
133
+ # @private
134
+ # @param [Hash{String => String}] param_types
135
+ # @param [Array<Object>] list
136
+ # @return [void]
137
+ def add_positionals!(param_types, list)
138
+ list.each do |p|
139
+ next unless p.name
140
+
141
+ param_types[p.name.to_s] = format_type(p.type)
142
+ end
143
+ end
144
+
145
+ # Build normalized `*args` metadata.
146
+ #
147
+ # @private
148
+ # @param [::RBS::Types::Function] func
149
+ # @return [Docscribe::Types::RestPositional, nil]
150
+ def build_rest_positional(func)
151
+ rp = func.rest_positionals
152
+ return nil unless rp
153
+
154
+ RestPositional.new(
155
+ name: rp.name&.to_s,
156
+ element_type: format_type(rp.type)
157
+ )
158
+ end
159
+
160
+ # Build normalized `**kwargs` metadata.
161
+ #
162
+ # Sorbet keyword-rest signatures describe the value type. For generated
163
+ # YARD output, we expose that as a Hash keyed by Symbol.
164
+ #
165
+ # @private
166
+ # @param [::RBS::Types::Function] func
167
+ # @return [Docscribe::Types::RestKeywords, nil]
168
+ def build_rest_keywords(func)
169
+ rk = func.rest_keywords
170
+ return nil unless rk
171
+
172
+ value_type = format_type(rk.type)
173
+
174
+ RestKeywords.new(
175
+ name: rk.name&.to_s,
176
+ type: "Hash<Symbol, #{value_type}>"
177
+ )
178
+ end
179
+
180
+ # Format an RBS type object into the YARD-ish type syntax used by
181
+ # generated comments.
182
+ #
183
+ # @private
184
+ # @param [Object] type
185
+ # @return [String]
186
+ def format_type(type)
187
+ Docscribe::Types::RBS::TypeFormatter.to_yard(
188
+ type,
189
+ collapse_generics: @collapse_generics
190
+ )
191
+ end
192
+
193
+ # Normalize container names so lookups are consistent.
194
+ #
195
+ # @private
196
+ # @param [String] name
197
+ # @return [String]
198
+ def normalize_container(name)
199
+ name.to_s.delete_prefix('::')
200
+ end
201
+
202
+ # Print one debug warning per provider instance when debugging is enabled.
203
+ #
204
+ # @private
205
+ # @param [String] msg
206
+ # @return [void]
207
+ def warn_once(msg)
208
+ return unless ENV['DOCSCRIBE_RBS_DEBUG'] == '1'
209
+ return if @warned
210
+
211
+ @warned = true
212
+ warn msg
213
+ end
214
+ end
215
+ end
216
+ end
217
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+ require 'docscribe/types/sorbet/base_provider'
5
+
6
+ module Docscribe
7
+ module Types
8
+ module Sorbet
9
+ # Sorbet provider that loads signatures from RBI directories.
10
+ #
11
+ # Each configured directory is scanned recursively for `.rbi` files, and
12
+ # any signatures that can be parsed are indexed into Docscribe's normalized
13
+ # signature model.
14
+ class RBIProvider < BaseProvider
15
+ # @param [Array<String>] rbi_dirs directories scanned recursively for
16
+ # `.rbi` files
17
+ # @param [Boolean] collapse_generics whether generic container types
18
+ # should be simplified during formatting
19
+ # @return [Object]
20
+ def initialize(rbi_dirs:, collapse_generics: false)
21
+ super(collapse_generics: collapse_generics)
22
+
23
+ Array(rbi_dirs).each do |dir|
24
+ path = Pathname(dir)
25
+ next unless path.directory?
26
+
27
+ path.glob('**/*.rbi').sort.each do |file|
28
+ load_from_string(file.read, label: file.to_s)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'docscribe/types/sorbet/base_provider'
4
+
5
+ module Docscribe
6
+ module Types
7
+ module Sorbet
8
+ # Sorbet provider for inline signatures present in the current Ruby source.
9
+ #
10
+ # This provider parses the source being rewritten and indexes any leading
11
+ # `sig` declarations it can resolve through the RBS RBI prototype bridge.
12
+ class SourceProvider < BaseProvider
13
+ # @param [String] source Ruby source containing inline `sig` declarations
14
+ # @param [String] file source label used in diagnostics/debug warnings
15
+ # @param [Boolean] collapse_generics whether generic container types
16
+ # should be simplified during formatting
17
+ # @return [Object]
18
+ def initialize(source:, file:, collapse_generics: false)
19
+ super(collapse_generics: collapse_generics)
20
+ load_from_string(source, label: file)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Docscribe
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docscribe
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - unurgunite
@@ -146,13 +146,47 @@ files:
146
146
  - README.md
147
147
  - exe/docscribe
148
148
  - lib/docscribe.rb
149
+ - lib/docscribe/cli.rb
150
+ - lib/docscribe/cli/config_builder.rb
151
+ - lib/docscribe/cli/init.rb
152
+ - lib/docscribe/cli/options.rb
153
+ - lib/docscribe/cli/run.rb
149
154
  - lib/docscribe/config.rb
155
+ - lib/docscribe/config/defaults.rb
156
+ - lib/docscribe/config/emit.rb
157
+ - lib/docscribe/config/filtering.rb
158
+ - lib/docscribe/config/loader.rb
159
+ - lib/docscribe/config/rbs.rb
160
+ - lib/docscribe/config/sorbet.rb
161
+ - lib/docscribe/config/sorting.rb
162
+ - lib/docscribe/config/template.rb
163
+ - lib/docscribe/config/utils.rb
150
164
  - lib/docscribe/infer.rb
165
+ - lib/docscribe/infer/ast_walk.rb
166
+ - lib/docscribe/infer/constants.rb
167
+ - lib/docscribe/infer/literals.rb
168
+ - lib/docscribe/infer/names.rb
169
+ - lib/docscribe/infer/params.rb
170
+ - lib/docscribe/infer/raises.rb
171
+ - lib/docscribe/infer/returns.rb
151
172
  - lib/docscribe/inline_rewriter.rb
173
+ - lib/docscribe/inline_rewriter/collector.rb
174
+ - lib/docscribe/inline_rewriter/doc_block.rb
175
+ - lib/docscribe/inline_rewriter/doc_builder.rb
176
+ - lib/docscribe/inline_rewriter/source_helpers.rb
177
+ - lib/docscribe/inline_rewriter/tag_sorter.rb
152
178
  - lib/docscribe/parsing.rb
179
+ - lib/docscribe/types/provider_chain.rb
180
+ - lib/docscribe/types/rbs/provider.rb
181
+ - lib/docscribe/types/rbs/type_formatter.rb
182
+ - lib/docscribe/types/signature.rb
183
+ - lib/docscribe/types/sorbet/base_provider.rb
184
+ - lib/docscribe/types/sorbet/rbi_provider.rb
185
+ - lib/docscribe/types/sorbet/source_provider.rb
153
186
  - lib/docscribe/version.rb
154
187
  homepage: https://github.com/unurgunite/docscribe
155
- licenses: []
188
+ licenses:
189
+ - MIT
156
190
  metadata:
157
191
  homepage_uri: https://github.com/unurgunite/docscribe
158
192
  source_code_uri: https://github.com/unurgunite/docscribe
@@ -172,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
172
206
  - !ruby/object:Gem::Version
173
207
  version: '0'
174
208
  requirements: []
175
- rubygems_version: 4.0.2
209
+ rubygems_version: 4.0.9
176
210
  specification_version: 4
177
211
  summary: Autogenerate documentation for Ruby code with YARD syntax.
178
212
  test_files: []