aboutsummaryrefslogtreecommitdiff
path: root/test/mocha/async.js
blob: d4e96609dd6f4505ca2cd15ee35423ac3cb07a53 (about) (plain)
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
var assert = require("assert");
var UglifyJS = require("../node");

describe("async", function() {
    it("Should reject `await` as symbol name within async functions only", function() {
        [
            "function await() {}",
            "function(await) {}",
            "function() { await; }",
            "function() { await:{} }",
            "function() { var await; }",
            "function() { function await() {} }",
            "function() { try {} catch (await) {} }",
        ].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("(async " + code + ")();");
            }, function(e) {
                return e instanceof UglifyJS.JS_Parse_Error;
            }, code);
        });
    });
    it("Should reject `await` expression outside of async functions", function() {
        [
            "await 42;",
            "function f() { await 42; }",
            "async function f() { function g() { await 42; } }",
        ].forEach(function(code) {
            assert.throws(function() {
                UglifyJS.parse(code);
            }, function(e) {
                return e instanceof UglifyJS.JS_Parse_Error;
            }, code);
        });
    });
    it("Should reject `await` expression directly on computed key of function argument", function() {
        [
            "function f({ [await 42]: a }) {}",
            "async function f({ [await 42]: a }) {}",
        ].forEach(function(code) {
            assert.throws(function() {
                UglifyJS.parse(code);
            }, function(e) {
                return e instanceof UglifyJS.JS_Parse_Error;
            }, code);
        });
    });
    it("Should accept `await` expression nested within computed key of function argument", function() {
        [
            "function f({ [async function() { await 42; }()]: a }) {}",
            "async function f({ [async function() { await 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");
        });
    });
});