Class: Orthoses::YARD

Inherits:
Object
  • Object
show all
Defined in:
lib/orthoses/yard.rb,
lib/orthoses/yard/version.rb,
lib/orthoses/yard/yard2rbs.rb

Overview

use Orthoses::YARD, parse: “lib/*/.rb”

Defined Under Namespace

Classes: YARD2RBS

Constant Summary collapse

VERSION =
"0.6.0"

Instance Method Summary collapse

Constructor Details

#initialize(loader, parse:, use_cache: true, log_level: nil, allow_empty_doc: false) ⇒ YARD

Returns a new instance of YARD.

Parameters:

  • loader
  • parse (<String>, String)

    Target files

  • use_cache (Boolean) (defaults to: true)

    Use cache .yardoc

  • log_level (Symbol, nil) (defaults to: nil)

    Set YARD log level

  • allow_empty_doc (Boolean) (defaults to: false)

    Generate RBS also from empty doc



14
15
16
17
18
19
20
# File 'lib/orthoses/yard.rb', line 14

def initialize(loader, parse:, use_cache: true, log_level: nil, allow_empty_doc: false)
  @loader = loader
  @parse = Array(parse)
  @use_cache = use_cache
  @log_level = log_level
  @allow_empty_doc = allow_empty_doc
end

Instance Method Details

#callvoid

This method returns an undefined value.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/orthoses/yard.rb', line 23

def call
  @loader.call.tap do |store|
    require 'yard'

    log.level = @log_level if @log_level

    ::YARD::Registry.load if @use_cache
    ::YARD.parse(@parse)
    ::YARD::Registry.save(true) if @use_cache
    ::YARD::Registry.root.children.each do |yardoc|
      # Skip anonymous yardoc
      next unless yardoc.file

      # Skip external doc (e.g. pry-doc)
      next unless @parse.any? { |pattern| File.fnmatch(pattern, yardoc.file, File::FNM_EXTGLOB | File::FNM_PATHNAME) }

      case yardoc.type
      when :class, :module
        YARD2RBS.run(yardoc: yardoc) do |namespace, docstring, rbs, skippable|
          next if skippable && !@allow_empty_doc
          comment = docstring.empty? ? '' : "# #{docstring.gsub("\n", "\n# ")}"
          if rbs.nil? && comment && !store.has_key?(namespace)
            store[namespace].comment = comment
          else
            Orthoses.logger.debug("#{namespace} << #{rbs}")
            store[namespace] << "#{comment.chomp}\n#{rbs}"
          end
        end
      end
    end
  end
end