Class: RBS::EnvironmentWalker
- Inherits:
-
Object
- Object
- RBS::EnvironmentWalker
- Includes:
- TSort
- Defined in:
- lib/rbs/environment_walker.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
Instance Method Summary collapse
- #builder ⇒ Object
- #each_type_name(type, &block) ⇒ Object
-
#initialize(env:) ⇒ EnvironmentWalker
constructor
A new instance of EnvironmentWalker.
- #only_ancestors!(only = true) ⇒ Object
- #only_ancestors? ⇒ Boolean
- #tsort_each_child(name, &block) ⇒ Object
- #tsort_each_node(&block) ⇒ Object
Constructor Details
#initialize(env:) ⇒ EnvironmentWalker
Returns a new instance of EnvironmentWalker.
5 6 7 8 |
# File 'lib/rbs/environment_walker.rb', line 5 def initialize(env:) @env = env @only_ancestors = nil end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
3 4 5 |
# File 'lib/rbs/environment_walker.rb', line 3 def env @env end |
Instance Method Details
#builder ⇒ Object
10 11 12 |
# File 'lib/rbs/environment_walker.rb', line 10 def builder @builder ||= DefinitionBuilder.new(env: env) end |
#each_type_name(type, &block) ⇒ Object
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 116 117 118 119 120 121 122 |
# File 'lib/rbs/environment_walker.rb', line 82 def each_type_name(type, &block) case type when RBS::Types::Bases::Any when RBS::Types::Bases::Class when RBS::Types::Bases::Instance when RBS::Types::Bases::Self when RBS::Types::Bases::Top when RBS::Types::Bases::Bottom when RBS::Types::Bases::Bool when RBS::Types::Bases::Void when RBS::Types::Bases::Nil when RBS::Types::Variable when RBS::Types::ClassSingleton yield type.name when RBS::Types::ClassInstance, RBS::Types::Interface yield type.name type.args.each do |ty| each_type_name(ty, &block) end when RBS::Types::Alias yield type.name when RBS::Types::Union, RBS::Types::Intersection, RBS::Types::Tuple type.types.each do |ty| each_type_name ty, &block end when RBS::Types::Optional each_type_name type.type, &block when RBS::Types::Literal # nop when RBS::Types::Record type.fields.each_value do |ty| each_type_name ty, &block end when RBS::Types::Proc type.each_type do |ty| each_type_name ty, &block end else raise "Unexpected type given: #{type}" end end |
#only_ancestors!(only = true) ⇒ Object
14 15 16 17 |
# File 'lib/rbs/environment_walker.rb', line 14 def only_ancestors!(only = true) @only_ancestors = only self end |
#only_ancestors? ⇒ Boolean
19 20 21 |
# File 'lib/rbs/environment_walker.rb', line 19 def only_ancestors? @only_ancestors end |
#tsort_each_child(name, &block) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 |
# File 'lib/rbs/environment_walker.rb', line 35 def tsort_each_child(name, &block) unless name.namespace.empty? yield name.namespace.to_type_name end case when name.class?, name.interface? definitions = [] case when name.class? definitions << builder.build_instance(name) definitions << builder.build_singleton(name) when name.interface? definitions << builder.build_interface(name, env.find_class(name)) end definitions.each do |definition| definition.ancestors.each do |ancestor| yield ancestor.name case ancestor when Definition::Ancestor::Instance, Definition::Ancestor::ExtensionInstance ancestor.args.each do |type| each_type_name type, &block end end end unless only_ancestors? definition.methods.each do |_, method| method.method_types.each do |method_type| method_type.type.each_type do |type| each_type_name type, &block end method_type.block&.type&.each_type do |type| each_type_name type, &block end end end end end when name.alias? each_type_name builder.(name), &block end end |
#tsort_each_node(&block) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/rbs/environment_walker.rb', line 25 def tsort_each_node(&block) env.each_decl do |name| yield name.absolute! end env.each_alias do |name, _| yield name.absolute! end end |