block-let-declaration.js revision 3ef787dbeca8a5fb1086949cda830dccee07bfbd
1// Copyright 2011 the V8 project authors. All rights reserved. 2// Redistribution and use in source and binary forms, with or without 3// modification, are permitted provided that the following conditions are 4// met: 5// 6// * Redistributions of source code must retain the above copyright 7// notice, this list of conditions and the following disclaimer. 8// * Redistributions in binary form must reproduce the above 9// copyright notice, this list of conditions and the following 10// disclaimer in the documentation and/or other materials provided 11// with the distribution. 12// * Neither the name of Google Inc. nor the names of its 13// contributors may be used to endorse or promote products derived 14// from this software without specific prior written permission. 15// 16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28// Flags: --harmony-scoping 29 30// Test let declarations in various settings. 31// TODO(ES6): properly activate extended mode 32"use strict"; 33 34// Global 35let x; 36let y = 2; 37const z = 4; 38 39// Block local 40{ 41 let y; 42 let x = 3; 43 const z = 5; 44} 45 46assertEquals(undefined, x); 47assertEquals(2,y); 48assertEquals(4,z); 49 50if (true) { 51 let y; 52 assertEquals(undefined, y); 53} 54 55// Invalid declarations are early errors in harmony mode and thus should trigger 56// an exception in eval code during parsing, before even compiling or executing 57// the code. Thus the generated function is not called here. 58function TestLocalThrows(str, expect) { 59 assertThrows("(function(){ 'use strict'; " + str + "})", expect); 60} 61 62function TestLocalDoesNotThrow(str) { 63 assertDoesNotThrow("(function(){ 'use strict'; " + str + "})()"); 64} 65 66// Test let declarations in statement positions. 67TestLocalThrows("if (true) let x;", SyntaxError); 68TestLocalThrows("if (true) {} else let x;", SyntaxError); 69TestLocalThrows("do let x; while (false)", SyntaxError); 70TestLocalThrows("while (false) let x;", SyntaxError); 71TestLocalThrows("label: let x;", SyntaxError); 72TestLocalThrows("for (;false;) let x;", SyntaxError); 73TestLocalThrows("switch (true) { case true: let x; }", SyntaxError); 74TestLocalThrows("switch (true) { default: let x; }", SyntaxError); 75 76// Test const declarations with initialisers in statement positions. 77TestLocalThrows("if (true) const x = 1;", SyntaxError); 78TestLocalThrows("if (true) {} else const x = 1;", SyntaxError); 79TestLocalThrows("do const x = 1; while (false)", SyntaxError); 80TestLocalThrows("while (false) const x = 1;", SyntaxError); 81TestLocalThrows("label: const x = 1;", SyntaxError); 82TestLocalThrows("for (;false;) const x = 1;", SyntaxError); 83TestLocalThrows("switch (true) { case true: const x = 1; }", SyntaxError); 84TestLocalThrows("switch (true) { default: const x = 1; }", SyntaxError); 85 86// Test const declarations without initialisers. 87TestLocalThrows("const x;", SyntaxError); 88TestLocalThrows("const x = 1, y;", SyntaxError); 89TestLocalThrows("const x, y = 1;", SyntaxError); 90 91// Test const declarations without initialisers in statement positions. 92TestLocalThrows("if (true) const x;", SyntaxError); 93TestLocalThrows("if (true) {} else const x;", SyntaxError); 94TestLocalThrows("do const x; while (false)", SyntaxError); 95TestLocalThrows("while (false) const x;", SyntaxError); 96TestLocalThrows("label: const x;", SyntaxError); 97TestLocalThrows("for (;false;) const x;", SyntaxError); 98TestLocalThrows("switch (true) { case true: const x; }", SyntaxError); 99TestLocalThrows("switch (true) { default: const x; }", SyntaxError); 100 101// Test var declarations in statement positions. 102TestLocalDoesNotThrow("if (true) var x;"); 103TestLocalDoesNotThrow("if (true) {} else var x;"); 104TestLocalDoesNotThrow("do var x; while (false)"); 105TestLocalDoesNotThrow("while (false) var x;"); 106TestLocalDoesNotThrow("label: var x;"); 107TestLocalDoesNotThrow("for (;false;) var x;"); 108TestLocalDoesNotThrow("switch (true) { case true: var x; }"); 109TestLocalDoesNotThrow("switch (true) { default: var x; }"); 110 111// Test function declarations in source element and 112// non-strict statement positions. 113function f() { 114 // Non-strict source element positions. 115 function g0() { 116 "use strict"; 117 // Strict source element positions. 118 function h() { } 119 { 120 function h1() { } 121 } 122 } 123 { 124 function g1() { } 125 } 126} 127f(); 128 129// Test function declarations in statement position in strict mode. 130TestLocalThrows("function f() { if (true) function g() {}", SyntaxError); 131TestLocalThrows("function f() { if (true) {} else function g() {}", SyntaxError); 132TestLocalThrows("function f() { do function g() {} while (false)", SyntaxError); 133TestLocalThrows("function f() { while (false) function g() {}", SyntaxError); 134TestLocalThrows("function f() { label: function g() {}", SyntaxError); 135TestLocalThrows("function f() { for (;false;) function g() {}", SyntaxError); 136TestLocalThrows("function f() { switch (true) { case true: function g() {} }", SyntaxError); 137TestLocalThrows("function f() { switch (true) { default: function g() {} }", SyntaxError); 138