summaryrefslogtreecommitdiffstats
path: root/js/src/tests/ecma_6/Function/function-name-assignment.js
blob: 5e4d1c004f0a2567d8ff46c2fe719db76846ce93 (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
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
var BUGNUMBER = 883377;
var summary = "Anonymous function name should be set based on assignment";

print(BUGNUMBER + ": " + summary);

var fooSymbol = Symbol("foo");
var emptySymbol = Symbol("");
var undefSymbol = Symbol();
var globalVar;

var exprs = [
    ["function() {}", false],
    ["function named() {}", true],
    ["function*() {}", false],
    ["function* named() {}", true],
    ["async function() {}", false],
    ["async function named() {}", true],
    ["() => {}", false],
    ["async () => {}", false],
    ["class {}", false],
    ["class named {}", true],
];

function testAssignmentExpression(expr, named) {
    eval(`
    var assignment;
    assignment = ${expr};
    assertEq(assignment.name, named ? "named" : "assignment");

    globalVar = ${expr};
    assertEq(globalVar.name, named ? "named" : "globalVar");

    var obj = { dynamic: null };
    with (obj) {
        dynamic = ${expr};
    }
    assertEq(obj.dynamic.name, named ? "named" : "dynamic");

    (function namedLambda(param1, param2) {
        var assignedToNamedLambda;
        assignedToNamedLambda = namedLambda = ${expr};
        assertEq(namedLambda.name, "namedLambda");
        assertEq(assignedToNamedLambda.name, named ? "named" : "namedLambda");

        param1 = ${expr};
        assertEq(param1.name, named ? "named" : "param1");

        {
            let param1 = ${expr};
            assertEq(param1.name, named ? "named" : "param1");

            param2 = ${expr};
            assertEq(param2.name, named ? "named" : "param2");
        }
    })();

    {
        let nextedLexical1, nextedLexical2;
        {
            let nextedLexical1 = ${expr};
            assertEq(nextedLexical1.name, named ? "named" : "nextedLexical1");

            nextedLexical2 = ${expr};
            assertEq(nextedLexical2.name, named ? "named" : "nextedLexical2");
        }
    }
    `);

    // Not applicable cases: not IsIdentifierRef.
    eval(`
    var inParen;
    (inParen) = ${expr};
    assertEq(inParen.name, named ? "named" : "");
    `);

    // Not applicable cases: not direct RHS.
    if (!expr.includes("=>")) {
        eval(`
        var a = true && ${expr};
        assertEq(a.name, named ? "named" : "");
        `);
    } else {
        // Arrow function cannot be RHS of &&.
        eval(`
        var a = true && (${expr});
        assertEq(a.name, named ? "named" : "");
        `);
    }

    // Not applicable cases: property.
    eval(`
    var obj = {};

    obj.prop = ${expr};
    assertEq(obj.prop.name, named ? "named" : "");

    obj["literal"] = ${expr};
    assertEq(obj["literal"].name, named ? "named" : "");
    `);

    // Not applicable cases: assigned again.
    eval(`
    var tmp = [${expr}];
    assertEq(tmp[0].name, named ? "named" : "");

    var assignment;
    assignment = tmp[0];
    assertEq(assignment.name, named ? "named" : "");
    `);
}
for (var [expr, named] of exprs) {
    testAssignmentExpression(expr, named);
}

function testVariableDeclaration(expr, named) {
    eval(`
    var varDecl = ${expr};
    assertEq(varDecl.name, named ? "named" : "varDecl");
    `);
}
for (var [expr, named] of exprs) {
    testVariableDeclaration(expr, named);
}

function testLexicalBinding(expr, named) {
    eval(`
    let lexical = ${expr};
    assertEq(lexical.name, named ? "named" : "lexical");

    const constLexical = ${expr};
    assertEq(constLexical.name, named ? "named" : "constLexical");
    `);
}
for (var [expr, named] of exprs) {
    testLexicalBinding(expr, named);
}

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