-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
tests.transform.js
233 lines (190 loc) · 7.5 KB
/
tests.transform.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
var assert = require("assert");
var recast = require("recast");
var types = recast.types;
var n = types.namedTypes;
var transform = require("..").transform;
var compile = require("..").compile;
var UglifyJS = require("uglify-js");
describe("_blockHoist nodes", function() {
it("should be hoisted to the outer body", function() {
var foo;
var names = [];
var ast = recast.parse([
"function *foo(doNotHoistMe, hoistMe) {",
" var sent = yield doNotHoistMe();",
" hoistMe();",
" names.push(sent);",
" return 123;",
"}"
].join("\n"), {
parser: require("babylon")
});
var hoistMeStmt = ast.program.body[0].body.body[1];
n.ExpressionStatement.assert(hoistMeStmt);
n.CallExpression.assert(hoistMeStmt.expression);
n.Identifier.assert(hoistMeStmt.expression.callee);
assert.strictEqual(hoistMeStmt.expression.callee.name, "hoistMe");
hoistMeStmt._blockHoist = 1;
eval(recast.print(transform(ast)).code);
assert.strictEqual(typeof foo, "function");
assert.ok(regeneratorRuntime.isGeneratorFunction(foo));
assert.strictEqual(names.length, 0);
var g = foo(function doNotHoistMe() {
names.push("doNotHoistMe");
return "yielded";
}, function hoistMe() {
names.push("hoistMe");
});
assert.deepEqual(names, ["hoistMe"]);
assert.deepEqual(g.next(), { value: "yielded", done: false });
assert.deepEqual(names, ["hoistMe", "doNotHoistMe"]);
assert.deepEqual(g.next("oyez"), { value: 123, done: true });
assert.deepEqual(names, ["hoistMe", "doNotHoistMe", "oyez"]);
});
});
describe("uglifyjs dead code removal", function() {
function uglifyAndParse(file1, file2) {
var code = {
"file1.js": file1,
"file2.js": file2
};
var options = {
toplevel: true,
// don't mangle function or variable names so we can find them
mangle: false,
output: {
// make it easier to parse the output
beautify: true
}
};
// uglify our code
var result = UglifyJS.minify(code, options);
// parse and return the output
return recast.parse(result.code, {
parser: require("babylon")
});
}
it("works with function expressions", function() {
var file1 = compile([
'var foo = function* () {};',
'var bar = function* () {};'
].join("\n")).code;
var file2 = compile('console.log(foo());').code;
var ast = uglifyAndParse(file1, file2);
// the results should have a single variable declaration
var variableDeclarations = ast.program.body.filter(function(b) {
return b.type === 'VariableDeclaration';
});
assert.strictEqual(variableDeclarations.length, 1);
assert.strictEqual(variableDeclarations[0].declarations.length, 1);
var declaration = variableDeclarations[0].declarations[0];
// named foo
assert.strictEqual(declaration.id.name, 'foo');
});
it("works with function declarations", function() {
var file1 = compile([
'function* foo() {};',
'function* bar() {};'
].join("\n")).code;
var file2 = compile('console.log(foo());').code;
var ast = uglifyAndParse(file1, file2);
// the results should have our foo() function
assert.ok(ast.program.body.some(function(b) {
return b.type === 'FunctionDeclaration' && b.id.name === 'foo';
}));
// but not our bar() function
assert.ok(!ast.program.body.some(function(b) {
return b.type === 'FunctionDeclaration' && b.id.name === 'bar';
}));
// and a single mark declaration
var variableDeclarations = ast.program.body.filter(function(b) {
return b.type === 'VariableDeclaration';
});
assert.strictEqual(variableDeclarations.length, 1);
var declarations = variableDeclarations[0].declarations;
assert.strictEqual(declarations.length, 1);
var declaration = declarations[0];
// with our function name as an argument'
assert.strictEqual(declaration.init.arguments.length, 1);
assert.strictEqual(declaration.init.arguments[0].name, 'foo');
});
})
context("functions", function() {
function marksCorrectly(marked, varName) {
// marked should be a VariableDeclarator
n.VariableDeclarator.assert(marked);
// using our variable name
assert.strictEqual(marked.id.name, varName);
// assiging a call expression to regeneratorRuntime.mark()
n.CallExpression.assert(marked.init);
assert.strictEqual(marked.init.callee.object.name, 'regeneratorRuntime')
assert.strictEqual(marked.init.callee.property.name, 'mark')
// with said call expression marked as a pure function
assert.strictEqual(marked.init.leadingComments[0].value, '#__PURE__');
}
describe("function declarations", function() {
it("should work with a single function", function() {
var ast = recast.parse('function* foo(){};', {
parser: require("babylon")
});
// get our declarations
const declaration = transform(ast).program.body[0];
n.VariableDeclaration.assert(declaration);
const declarations = declaration.declarations;
// verify our declaration is marked correctly
marksCorrectly(declarations[0], '_marked');
// and has our function name as its first argument
assert.strictEqual(declarations[0].init.arguments[0].name, 'foo');
});
it("should work with multiple functions", function() {
var ast = recast.parse([
'function* foo() {};',
'function* bar() {};'
].join("\n"), {
parser: require("babylon")
});
// get our declarations
const declaration = transform(ast).program.body[0];
n.VariableDeclaration.assert(declaration);
const declarations = declaration.declarations;
// verify our declarations are marked correctly and have our function name
// as their first argument
marksCorrectly(declarations[0], '_marked');
n.Identifier.assert(declarations[0].init.arguments[0]);
assert.strictEqual(declarations[0].init.arguments[0].name, 'foo');
marksCorrectly(declarations[1], '_marked2');
n.Identifier.assert(declarations[1].init.arguments[0]);
assert.strictEqual(declarations[1].init.arguments[0].name, 'bar');
});
});
describe("function expressions", function() {
it("should work with a named function", function() {
var ast = recast.parse('var a = function* foo(){};', {
parser: require("babylon")
});
// get our declarations
const declaration = transform(ast).program.body[0];
n.VariableDeclaration.assert(declaration);
const declarator = declaration.declarations[0];
// verify our declaration is marked correctly
marksCorrectly(declarator, 'a');
// and that our first argument is our original function expression
n.FunctionExpression.assert(declarator.init.arguments[0]);
assert.strictEqual(declarator.init.arguments[0].id.name, 'foo');
});
it("should work with an anonymous function", function() {
var ast = recast.parse('var a = function* (){};', {
parser: require("babylon")
});
// get our declarations
const declaration = transform(ast).program.body[0];
n.VariableDeclaration.assert(declaration);
const declarator = declaration.declarations[0];
// verify our declaration is marked correctly
marksCorrectly(declarator, 'a');
// and that our first argument is our original function expression
n.FunctionExpression.assert(declarator.init.arguments[0]);
assert.strictEqual(declarator.init.arguments[0].id.name, '_callee');
});
});
});