Method: Parlour::TypeParser#parse_path_to_object

Defined in:
lib/parlour/type_parser.rb

#parse_path_to_object(path, is_within_eigenclass: false) ⇒ Object



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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
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
393
394
395
396
397
398
# File 'lib/parlour/type_parser.rb', line 152

def parse_path_to_object(path, is_within_eigenclass: false)
  node = path.traverse(ast)

  case node.type
  when :class
    parse_err 'cannot declare classes in an eigenclass', node if is_within_eigenclass

    name, superclass, body = *node
    final = body_has_modifier?(body, :final!)
    sealed = body_has_modifier?(body, :sealed!)
    abstract = body_has_modifier?(body, :abstract!)
    includes, extends = body ? body_includes_and_extends(body) : [[], []]

    # Create all classes, if we're given a definition like "class A::B"
    *parent_names, this_name = constant_names(name)
    target = T.let(nil, T.nilable(RbiGenerator::Namespace))
    top_level = T.let(nil, T.nilable(RbiGenerator::Namespace))
    parent_names.each do |n|
      new_obj = RbiGenerator::Namespace.new(
        generator,
        n.to_s,
        false,
        false,
      )
      target.children << new_obj if target
      target = new_obj
      top_level ||= new_obj
    end if parent_names

    # Instantiate the correct kind of class
    if ['T::Struct', '::T::Struct'].include?(node_to_s(superclass))
      # Find all of this struct's props and consts
      # The body is typically a `begin` element but when there's only
      # one node there's no wrapping block and instead it would directly
      # be the node.
      prop_nodes = body.nil? ? [] :
        (body.type == :begin ? body.to_a : [body]).select { |x| x.type == :send && [:prop, :const].include?(x.to_a[1]) }

      props = prop_nodes.map do |prop_node|
        _, prop_type, name_node, type_node, extras_hash_node = *prop_node

        # "const" is just "prop ..., immutable: true"
        extras_hash = extras_hash_node.to_a.map do |pair_node|
          key_node, value_node = *pair_node
          parse_err 'prop/const key must be a symbol', prop_node unless key_node.type == :sym
          key = key_node.to_a.first

          value =
            if key == :default
              T.must(node_to_s(value_node))
            else
              case value_node.type
              when :true
                true
              when :false
                false
              when :sym
                value_node.to_a.first
              else
                T.must(node_to_s(value_node))
              end
            end

          [key, value]
        end.to_h

        if prop_type == :const
          parse_err 'const cannot use immutable key', prop_node unless extras_hash[:immutable].nil?
          extras_hash[:immutable] = true
        end

        # Get prop/const name
        parse_err 'prop/const name must be a symbol or string', prop_node unless [:sym, :str].include?(name_node.type)
        name = name_node.to_a.first.to_s

        RbiGenerator::StructProp.new(
          name,
          T.must(node_to_s(type_node)),
          **T.unsafe(extras_hash)
        )
      end

      final_obj = RbiGenerator::StructClassNamespace.new(
        generator,
        this_name.to_s,
        final,
        sealed,
        props,
        abstract,
      )
    elsif ['T::Enum', '::T::Enum'].include?(node_to_s(superclass))
      # Look for (block (send nil :enums) ...) structure
      enums_node = body.nil? ? nil :
        (body.type == :begin ? body.to_a : [body]).find { |x| x.type == :block && x.to_a[0].type == :send && x.to_a[0].to_a[1] == :enums }

      # Find the constant assigments within this block
      # (If there's only one constant assignment, then that `casgn` node
      # will be a direct child, not wrapped in a body)
      enum_inner = enums_node.to_a[2]
      if enum_inner.nil?
        constant_nodes = []
      elsif enum_inner.type == :begin
        constant_nodes = enum_inner.to_a
      else
        constant_nodes = [enum_inner]
      end

      # Convert this to an array to enums as EnumClassNamespace expects
      enums = constant_nodes.map do |constant_node|
        _, name, new_node = *constant_node
        serialize_value = node_to_s(new_node.to_a[2])

        serialize_value ? [name.to_s, serialize_value] : name.to_s
      end

      final_obj = RbiGenerator::EnumClassNamespace.new(
        generator,
        this_name.to_s,
        final,
        sealed,
        enums,
        abstract,
      )
    else
      final_obj = RbiGenerator::ClassNamespace.new(
        generator,
        this_name.to_s,
        final,
        sealed,
        node_to_s(superclass),
        abstract,
      )
    end

    final_obj.children.concat(parse_path_to_object(path.child(2))) if body
    final_obj.create_includes(includes)
    final_obj.create_extends(extends)

    if target
      target.children << final_obj
      [T.must(top_level)]
    else
      [final_obj]
    end
  when :module
    parse_err 'cannot declare modules in an eigenclass', node if is_within_eigenclass

    name, body = *node
    abstract = body_has_modifier?(body, :abstract!)
    final = body_has_modifier?(body, :final!)
    sealed = body_has_modifier?(body, :sealed!)
    interface = body_has_modifier?(body, :interface!)
    includes, extends = body ? body_includes_and_extends(body) : [[], []]

    # Create all modules, if we're given a definition like "module A::B"
    *parent_names, this_name = constant_names(name)
    target = T.let(nil, T.nilable(RbiGenerator::Namespace))
    top_level = T.let(nil, T.nilable(RbiGenerator::Namespace))
    parent_names.each do |n|
      new_obj = RbiGenerator::Namespace.new(
        generator,
        n.to_s,
        false,
        false,
      )
      target.children << new_obj if target
      target = new_obj
      top_level ||= new_obj
    end if parent_names

    final_obj = RbiGenerator::ModuleNamespace.new(
      generator,
      this_name.to_s,
      final,
      sealed,
      interface,
      abstract,
    ) do |m|
      m.children.concat(parse_path_to_object(path.child(1))) if body
      m.create_includes(includes)
      m.create_extends(extends)
    end

    if target
      target.children << final_obj
      [T.must(top_level)]
    else
      [final_obj]
    end
  when :send, :block
    if sig_node?(node)
      parse_sig_into_methods(path, is_within_eigenclass: is_within_eigenclass)
    elsif node.type == :send &&
        [:attr_reader, :attr_writer, :attr_accessor].include?(node.to_a[1]) &&
        !previous_sibling_sig_node?(path)
      parse_method_into_methods(path, is_within_eigenclass: is_within_eigenclass)
    else
      []
    end
  when :def, :defs
    if previous_sibling_sig_node?(path)
      []
    else
      parse_method_into_methods(path, is_within_eigenclass: is_within_eigenclass)
    end
  when :sclass
    parse_err 'cannot access eigen of non-self object', node unless node.to_a[0].type == :self
    parse_path_to_object(path.child(1), is_within_eigenclass: true)
  when :begin
    # Just map over all the things
    node.to_a.length.times.map do |c|
      parse_path_to_object(path.child(c), is_within_eigenclass: is_within_eigenclass)
    end.flatten
  when :casgn
    _, name, body = *node

    # Determine whether this is a constant or a type alias
    # A type alias looks like:
    #   (block (send (const nil :T) :type_alias) (args) (type_to_alias))
    if body.type == :block &&
      body.to_a[0].type == :send &&
      body.to_a[0].to_a[0]&.type == :const &&
      body.to_a[0].to_a[0]&.to_a == [nil, :T] &&
      body.to_a[0].to_a[1] == :type_alias

      [Parlour::RbiGenerator::TypeAlias.new(
        generator,
        name: T.must(name).to_s,
        type: T.must(node_to_s(body.to_a[2])),
      )]
    else
      heredocs = find_heredocs(body)
      [Parlour::RbiGenerator::Constant.new(
        generator,
        name: T.must(name).to_s,
        value: T.must(node_to_s(body)),
        heredocs: heredocs
      )]
    end
  else
    if unknown_node_errors
      parse_err "don't understand node type #{node.type}", node
    else
      []
    end
  end
end