Class: JsDuck::Js::Returns

Inherits:
Object
  • Object
show all
Includes:
Util::Singleton
Defined in:
lib/jsduck/js/returns.rb

Overview

Analyzes the AST of a Function for possible return values.

Instance Method Summary collapse

Methods included from Util::Singleton

included

Instance Method Details

#chainable?(ast) ⇒ Boolean

True when function always finishes with returning this.

Returns:

  • (Boolean)


11
12
13
# File 'lib/jsduck/js/returns.rb', line 11

def chainable?(ast)
  detect(ast) == [:this]
end

#detect(ast) ⇒ Object

Detects possible return types of the given function.

For now there are three possible detected return values:

  • :this - the code contins ‘return this;’

  • “undefined” - the code finishes by returning undefined or without explicitly returning anything

  • :other - some other value is returned.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jsduck/js/returns.rb', line 26

def detect(ast)
  h = return_types_hash(ast["body"]["body"])

  # Replace the special :void value that signifies possibility of
  # exiting without explicitly returning anything
  if h[:void]
    h["undefined"] = true
    h.delete(:void)
  end

  h.keys
end