Method: Jazzy::SourceKitten.make_source_declarations

Defined in:
lib/jazzy/sourcekitten.rb

.make_source_declarations(docs, parent = nil, mark = SourceMark.new) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'lib/jazzy/sourcekitten.rb', line 538

def self.make_source_declarations(docs, parent = nil, mark = SourceMark.new)
  declarations = []
  current_mark = mark
  Array(docs).each do |doc|
    if doc.key?('key.diagnostic_stage')
      declarations += make_source_declarations(
        doc['key.substructure'], parent
      )
      next
    end
    declaration = SourceDeclaration.new
    declaration.parent_in_code = parent
    declaration.type =
      SourceDeclaration::Type.new(doc['key.kind'],
                                  doc['key.fully_annotated_decl'])
    declaration.typename = doc['key.typename']
    declaration.objc_name = doc['key.name']
    documented_name = if Config.instance.hide_objc? && doc['key.swift_name']
                        doc['key.swift_name']
                      else
                        declaration.objc_name
                      end
    if declaration.type.task_mark?(documented_name)
      current_mark = SourceMark.new(documented_name)
    end
    if declaration.type.swift_enum_case?
      # Enum "cases" are thin wrappers around enum "elements".
      declarations += make_source_declarations(
        doc['key.substructure'], parent, current_mark
      )
      next
    end
    next unless declaration.type.should_document?

    unless declaration.type.name
      raise 'Please file an issue at ' \
        'https://github.com/realm/jazzy/issues about adding support ' \
        "for `#{declaration.type.kind}`."
    end

    unless documented_name
      warn 'Found a declaration without `key.name` that will be ' \
        'ignored.  Documentation may be incomplete.  This is probably ' \
        'caused by unresolved compiler errors: check the sourcekitten ' \
        'output for error messages.'
      next
    end

    declaration.file = Pathname(doc['key.filepath']) if doc['key.filepath']
    declaration.usr = doc['key.usr']
    declaration.type_usr = doc['key.typeusr']
    declaration.module_name =
      if declaration.swift?
        # Filter out Apple sub-framework implementation names
        doc['key.modulename']&.sub(/\..*$/, '')
      else
        # ObjC best effort, category original module is unavailable
        @current_module_name
      end
    declaration.doc_module_name = @current_module_name
    declaration.name = documented_name
    declaration.mark = current_mark
    declaration.access_control_level =
      SourceDeclaration::AccessControlLevel.from_doc(doc)
    declaration.line = doc['key.doc.line'] || doc['key.line']
    declaration.column = doc['key.doc.column'] || doc['key.column']
    declaration.start_line = doc['key.parsed_scope.start']
    declaration.end_line = doc['key.parsed_scope.end']
    declaration.deprecated = doc['key.always_deprecated']
    declaration.unavailable = doc['key.always_unavailable']
    declaration.generic_requirements =
      find_generic_requirements(doc['key.parsed_declaration'])
    inherited_types = doc['key.inheritedtypes'] || []
    declaration.inherited_types =
      inherited_types.map { |type| type['key.name'] }.compact
    declaration.async =
      doc['key.symgraph_async'] ||
      if xml_declaration = doc['key.fully_annotated_decl']
        swift_async?(xml_declaration)
      end

    next unless make_doc_info(doc, declaration)

    declaration.children = make_substructure(doc, declaration)
    next if declaration.type.extension? &&
            declaration.children.empty? &&
            !declaration.inherited_types?

    declarations << declaration
  end
  declarations
end