-
Notifications
You must be signed in to change notification settings - Fork 8
/
docscript.js
338 lines (297 loc) · 9.73 KB
/
docscript.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
const acorn = require("acorn");
const astring = require('astring');
const walk = require("acorn/dist/walk");
const falafel = require('falafel');
var tt = acorn.tokTypes;
const babel = require("babel-core");
const es7 = require('acorn-es7')(acorn);
acorn.plugins.docscript = function(parser) {
parser.extend("readToken", function(nextMethod) {
return function(code) {
// console.log(`Reading a token! ${code}`)
return nextMethod.call(this, code)
}
})
parser.extend("parseExpressionStatement", function(nextMethod) {
return function(node, expr) {
// console.log(this.type);
// console.log("hi");
if (expr.type == 'Identifier') {
// console.log(node);
if (this.type == tt.braceL) {
let func = this.startNode();
func.docscript = true;
func.body = this.parseBlock();
func.params = [];
func.generator = false;
func.expression = false;
let arg = this.startNode();
arg.start = this.start - 1;
arg.properties = [];
node.callee = expr;
node.arguments = [
this.finishNode(arg, "ObjectExpression"),
this.finishNode(func, "ArrowFunctionExpression")
];
this.semicolon();
return this.finishNode(node, "CallExpression");
}
} else if (expr.type == "CallExpression") {
// console.log(expr);
if (this.type == tt.braceL) {
let func = this.startNode();
func.docscript = true;
func.body = this.parseBlock();
func.params = [];
func.generator = false;
func.expression = false;
expr.arguments.push(this.finishNode(func, "ArrowFunctionExpression"));
this.semicolon();
return this.finishNode(expr, "CallExpression");
}
}
return nextMethod.call(this, node, expr);
}
});
parser.extend("parseClassSuper", function(nextMethod) {
return function(node) {
// console.log("hello world");
// console.log(node);
// node.superClass = this.eat(tt._extends) ? (this.parseExprSubscripts()) : null
if (this.eat(tt._extends)) {
this.inExtends = true;
node.superClass = this.parseExprSubscripts();
this.inExtends = false;
}
}
});
// enables var a = b {} to be parsed
parser.extend("parseSubscripts", function(nextMethod) {
return function(base, startPos, startLoc, noCalls) {
// handles a {};
// console.log("hi");
// console.log(this);
// console.log(noCalls);
// If we are inside a subscript of a class declaration in extends
// do not enable the syntax simplification.
if (this.inExtends) {
return nextMethod.call(this, base, startPos, startLoc, noCalls);
}
if (!noCalls && this.type == tt.braceL) {
let func = this.startNode();
func.docscript = true;
let oldInFunction = this.inFunction;
this.inFunction = true;
func.body = this.parseBlock();
this.inFunction = oldInFunction;
func.params = [];
func.generator = false;
func.expression = false;
// let arg = this.startNode();
// arg.properties = [];
let node = this.startNodeAt(startPos, startLoc)
node.callee = base;
node.arguments = [
// this.finishNode(arg, "ObjectExpression"),
this.finishNode(func, "ArrowFunctionExpression")
];
return this.finishNode(node, "CallExpression")
}
let expr = nextMethod.call(
this, base, startPos, startLoc, noCalls);
// If we are in a CallExpression which is followed by a {
// then eat that and move that into the arguments of the call.
if (expr.type == "CallExpression" && this.type == tt.braceL) {
// Makes sure that the first argument of the call is an
// ObjectExpression.
// if (expr.arguments.length != 1) {
// this.raise(this.start, "More than 1 argument");
// } else if (expr.arguments[0].type != "ObjectExpression") {
// this.raise(this.start, "First argument isn't an object!!");
// }
let func = this.startNode();
func.docscript = true;
let oldInFunction = this.inFunction;
this.inFunction = true;
func.body = this.parseBlock();
this.inFunction = oldInFunction;
func.params = [];
func.generator = false;
func.expression = false;
// func.parent = undefined;
// Adds the function body as an argument
expr.arguments.push(this.finishNode(func, "ArrowFunctionExpression"));
// And fixes the start and end indexes to include the function
// block.
expr.end = func.end;
// console.log(expr);
// debugger;
}
return expr;
}
});
}
// TODO(goto): figure out how to extent to enable a() {};
// let ast = acorn.parse(docscripts[0], {
// let ast = acorn.parse("var a = b(function() {});", {
// let ast = acorn.parse("var a = foo(function() {});", {
// let ast = acorn.parse("var a = b {};", {
// let ast = acorn.parse("b { c {} };", {
// TODO: let ast = acorn.parse(`b { c { d(); } };`, {
// let ast = acorn.parse(`d("hi");`, {
//let ast = acorn.parse(`d {};`, {
// plugins: {docscript: true}
//});
//console.log(JSON.stringify(ast, undefined, " "));
// console.log(generate(ast));
// return;
function visitor(node) {
//function inside(node, pred) {
// let inside = false;
// let parent = node.parent;
// while (parent) {
// if (pred(parent)) {
// inside = true;
// break;
//return true;
//}
// parent = parent.parent;
//};
//return false;
//}
//if (node.type == "Identifier") {
// if (inside(node, n => n.docscript) &&
// node.parent.type == "CallExpression" &&
// node.parent.callee == node) {
//console.log(node);
// node.update(`("${node.name}" in this ? this.${node.name}.bind(this) : ${node.name}.bind(this))`);
// node.update(`${node.name}.call(this)`);
// }
//} else
if (node.type === "CallExpression") {
// return;
// console.log("hi");
// let method = node.callee.type == "MemberExpression";
// console.log(node.callee);
if (node.callee.type == "MemberExpression") {
// node.callee.type == "NewExpression") {
// do not interfere with method calls or new.
// console.log("hi");
// throw new Error("hi");
return;
}
// let expand = inside(node, n => n.docscript);
// if (node.arguments.length > 0 &&
// node.arguments[node.arguments.length - 1].docscript) {
let params = ``;
let docscript = false;
for (let i = 0; i < node.arguments.length; i++) {
// console.log(i);
let param = node.arguments[i];
if (!param.docscript) {
params += `${param.source()}`;
} else {
docscript = true;
// TODO(goto): fill {} in with the parameters from do ()
// console.log("hi");
//console.log(param.source());
// params += `(__args__) => { let {} = __args__; ${param.source() } }`;
params += `function() ${param.source() }`;
}
if (i < (node.arguments.length - 1)) {
params += ", ";
}
}
// let expansion = expand ? (".call(this" + (node.arguments.length > 0 ? ", " : "")) : "(";
//console.log(node.callee.source());
// console.log("hi");
if (docscript) {
if (node.callee.type == "NewExpression") {
node.update(`${node.callee.source()}(${params})`);
} else if (node.callee.type == "MemberExpression") {
} else {
node.update(`this.${node.callee.source()}(${params})`);
}
} else {
node.update(`${node.callee.source()}(${params})`);
}
} else if (node.type == "ExpressionStatement") {
// console.log();
//if (inside(node, n => n.docscript) &&
// node.expression.type == "CallExpression") {
// // console.log(node);
// node.update(`${node.source()};`);
//}
} else if (node.type == "ClassDeclaration" &&
node.decorators &&
// TODO(goto): deal with multiple decorators.
node.decorators.length == 1) {
let name = node.id.source();
let decorator = node.decorators[0].expression.source();
node.update(`
let ${name} = (function() {
class ${name} ${node.body.source()}
${name} = ${decorator}(${name}) || ${name};
return ${name};
})();`);
}
}
class DocScript {
static compile(code) {
// NOTE(goto): major hack ahead, parses using acorn, generates
// text source code again, then uses falafel to parse (again!)
// from the source code generated from the ast.
// There is a bug in falafel where it takes the code as input rather
// than taking it from the resulting ast constructed by the parser
// to generate the result.
// This has certainly performance implications (as you are parsing twice)
// but may have semantic implications too. This ought to be fixed.
// let ast = acorn.parse(code, {
// plugins: {docscript: true}
// });
// let generated = astring.generate(ast);
// Uses the babel transform to transform @decorators
// let decorated = babel.transform(code, {
// plugins: ["transform-decorators-legacy"]
// }).code;
var result = falafel(code, {
parser: acorn,
plugins: { docscript: true, es7: true },
ecmaVersion: 7,
sourceType: "module"
}, visitor);
return result;
}
static eval(code, opt_stdout) {
let result = DocScript.compile(code);
let stdout = `
var console = {
log: function(str) {
if (opt_stdout) {
opt_stdout.push(str);
}
}
};
`;
// console.log(`${docscript} ${result}`);
return eval(`${DocScript.api()}; ${stdout}; ${result}`);
}
}
// let result = DocScript.compile("let a = 1; @foo class A {} hello {}");
// console.log(result);
//console.log(JSON.stringify(acorn.parse(`d() { 1 };`, {
// plugins: {docscript: true}
//}), undefined, " "));
// return;
// let code = `d {};`;
// let ast = acorn.parse(code, {
// plugins: {docscript: true}
// });
// console.log(JSON.stringify(ast, undefined, " "));
// var result = falafel(code, {
// parser: acorn, plugins: { docscript: true }
// }, visitor);
// console.log(result);
module.exports = {
DocScript: DocScript
};