Class: Yard2rbs::YardParser
- Inherits:
-
Object
- Object
- Yard2rbs::YardParser
- Defined in:
- lib/yard2rbs/yard_parser.rb
Class Method Summary collapse
-
.convert(types_str) ⇒ Array<String>
Converts YARD type string into RBS type array e.g.
- .parse(comments) ⇒ Hash<Symbol, Array<String> | Hash<String, String>>
Class Method Details
.convert(types_str) ⇒ Array<String>
Converts YARD type string into RBS type array e.g. Input: “String, Array<String>, Hash<String, String>”
Output: ["String", "Array[String]", "Hash[String, String]"]
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/yard2rbs/yard_parser.rb', line 58 def convert(types_str) types = [] nested_level = 0 current_type = [] types_str.each_char.with_index(1) do |char, index| case char when ',' if nested_level > 0 current_type << char else types << current_type.join current_type = [] end when '<' nested_level += 1 current_type << "[" when '>' nested_level -= 1 current_type << "]" when ' ' if current_type.any? current_type << " " end else current_type << char end if index == types_str.size types << current_type.join end end types end |
.parse(comments) ⇒ Hash<Symbol, Array<String> | Hash<String, String>>
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/yard2rbs/yard_parser.rb', line 12 def parse(comments) params = {} returns = [] yieldparams = {} yieldreturns = [] comments&.each do |comment| case comment when /@param/ if matches = comment.match(/# @param ([^\s]+) \[([^\]]+)\].*/) params[matches[1]] = convert(matches[2]) end when /@return/ if matches = comment.match(/# @return \[([^\]]+)\].*/) returns += convert(matches[1]) end when /@yieldparam/ if matches = comment.match(/# @yieldparam ([^\s]+) \[([^\]]+)\].*/) yieldparams[matches[1]] = convert(matches[2]) end when /@yieldreturn/ if matches = comment.match(/# @yieldreturn \[([^\]]+)\].*/) yieldreturns += convert(matches[1]) end end end { params:, returns:, yieldparams:, yieldreturns:, } end |