diff --git a/src/com/google/javascript/jscomp/ControlFlowAnalysis.java b/src/com/google/javascript/jscomp/ControlFlowAnalysis.java index 39bb2e2beda..b99549c3b4c 100644 --- a/src/com/google/javascript/jscomp/ControlFlowAnalysis.java +++ b/src/com/google/javascript/jscomp/ControlFlowAnalysis.java @@ -78,7 +78,7 @@ public int compare( private int astPositionCounter; private int priorityCounter; - private final boolean shouldTraverseFunctions; + private final boolean shouldTraverseFunctionsAndClasses; private final boolean edgeAnnotations; // We need to store where we started, in case we aren't doing a flow analysis @@ -128,15 +128,13 @@ public int compare( * Constructor. * * @param compiler Compiler instance. - * @param shouldTraverseFunctions Whether functions should be traversed (true - * by default). - * @param edgeAnnotations Whether to allow edge annotations. By default, - * only node annotations are allowed. + * @param shouldTraverseFunctions Whether functions should be traversed + * @param edgeAnnotations Whether to allow edge annotations. */ ControlFlowAnalysis(AbstractCompiler compiler, - boolean shouldTraverseFunctions, boolean edgeAnnotations) { + boolean shouldTraverseFunctionsAndClasses, boolean edgeAnnotations) { this.compiler = compiler; - this.shouldTraverseFunctions = shouldTraverseFunctions; + this.shouldTraverseFunctionsAndClasses = shouldTraverseFunctionsAndClasses; this.edgeAnnotations = edgeAnnotations; } @@ -163,7 +161,7 @@ public void process(Node externs, Node root) { DiGraphNode entry = cfg.getEntry(); prioritizeFromEntryNode(entry); - if (shouldTraverseFunctions) { + if (shouldTraverseFunctionsAndClasses) { // If we're traversing inner functions, we need to rank the // priority of them too. for (DiGraphNode candidate : cfg.getDirectedGraphNodes()) { @@ -219,8 +217,10 @@ public boolean shouldTraverse( astPosition.put(n, astPositionCounter++); switch (n.getType()) { + case Token.CLASS: + return shouldTraverseFunctionsAndClasses; case Token.FUNCTION: - if (shouldTraverseFunctions || n == cfg.getEntry().getValue()) { + if (shouldTraverseFunctionsAndClasses || n == cfg.getEntry().getValue()) { exceptionHandler.push(n); return true; } diff --git a/test/com/google/javascript/jscomp/CheckUnreachableCodeTest.java b/test/com/google/javascript/jscomp/CheckUnreachableCodeTest.java index 76da35afb36..c8fa6a35254 100644 --- a/test/com/google/javascript/jscomp/CheckUnreachableCodeTest.java +++ b/test/com/google/javascript/jscomp/CheckUnreachableCodeTest.java @@ -297,7 +297,22 @@ public void testReturnsInShorthandFunctionOfObjLit() { "}}")); } + public void testObjLit() { + setAcceptedLanguage(LanguageMode.ECMASCRIPT6); + assertUnreachable("var a = {c(){if(true){return;}x = 1;}};"); + } + public void testClass() { + setAcceptedLanguage(LanguageMode.ECMASCRIPT6); + testSame("class C{func(){}}"); + assertUnreachable("class C{func(){if (true){return;} else {return;}}}"); + assertUnreachable("class C{func(){if (true){return;} x = 1;}}"); + testSame("var C = class{func(){}}"); + testSame("let C = class{func(){}}"); + testSame("var C; C = class{func(){}}"); + testSame("let C; C = class{func(){}}"); + assertUnreachable("var C = class{func(){if (true){return;} x = 1;}}"); + } private void assertUnreachable(String js) { test(js, js, null, CheckUnreachableCode.UNREACHABLE_CODE);