-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
163 lines (134 loc) · 4.13 KB
/
index.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
var acorn = require('acorn');
require('acorn-es7-plugin')(acorn);
var fs = require('fs');
var walk = require('acorn/dist/walk');
var tern = require('tern');
var infer = require('tern/lib/infer');
var parser = {options: {}};
function makePromise(argument) {
var child = new acorn.Node(parser);
child.type = 'CallExpression';
child.callee = new acorn.Node(parser);
child.callee.type = 'MemberExpression';
child.callee.computed = false;
child.callee.object = new acorn.Node(parser);
child.callee.object.type = 'Identifier';
child.callee.object.name = 'Promise';
child.callee.property = new acorn.Node(parser);
child.callee.property.type = 'Identifier';
child.callee.property.name = 'resolve';
if(argument) {
child.arguments = [argument];
} else {
child.arguments = [];
}
return child;
}
function addFakeReturn(node) {
var block = node.body;
if(block == null || block.type != 'BlockStatement') {
// TODO: can this happen?
return;
}
var fakeReturn = new acorn.Node(parser);
fakeReturn.type = 'ReturnStatement';
fakeReturn.argument = makePromise();
block.body.push(fakeReturn);
}
var transformVisitor = {
FunctionDeclaration: function(node, state, ancestors) {
if(node.async) {
addFakeReturn(node);
}
},
ArrowFunctionExpression: function(node, state, ancestors) {
if(node.async) {
if(node.expression) {
// Implicit return
node.body = makePromise(node.body);
} else {
// Explicit return. Add a return statement in case there is none.
addFakeReturn(node);
}
}
},
ReturnStatement: function(node, state, ancestors) {
var isAsync = false;
// TODO: is this the correct way to get the current function?
for(var i = ancestors.length - 1; i >= 0; i--) {
var ancestor = ancestors[i];
if(ancestor.type == 'FunctionDeclaration' || ancestor.type == 'ArrowFunctionExpression') {
isAsync = ancestor.async;
break;
}
}
if(!isAsync) {
return;
}
// Transform `return <expression>` into `return Promise.resolve(expression)`
node.argument = makePromise(node.argument);
},
AwaitExpression: function(node, state, ancestors) {
var expression = node.argument;
// Replace the current node with a call
// `await <expression>` => `__Promise_value(<expression>)`
node.type = 'CallExpression';
delete node.argument;
node.await = true;
node.callee = new acorn.Node(parser);
node.callee.type = 'Identifier';
node.callee.name = '__Promise_value';
node.callee.start = node.start;
node.callee.end = node.end;
node.arguments = [expression];
}
};
function AwaitExpression(node, st, c) {
c(node.argument, st, 'Expression');
}
// Since other walks may be performed before we had a chance to replace the
// AwaitExpression, we need to add this on the base one.
if(walk.base.AwaitExpression == null) {
walk.base.AwaitExpression = AwaitExpression;
}
var baseVisitor = Object.create(walk.base);
baseVisitor.AwaitExpression = AwaitExpression;
var asyncAwaitDefs = {
'!name': 'AsyncAwait',
'__Promise_value': {
"!type": "fn(value: ?) -> !custom:Promise_value"
}
};
tern.registerPlugin("asyncawait", function(server, options) {
server.on("preParse", function(text, options) {
// Set acorn parser options
if(!options.plugins) {
options.plugins = {};
}
options.plugins.asyncawait = true;
options.ecmaVersion = 7;
});
server.on('postParse', function(ast, text) {
// Transform the AST
walk.ancestor(ast, transformVisitor, baseVisitor, {});
});
server.addDefs(asyncAwaitDefs);
var PromiseResolvesTo = infer.constraint({
construct: function(output) { this.output = output; },
addType: function(tp) {
if (tp.constructor == infer.Obj && tp.name == "Promise") {
tp.getProp(":t").propagate(this.output);
} else {
tp.propagate(this.output);
}
}
});
infer.registerFunction("Promise_value", function(_self, args, argNodes) {
var self = new infer.AVal;
if (args.length) {
var aval = args[0];
aval.propagate(new PromiseResolvesTo(self));
}
return self;
});
});