forked from espruino/Espruino
-
Notifications
You must be signed in to change notification settings - Fork 7
/
build_tern_json.js
executable file
·118 lines (106 loc) · 3.8 KB
/
build_tern_json.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
#!/usr/bin/env node
// sudo npm install -g marked highlight.js
// This build a JSON description file for Tern.js as
// specified here: http://ternjs.net/doc/manual.html#typedef
var marked = require('marked');
// yay - new marked version incompatible with old one...
if (marked.marked) marked=marked.marked;
// Set default options except highlight which has no default
marked.setOptions({
gfm: true, // github markdown
highlight: function (code) {
return require('highlight.js').highlightAuto(code).value;
},
tables: true,
breaks: false,
pedantic: false,
sanitize: false,
smartLists: true,
smartypants: false,
langPrefix: 'lang-'
});
var hadErrors = false;
require("./common.js").readAllWrapperFiles(function(json) {
var tern = { "!name": "Espruino" };
// Handle classes/libraries first
json.forEach(function (j) {
try {
if (j.type=="class") {
var o = { "!type": "fn()" };
o["!doc"] = marked(j.getDescription());
o["!url"] = j.getURL();
tern[j.class] = o;
if ("prototype" in j) {
o["prototype"] = { "!proto": j.prototype+".prototype" };
}
} else if (j.type=="object") {
var o = { "!type": ("instanceof" in j) ? ("+"+j.instanceof) : "?" };
o["!doc"] = marked(j.getDescription());
o["!url"] = j.getURL();
tern[j.name] = o;
} else if (j.type=="library") {
// TODO: bind this into 'require' somehow
var o = { "!type": "fn()" };
o["!doc"] = marked(j.getDescription());
o["!url"] = j.getURL();
tern[j.class] = o;
}
} catch (e) {
console.error("Exception "+e, j);
hadErrors = true;
}
});
// Handle contents
json.forEach(function (j) {
try {
if (["include"].indexOf(j.type)>=0) {
// meh
} else if (["class","object","library"].indexOf(j.type)>=0) {
// aready handled above
} else if (["init","idle","kill","hwinit"].indexOf(j.type)>=0) {
// internal
} else if (["event"].indexOf(j.type)>=0) {
// TODO: handle events
} else if (["constructor"].indexOf(j.type)>=0) {
var o = tern[j.class]; // overwrite existing class with constructor
if (o===undefined) o=tern[j.class]={};
o["!type"] = j.getTernType();
o["!doc"] = marked(j.getDescription());
o["!url"] = j.getURL();
} else if (["staticproperty","staticmethod"].indexOf(j.type)>=0) {
var o = { "!type": j.getTernType() };
o["!doc"] = marked(j.getDescription());
o["!url"] = j.getURL();
tern[j.class][j.name] = o;
} else if (["property","method"].indexOf(j.type)>=0) {
var o = { "!type": j.getTernType() };
o["!doc"] = marked(j.getDescription());
o["!url"] = j.getURL();
if (!("prototype" in tern[j.class])) {
tern[j.class]["prototype"] = { };
if (["Object","Function","Array","String","Number","Boolean","RegExp"].indexOf(j.class)>=0)
tern[j.class]["prototype"]["!stdProto"] = j.class;
}
tern[j.class]["prototype"][j.name] = o;
} else if (["function","variable"].indexOf(j.type)>=0) {
var o = { "!type": j.getTernType() };
o["!doc"] = marked(j.getDescription());
o["!url"] = j.getURL();
tern[j.name] = o;
} else if (j.type.startsWith("EV_SERIAL")) {
// internal - no type info needed
} else
console.warn("Unknown type "+j.type+" for ",j);
} catch (e) {
console.error("Exception "+e, e.stack, j);
hadErrors = true;
}
});
// FIXME: not sure why (because Telnet has children when it shouldn't?) but this breaks tern's parsing :(
delete tern["Telnet"]["!type"];
if (hadErrors) {
console.log("ERROR PROCESSING JSWRAP FILES");
process.exit(1);
}
console.log(JSON.stringify(tern,null,2));
});