Class: YARD::Handlers::RBS::MethodHandler
- Defined in:
- lib/yard/handlers/rbs/method_handler.rb
Overview
Handles RBS method definitions (def name: signature).
Creates a CodeObjects::MethodObject for each declaration and infers @param, @return, @yield, and @yieldparam tags from the RBS type signature when those tags are absent from the docstring.
Class Method Summary collapse
-
.bracket_depth(str) ⇒ Object
Return the bracket depth of the full string (should be 0 for well-formed types).
-
.rbs_type_to_yard_types(rbs) ⇒ Array<String>
Convert an RBS type string to an array of YARD type strings.
-
.split_on_pipe(str) ⇒ Object
Split
stron|that are not inside brackets.
Class Method Details
.bracket_depth(str) ⇒ Object
Return the bracket depth of the full string (should be 0 for well-formed types).
327 328 329 330 331 332 333 334 335 336 |
# File 'lib/yard/handlers/rbs/method_handler.rb', line 327 def self.bracket_depth(str) depth = 0 str.each_char do |c| case c when '(', '[', '{' then depth += 1 when ')', ']', '}' then depth -= 1 end end depth end |
.rbs_type_to_yard_types(rbs) ⇒ Array<String>
Convert an RBS type string to an array of YARD type strings.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/yard/handlers/rbs/method_handler.rb', line 32 def self.rbs_type_to_yard_types(rbs) rbs = rbs.strip return ['void'] if rbs == 'void' return ['Boolean'] if rbs == 'bool' return ['Object'] if rbs == 'untyped' return ['nil'] if rbs == 'nil' # Strip outer parentheses: `(String | Integer)` → recurse on inner. if rbs.start_with?('(') && rbs.end_with?(')') && bracket_depth(rbs[1..-2]) == 0 return rbs_type_to_yard_types(rbs[1..-2]) end # `Type?` is shorthand for `Type | nil` when the ? is outermost. if rbs =~ /\A(.+)\?\z/ && bracket_depth($1) == 0 return rbs_type_to_yard_types($1) + ['nil'] end split_on_pipe(rbs).map { |t| t.strip } end |
.split_on_pipe(str) ⇒ Object
Split str on | that are not inside brackets.
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/yard/handlers/rbs/method_handler.rb', line 299 def self.split_on_pipe(str) depth = 0 parts = [] cur = String.new('') str.each_char do |c| case c when '(', '[', '{' depth += 1 cur << c when ')', ']', '}' depth -= 1 cur << c when '|' if depth == 0 parts << cur.strip cur = String.new('') else cur << c end else cur << c end end parts << cur.strip unless cur.strip.empty? parts end |