Class: JsDuck::Js::Class

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

Overview

Auto-detection of classes.

Instance Method Summary collapse

Methods included from Util::Singleton

included

Instance Method Details

#detect(ast, docs) ⇒ Object

Checks if AST node is a class, and if so, returns doc-hash with clas name and various auto-detected attributes. When not a class returns nil.



16
17
18
19
20
21
22
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
55
56
57
# File 'lib/jsduck/js/class.rb', line 16

def detect(ast, docs)
  @docs = docs

  exp = ast.expression_statement? ? ast["expression"] : nil
  var = ast.variable_declaration? ? ast["declarations"][0] : nil

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

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

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

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

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

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

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

    # { ... }
  elsif ast.object_expression?
    make("", ast)

  else
    nil
  end
end

#make(name, ast = nil) ⇒ Object

Produces a doc-hash for a class.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jsduck/js/class.rb', line 60

def make(name, ast=nil)
  cls = {
    :tagname => :class,
    :name => name,
  }

  # apply information from Ext.extend, Ext.define, or {}
  if ast
    if ast.ext_define?
      detect_ext_define(cls, ast)
    elsif ast.ext_extend?
      detect_ext_something(:extends, cls, ast)
    elsif ast.ext_override?
      detect_ext_something(:override, cls, ast)
    elsif ast.object_expression?
      detect_class_members_from_object(cls, ast)
    elsif ast.array_expression?
      detect_class_members_from_array(cls, ast)
    end
  end

  return cls
end