summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests/unicode-identifier-escape.js
blob: a9a9a348a21800a5bec96cb06bfe9581428f453a (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
test("basic escapes", () => {
    var foo = {};
    foo.brown = 12389;

    expect(foo.brown).toBe(12389);
    expect(foo.br\u006fwn).toBe(12389);
    expect(foo.br\u{6f}wn).toBe(12389);
    expect(foo.\u{62}\u{72}\u{6f}\u{77}\u{6e}).toBe(12389);
});

test("non-ascii escapes", () => {
    var foo = {};
    foo.𝓑𝓻𝓸𝔀𝓷 = 12389;

    expect(foo.𝓑𝓻𝓸𝔀𝓷).toBe(12389);
    expect(foo.𝓑𝓻\u{1d4f8}𝔀𝓷).toBe(12389);
    expect(foo.\u{1d4d1}\u{1d4fb}\u{1d4f8}\u{1d500}\u{1d4f7}).toBe(12389);

    // U-16 High surrogate pair is allowed in string but not in identifier.
    expect("foo.𝓑𝓻\ud835\udcf8𝔀𝓷").toEval();
    expect("foo.𝓑𝓻\\ud835\\udcf8𝔀𝓷").not.toEval();
});

describe("escaped keywords", () => {
    // We must double escape the slashes here else the strings already convert
    // the escaped characters (and string is more lenient).
    test("keywords cannot be used in an escaped form", () => {
        expect("\\u{69}\\u{66}(true) throw 'Should fail'").not.toEval();
        expect("wh\\u{69}le(true) throw 'Should fail'").not.toEval();

        expect("l\\u{65}t a = 3;").not.toEval();
        expect("function *G(){ yiel\\0064 3; }").not.toEval();
    });

    test("escaped keywords cannot be used as standalone variables", () => {
        expect("var fu\\u{6e}ction = 4").not.toEval();
        expect("var \\u0077ith = 4").not.toEval();
    });

    test("'yield' and 'let' can be escaped as variables", () => {
        var l\u{65}t = 3;
        var yi\u0065ld = 5;
        expect(let).toBe(3);
        expect(yield).toBe(5);
    });

    test("'let' cannot be used in a lexical declaration but 'yield' can", () => {
        expect("const l\\u{65}t = 3;").not.toEval();

        const yi\u0065ld = 5;
        expect(yield).toBe(5);
    });

    test("escaped 'yield' and 'let' variables are not allowed in strict mode", () => {
        expect("function f() { 'use strict'; var l\\u{65}t = 3; }").not.toEval();
        expect("function g() { 'use strict'; var yi\u0065ld = 5; }").not.toEval();
    });

    test("cannot use escaped 'yield' variable or label in generator context", () => {
        expect("function *g() { var yi\u0065ld = 5; }").not.toEval();
        expect("function *g() { yi\u0065ld: 5; }").not.toEval();
    });

    test("can use escaped 'let' variable and label in generator context", () => {
        expect("function *i() { var \\u{6c}et = 6; }").toEval();
        expect("function *j() { \\u{6c}et: 6; }").toEval();
    });

    test("can use keywords in some contexts", () => {
        var obj = {
            \u{69}\u{66}: 3,
            wh\u{69}le() {
                return 4;
            },
            ca\u0073e: "case",
            get true() {
                return false;
            },
        };

        expect(obj.\u{69}f).toBe(3);
        expect(obj.whi\u{6c}e()).toBe(4);
        expect(obj.\u{63}ase).toBe("case");
        expect(obj.\u0074r\u{0000075}e).toBeFalse();
    });
});