-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathjsdocJson2ternjsJson.js
229 lines (210 loc) · 5.95 KB
/
jsdocJson2ternjsJson.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
var fs = require('fs');
var _ = require('underscore');
var jsdoc = eval(fs.readFileSync(process.argv[2], 'utf8'));
var O = {
"!name": "meteor",
Match: {
"Any": "?",
"String": "?",
"Number": "?",
"Boolean": "?",
"undefined": "?",
"null": "?",
"Integer": "?",
"ObjectIncluding": "?",
"Object": "?",
"Optional": "fn(pattern: string)",
"OneOf": "fn()",
"Where": "fn(condition: bool)"
},
MeteorSubscribeHandle: {
'stop': 'fn()',
'ready': 'fn() -> bool'
}
};
var specialFunctionReturns = {
'Meteor.subscribe': 'MeteorSubscribeHandle',
'Tracker.autorun': '+Tracker.Computation',
'Blaze.TemplateInstance#autorun': '+Tracker.Computation'
};
var commonSkips = ['longname', 'kind', 'name', 'scope', 'memberof', 'options', 'instancename'];
var nameToId = eval(fs.readFileSync('/Users/imslavko/work/meteor/docs/client/full-api/nameToId.js', 'utf8'));
var getDocsUrl = function (name) {
function h () {
var special = {
Meteor: 'core',
Match: 'check',
Mongo: 'mongo_collections',
Session: 'session',
Accounts: 'accounts_api',
Template: 'templates_api',
Blaze: 'blaze',
Tracker: 'tracker',
EJSON: 'ejson',
ReactiveVar: 'reactivevar',
HTTP: 'http',
Email: 'email',
Assets: 'assets',
Package: 'packagejs',
App: 'mobileconfigjs'
};
return special[name] || nameToId[name] || name.replace(/[.#]/g, "-");
}
return 'https://docs.meteor.com/#/full/' + h();
};
var set = function (def, o) {
var path = def.longname;
path = path.replace('#', '.prototype.');
path = path.split('.')
var t = O;
_.each(path.slice(0, path.length - 1), function (prop) {
if (! t[prop])
t[prop] = {};
t = t[prop];
});
var lastProp = path[path.length - 1];
t[lastProp] = _.extend({}, t[lastProp], o, {
'!url': getDocsUrl(def.longname)
});
};
var attach = {};
attach.namespace = function (namespace) {
var o = {};
// try to discover declarations first
_.each(namespace, function (def, symbol) {
if (def.kind === 'typedef' || def.kind === 'class')
attach[def.kind](def);
});
_.each(namespace, function (def, symbol) {
if (dealWithMisc(def, symbol, o)) return;
if (def.kind === 'typedef' || def.kind === 'class') return;
attach[def.kind](def);
});
if (namespace !== jsdoc)
set(namespace, o);
};
var typedefs = {};
attach.typedef = function (td) {
typedefs[td.longname] = td;
};
attach.member = function (member) {
var o = {};
_.each(member, function (def, symbol) {
if (symbol === 'type') {
o['!type'] = processParamType(def.names[0]); // XXX hardcoding first acceptable type
if (! o['!type'])
delete o['!type'];
return;
}
if (dealWithMisc(def, symbol, o)) return;
});
set(member, o);
};
var classes = {};
attach['class'] = attach['function'] = function (fun) {
// set all classes to a table
if (fun.kind === 'class')
classes[fun.name] = fun;
var o = {};
var params = [];
var returns = null;
_.each(fun, function (def, symbol) {
if (symbol === 'params') {
params = _.map(def, function (param) {
var name = processParamName(param);
var type = null;
if (! name.match(/\.\.\.\?$/) && name !== 'thisArg?')
type = processParamType(param.type.names[0]); // XXX hardcoding first acceptable type
else
name = name.substr(0, name.length - 1);
return name + (type ? ': ' + type : '');
});
return;
}
if (symbol === 'returns') {
returns = processParamType(def[0].type.names[0]);
return;
}
if (dealWithMisc(def, symbol, o)) return;
});
// XXX no return type
o['!type'] = 'fn(' + params.join(', ') + ')';
if (_.has(specialFunctionReturns, fun.longname))
o['!type'] += ' -> ' + specialFunctionReturns[fun.longname];
else if (returns)
o['!type'] += ' -> ' + returns;
set(fun, o);
};
function processParamName (param) {
var r = '';
if (! param.name)
throw new Error('param w/o a name');
r = param.name;
if (param.optional)
r += '?';
return r;
};
function embedTypeDef (td) {
if (td.type.names[0] !== 'function')
throw new Error('This script doesnt know how to embed non-callback typedefs yet');
// XXX doesn't put the return value
return 'fn(' + _.map(td.params, function (p) {
return processParamType(p.type.names[0])
}).join(', ') + ')';
}
function processParamType (type) {
if (! type || type === 'Any')
return null;
var rgx = /^Array\.<(.*)>$/;
var m = type.match(rgx);
var isArray = false;
if (m) {
type = m[1];
isArray = true;
}
if (_.contains(['String', 'Number'], type))
type = type.toLowerCase();
else if (type === 'Boolean')
type = 'bool';
else if (type === 'Object')
type = '?';
else if (type === 'function')
type = 'fn()';
// XXX do something with this?
else if (_.contains(['Error', 'Buffer', 'Tracker.Computation', 'EJSON', 'EJSONable', 'JSONCompatible', 'MongoSelector', 'MongoModifier', 'Template', 'DOMNode', 'DOMElement', 'Blaze.View', 'EventMap', 'MatchPattern', 'Mongo.Collection', 'Mongo.Cursor', 'SubscriptionHandle', 'Blaze.TemplateInstance'], type))
type = "+" + type;
else if (type === 'Integer')
type = 'number';
else if (classes[type])
type = "+" + type;
else if (typedefs[type])
type = embedTypeDef(typedefs[type]);
else
throw new Error('Unknown type: ' + type);
if (isArray)
type = '[' + type + ']';
return type;
}
function dealWithMisc (def, symbol, o) {
if (symbol === 'summary') {
o['!doc'] = def;
return true;
}
if (symbol === 'locus') {
o['!data'] = {
'!locus': def
};
o['!doc'] = def + '\n' + (o['!doc'] || '');
return true;
}
if (symbol === 'filepath' || symbol === 'lineno')
return true;
if (_.contains(commonSkips, symbol))
return true;
if (! def.kind) {
def.kind = 'namespace';
def.longname = symbol;
}
}
attach.namespace(jsdoc);
console.log(JSON.stringify(O, null, 2))