class JsDuck::Process::ReturnValues

Auto-detector return values and @chainable tags.

Adds @chainable tag when doc-comment contains @return {OwnerClass} this. Also the other way around: when @chainable found, adds appropriate @return.

Public Class Methods

new(relations) click to toggle source
# File lib/jsduck/process/return_values.rb, line 10
def initialize(relations)
  @relations = relations
  @cls = nil
end

Public Instance Methods

process_all!() click to toggle source
# File lib/jsduck/process/return_values.rb, line 15
def process_all!
  @relations.each do |cls|
    @cls = cls
    cls.find_members(:tagname => :method, :local => true, :static => false).each do |m|
      process(m)
    end
  end
end

Private Instance Methods

add_chainable(m) click to toggle source
# File lib/jsduck/process/return_values.rb, line 48
def add_chainable(m)
  m[:chainable] = true
end
add_return_new(m) click to toggle source
# File lib/jsduck/process/return_values.rb, line 58
def add_return_new(m)
  if m[:return] == nil || m[:return][:type] == "Object"
    # Create a whole new :return hash.
    # If we were to just change the :type field it would modify
    # the type of all the inherited constructor docs.
    m[:return] = {
      :type => @cls[:name],
      :doc => m[:return] ? m[:return][:doc] : "",
    }
  end
end
add_return_this(m) click to toggle source
# File lib/jsduck/process/return_values.rb, line 52
def add_return_this(m)
  if m[:return] == nil
    m[:return] = {:type => @cls[:name], :doc => "this"}
  end
end
chainable?(m) click to toggle source
# File lib/jsduck/process/return_values.rb, line 40
def chainable?(m)
  m[:chainable]
end
constructor?(m) click to toggle source
# File lib/jsduck/process/return_values.rb, line 36
def constructor?(m)
  m[:name] == "constructor"
end
process(m) click to toggle source
# File lib/jsduck/process/return_values.rb, line 26
def process(m)
  if constructor?(m)
    add_return_new(m)
  elsif chainable?(m)
    add_return_this(m)
  elsif returns_this?(m)
    add_chainable(m)
  end
end
returns_this?(m) click to toggle source
# File lib/jsduck/process/return_values.rb, line 44
def returns_this?(m)
  m[:return] && m[:return][:type] == @cls[:name] && m[:return][:doc] =~ /\Athis\b/
end