summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/async-functions/await-in-arrow-parameters.js
blob: ebb4ea9dac493c81c45c58f3da5534926908d448 (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
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
var ieval = eval;
var AsyncFunction = async function(){}.constructor;

var functionContext = {
    Function: {
        constructor: Function,
        toSourceBody: code => `function f() { ${code} }`,
        toSourceParameter: code => `function f(x = ${code}) { }`,
    },
    AsyncFunction: {
        constructor: AsyncFunction,
        toSourceBody: code => `async function f() { ${code} }`,
        toSourceParameter: code => `async function f(x = ${code}) { }`,
    },
};

function assertSyntaxError(kind, code) {
    var {constructor, toSourceBody, toSourceParameter} = functionContext[kind];
    var body = toSourceBody(code);
    var parameter = toSourceParameter(code);

    assertThrowsInstanceOf(() => { constructor(code); }, SyntaxError, constructor.name + ":" + code);
    assertThrowsInstanceOf(() => { constructor(`x = ${code}`, ""); }, SyntaxError, constructor.name + ":" + code);

    assertThrowsInstanceOf(() => { eval(body); }, SyntaxError, "eval:" + body);
    assertThrowsInstanceOf(() => { ieval(body); }, SyntaxError, "indirect eval:" + body);

    assertThrowsInstanceOf(() => { eval(parameter); }, SyntaxError, "eval:" + parameter);
    assertThrowsInstanceOf(() => { ieval(parameter); }, SyntaxError, "indirect eval:" + parameter);
}

function assertNoSyntaxError(kind, code) {
    var {constructor, toSourceBody, toSourceParameter} = functionContext[kind];
    var body = toSourceBody(code);
    var parameter = toSourceParameter(code);

    constructor(code);
    constructor(`x = ${code}`, "");

    eval(body);
    ieval(body);

    eval(parameter);
    ieval(parameter);
}

function assertSyntaxErrorAsync(code) {
    assertNoSyntaxError("Function", code);
    assertSyntaxError("AsyncFunction", code);
}

function assertSyntaxErrorBoth(code) {
    assertSyntaxError("Function", code);
    assertSyntaxError("AsyncFunction", code);
}


// Bug 1353691
// |await| expression is invalid in arrow functions in async-context.
// |await/r/g| first parses as |AwaitExpression RegularExpressionLiteral|, when reparsing the
// arrow function, it is parsed as |IdentRef DIV IdentRef DIV IdentRef|. We need to ensure in this
// case, that we still treat |await| as a keyword and hence throw a SyntaxError.
assertSyntaxErrorAsync("(a = await/r/g) => {}");
assertSyntaxErrorBoth("async(a = await/r/g) => {}");

// Also applies when nesting arrow functions.
assertSyntaxErrorAsync("(a = (b = await/r/g) => {}) => {}");
assertSyntaxErrorBoth("async(a = (b = await/r/g) => {}) => {}");
assertSyntaxErrorBoth("(a = async(b = await/r/g) => {}) => {}");
assertSyntaxErrorBoth("async(a = async(b = await/r/g) => {}) => {}");


// Bug 1355860
// |await| cannot be used as rest-binding parameter in arrow functions in async-context.
assertSyntaxErrorAsync("(...await) => {}");
assertSyntaxErrorBoth("async(...await) => {}");

assertSyntaxErrorAsync("(a, ...await) => {}");
assertSyntaxErrorBoth("async(a, ...await) => {}");

// Also test nested arrow functions.
assertSyntaxErrorAsync("(a = (...await) => {}) => {}");
assertSyntaxErrorBoth("(a = async(...await) => {}) => {}");
assertSyntaxErrorBoth("async(a = (...await) => {}) => {}");
assertSyntaxErrorBoth("async(a = async(...await) => {}) => {}");

assertSyntaxErrorAsync("(a = (b, ...await) => {}) => {}");
assertSyntaxErrorBoth("(a = async(b, ...await) => {}) => {}");
assertSyntaxErrorBoth("async(a = (b, ...await) => {}) => {}");
assertSyntaxErrorBoth("async(a = async(b, ...await) => {}) => {}");


if (typeof reportCompare === "function")
    reportCompare(true, true);