summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests/parser-line-terminators.js
blob: ab88b0eb44d520d396cc500c3fecbb301410242f (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
/*
These tests deliberately produce syntax errors to check what line the parser thinks we're on.
Note that line numbers are higher than you might expect as the parsed code is:

function anonymous(
) {
<code>
}

⚠ PLEASE MAKE SURE TO NOT LET YOUR EDITOR REMOVE THE LS/PS LINE TERMINATORS!
*/

test("LINE FEED is a line terminator", () => {
    expect(() => {
        Function("\n\n@");
    }).toThrowWithMessage(SyntaxError, "line: 5, column: 1");
});

test("CARRIAGE RETURN is a line terminator", () => {
    expect(() => {
        Function("\r\r@");
    }).toThrowWithMessage(SyntaxError, "line: 5, column: 1");
});

test("LINE SEPARATOR is a line terminator", () => {
    expect(() => {
        Function("

@");
    }).toThrowWithMessage(SyntaxError, "line: 5, column: 1");
});

test("PARAGRAPH SEPARATOR is a line terminator", () => {
    expect(() => {
        Function("

@");
    }).toThrowWithMessage(SyntaxError, "line: 5, column: 1");
});

test("CR LF is counted as only one line terminator", () => {
    expect(() => {
        Function("\r\n\r\n@");
    }).toThrowWithMessage(SyntaxError, "line: 5, column: 1");
});

test("LF/CR are not allowed in string literal", () => {
    expect(() => {
        Function(`"
        "`);
    }).toThrowWithMessage(SyntaxError, "Unexpected token UnterminatedStringLiteral");
});

test("LS/PS are allowed in string literal", () => {
    expect(`"
"`).toEval();
    expect(`"
"`).toEval();
});

test("line terminators can be mixed (but please don't)", () => {
    expect(() => {
        Function("\r
\r\n
\n\r@");
    }).toThrowWithMessage(SyntaxError, "line: 9, column: 1");
});

test("all line terminators are valid for line continuations", () => {
    expect(Function('return "a\\\nb"')()).toBe("ab");
    expect(Function('return "a\\\rb"')()).toBe("ab");
    expect(Function('return "a\\
b"')()).toBe("ab");
    expect(Function('return "a\\
b"')()).toBe("ab");
});