1 // Copyright 2015 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 // Var-let conflict in a function throws, even if the var is in an eval 6 7 // Throws at the top level of a function 8 assertThrows(function() { 9 let x = 1; 10 eval('var x'); 11 }, SyntaxError); 12 13 // If the eval is in its own block scope, throws 14 assertThrows(function() { 15 let y = 1; 16 { eval('var y'); } 17 }, SyntaxError); 18 19 // If the let is in its own block scope, with the eval, throws 20 assertThrows(function() { 21 { 22 let x = 1; 23 eval('var x'); 24 } 25 }, SyntaxError); 26 27 // Legal if the let is no longer visible 28 assertDoesNotThrow(function() { 29 { 30 let x = 1; 31 } 32 eval('var x'); 33 }); 34 35 // All the same works for const: 36 // Throws at the top level of a function 37 assertThrows(function() { 38 const x = 1; 39 eval('var x'); 40 }, SyntaxError); 41 42 // If the eval is in its own block scope, throws 43 assertThrows(function() { 44 const y = 1; 45 { eval('var y'); } 46 }, SyntaxError); 47 48 // If the const is in its own block scope, with the eval, throws 49 assertThrows(function() { 50 { 51 const x = 1; 52 eval('var x'); 53 } 54 }, SyntaxError); 55 56 // Legal if the const is no longer visible 57 assertDoesNotThrow(function() { 58 { 59 const x = 1; 60 } 61 eval('var x'); 62 }); 63 64 // In global scope 65 let caught = false; 66 try { 67 let z = 1; 68 eval('var z'); 69 } catch (e) { 70 caught = true; 71 } 72 assertTrue(caught); 73 74 // Let declarations beyond a function boundary don't conflict 75 caught = false; 76 try { 77 let a = 1; 78 (function() { 79 eval('var a'); 80 })(); 81 } catch (e) { 82 caught = true; 83 } 84 assertFalse(caught); 85 86 // var across with doesn't conflict 87 caught = false; 88 try { 89 (function() { 90 with ({x: 1}) { 91 eval("var x"); 92 } 93 })(); 94 } catch (e) { 95 caught = true; 96 } 97 assertFalse(caught); 98 99 // var can still conflict with let across a with 100 caught = false; 101 try { 102 (function() { 103 let x; 104 with ({x: 1}) { 105 eval("var x"); 106 } 107 })(); 108 } catch (e) { 109 caught = true; 110 } 111 assertTrue(caught); 112 113 // Functions declared in eval also conflict 114 caught = false 115 try { 116 (function() { 117 { 118 let x = 1; 119 eval('function x() {}'); 120 } 121 })(); 122 } catch (e) { 123 caught = true; 124 } 125 assertTrue(caught); 126 127 // TODO(littledan): Hoisting x out of the block should be 128 // prevented in this case BUG(v8:4479) 129 caught = false 130 try { 131 (function() { 132 { 133 let x = 1; 134 eval('{ function x() {} }'); 135 } 136 })(); 137 } catch (e) { 138 caught = true; 139 } 140 // TODO(littledan): switch to assertTrue when bug is fixed 141 assertTrue(caught); 142