forked from omarkhan/coffeedoc
/
coffeedoc.js
202 lines (198 loc) · 6.25 KB
/
coffeedoc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
(function() {
/*
Documentation functions
=======================
These functions extract relevant documentation info from AST nodes as returned
by the coffeescript parser.
*/ var coffeescript, documentClass, documentFunction, getFullName, removeLeadingWhitespace;
coffeescript = require('coffee-script');
exports.documentModule = function(script, parser) {
/*
Given a module's source code and an AST parser, returns module information
in the form:
{
"docstring": "Module docstring",
"classes": [class1, class1...],
"functions": [func1, func2...]
}
AST parsers are defined in the `parsers.coffee` module
*/ var c, doc, docstring, f, first_obj, nodes;
nodes = parser.getNodes(coffeescript.nodes(script));
first_obj = nodes[0];
if ((first_obj != null ? first_obj.type : void 0) === 'Comment') {
docstring = removeLeadingWhitespace(first_obj.comment);
} else {
docstring = null;
}
doc = {
docstring: docstring,
deps: parser.getDependencies(nodes),
classes: (function() {
var _i, _len, _ref, _results;
_ref = parser.getClasses(nodes);
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
c = _ref[_i];
_results.push(documentClass(c));
}
return _results;
})(),
functions: (function() {
var _i, _len, _ref, _results;
_ref = parser.getFunctions(nodes);
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
f = _ref[_i];
_results.push(documentFunction(f));
}
return _results;
})()
};
return doc;
};
getFullName = function(variable) {
/*
Given a variable node, returns its full name
*/ var name, p;
name = variable.base.value;
if (variable.properties.length > 0) {
name += '.' + ((function() {
var _i, _len, _ref, _results;
_ref = variable.properties;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
p = _ref[_i];
_results.push(p.name.value);
}
return _results;
})()).join('.');
}
return name;
};
documentClass = function(cls) {
/*
Evaluates a class object as returned by the coffeescript parser, returning
an object of the form:
{
"name": "MyClass",
"docstring": "First comment following the class definition"
"parent": "MySuperClass",
"methods": [method1, method2...]
}
*/ var doc, docstring, emptyclass, first_obj, m, methods, n, parent, _ref, _ref2;
if (cls.type === 'Assign') {
cls = cls.value;
}
emptyclass = !(((_ref = cls.body.expressions[0]) != null ? _ref.base : void 0) != null);
first_obj = emptyclass ? cls.body.expressions[0] : (_ref2 = cls.body.expressions[0].base) != null ? _ref2.objects[0] : void 0;
if ((first_obj != null ? first_obj.type : void 0) === 'Comment') {
docstring = removeLeadingWhitespace(first_obj.comment);
} else {
docstring = null;
}
methods = emptyclass ? [] : (function() {
var _i, _len, _ref3, _results;
_ref3 = cls.body.expressions[0].base.objects;
_results = [];
for (_i = 0, _len = _ref3.length; _i < _len; _i++) {
n = _ref3[_i];
if (n.type === 'Assign' && n.value.type === 'Code') {
_results.push(n);
}
}
return _results;
})();
if (cls.parent != null) {
parent = getFullName(cls.parent);
} else {
parent = null;
}
doc = {
name: getFullName(cls.variable),
docstring: docstring,
parent: parent,
methods: (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = methods.length; _i < _len; _i++) {
m = methods[_i];
_results.push(documentFunction(m));
}
return _results;
})()
};
return doc;
};
documentFunction = function(func) {
/*
Evaluates a function object as returned by the coffeescript parser,
returning an object of the form:
{
"name": "myFunc",
"docstring": "First comment following the function definition",
"params": ["param1", "param2"...]
}
*/ var doc, docstring, first_obj, p, params;
first_obj = func.value.body.expressions[0];
if (first_obj != null ? first_obj.comment : void 0) {
docstring = removeLeadingWhitespace(first_obj.comment);
} else {
docstring = null;
}
if (func.value.params) {
params = (function() {
var _i, _len, _ref, _results;
_ref = func.value.params;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
p = _ref[_i];
_results.push(p.splat ? p.name.value + '...' : p.name.value);
}
return _results;
})();
} else {
params = [];
}
return doc = {
name: getFullName(func.variable),
docstring: docstring,
params: params
};
};
removeLeadingWhitespace = function(str) {
/*
Given a string, returns it with leading whitespace removed but with
indentation preserved
*/ var indentation, leading_whitespace, line, lines;
lines = str.split('\n');
while (/^ *$/.test(lines[0])) {
lines.shift();
}
if (lines.length === 0) {
return null;
}
indentation = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = lines.length; _i < _len; _i++) {
line = lines[_i];
if (/^ *$/.test(line)) {
continue;
}
_results.push(line.match(/^ */)[0].length);
}
return _results;
})();
indentation = Math.min.apply(Math, indentation);
leading_whitespace = new RegExp("^ {" + indentation + "}");
return ((function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = lines.length; _i < _len; _i++) {
line = lines[_i];
_results.push(line.replace(leading_whitespace, ''));
}
return _results;
})()).join('\n');
};
}).call(this);