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).
317 318 319 320 321 322 323 324 325 326 |
# File 'lib/yard/handlers/rbs/method_handler.rb', line 317 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.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/yard/handlers/rbs/method_handler.rb', line 30 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.
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/yard/handlers/rbs/method_handler.rb', line 289 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 |