Class: Decode::RBS::Method
Overview
Represents a Ruby method definition wrapper for RBS generation.
Instance Method Summary collapse
-
#build_function_type_from_ast(definition, index) ⇒ Object
Build a complete RBS function type from AST information.
-
#initialize(definition) ⇒ Method
constructor
Initialize a new method wrapper.
-
#keyword_arguments ⇒ Object
Extract keyword arguments from the method definition.
-
#parameters ⇒ Object
Extract parameters from the method definition.
-
#return_type ⇒ Object
Extract return type from the method definition.
-
#signatures ⇒ Object
Extract method signatures from the method definition.
-
#to_rbs_ast(index = nil) ⇒ Object
Convert the method definition to RBS AST.
Methods inherited from Wrapper
Constructor Details
#initialize(definition) ⇒ Method
Initialize a new method wrapper.
17 18 19 20 21 22 23 |
# File 'lib/decode/rbs/method.rb', line 17 def initialize(definition) super @signatures = nil @keyword_arguments = nil @return_type = nil @parameters = nil end |
Instance Method Details
#build_function_type_from_ast(definition, index) ⇒ Object
Build a complete RBS function type from AST information.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/decode/rbs/method.rb', line 121 def build_function_type_from_ast(definition, index) node = definition.node # Only return nil if we don't have an AST node at all return nil unless node&.respond_to?(:parameters) doc_types = extract_documented_parameter_types(definition) required_positionals = [] optional_positionals = [] rest_positionals = nil required_keywords = {} optional_keywords = {} keyword_rest = nil # Only process parameters if the node actually has them: if node.parameters # Handle required positional parameters: if node.parameters.respond_to?(:requireds) && node.parameters.requireds node.parameters.requireds.each do |param| name = param.name type = doc_types[name.to_s] || ::RBS::Parser.parse_type("untyped") required_positionals << ::RBS::Types::Function::Param.new( type: type, name: name.to_sym ) end end # Handle optional positional parameters (with defaults): if node.parameters.respond_to?(:optionals) && node.parameters.optionals node.parameters.optionals.each do |param| name = param.name # For optional parameters, use the documented type as-is (don't make it nullable): type = doc_types[name.to_s] || ::RBS::Parser.parse_type("untyped") optional_positionals << ::RBS::Types::Function::Param.new( type: type, name: name.to_sym ) end end # Handle rest parameter (*args): if node.parameters.respond_to?(:rest) && node.parameters.rest rest_param = node.parameters.rest name = rest_param.respond_to?(:name) && rest_param.name ? rest_param.name : :args base_type = doc_types[name.to_s] || ::RBS::Parser.parse_type("untyped") rest_positionals = ::RBS::Types::Function::Param.new( type: base_type, name: name.to_sym ) end # Handle keyword parameters: if node.parameters.respond_to?(:keywords) && node.parameters.keywords node.parameters.keywords.each do |param| name = param.name type = doc_types[name.to_s] || ::RBS::Parser.parse_type("untyped") if param.respond_to?(:value) && param.value # Has default value - optional keyword: optional_keywords[name.to_sym] = type else # No default value - required keyword: required_keywords[name.to_sym] = type end end end # Handle keyword rest parameter (**kwargs): if node.parameters.respond_to?(:keyword_rest) && node.parameters.keyword_rest rest_param = node.parameters.keyword_rest if rest_param.respond_to?(:name) && rest_param.name name = rest_param.name base_type = doc_types[name.to_s] || ::RBS::Parser.parse_type("untyped") keyword_rest = ::RBS::Types::Function::Param.new( type: base_type, name: name.to_sym ) end end end return_type = extract_return_type(@definition, index) || ::RBS::Parser.parse_type("untyped") ::RBS::Types::Function.new( required_positionals: required_positionals, optional_positionals: optional_positionals, rest_positionals: rest_positionals, trailing_positionals: [], required_keywords: required_keywords, optional_keywords: optional_keywords, rest_keywords: keyword_rest, return_type: return_type ) end |
#keyword_arguments ⇒ Object
Extract keyword arguments from the method definition.
33 34 35 |
# File 'lib/decode/rbs/method.rb', line 33 def keyword_arguments @keyword_arguments ||= extract_keyword_arguments(@definition, nil) end |
#parameters ⇒ Object
Extract parameters from the method definition.
45 46 47 |
# File 'lib/decode/rbs/method.rb', line 45 def parameters @parameters ||= extract_parameters(@definition, nil) end |
#return_type ⇒ Object
Extract return type from the method definition.
39 40 41 |
# File 'lib/decode/rbs/method.rb', line 39 def return_type @return_type ||= extract_return_type(@definition, nil) || ::RBS::Parser.parse_type("untyped") end |
#signatures ⇒ Object
Extract method signatures from the method definition.
27 28 29 |
# File 'lib/decode/rbs/method.rb', line 27 def signatures @signatures ||= extract_signatures end |
#to_rbs_ast(index = nil) ⇒ Object
Convert the method definition to RBS AST
50 51 52 53 54 55 56 57 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/decode/rbs/method.rb', line 50 def to_rbs_ast(index = nil) method_name = @definition.name comment = self.comment overloads = [] if signatures.any? signatures.each do |signature_string| method_type = ::RBS::Parser.parse_method_type(signature_string) overloads << ::RBS::AST::Members::MethodDefinition::Overload.new( method_type: method_type, annotations: [] ) end else return_type = self.return_type # Get parameters using AST-based detection if ast_function = build_function_type_from_ast(@definition, index) method_type = ::RBS::MethodType.new( type_params: [], type: ast_function, block: extract_block_type(@definition, index), location: nil ) else # Fall back to documentation-based approach parameters = self.parameters keywords = self.keyword_arguments block_type = extract_block_type(@definition, index) method_type = ::RBS::MethodType.new( type_params: [], type: ::RBS::Types::Function.new( required_positionals: parameters, optional_positionals: [], rest_positionals: nil, trailing_positionals: [], required_keywords: keywords[:required], optional_keywords: keywords[:optional], rest_keywords: nil, return_type: return_type ), block: block_type, location: nil ) end overloads << ::RBS::AST::Members::MethodDefinition::Overload.new( method_type: method_type, annotations: [] ) end kind = @definition.receiver ? :singleton : :instance ::RBS::AST::Members::MethodDefinition.new( name: method_name.to_sym, kind: kind, overloads: overloads, annotations: [], location: nil, comment: comment, overloading: false, visibility: @definition.visibility || :public ) end |