Skip to content

Commit

Permalink
Merge pull request #355 from Shopify/at-type-visitor
Browse files Browse the repository at this point in the history
Introduce Type::Visitor
  • Loading branch information
Morriar authored Aug 7, 2024
2 parents 5f10812 + e11f133 commit 574748b
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rbi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Error < StandardError
require "rbi/rewriters/sort_nodes"
require "rbi/parser"
require "rbi/type_parser"
require "rbi/type_visitor"
require "rbi/printer"
require "rbi/formatter"
require "rbi/version"
112 changes: 112 additions & 0 deletions lib/rbi/type_visitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# typed: strict
# frozen_string_literal: true

module RBI
class Type
class Visitor
extend T::Sig

class Error < RBI::Error; end

sig { params(node: Type).void }
def visit(node)
case node
when Type::Simple
visit_simple(node)
when Type::Void
visit_void(node)
when Type::Boolean
visit_boolean(node)
when Type::Nilable
visit_nilable(node)
when Type::Untyped
visit_untyped(node)
when Type::Generic
visit_generic(node)
when Type::Anything
visit_anything(node)
when Type::NoReturn
visit_no_return(node)
when Type::SelfType
visit_self_type(node)
when Type::AttachedClass
visit_attached_class(node)
when Type::ClassOf
visit_class_of(node)
when Type::All
visit_all(node)
when Type::Any
visit_any(node)
when Type::Tuple
visit_tuple(node)
when Type::Shape
visit_shape(node)
when Type::Proc
visit_proc(node)
when Type::TypeParameter
visit_type_parameter(node)
when Type::Class
visit_class(node)
else
raise Error, "Unhandled node: #{node.class}"
end
end

private

sig { params(type: Type::All).void }
def visit_all(type); end

sig { params(type: Type::Any).void }
def visit_any(type); end

sig { params(type: Type::Anything).void }
def visit_anything(type); end

sig { params(type: Type::AttachedClass).void }
def visit_attached_class(type); end

sig { params(type: Type::Boolean).void }
def visit_boolean(type); end

sig { params(type: Type::Class).void }
def visit_class(type); end

sig { params(type: Type::ClassOf).void }
def visit_class_of(type); end

sig { params(type: Type::Generic).void }
def visit_generic(type); end

sig { params(type: Type::Nilable).void }
def visit_nilable(type); end

sig { params(type: Type::Simple).void }
def visit_simple(type); end

sig { params(type: Type::NoReturn).void }
def visit_no_return(type); end

sig { params(type: Type::Proc).void }
def visit_proc(type); end

sig { params(type: Type::SelfType).void }
def visit_self_type(type); end

sig { params(type: Type::Void).void }
def visit_void(type); end

sig { params(type: Type::Shape).void }
def visit_shape(type); end

sig { params(type: Type::Tuple).void }
def visit_tuple(type); end

sig { params(type: Type::TypeParameter).void }
def visit_type_parameter(type); end

sig { params(type: Type::Untyped).void }
def visit_untyped(type); end
end
end
end

0 comments on commit 574748b

Please sign in to comment.