Class: JsDuck::Ast

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

Overview

Analyzes the AST produced by EsprimaParser.

Instance Method Summary collapse

Constructor Details

#initialize(docs = [], options = {}) ⇒ Ast

Should be initialized with EsprimaParser#parse result.



11
12
13
14
15
16
# File 'lib/jsduck/ast.rb', line 11

def initialize(docs = [], options = {})
  @serializer = JsDuck::Serializer.new
  @evaluator = JsDuck::Evaluator.new
  @ext_patterns = JsDuck::ExtPatterns.new(options[:ext_namespaces] || ["Ext"])
  @docs = docs
end

Instance Method Details

#detect(ast) ⇒ 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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/jsduck/ast.rb', line 51

def detect(ast)
  ast = ast || {}

  exp = expression?(ast) ? ast["expression"] : nil
  var = var?(ast) ? ast["declarations"][0] : nil

  # Ext.define("Class", {})
  if exp && ext_define?(exp)
    make_class(to_value(exp["arguments"][0]), exp)

  # Ext.override(Class, {})
  elsif exp && ext_override?(exp)
    make_class("", exp)

  # foo = Ext.extend(Parent, {})
  elsif exp && assignment?(exp) && ext_extend?(exp["right"])
    make_class(to_s(exp["left"]), exp["right"])

  # Foo = ...
  elsif exp && assignment?(exp) && class_name?(to_s(exp["left"]))
    make_class(to_s(exp["left"]), exp["right"])

  # var foo = Ext.extend(Parent, {})
  elsif var && var["init"] && ext_extend?(var["init"])
    make_class(to_s(var["id"]), var["init"])

  # var Foo = ...
  elsif var && class_name?(to_s(var["id"]))
    make_class(to_s(var["id"]), var["right"])

  # function Foo() {}
  elsif function?(ast) && ast["id"] && class_name?(to_s(ast["id"]))
    make_class(to_s(ast["id"]))

  # { ... }
  elsif object?(ast)
    make_class("", ast)

  # function foo() {}
  elsif function?(ast)
    make_method(ast["id"] ? to_s(ast["id"]) : "", ast)

  # foo = function() {}
  elsif exp && assignment?(exp) && function?(exp["right"])
    make_method(to_s(exp["left"]), exp["right"])

  # var foo = function() {}
  elsif var && var["init"] && function?(var["init"])
    make_method(to_s(var["id"]), var["init"])

  # (function() {})
  elsif exp && function?(exp)
    make_method(exp["id"] ? to_s(exp["id"]) : "", exp)

  # foo: function() {}
  elsif property?(ast) && function?(ast["value"])
    make_method(key_value(ast["key"]), ast["value"])

  # this.fireEvent("foo", ...)
  elsif exp && fire_event?(exp)
    make_event(to_value(exp["arguments"][0]))

  # foo = ...
  elsif exp && assignment?(exp)
    make_property(to_s(exp["left"]), exp["right"])

  # var foo = ...
  elsif var
    make_property(to_s(var["id"]), var["init"])

  # foo: ...
  elsif property?(ast)
    make_property(key_value(ast["key"]), ast["value"])

  # foo;
  elsif exp && ident?(exp)
    make_property(to_s(exp))

  # "foo"  (inside some expression)
  elsif string?(ast)
    make_property(to_value(ast))

  # "foo";  (as a statement of it's own)
  elsif exp && string?(exp)
    make_property(to_value(exp))

  else
    make_property()
  end
end

#detect_all!Object

Performs the detection of code in all docsets.

destructively by modifying the passed-in docsets.)



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

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