Debian Codegraph @

js/src/tests/non262/syntax/escaped-strict-reserved-words-and-yield.js

raw
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 const testCases = [
6 // Label identifier.
7 id => `${id}: ;`,
8
9 // Binding identifier.
10 id => `var ${id};`,
11 id => `let ${id};`,
12 id => `const ${id} = 0;`,
13
14 // Binding identifier in binding pattern.
15 id => `var [${id}] = [];`,
16 id => `var [${id} = 0] = [];`,
17 id => `var [...${id}] = [];`,
18 id => `var {a: ${id}} = {};`,
19 id => `var {${id}} = {};`,
20 id => `var {${id} = 0} = {};`,
21
22 id => `let [${id}] = [];`,
23 id => `let [${id} = 0] = [];`,
24 id => `let [...${id}] = [];`,
25 id => `let {a: ${id}} = {};`,
26 id => `let {${id}} = {};`,
27 id => `let {${id} = 0} = {};`,
28
29 id => `const [${id}] = [];`,
30 id => `const [${id} = 0] = [];`,
31 id => `const [...${id}] = [];`,
32 id => `const {a: ${id}} = {};`,
33 id => `const {${id}} = {};`,
34 id => `const {${id} = 0} = {};`,
35
36 // Identifier reference.
37 id => `void ${id};`,
38 ];
39
40 const strictReservedWords = [
41 "implements",
42 "interface",
43 "package",
44 "private",
45 "protected",
46 "public",
47 ];
48
49 function escapeWord(s) {
50 return "\\u00" + s.charCodeAt(0).toString(16) + s.substring(1);
51 }
52
53 for (let strictReservedWordOrYield of [...strictReservedWords, "yield"]) {
54 let escapedStrictReservedWordOrYield = escapeWord(strictReservedWordOrYield);
55
56 for (let testCase of testCases) {
57 eval(testCase(strictReservedWordOrYield));
58 eval(testCase(escapedStrictReservedWordOrYield));
59
60 assertThrowsInstanceOf(() => eval(String.raw`
61 "use strict";
62 ${testCase(strictReservedWordOrYield)}
63 `), SyntaxError);
64
65 assertThrowsInstanceOf(() => eval(String.raw`
66 "use strict";
67 ${testCase(escapedStrictReservedWordOrYield)}
68 `), SyntaxError);
69 }
70 }
71
72 // |yield| is always a keyword in generator functions.
73 for (let testCase of testCases) {
74 let yield = "yield";
75 let escapedYield = escapeWord("yield");
76
77 assertThrowsInstanceOf(() => eval(String.raw`
78 function* g() {
79 ${testCase(yield)}
80 }
81 `), SyntaxError);
82
83 assertThrowsInstanceOf(() => eval(String.raw`
84 function* g() {
85 ${testCase(escapedYield)}
86 }
87 `), SyntaxError);
88
89 assertThrowsInstanceOf(() => eval(String.raw`
90 "use strict";
91 function* g() {
92 ${testCase(yield)}
93 }
94 `), SyntaxError);
95
96 assertThrowsInstanceOf(() => eval(String.raw`
97 "use strict";
98 function* g() {
99 ${testCase(escapedYield)}
100 }
101 `), SyntaxError);
102 }
103
104 if (typeof reportCompare === "function")
105 reportCompare(0, 0, "ok");