Class: RBS::Prototype::RBI
- Inherits:
-
Object
- Object
- RBS::Prototype::RBI
- Includes:
- Helpers
- Defined in:
- lib/rbs/prototype/rbi.rb,
sig/prototype/rbi.rbs
Defined Under Namespace
Classes: Context
Instance Attribute Summary collapse
-
#decls ⇒ Array[AST::Declarations::t]
readonly
Returns the value of attribute decls.
-
#last_sig ⇒ Array[RubyVM::AbstractSyntaxTree::Node]?
readonly
Last subsequent
sigcalls. -
#modules ⇒ Array[module_decl]
readonly
A stack representing the module nesting structure in the Ruby code.
Instance Method Summary collapse
- #append_decl(decl) ⇒ void
- #attribute_type(kind, sigs) ⇒ Types::t
- #call_node?(node, name:, receiver: -> (node) { node.type == :CONST && node.children[0] == :T }, args: -> (node) { true }) ⇒ Boolean
-
#const_to_name(node) ⇒ TypeName
Receives a constant node and returns
TypeNameinstance. - #current_context ⇒ Context?
- #current_context! ⇒ Context
-
#current_module ⇒ module_decl?
The inner most module/class definition, returns
nilon toplevel. -
#current_module! ⇒ module_decl
The inner most module/class definition, raises on toplevel.
-
#each_arg(array) {|arg0| ... } ⇒ Object
Receives
:ARRAYor:LISTnode and yields the child nodes. -
#each_child(node) {|arg0| ... } ⇒ Object
Receives node and yields the child nodes.
-
#initialize ⇒ RBI
constructor
A new instance of RBI.
- #join_comments(nodes, comments) ⇒ AST::Comment
-
#member_visibility(context) ⇒ AST::Members::visibility?
Visibility of a member, given as
private def ...in RBS. - #method_type(args_node, type_node, variables:, overloads:) ⇒ MethodType?
-
#node_to_hash(node) ⇒ Hash[Symbol, RubyVM::AbstractSyntaxTree::Node]?
Receives a keyword
:HASHnode and returns hash instance. - #parse(string) ⇒ void
- #parse_params(args_node, args, method_type, variables:, overloads:) ⇒ MethodType
-
#pop_sig ⇒ Array[RubyVM::AbstractSyntaxTree::Node]?
Clear the
sigcall list. - #proc_type?(type_node) ⇒ Boolean
- #process(node, outer: [], comments:) ⇒ void
- #process_attribute(node, comments:) ⇒ void
- #process_visibility(node, outer:, comments:) ⇒ void
- #push_class(name, super_class, comment:) ⇒ Object
- #push_module(name, comment:) { ... } ⇒ void
-
#push_sig(node) ⇒ void
Put a
sigcall to current list. - #sync_visibility(visibility) ⇒ void
- #type_of(type_node, variables:) ⇒ Types::t
- #type_of0(type_node, variables:) ⇒ Types::t
Methods included from Helpers
#any_node?, #args_from_node, #block_from_body, #each_node, #keyword_hash?, #parse_comments, #symbol_literal_node?, #untyped
Constructor Details
#initialize ⇒ RBI
Returns a new instance of RBI.
22 23 24 25 26 27 28 |
# File 'lib/rbs/prototype/rbi.rb', line 22 def initialize @decls = [] @modules = [] @contexts = [] @emitted_visibility = {} end |
Instance Attribute Details
#decls ⇒ Array[AST::Declarations::t] (readonly)
Returns the value of attribute decls.
8 9 10 |
# File 'lib/rbs/prototype/rbi.rb', line 8 def decls @decls end |
#last_sig ⇒ Array[RubyVM::AbstractSyntaxTree::Node]? (readonly)
Last subsequent sig calls
24 25 26 |
# File 'sig/prototype/rbi.rbs', line 24 def last_sig @last_sig end |
#modules ⇒ Array[module_decl] (readonly)
A stack representing the module nesting structure in the Ruby code
21 22 23 |
# File 'sig/prototype/rbi.rbs', line 21 def modules @modules end |
Instance Method Details
#append_decl(decl) ⇒ void
This method returns an undefined value.
35 36 37 38 39 40 41 |
# File 'lib/rbs/prototype/rbi.rb', line 35 def append_decl(decl) if mod = current_module mod.members << decl else decls << decl end end |
#attribute_type(kind, sigs) ⇒ Types::t
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/rbs/prototype/rbi.rb', line 394 def attribute_type(kind, sigs) any = Types::Bases::Any.new(location: nil) return any unless sigs method_types = sigs.filter_map do |sig| method_type(nil, sig, variables: current_module!.type_params, overloads: sigs.size) end function = method_types.last&.type return any unless function.is_a?(Types::Function) parameter_type = function.required_positionals.first&.type return_type = function.return_type case kind when :attr_reader return_type when :attr_writer parameter_type || return_type when :attr_accessor if return_type.is_a?(Types::Bases::Any) || return_type.is_a?(Types::Bases::Void) parameter_type || any else return_type end else any end end |
#call_node?(node, name:, receiver: -> (node) { node.type == :CONST && node.children[0] == :T }, args: -> (node) { true }) ⇒ Boolean
713 714 715 |
# File 'lib/rbs/prototype/rbi.rb', line 713 def call_node?(node, name:, receiver: -> (node) { node.type == :CONST && node.children[0] == :T }, args: -> (node) { true }) node.type == :CALL && receiver[node.children[0]] && name == node.children[1] && args[node.children[2]] end |
#const_to_name(node) ⇒ TypeName
Receives a constant node and returns TypeName instance
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 116 117 118 119 120 121 122 123 124 |
# File 'sig/prototype/rbi.rbs', line 90 def const_to_name(node) case node.type when :CONST TypeName.new(name: node.children[0], namespace: Namespace.empty) when :COLON2 if node.children[0] namespace = const_to_name(node.children[0]).to_namespace else namespace = Namespace.empty end type_name = TypeName.new(name: node.children[1], namespace: namespace) case type_name.to_s.delete_prefix("::") when "T::Array" BuiltinNames::Array.name when "T::Hash" BuiltinNames::Hash.name when "T::Range" BuiltinNames::Range.name when "T::Enumerator" BuiltinNames::Enumerator.name when "T::Enumerable" BuiltinNames::Enumerable.name when "T::Set" BuiltinNames::Set.name else type_name end when :COLON3 TypeName.new(name: node.children[0], namespace: Namespace.root) else raise "Unexpected node type: #{node.type}" end end |
#current_context ⇒ Context?
95 96 97 |
# File 'lib/rbs/prototype/rbi.rb', line 95 def current_context @contexts.last end |
#current_context! ⇒ Context
99 100 101 |
# File 'lib/rbs/prototype/rbi.rb', line 99 def current_context! current_context or raise end |
#current_module ⇒ module_decl?
The inner most module/class definition, returns nil on toplevel
45 46 47 |
# File 'sig/prototype/rbi.rbs', line 45 def current_module modules.last end |
#current_module! ⇒ module_decl
The inner most module/class definition, raises on toplevel
48 49 50 |
# File 'sig/prototype/rbi.rbs', line 48 def current_module! current_module or raise end |
#each_arg(array) ⇒ void #each_arg(array) ⇒ Enumerator[RubyVM::AbstractSyntaxTree::Node, void]
Receives :ARRAY or :LIST node and yields the child nodes.
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'sig/prototype/rbi.rbs', line 93 def each_arg(array, &block) if block_given? if array&.type == :ARRAY || array&.type == :LIST array.children.each do |arg| if arg yield arg end end end else enum_for :each_arg, array end end |
#each_child(node) ⇒ void #each_child(node) ⇒ Enumerator[RubyVM::AbstractSyntaxTree::Node, void]
Receives node and yields the child nodes.
97 98 99 100 101 102 103 |
# File 'sig/prototype/rbi.rbs', line 97 def each_child(node) node.children.each do |child| if child.is_a?(RubyVM::AbstractSyntaxTree::Node) yield child end end end |
#join_comments(nodes, comments) ⇒ AST::Comment
150 151 152 153 |
# File 'lib/rbs/prototype/rbi.rb', line 150 def join_comments(nodes, comments) cs = nodes.map {|node| comments[node.first_lineno - 1] }.compact AST::Comment.new(string: cs.map(&:string).join("\n"), location: nil) end |
#member_visibility(context) ⇒ AST::Members::visibility?
Visibility of a member, given as private def ... in RBS
Returns nil for members in a visibility section, which sync_visibility emits instead.
106 107 108 109 110 111 |
# File 'lib/rbs/prototype/rbi.rb', line 106 def member_visibility(context) # RBS visibility sections don't apply to singleton members, so they need their own visibility. if context.singleton && context.visibility != :public :private end end |
#method_type(args_node, type_node, variables:, overloads:) ⇒ MethodType?
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
# File 'lib/rbs/prototype/rbi.rb', line 423 def method_type(args_node, type_node, variables:, overloads:) if type_node if type_node.type == :CALL method_type = method_type(args_node, type_node.children[0], variables: variables, overloads: overloads) or raise else method_type = MethodType.new( type: Types::Function.empty(Types::Bases::Any.new(location: nil)), block: nil, location: nil, type_params: [] ) end name, args = case type_node.type when :CALL [ type_node.children[1], type_node.children[2] ] when :FCALL, :VCALL [ type_node.children[0], type_node.children[1] ] end case name when :returns return_type = each_arg(args).to_a[0] method_type.update(type: method_type.type.with_return_type(type_of(return_type, variables: variables))) when :params if args_node parse_params(args_node, args, method_type, variables: variables, overloads: overloads) else vars = (node_to_hash(each_arg(args).to_a[0]) || {}).transform_values {|value| type_of(value, variables: variables) } required_positionals = vars.map do |name, type| Types::Function::Param.new(name: name, type: type) end if method_type.type.is_a?(RBS::Types::Function) method_type.update(type: method_type.type.update(required_positionals: required_positionals)) else method_type end end when :type_parameters type_params = [] #: Array[AST::TypeParam] each_arg args do |node| if name = symbol_literal_node?(node) type_params << AST::TypeParam.new( name: name, variance: :invariant, upper_bound: nil, lower_bound: nil, location: nil, default_type: nil ) end end method_type.update(type_params: type_params) when :void method_type.update(type: method_type.type.with_return_type(Types::Bases::Void.new(location: nil))) when :proc method_type else method_type end end end |
#node_to_hash(node) ⇒ Hash[Symbol, RubyVM::AbstractSyntaxTree::Node]?
Receives a keyword :HASH node and returns hash instance.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'sig/prototype/rbi.rbs', line 101 def node_to_hash(node) if node&.type == :HASH # @type var hash: Hash[Symbol, untyped] hash = {} each_arg(node.children[0]).each_slice(2) do |var, type| var or raise if (name = symbol_literal_node?(var)) && type hash[name] = type end end hash end end |
#parse(string) ⇒ void
This method returns an undefined value.
30 31 32 33 |
# File 'lib/rbs/prototype/rbi.rb', line 30 def parse(string) comments = parse_comments(string, include_trailing: true) process RubyVM::AbstractSyntaxTree.parse(string), comments: comments end |
#parse_params(args_node, args, method_type, variables:, overloads:) ⇒ MethodType
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 |
# File 'lib/rbs/prototype/rbi.rb', line 496 def parse_params(args_node, args, method_type, variables:, overloads:) vars = (node_to_hash(each_arg(args).to_a[0]) || {}).transform_values {|value| type_of(value, variables: variables) } # @type var required_positionals: Array[Types::Function::Param] required_positionals = [] # @type var optional_positionals: Array[Types::Function::Param] optional_positionals = [] # @type var rest_positionals: Types::Function::Param? rest_positionals = nil # @type var trailing_positionals: Array[Types::Function::Param] trailing_positionals = [] # @type var required_keywords: Hash[Symbol, Types::Function::Param] required_keywords = {} # @type var optional_keywords: Hash[Symbol, Types::Function::Param] optional_keywords = {} # @type var rest_keywords: Types::Function::Param? rest_keywords = nil var_names = args_node.children[0] pre_num, _pre_init, opt, _first_post, post_num, _post_init, rest, kw, kwrest, block = args_node.children[1].children pre_num.times.each do |i| name = var_names[i] type = vars[name] || Types::Bases::Any.new(location: nil) required_positionals << Types::Function::Param.new(type: type, name: name) end index = pre_num while opt name = var_names[index] if (type = vars[name]) optional_positionals << Types::Function::Param.new(type: type, name: name) end index += 1 opt = opt.children[1] end if rest name = var_names[index] if (type = vars[name]) rest_positionals = Types::Function::Param.new(type: type, name: name) end index += 1 end post_num.times do |i| name = var_names[i+index] if (type = vars[name]) trailing_positionals << Types::Function::Param.new(type: type, name: name) end index += 1 end while kw name, value = kw.children[0].children if (type = vars[name]) if value optional_keywords[name] = Types::Function::Param.new(type: type, name: name) else required_keywords[name] = Types::Function::Param.new(type: type, name: name) end end kw = kw.children[1] end if kwrest name = kwrest.children[0] if (type = vars[name]) rest_keywords = Types::Function::Param.new(type: type, name: name) end end method_block = nil if block if (type = vars[block]) if type.is_a?(Types::Proc) method_block = Types::Block.new(required: true, type: type.type, self_type: nil) elsif type.is_a?(Types::Bases::Any) method_block = Types::Block.new( required: true, type: Types::Function.empty(Types::Bases::Any.new(location: nil)), self_type: nil ) # Handle an optional block like `T.nilable(T.proc.void)`. elsif type.is_a?(Types::Optional) && (proc_type = type.type).is_a?(Types::Proc) method_block = Types::Block.new(required: false, type: proc_type.type, self_type: nil) else STDERR.puts "Unexpected block type: #{type}" PP.pp args_node, STDERR method_block = Types::Block.new( required: true, type: Types::Function.empty(Types::Bases::Any.new(location: nil)), self_type: nil ) end else if overloads == 1 method_block = Types::Block.new( required: false, type: Types::Function.empty(Types::Bases::Any.new(location: nil)), self_type: nil ) end end end if method_type.type.is_a?(Types::Function) method_type.update( type: method_type.type.update( required_positionals: required_positionals, optional_positionals: optional_positionals, rest_positionals: rest_positionals, trailing_positionals: trailing_positionals, required_keywords: required_keywords, optional_keywords: optional_keywords, rest_keywords: rest_keywords ), block: method_block ) else method_type end end |
#pop_sig ⇒ Array[RubyVM::AbstractSyntaxTree::Node]?
Clear the sig call list
65 66 67 68 69 |
# File 'sig/prototype/rbi.rbs', line 65 def pop_sig @last_sig.tap do @last_sig = nil end end |
#proc_type?(type_node) ⇒ Boolean
705 706 707 708 709 710 711 |
# File 'lib/rbs/prototype/rbi.rb', line 705 def proc_type?(type_node) if call_node?(type_node, name: :proc) true else type_node.type == :CALL && proc_type?(type_node.children[0]) end end |
#process(node, outer: [], comments:) ⇒ void
This method returns an undefined value.
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/rbs/prototype/rbi.rb', line 155 def process(node, outer: [], comments:) case node.type when :CLASS comment = comments[node.first_lineno - 1] push_class node.children[0], node.children[1], comment: comment do process node.children[2], outer: outer + [node], comments: comments end when :MODULE comment = comments[node.first_lineno - 1] push_module node.children[0], comment: comment do process node.children[1], outer: outer + [node], comments: comments end when :SCLASS if node.children[0].type == :SELF @contexts << Context.new(singleton: true, visibility: :public) begin process node.children[1], outer: outer + [node], comments: comments ensure @contexts.pop end end when :FCALL case node.children[0] when :include each_arg node.children[1] do |arg| if arg.type == :CONST || arg.type == :COLON2 || arg.type == :COLON3 name = const_to_name(arg) include_member = AST::Members::Include.new( name: name, args: [], annotations: [], location: nil, comment: nil ) current_module!.members << include_member end end when :extend each_arg node.children[1] do |arg| if arg.type == :CONST || arg.type == :COLON2 || arg.type == :COLON3 name = const_to_name(arg) unless ["T::Generic", "T::Helpers", "T::Sig"].include?(name.to_s.delete_prefix("::")) member = AST::Members::Extend.new( name: name, args: [], annotations: [], location: nil, comment: nil ) current_module!.members << member end end end when :sig out = outer.last or raise push_sig out.children.last.children.last when :attr_reader, :attr_writer, :attr_accessor process_attribute node, comments: comments when :private, :protected, :public process_visibility node, outer: outer, comments: comments when :alias_method new, old = each_arg(node.children[1]).map {|x| x.children[0] } current_module!.members << AST::Members::Alias.new( new_name: new, old_name: old, location: nil, annotations: [], kind: current_context!.singleton ? :singleton : :instance, comment: nil ) end when :VCALL case node.children[0] when :private, :protected, :public current_context!.visibility = node.children[0] end when :DEFS sigs = pop_sig if sigs comment = join_comments(sigs, comments) args = node.children[2] types = sigs.map {|sig| method_type(args, sig, variables: current_module!.type_params, overloads: sigs.size) }.compact current_module!.members << AST::Members::MethodDefinition.new( name: node.children[1], location: nil, annotations: [], overloads: types.map {|type| AST::Members::MethodDefinition::Overload.new(annotations: [], method_type: type) }, kind: :singleton, comment: comment, overloading: false, visibility: nil ) end when :DEFN sigs = pop_sig if sigs context = current_context! sync_visibility(context.visibility) comment = join_comments(sigs, comments) args = node.children[1] types = sigs.map {|sig| method_type(args, sig, variables: current_module!.type_params, overloads: sigs.size) }.compact current_module!.members << AST::Members::MethodDefinition.new( name: node.children[0], location: nil, annotations: [], overloads: types.map {|type| AST::Members::MethodDefinition::Overload.new(annotations: [], method_type: type) }, kind: context.singleton ? :singleton : :instance, comment: comment, overloading: false, visibility: member_visibility(context) ) end when :CDECL if (send = node.children.last) && send.type == :FCALL && send.children[0] == :type_member unless each_arg(send.children[1]).any? {|node| node.type == :HASH && each_arg(node.children[0]).each_slice(2).any? {|a, _| symbol_literal_node?(a) == :fixed } } # @type var variance: AST::TypeParam::variance? if (a0 = each_arg(send.children[1]).to_a[0]) && (v = symbol_literal_node?(a0)) variance = case v when :out :covariant when :in :contravariant end end current_module!.type_params << AST::TypeParam.new( name: node.children[0], variance: variance || :invariant, location: nil, upper_bound: nil, lower_bound: nil, default_type: nil ) end else name = node.children[0].yield_self do |n| n.is_a?(Symbol) ? TypeName.new(namespace: Namespace.empty, name: n) : const_to_name(n) end value_node = node.children.last type = if value_node && value_node.type == :CALL && value_node.children[1] == :let type_node = each_arg(value_node.children[2]).to_a[1] type_of type_node, variables: current_module&.type_params || [] else Types::Bases::Any.new(location: nil) end append_decl AST::Declarations::Constant.new( name: name, type: type, location: nil, comment: nil, annotations: [] ) end when :ALIAS sync_visibility(current_context!.visibility) current_module!.members << AST::Members::Alias.new( new_name: node.children[0].children[0], old_name: node.children[1].children[0], location: nil, annotations: [], kind: current_context!.singleton ? :singleton : :instance, comment: nil ) else each_child node do |child| process child, outer: outer + [node], comments: comments end end end |
#process_attribute(node, comments:) ⇒ void
This method returns an undefined value.
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/rbs/prototype/rbi.rb', line 359 def process_attribute(node, comments:) sigs = pop_sig kind = node.children[0] context = current_context! sync_visibility(context.visibility) type = attribute_type(kind, sigs) comment = join_comments(sigs, comments) if sigs member_class = case kind when :attr_reader AST::Members::AttrReader when :attr_writer AST::Members::AttrWriter when :attr_accessor AST::Members::AttrAccessor else raise "Unexpected attribute kind: #{kind}" end each_arg node.children[1] do |arg| if name = symbol_literal_node?(arg) current_module!.members << member_class.new( name: name, type: type, ivar_name: nil, kind: context.singleton ? :singleton : :instance, annotations: [], location: nil, comment: comment, visibility: member_visibility(context) ) end end end |
#process_visibility(node, outer:, comments:) ⇒ void
This method returns an undefined value.
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/rbs/prototype/rbi.rb', line 336 def process_visibility(node, outer:, comments:) visibility = node.children[0] args = each_arg(node.children[1]).to_a context = current_context! if args.empty? context.visibility = visibility else previous_visibility = context.visibility context.visibility = visibility begin args.each do |arg| if arg.type == :DEFN || arg.type == :DEFS process arg, outer: outer + [node], comments: comments end end ensure context.visibility = previous_visibility end end end |
#push_class(name, super_class, comment:) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rbs/prototype/rbi.rb', line 43 def push_class(name, super_class, comment:) class_decl = AST::Declarations::Class.new( name: const_to_name(name), super_class: super_class && AST::Declarations::Class::Super.new(name: const_to_name(super_class), args: [], location: nil), type_params: [], members: [], annotations: [], location: nil, comment: comment ) append_decl class_decl modules << class_decl @contexts << Context.new(singleton: false, visibility: :public) @emitted_visibility[class_decl.object_id] = :public yield ensure @contexts.pop modules.pop end |
#push_module(name, comment:) { ... } ⇒ void
This method returns an undefined value.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rbs/prototype/rbi.rb', line 65 def push_module(name, comment:) module_decl = AST::Declarations::Module.new( name: const_to_name(name), type_params: [], members: [], annotations: [], location: nil, self_types: [], comment: comment ) append_decl module_decl modules << module_decl @contexts << Context.new(singleton: false, visibility: :public) @emitted_visibility[module_decl.object_id] = :public yield ensure @contexts.pop modules.pop end |
#push_sig(node) ⇒ void
This method returns an undefined value.
Put a sig call to current list.
62 63 64 65 66 67 68 |
# File 'sig/prototype/rbi.rbs', line 62 def push_sig(node) if last_sig = @last_sig last_sig << node else @last_sig = [node] end end |
#sync_visibility(visibility) ⇒ void
This method returns an undefined value.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/rbs/prototype/rbi.rb', line 113 def sync_visibility(visibility) # Visibility sections don't apply to singleton members in RBS. return if current_context!.singleton # RBS has no protected visibility. Private is the conservative fallback. visibility = :private if visibility == :protected mod = current_module! return if @emitted_visibility[mod.object_id] == visibility member = case visibility when :public AST::Members::Public.new(location: nil) when :private AST::Members::Private.new(location: nil) else raise "Unexpected visibility: #{visibility}" end mod.members << member @emitted_visibility[mod.object_id] = visibility end |
#type_of(type_node, variables:) ⇒ Types::t
621 622 623 624 625 626 627 628 629 630 631 632 633 634 |
# File 'lib/rbs/prototype/rbi.rb', line 621 def type_of(type_node, variables:) type = type_of0(type_node, variables: variables) case when type.is_a?(Types::ClassInstance) && type.name.name == BuiltinNames::BasicObject.name.name Types::Bases::Any.new(location: nil) when type.is_a?(Types::ClassInstance) && type.name.to_s.delete_prefix("::") == "T::Boolean" Types::Bases::Bool.new(location: nil) when type.is_a?(Types::ClassInstance) && type.name.to_s.delete_prefix("::") == "T::Class" Types::Bases::Any.new(location: nil) else type end end |
#type_of0(type_node, variables:) ⇒ Types::t
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 |
# File 'lib/rbs/prototype/rbi.rb', line 636 def type_of0(type_node, variables:) case when type_node.type == :CONST if variables.include?(type_node.children[0]) Types::Variable.new(name: type_node.children[0], location: nil) else Types::ClassInstance.new(name: const_to_name(type_node), args: [], location: nil) end when type_node.type == :COLON2 || type_node.type == :COLON3 Types::ClassInstance.new(name: const_to_name(type_node), args: [], location: nil) when call_node?(type_node, name: :[], receiver: -> (_) { true }) # The type_node represents a type application receiver = type_node.children[0] if [:CONST, :COLON2, :COLON3].include?(receiver.type) && const_to_name(receiver).to_s.delete_prefix("::") == "T::Class" return Types::Bases::Any.new(location: nil) end type = type_of(type_node.children[0], variables: variables) type.is_a?(Types::ClassInstance) or raise each_arg(type_node.children[2]) do |arg| type.args << type_of(arg, variables: variables) end type when call_node?(type_node, name: :type_parameter) name = each_arg(type_node.children[2]).to_a[0].children[0] Types::Variable.new(name: name, location: nil) when call_node?(type_node, name: :any) types = each_arg(type_node.children[2]).to_a.map {|node| type_of(node, variables: variables) } Types::Union.new(types: types, location: nil) when call_node?(type_node, name: :all) types = each_arg(type_node.children[2]).to_a.map {|node| type_of(node, variables: variables) } Types::Intersection.new(types: types, location: nil) when call_node?(type_node, name: :untyped) Types::Bases::Any.new(location: nil) when call_node?(type_node, name: :nilable) type = type_of each_arg(type_node.children[2]).to_a[0], variables: variables Types::Optional.new(type: type, location: nil) when call_node?(type_node, name: :self_type) Types::Bases::Self.new(location: nil) when call_node?(type_node, name: :attached_class) Types::Bases::Instance.new(location: nil) when call_node?(type_node, name: :noreturn) Types::Bases::Bottom.new(location: nil) when call_node?(type_node, name: :class_of) type = type_of each_arg(type_node.children[2]).to_a[0], variables: variables case type when Types::ClassInstance Types::ClassSingleton.new(name: type.name, location: nil) else STDERR.puts "Unexpected type for `class_of`: #{type}" Types::Bases::Any.new(location: nil) end when type_node.type == :ARRAY, type_node.type == :LIST types = each_arg(type_node).map {|node| type_of(node, variables: variables) } Types::Tuple.new(types: types, location: nil) else if proc_type?(type_node) method_type = method_type(nil, type_node, variables: variables, overloads: 1) or raise Types::Proc.new(type: method_type.type, block: nil, location: nil, self_type: nil) else STDERR.puts "Unexpected type_node:" PP.pp type_node, STDERR Types::Bases::Any.new(location: nil) end end end |