Class: Spoom::Model::Builder

Inherits:
NamespaceVisitor show all
Extended by:
T::Sig
Defined in:
lib/spoom/model/builder.rb

Overview

Populate a Model by visiting the nodes from a Ruby file

Instance Method Summary collapse

Methods inherited from NamespaceVisitor

#visit

Methods inherited from Visitor

#visit_alias_global_variable_node, #visit_alias_method_node, #visit_alternation_pattern_node, #visit_and_node, #visit_arguments_node, #visit_array_node, #visit_array_pattern_node, #visit_assoc_node, #visit_assoc_splat_node, #visit_back_reference_read_node, #visit_begin_node, #visit_block_argument_node, #visit_block_local_variable_node, #visit_block_node, #visit_block_parameter_node, #visit_block_parameters_node, #visit_break_node, #visit_call_and_write_node, #visit_call_operator_write_node, #visit_call_or_write_node, #visit_call_target_node, #visit_capture_pattern_node, #visit_case_match_node, #visit_case_node, #visit_child_nodes, #visit_class_variable_and_write_node, #visit_class_variable_operator_write_node, #visit_class_variable_or_write_node, #visit_class_variable_read_node, #visit_class_variable_target_node, #visit_class_variable_write_node, #visit_constant_and_write_node, #visit_constant_operator_write_node, #visit_constant_or_write_node, #visit_constant_path_and_write_node, #visit_constant_path_node, #visit_constant_path_operator_write_node, #visit_constant_path_or_write_node, #visit_constant_path_target_node, #visit_constant_read_node, #visit_constant_target_node, #visit_defined_node, #visit_else_node, #visit_embedded_statements_node, #visit_embedded_variable_node, #visit_ensure_node, #visit_false_node, #visit_find_pattern_node, #visit_flip_flop_node, #visit_float_node, #visit_for_node, #visit_forwarding_arguments_node, #visit_forwarding_parameter_node, #visit_forwarding_super_node, #visit_global_variable_and_write_node, #visit_global_variable_operator_write_node, #visit_global_variable_or_write_node, #visit_global_variable_read_node, #visit_global_variable_target_node, #visit_global_variable_write_node, #visit_hash_node, #visit_hash_pattern_node, #visit_if_node, #visit_imaginary_node, #visit_implicit_node, #visit_implicit_rest_node, #visit_in_node, #visit_index_and_write_node, #visit_index_operator_write_node, #visit_index_or_write_node, #visit_index_target_node, #visit_instance_variable_and_write_node, #visit_instance_variable_operator_write_node, #visit_instance_variable_or_write_node, #visit_instance_variable_read_node, #visit_instance_variable_target_node, #visit_instance_variable_write_node, #visit_integer_node, #visit_interpolated_match_last_line_node, #visit_interpolated_regular_expression_node, #visit_interpolated_string_node, #visit_interpolated_symbol_node, #visit_interpolated_x_string_node, #visit_keyword_hash_node, #visit_keyword_rest_parameter_node, #visit_lambda_node, #visit_local_variable_and_write_node, #visit_local_variable_operator_write_node, #visit_local_variable_or_write_node, #visit_local_variable_read_node, #visit_local_variable_target_node, #visit_local_variable_write_node, #visit_match_last_line_node, #visit_match_predicate_node, #visit_match_required_node, #visit_match_write_node, #visit_missing_node, #visit_multi_target_node, #visit_next_node, #visit_nil_node, #visit_no_keywords_parameter_node, #visit_numbered_parameters_node, #visit_numbered_reference_read_node, #visit_optional_keyword_parameter_node, #visit_optional_parameter_node, #visit_or_node, #visit_parameters_node, #visit_parentheses_node, #visit_pinned_expression_node, #visit_pinned_variable_node, #visit_post_execution_node, #visit_pre_execution_node, #visit_program_node, #visit_range_node, #visit_rational_node, #visit_redo_node, #visit_regular_expression_node, #visit_required_keyword_parameter_node, #visit_required_parameter_node, #visit_rescue_modifier_node, #visit_rescue_node, #visit_rest_parameter_node, #visit_retry_node, #visit_return_node, #visit_self_node, #visit_source_encoding_node, #visit_source_file_node, #visit_source_line_node, #visit_splat_node, #visit_statements_node, #visit_string_node, #visit_super_node, #visit_symbol_node, #visit_true_node, #visit_undef_node, #visit_unless_node, #visit_until_node, #visit_when_node, #visit_while_node, #visit_x_string_node, #visit_yield_node

Constructor Details

#initialize(model, file) ⇒ Builder

Returns a new instance of Builder.



11
12
13
14
15
16
17
18
19
# File 'lib/spoom/model/builder.rb', line 11

def initialize(model, file)
  super()

  @model = model
  @file = file
  @namespace_nesting = T.let([], T::Array[Namespace])
  @visibility_stack = T.let([Visibility::Public], T::Array[Visibility])
  @last_sigs = T.let([], T::Array[Sig])
end

Instance Method Details

#visit_call_node(node) ⇒ Object



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
220
221
222
223
224
# File 'lib/spoom/model/builder.rb', line 146

def visit_call_node(node)
  return if node.receiver && !node.receiver.is_a?(Prism::SelfNode)

  current_namespace = @namespace_nesting.last

  case node.name
  when :attr_accessor
    sigs = collect_sigs
    node.arguments&.arguments&.each do |arg|
      next unless arg.is_a?(Prism::SymbolNode)

      AttrAccessor.new(
        @model.register_symbol([*@names_nesting, arg.slice.delete_prefix(":")].join("::")),
        owner: current_namespace,
        location: node_location(arg),
        visibility: current_visibility,
        sigs: sigs,
      )
    end
  when :attr_reader
    sigs = collect_sigs
    node.arguments&.arguments&.each do |arg|
      next unless arg.is_a?(Prism::SymbolNode)

      AttrReader.new(
        @model.register_symbol([*@names_nesting, arg.slice.delete_prefix(":")].join("::")),
        owner: current_namespace,
        location: node_location(arg),
        visibility: current_visibility,
        sigs: sigs,
      )
    end
  when :attr_writer
    sigs = collect_sigs
    node.arguments&.arguments&.each do |arg|
      next unless arg.is_a?(Prism::SymbolNode)

      AttrWriter.new(
        @model.register_symbol([*@names_nesting, arg.slice.delete_prefix(":")].join("::")),
        owner: current_namespace,
        location: node_location(arg),
        visibility: current_visibility,
        sigs: sigs,
      )
    end
  when :include
    node.arguments&.arguments&.each do |arg|
      next unless arg.is_a?(Prism::ConstantReadNode) || arg.is_a?(Prism::ConstantPathNode)
      next unless current_namespace

      current_namespace.mixins << Include.new(arg.slice)
    end
  when :prepend
    node.arguments&.arguments&.each do |arg|
      next unless arg.is_a?(Prism::ConstantReadNode) || arg.is_a?(Prism::ConstantPathNode)
      next unless current_namespace

      current_namespace.mixins << Prepend.new(arg.slice)
    end
  when :extend
    node.arguments&.arguments&.each do |arg|
      next unless arg.is_a?(Prism::ConstantReadNode) || arg.is_a?(Prism::ConstantPathNode)
      next unless current_namespace

      current_namespace.mixins << Extend.new(arg.slice)
    end
  when :public, :private, :protected
    @visibility_stack << Visibility.from_serialized(node.name.to_s)
    if node.arguments
      super
      @visibility_stack.pop
    end
  when :sig
    @last_sigs << Sig.new(node.slice)
  else
    @last_sigs.clear
    super
  end
end

#visit_class_node(node) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/spoom/model/builder.rb', line 24

def visit_class_node(node)
  @namespace_nesting << Class.new(
    @model.register_symbol(@names_nesting.join("::")),
    owner: @namespace_nesting.last,
    location: node_location(node),
    superclass_name: node.superclass&.slice,
  )
  @visibility_stack << Visibility::Public
  super
  @visibility_stack.pop
  @namespace_nesting.pop
  @last_sigs.clear
end

#visit_constant_path_write_node(node) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/spoom/model/builder.rb', line 71

def visit_constant_path_write_node(node)
  @last_sigs.clear

  name = node.target.slice
  full_name = if name.start_with?("::")
    name.delete_prefix("::")
  else
    [*@names_nesting, name].join("::")
  end

  Constant.new(
    @model.register_symbol(full_name),
    owner: @namespace_nesting.last,
    location: node_location(node),
    value: node.value.slice,
  )

  super
end

#visit_constant_write_node(node) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/spoom/model/builder.rb', line 92

def visit_constant_write_node(node)
  @last_sigs.clear

  Constant.new(
    @model.register_symbol([*@names_nesting, node.name.to_s].join("::")),
    owner: @namespace_nesting.last,
    location: node_location(node),
    value: node.value.slice,
  )

  super
end

#visit_def_node(node) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/spoom/model/builder.rb', line 127

def visit_def_node(node)
  recv = node.receiver

  if !recv || recv.is_a?(Prism::SelfNode)
    Method.new(
      @model.register_symbol([*@names_nesting, node.name.to_s].join("::")),
      owner: @namespace_nesting.last,
      location: node_location(node),
      visibility: current_visibility,
      sigs: collect_sigs,
    )
  end

  super
end

#visit_module_node(node) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/spoom/model/builder.rb', line 55

def visit_module_node(node)
  @namespace_nesting << Module.new(
    @model.register_symbol(@names_nesting.join("::")),
    owner: @namespace_nesting.last,
    location: node_location(node),
  )
  @visibility_stack << Visibility::Public
  super
  @visibility_stack.pop
  @namespace_nesting.pop
  @last_sigs.clear
end

#visit_multi_write_node(node) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/spoom/model/builder.rb', line 106

def visit_multi_write_node(node)
  @last_sigs.clear

  node.lefts.each do |const|
    case const
    when Prism::ConstantTargetNode, Prism::ConstantPathTargetNode
      Constant.new(
        @model.register_symbol([*@names_nesting, const.slice].join("::")),
        owner: @namespace_nesting.last,
        location: node_location(const),
        value: node.value.slice,
      )
    end
  end

  super
end

#visit_singleton_class_node(node) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/spoom/model/builder.rb', line 39

def visit_singleton_class_node(node)
  @namespace_nesting << SingletonClass.new(
    @model.register_symbol(@names_nesting.join("::")),
    owner: @namespace_nesting.last,
    location: node_location(node),
  )
  @visibility_stack << Visibility::Public
  super
  @visibility_stack.pop
  @namespace_nesting.pop
  @last_sigs.clear
end