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
140
141
142
143
144
145
146
147
148
|
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This test case is weird in the sense the actual work happens at the eval
// at the end. If template strings are not enabled, the test cases would throw
// a syntax error and we'd have failure reported. To avoid that, the entire
// test case is commented out and is part of a function. We use toString to
// get the string version, obtain the actual lines to run, and then use eval to
// do the actual evaluation.
function syntaxError (script) {
try {
Function(script);
} catch (e) {
if (e.name === "SyntaxError") {
return;
}
}
throw "Expected syntax error: " + script;
}
// TEST BEGIN
// unterminated quasi literal
syntaxError("`");
syntaxError("`$");
syntaxError("`${");
syntaxError("`${}");
syntaxError("`${1}");
syntaxError("`${1 + 2}");
// almost template substitutions
assertEq("$", `$`);
assertEq("$}", `$}`);
assertEq("}", `}`);
assertEq("{", `{`);
// character escape sequence (single escape character)
assertEq("\'", `\'`);
assertEq("\"", `\"`);
assertEq("\\", `\\`);
assertEq("\b", `\b`);
assertEq("\f", `\f`);
assertEq("\n", `\n`);
assertEq("\r", `\r`);
assertEq("\t", `\t`);
assertEq("\v", `\v`);
assertEq("\r\n", `\r\n`);
assertEq("\0", eval("`\\" + String.fromCharCode(0) + "`"));
assertEq("$", `\$`);
assertEq(".", `\.`);
assertEq("A", `\A`);
assertEq("a", `\a`);
// digit escape sequence
assertEq("\0", `\0`);
syntaxError("`\\1`");
syntaxError("`\\2`");
syntaxError("`\\3`");
syntaxError("`\\4`");
syntaxError("`\\5`");
syntaxError("`\\6`");
syntaxError("`\\7`");
syntaxError("`\\01`");
syntaxError("`\\001`");
syntaxError("`\\00`");
// hex escape sequence
syntaxError("`\\x`");
syntaxError("`\\x0`");
syntaxError("`\\x0Z`");
syntaxError("`\\xZ`");
assertEq("\0", `\x00`);
assertEq("$", `\x24`);
assertEq(".", `\x2E`);
assertEq("A", `\x41`);
assertEq("a", `\x61`);
assertEq("AB", `\x41B`);
assertEq(String.fromCharCode(0xFF), `\xFF`);
// unicode escape sequence
assertEq("\0", `\u0000`);
assertEq("$", `\u0024`);
assertEq(".", `\u002E`);
assertEq("A", `\u0041`);
assertEq("a", `\u0061`);
assertEq("AB", `\u0041B`);
assertEq(String.fromCharCode(0xFFFF), `\uFFFF`);
// line continuation
assertEq("", eval("`\\\n`"));
assertEq("", eval("`\\\r`"));
assertEq("", eval("`\\\u2028`"));
assertEq("", eval("`\\\u2029`"));
assertEq("\u2028", eval("`\u2028`"));
assertEq("\u2029", eval("`\u2029`"));
assertEq("a\nb", eval("`a\rb`"))
assertEq("a\nb", eval("`a\r\nb`"))
assertEq("a\n\nb", eval("`a\r\rb`"))
// source character
for (var i = 0; i < 0xFF; ++i) {
var c = String.fromCharCode(i);
if (c == "`" || c == "\\" || c == "\r") continue;
assertEq(c, eval("`" + c + "`"));
}
assertEq("", ``);
assertEq("`", `\``);
assertEq("$", `$`);
assertEq("$$", `$$`);
assertEq("$$}", `$$}`);
// multi-line
assertEq(`hey
there`, "hey\nthere");
// differences between strings and template strings
syntaxError("var obj = { `illegal`: 1}");
// test for JSON.parse
assertThrowsInstanceOf(() => JSON.parse('[1, `false`]'), SyntaxError);
syntaxError('({get `name`() { return 10; }});');
// test for "use strict" directive
assertEq(5, Function("`use strict`; return 05;")());
var func = function f() {
`ignored string`;
"use strict";
return 06;
}
assertEq(6, func());
syntaxError("\"use strict\"; return 06;");
reportCompare(0, 0, "ok");
|