| 1 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
// This test case is weird in the sense the actual work happens at the eval |
| 6 |
// at the end. If template strings are not enabled, the test cases would throw |
| 7 |
// a syntax error and we'd have failure reported. To avoid that, the entire |
| 8 |
// test case is commented out and is part of a function. We use toString to |
| 9 |
// get the string version, obtain the actual lines to run, and then use eval to |
| 10 |
// do the actual evaluation. |
| 11 |
|
| 12 |
function syntaxError (script) { |
| 13 |
try { |
| 14 |
Function(script); |
| 15 |
} catch (e) { |
| 16 |
if (e.name === "SyntaxError") { |
| 17 |
return; |
| 18 |
} |
| 19 |
} |
| 20 |
throw "Expected syntax error: " + script; |
| 21 |
} |
| 22 |
|
| 23 |
// TEST BEGIN |
| 24 |
|
| 25 |
// unterminated quasi literal |
| 26 |
syntaxError("`"); |
| 27 |
syntaxError("`$"); |
| 28 |
syntaxError("`${"); |
| 29 |
syntaxError("`${}"); |
| 30 |
syntaxError("`${1}"); |
| 31 |
syntaxError("`${1 + 2}"); |
| 32 |
|
| 33 |
// almost template substitutions |
| 34 |
assertEq("$", `$`); |
| 35 |
assertEq("$}", `$}`); |
| 36 |
assertEq("}", `}`); |
| 37 |
assertEq("{", `{`); |
| 38 |
|
| 39 |
|
| 40 |
// character escape sequence (single escape character) |
| 41 |
assertEq("\'", `\'`); |
| 42 |
assertEq("\"", `\"`); |
| 43 |
assertEq("\\", `\\`); |
| 44 |
assertEq("\b", `\b`); |
| 45 |
assertEq("\f", `\f`); |
| 46 |
assertEq("\n", `\n`); |
| 47 |
assertEq("\r", `\r`); |
| 48 |
assertEq("\t", `\t`); |
| 49 |
assertEq("\v", `\v`); |
| 50 |
assertEq("\r\n", `\r\n`); |
| 51 |
|
| 52 |
|
| 53 |
assertEq("\0", eval("`\\" + String.fromCharCode(0) + "`")); |
| 54 |
assertEq("$", `\$`); |
| 55 |
assertEq(".", `\.`); |
| 56 |
assertEq("A", `\A`); |
| 57 |
assertEq("a", `\a`); |
| 58 |
|
| 59 |
|
| 60 |
// digit escape sequence |
| 61 |
assertEq("\0", `\0`); |
| 62 |
syntaxError("`\\1`"); |
| 63 |
syntaxError("`\\2`"); |
| 64 |
syntaxError("`\\3`"); |
| 65 |
syntaxError("`\\4`"); |
| 66 |
syntaxError("`\\5`"); |
| 67 |
syntaxError("`\\6`"); |
| 68 |
syntaxError("`\\7`"); |
| 69 |
syntaxError("`\\01`"); |
| 70 |
syntaxError("`\\001`"); |
| 71 |
syntaxError("`\\00`"); |
| 72 |
|
| 73 |
// hex escape sequence |
| 74 |
syntaxError("`\\x`"); |
| 75 |
syntaxError("`\\x0`"); |
| 76 |
syntaxError("`\\x0Z`"); |
| 77 |
syntaxError("`\\xZ`"); |
| 78 |
|
| 79 |
assertEq("\0", `\x00`); |
| 80 |
assertEq("$", `\x24`); |
| 81 |
assertEq(".", `\x2E`); |
| 82 |
assertEq("A", `\x41`); |
| 83 |
assertEq("a", `\x61`); |
| 84 |
assertEq("AB", `\x41B`); |
| 85 |
assertEq(String.fromCharCode(0xFF), `\xFF`); |
| 86 |
|
| 87 |
|
| 88 |
// unicode escape sequence |
| 89 |
|
| 90 |
assertEq("\0", `\u0000`); |
| 91 |
assertEq("$", `\u0024`); |
| 92 |
assertEq(".", `\u002E`); |
| 93 |
assertEq("A", `\u0041`); |
| 94 |
assertEq("a", `\u0061`); |
| 95 |
assertEq("AB", `\u0041B`); |
| 96 |
assertEq(String.fromCharCode(0xFFFF), `\uFFFF`); |
| 97 |
|
| 98 |
|
| 99 |
// line continuation |
| 100 |
assertEq("", eval("`\\\n`")); |
| 101 |
assertEq("", eval("`\\\r`")); |
| 102 |
assertEq("", eval("`\\\u2028`")); |
| 103 |
assertEq("", eval("`\\\u2029`")); |
| 104 |
assertEq("\u2028", eval("`\u2028`")); |
| 105 |
assertEq("\u2029", eval("`\u2029`")); |
| 106 |
|
| 107 |
assertEq("a\nb", eval("`a\rb`")) |
| 108 |
assertEq("a\nb", eval("`a\r\nb`")) |
| 109 |
assertEq("a\n\nb", eval("`a\r\rb`")) |
| 110 |
|
| 111 |
|
| 112 |
// source character |
| 113 |
for (var i = 0; i < 0xFF; ++i) { |
| 114 |
var c = String.fromCharCode(i); |
| 115 |
if (c == "`" || c == "\\" || c == "\r") continue; |
| 116 |
assertEq(c, eval("`" + c + "`")); |
| 117 |
} |
| 118 |
|
| 119 |
assertEq("", ``); |
| 120 |
assertEq("`", `\``); |
| 121 |
assertEq("$", `$`); |
| 122 |
assertEq("$$", `$$`); |
| 123 |
assertEq("$$}", `$$}`); |
| 124 |
|
| 125 |
// multi-line |
| 126 |
assertEq(`hey |
| 127 |
there`, "hey\nthere"); |
| 128 |
|
| 129 |
// differences between strings and template strings |
| 130 |
syntaxError("var obj = { `illegal`: 1}"); |
| 131 |
|
| 132 |
// test for JSON.parse |
| 133 |
assertThrowsInstanceOf(() => JSON.parse('[1, `false`]'), SyntaxError); |
| 134 |
|
| 135 |
syntaxError('({get `name`() { return 10; }});'); |
| 136 |
|
| 137 |
// test for "use strict" directive |
| 138 |
assertEq(5, Function("`use strict`; return 05;")()); |
| 139 |
var func = function f() { |
| 140 |
`ignored string`; |
| 141 |
"use strict"; |
| 142 |
return 06; |
| 143 |
} |
| 144 |
assertEq(6, func()); |
| 145 |
syntaxError("\"use strict\"; return 06;"); |
| 146 |
|
| 147 |
|
| 148 |
reportCompare(0, 0, "ok"); |