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
|
var assert = require("assert");
var UglifyJS = require("../node");
describe("generator", function() {
it("Should reject `yield` as symbol name within generator functions only", function() {
[
"function yield() {}",
"function(yield) {}",
"function() { yield:{} }",
"function() { var yield; }",
"function() { function yield() {} }",
"function() { try {} catch (yield) {} }",
].forEach(function(code) {
var ast = UglifyJS.parse("(" + code + ")();");
assert.strictEqual(ast.TYPE, "Toplevel");
assert.strictEqual(ast.body.length, 1);
assert.strictEqual(ast.body[0].TYPE, "SimpleStatement");
assert.strictEqual(ast.body[0].body.TYPE, "Call");
assert.strictEqual(ast.body[0].body.expression.TYPE, "Function");
assert.throws(function() {
UglifyJS.parse("(" + code.replace(/^function/, "function*") + ")();");
}, function(e) {
return e instanceof UglifyJS.JS_Parse_Error;
}, code);
});
});
it("Should reject `yield` expression outside of generator functions", function() {
[
"yield 42;",
"function f() { yield 42; }",
"function* f() { function g() { yield 42; } }",
].forEach(function(code) {
assert.throws(function() {
UglifyJS.parse(code);
}, function(e) {
return e instanceof UglifyJS.JS_Parse_Error;
}, code);
});
});
it("Should reject `yield` expression directly on computed key of function argument", function() {
[
"function f({ [yield 42]: a }) {}",
"function* f({ [yield 42]: a }) {}",
].forEach(function(code) {
assert.throws(function() {
UglifyJS.parse(code);
}, function(e) {
return e instanceof UglifyJS.JS_Parse_Error;
}, code);
});
});
it("Should accept `yield` expression nested within computed key of function argument", function() {
[
"function f({ [function*() { yield 42; }()]: a }) {}",
"function* f({ [function*() { yield 42; }()]: a }) {}",
].forEach(function(code) {
var ast = UglifyJS.parse(code);
assert.strictEqual(ast.TYPE, "Toplevel");
assert.strictEqual(ast.body.length, 1);
assert.strictEqual(ast.body[0].argnames.length, 1);
assert.strictEqual(ast.body[0].argnames[0].TYPE, "DestructuredObject");
});
});
it("Should reject `yield*` without an expression", function() {
[
"yield*",
"yield*;",
"yield*,",
"(yield*)",
"[ yield* ]",
"42[yield*]",
"yield* && 42",
].forEach(function(code) {
code = "function* f() { " + code + " }";
assert.throws(function() {
UglifyJS.parse(code);
}, function(e) {
return e instanceof UglifyJS.JS_Parse_Error;
}, code);
});
});
});
|