Class: JsDuck::Js::Ast

Inherits:
Object
  • Object
show all
Defined in:
lib/jsduck/js/ast.rb

Overview

Analyzes the AST produced by EsprimaParser.

Instance Method Summary collapse

Constructor Details

#initialize(docs = []) ⇒ Ast

Should be initialized with EsprimaParser#parse result.



13
14
15
# File 'lib/jsduck/js/ast.rb', line 13

def initialize(docs = [])
  @docs = docs
end

Instance Method Details

#detect(node) ⇒ Object

Given Esprima-produced syntax tree, detects documentation data.

This method is exposed for testing purposes only, JSDuck itself only calls the above #detect_all method.

other properties relative to the tag. Like so:

{ :tagname => :method, :name => "foo", ... }

Parameters:

  • ast

    :code from Result of EsprimaParser



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jsduck/js/ast.rb', line 50

def detect(node)
  ast = Js::Node.create(node)

  if doc = Js::Class.detect(ast, @docs)
    doc
  elsif doc = Js::Method.detect(ast)
    doc
  elsif doc = Js::Event.detect(ast)
    doc
  elsif doc = Js::Property.detect(ast)
    doc
  else
    Js::Property.make()
  end
end

#detect_all!Object

Performs the detection of code in all docsets.

destructively by modifying the passed-in docsets.)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jsduck/js/ast.rb', line 22

def detect_all!
  # First deal only with doc-comments
  doc_comments = @docs.find_all {|d| d[:type] == :doc_comment }

  # Detect code in each docset.  Sometimes a docset has already
  # been detected as part of detecting some previous docset (like
  # Class detecting all of its configs) - in such case, skip.
  doc_comments.each do |docset|
    code = docset[:code]
    docset[:code] = detect(code) unless code && code[:tagname]
  end

  # Return all doc-comments + other comments for which related
  # code was detected.
  @docs.find_all {|d| d[:type] == :doc_comment || d[:code] && d[:code][:tagname] }
end