Class: RBS::EnvironmentWalker

Inherits:
Object
  • Object
show all
Includes:
TSort, TSort[node]
Defined in:
sig/environment_walker.rbs,
lib/rbs/environment_walker.rb

Overview

EnvironmentWalker provides topological sort of class/module definitions.

If a method, attribute, or ancestor in a class definition have a reference to another class, it is dependency.

walker = EnvironmentWalker.new(env: env)

walker.each_strongly_connected_component do |scc|
  # Yields an array of strongly connected components.
end

The #only_ancestors! method limits the dependency only to ancestors. Only super classes and included modules are dependencies with the option. This is useful to calculate the dependencies of class hierarchy.

walker = EnvironmentWalker.new(env: env).only_ancestors!

walker.each_strongly_connected_component do |scc|
  # Yields an array of strongly connected components.
end

Defined Under Namespace

Classes: InstanceNode, SingletonNode, TypeNameNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:) ⇒ EnvironmentWalker

Returns a new instance of EnvironmentWalker.

Parameters:



11
12
13
14
# File 'lib/rbs/environment_walker.rb', line 11

def initialize(env:)
  @env = env
  @only_ancestors = false
end

Instance Attribute Details

#builderDefinitionBuilder (readonly)

Returns the value of attribute builder.

Returns:



16
17
18
# File 'lib/rbs/environment_walker.rb', line 16

def builder
  @builder ||= DefinitionBuilder.new(env: env)
end

#envEnvironment (readonly)

Returns the value of attribute env.

Returns:



9
10
11
# File 'lib/rbs/environment_walker.rb', line 9

def env
  @env
end

#only_ancestorsBoolean (readonly)

Returns the value of attribute only_ancestors.

Returns:

  • (Boolean)


43
44
45
# File 'sig/environment_walker.rbs', line 43

def only_ancestors
  @only_ancestors
end

Instance Method Details

#each_type_name(type) {|arg0| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • (Types::t)

Yields:

Yield Parameters:

Yield Returns:

  • (void)


99
100
101
102
103
# File 'lib/rbs/environment_walker.rb', line 99

def each_type_name(type, &block)
  each_type_node(type) do |node|
    yield node.type_name
  end
end

#each_type_node(type) {|arg0| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • (Types::t)

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
# File 'lib/rbs/environment_walker.rb', line 105

def each_type_node(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 SingletonNode.new(type_name: type.name)
  when RBS::Types::ClassInstance
    yield InstanceNode.new(type_name: type.name)
    type.args.each do |ty|
      each_type_node(ty, &block)
    end
  when RBS::Types::Interface
    yield TypeNameNode.new(type_name: type.name)
    type.args.each do |ty|
      each_type_node(ty, &block)
    end
  when RBS::Types::Alias
    yield TypeNameNode.new(type_name: type.name)
    type.args.each do |ty|
      each_type_node(ty, &block)
    end
  when RBS::Types::Union, RBS::Types::Intersection, RBS::Types::Tuple
    type.types.each do |ty|
      each_type_node ty, &block
    end
  when RBS::Types::Optional
    each_type_node type.type, &block
  when RBS::Types::Literal
    # nop
  when RBS::Types::Record
    type.fields.each_value do |ty|
      each_type_node ty, &block
    end
  when RBS::Types::Proc
    type.each_type do |ty|
      each_type_node ty, &block
    end
  else
    raise "Unexpected type given: #{type}"
  end
end

#only_ancestors!(only = true) ⇒ self

Parameters:

  • only (Boolean) (defaults to: true)

Returns:

  • (self)


20
21
22
23
# File 'lib/rbs/environment_walker.rb', line 20

def only_ancestors!(only = true)
  @only_ancestors = only
  self
end

#only_ancestors?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/rbs/environment_walker.rb', line 25

def only_ancestors?
  @only_ancestors
end

#tsort_each_child(node) {|arg0| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • (node)

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rbs/environment_walker.rb', line 44

def tsort_each_child(node, &block)
  name = node.type_name

  unless name.namespace.empty?
    yield SingletonNode.new(type_name: name.namespace.to_type_name)
  end

  case node
  when TypeNameNode
    case
    when name.interface?
      definition = builder.build_interface(name)
      unless only_ancestors?
        definition.each_type do |type|
          each_type_node type, &block
        end
      end
    when name.alias?
      each_type_node builder.expand_alias1(name), &block
    else
      raise "Unexpected TypeNameNode with type_name=#{name}"
    end

  when InstanceNode, SingletonNode
    definition = if node.is_a?(InstanceNode)
                   builder.build_instance(name)
                 else
                   builder.build_singleton(name)
                 end

    if ancestors = definition.ancestors
      ancestors.ancestors.each do |ancestor|
        case ancestor
        when Definition::Ancestor::Instance
          yield InstanceNode.new(type_name: ancestor.name)

          unless only_ancestors?
            ancestor.args.each do |type|
              each_type_node type, &block
            end
          end
        when Definition::Ancestor::Singleton
          yield SingletonNode.new(type_name: ancestor.name)
        end
      end
    end

    unless only_ancestors?
      definition.each_type do |type|
        each_type_node type, &block
      end
    end
  end
end

#tsort_each_node {|arg0| ... } ⇒ void

This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rbs/environment_walker.rb', line 31

def tsort_each_node(&block)
  env.class_decls.each_key do |type_name|
    yield InstanceNode.new(type_name: type_name)
    yield SingletonNode.new(type_name: type_name)
  end
  env.interface_decls.each_key do |type_name|
    yield TypeNameNode.new(type_name: type_name)
  end
  env.type_alias_decls.each_key do |type_name|
    yield TypeNameNode.new(type_name: type_name)
  end
end