OLD | NEW |
| (Empty) |
1 // Copyright 2016 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 function checkIsRedeclarationError(code) { | |
6 try { | |
7 eval(` | |
8 checkIsRedeclarationError : { | |
9 break checkIsRedeclarationError; | |
10 ${code} | |
11 } | |
12 `); | |
13 assertUnreachable(); | |
14 } catch(e) { | |
15 assertInstanceof(e, SyntaxError ); | |
16 assertTrue( e.toString().indexOf("has already been declared") >= 0 ); | |
17 } | |
18 } | |
19 | |
20 function checkIsNotRedeclarationError(code) { | |
21 assertDoesNotThrow(()=>eval(` | |
22 checkIsNotRedeclarationError_label : { | |
23 break checkIsNotRedeclarationError_label; | |
24 ${code} | |
25 } | |
26 `)); | |
27 } | |
28 | |
29 | |
30 let var_e = [ | |
31 'var e', | |
32 'var {e}', | |
33 'var [e]', | |
34 'var {f:e}', | |
35 'var [[[], e]]' | |
36 ]; | |
37 | |
38 let not_var_e = [ | |
39 'var f', | |
40 'var {}', | |
41 'var {e:f}', | |
42 'e', | |
43 '{e}', | |
44 'let e', | |
45 'const e', | |
46 'let {e}', | |
47 'const {e}', | |
48 'let [e]', | |
49 'const [e]', | |
50 'let {f:e}', | |
51 'const {f:e}' | |
52 ]; | |
53 | |
54 // Check that `for (var ... of ...)` cannot redeclare a simple catch variable | |
55 // but `for (var ... in ...)` can. | |
56 for (let binding of var_e) { | |
57 checkIsRedeclarationError(` | |
58 try { | |
59 throw 0; | |
60 } catch(e) { | |
61 for (${binding} of []); | |
62 } | |
63 `); | |
64 | |
65 checkIsNotRedeclarationError(` | |
66 try { | |
67 throw 0; | |
68 } catch(e) { | |
69 for (${binding} in []); | |
70 } | |
71 `); | |
72 } | |
73 | |
74 // Check that the above error occurs even for nested catches. | |
75 for (let binding of var_e) { | |
76 checkIsRedeclarationError(` | |
77 try { | |
78 throw 0; | |
79 } catch(e) { | |
80 try { | |
81 throw 1; | |
82 } catch(f) { | |
83 try { | |
84 throw 2; | |
85 } catch({}) { | |
86 for (${binding} of []); | |
87 } | |
88 } | |
89 } | |
90 `); | |
91 | |
92 checkIsNotRedeclarationError(` | |
93 try { | |
94 throw 0; | |
95 } catch(e) { | |
96 try { | |
97 throw 1; | |
98 } catch(f) { | |
99 try { | |
100 throw 2; | |
101 } catch({}) { | |
102 for (${binding} in []); | |
103 } | |
104 } | |
105 } | |
106 `); | |
107 } | |
108 | |
109 // Check that the above error does not occur if a declaration scope is between | |
110 // the catch and the loop. | |
111 for (let binding of var_e) { | |
112 checkIsNotRedeclarationError(` | |
113 try { | |
114 throw 0; | |
115 } catch(e) { | |
116 (()=>{for (${binding} of []);})(); | |
117 } | |
118 `); | |
119 | |
120 checkIsNotRedeclarationError(` | |
121 try { | |
122 throw 0; | |
123 } catch(e) { | |
124 (function(){for (${binding} of []);})(); | |
125 } | |
126 `); | |
127 } | |
128 | |
129 // Check that there is no error when not declaring a var named e. | |
130 for (let binding of not_var_e) { | |
131 checkIsNotRedeclarationError(` | |
132 try { | |
133 throw 0; | |
134 } catch(e) { | |
135 for (${binding} of []); | |
136 } | |
137 `); | |
138 } | |
139 | |
140 // Check that there is an error for both for-in and for-of when redeclaring | |
141 // a non-simple catch parameter | |
142 for (let binding of var_e) { | |
143 checkIsRedeclarationError(` | |
144 try { | |
145 throw 0; | |
146 } catch({e}) { | |
147 for (${binding} of []); | |
148 } | |
149 `); | |
150 | |
151 checkIsRedeclarationError(` | |
152 try { | |
153 throw 0; | |
154 } catch({e}) { | |
155 for (${binding} in []); | |
156 } | |
157 `); | |
158 } | |
159 | |
160 // Check that the above error occurs even for nested catches. | |
161 for (let binding of var_e) { | |
162 checkIsRedeclarationError(` | |
163 try { | |
164 throw 0; | |
165 } catch({e}) { | |
166 try { | |
167 throw 1; | |
168 } catch(f) { | |
169 try { | |
170 throw 2; | |
171 } catch({}) { | |
172 for (${binding} of []); | |
173 } | |
174 } | |
175 } | |
176 `); | |
177 | |
178 checkIsRedeclarationError(` | |
179 try { | |
180 throw 0; | |
181 } catch({e}) { | |
182 try { | |
183 throw 1; | |
184 } catch(f) { | |
185 try { | |
186 throw 2; | |
187 } catch({}) { | |
188 for (${binding} in []); | |
189 } | |
190 } | |
191 } | |
192 `); | |
193 } | |
OLD | NEW |