/* * Copyright 2008 The Closure Compiler Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.javascript.jscomp; /** * Test for {@link UnreachableCodeElimination}. * */ public class UnreachableCodeEliminationTest extends CompilerTestCase { private boolean removeNoOpStatements = true; @Override protected CompilerPass getProcessor(Compiler compiler) { return new UnreachableCodeElimination(compiler, removeNoOpStatements); } @Override public void setUp() throws Exception { super.setUp(); removeNoOpStatements = true; } public void testRemoveUnreachableCode() { // switch statement with stuff after "return" test("function foo(){switch(foo){case 1:x=1;return;break;" + "case 2:{x=2;return;break}default:}}", "function foo(){switch(foo){case 1:x=1;return;" + "case 2:{x=2}default:}}"); // if/else statements with returns test("function bar(){if(foo)x=1;else if(bar){return;x=2}" + "else{x=3;return;x=4}return 5;x=5}", "function bar(){if(foo)x=1;else if(bar){return}" + "else{x=3;return}return 5}"); // if statements without blocks test("function foo(){if(x==3)return;x=4;y++;while(y==4){return;x=3}}", "function foo(){if(x==3)return;x=4;y++;while(y==4){return}}"); // for/do/while loops test("function baz(){for(i=0;i= 0) {\n" + " return b.o;\n" + " } else {\n" + " return;\n" + " }\n" + " break;\n" + " }\n" + "}", "function a(b) {\n" + " switch (b.v) {\n" + " case 'SWITCH':\n" + " if (b.i >= 0) {\n" + " return b.o;\n" + " } else {\n" + " }\n" + " }\n" + "}"); } public void testIssue4177428a() { test( "f = function() {\n" + " var action;\n" + " a: {\n" + " var proto = null;\n" + " try {\n" + " proto = new Proto\n" + " } finally {\n" + " action = proto;\n" + " break a\n" + // Remove this... " }\n" + " }\n" + " alert(action)\n" + // but not this. "};", "f = function() {\n" + " var action;\n" + " a: {\n" + " var proto = null;\n" + " try {\n" + " proto = new Proto\n" + " } finally {\n" + " action = proto;\n" + " }\n" + " }\n" + " alert(action)\n" + // but not this. "};" ); } public void testIssue4177428b() { test( "f = function() {\n" + " var action;\n" + " a: {\n" + " var proto = null;\n" + " try {\n" + " try {\n" + " proto = new Proto\n" + " } finally {\n" + " action = proto;\n" + " break a\n" + // Remove this... " }\n" + " } finally {\n" + " }\n" + " }\n" + " alert(action)\n" + // but not this. "};", "f = function() {\n" + " var action;\n" + " a: {\n" + " var proto = null;\n" + " try {\n" + " try {\n" + " proto = new Proto\n" + " } finally {\n" + " action = proto;\n" + " break a\n" + // Remove this... " }\n" + " } finally {\n" + " }\n" + " }\n" + " alert(action)\n" + // but not this. "};" ); } public void testIssue4177428c() { test( "f = function() {\n" + " var action;\n" + " a: {\n" + " var proto = null;\n" + " try {\n" + " } finally {\n" + " try {\n" + " proto = new Proto\n" + " } finally {\n" + " action = proto;\n" + " break a\n" + // Remove this... " }\n" + " }\n" + " }\n" + " alert(action)\n" + // but not this. "};", "f = function() {\n" + " var action;\n" + " a: {\n" + " var proto = null;\n" + " try {\n" + " } finally {\n" + " try {\n" + " proto = new Proto\n" + " } finally {\n" + " action = proto;\n" + " }\n" + " }\n" + " }\n" + " alert(action)\n" + // but not this. "};" ); } public void testIssue4177428_continue() { test( "f = function() {\n" + " var action;\n" + " a: do {\n" + " var proto = null;\n" + " try {\n" + " proto = new Proto\n" + " } finally {\n" + " action = proto;\n" + " continue a\n" + // Remove this... " }\n" + " } while(false)\n" + " alert(action)\n" + // but not this. "};", "f = function() {\n" + " var action;\n" + " a: do {\n" + " var proto = null;\n" + " try {\n" + " proto = new Proto\n" + " } finally {\n" + " action = proto;\n" + " }\n" + " } while (false)\n" + " alert(action)\n" + "};" ); } public void testIssue4177428_return() { test( "f = function() {\n" + " var action;\n" + " a: {\n" + " var proto = null;\n" + " try {\n" + " proto = new Proto\n" + " } finally {\n" + " action = proto;\n" + " return\n" + // Remove this... " }\n" + " }\n" + " alert(action)\n" + // and this. "};", "f = function() {\n" + " var action;\n" + " a: {\n" + " var proto = null;\n" + " try {\n" + " proto = new Proto\n" + " } finally {\n" + " action = proto;\n" + " }\n" + " }\n" + "};" ); } public void testIssue4177428_multifinally() { testSame( "a: {\n" + " try {\n" + " try {\n" + " } finally {\n" + " break a;\n" + " }\n" + " } finally {\n" + " x = 1;\n" + " }\n" + "}"); } public void testIssue5215541_deadVarDeclar() { testSame("throw 1; var x"); testSame("throw 1; function x() {}"); testSame("throw 1; var x; var y;"); test("throw 1; var x = foo", "var x; throw 1"); } public void testForInLoop() { testSame("for(var x in y) {}"); } }