Skip to content

Commit

Permalink
Upgraded ControlFlowAnalysis to handle classes (Do not traverse class…
Browse files Browse the repository at this point in the history
…) (reflected in CheckUnreachableCodeTest)

-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=99945982
  • Loading branch information
blickly committed Aug 5, 2015
1 parent 0786e26 commit 0098149
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/com/google/javascript/jscomp/ControlFlowAnalysis.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public int compare(
private int astPositionCounter; private int astPositionCounter;
private int priorityCounter; private int priorityCounter;


private final boolean shouldTraverseFunctions; private final boolean shouldTraverseFunctionsAndClasses;
private final boolean edgeAnnotations; private final boolean edgeAnnotations;


// We need to store where we started, in case we aren't doing a flow analysis // We need to store where we started, in case we aren't doing a flow analysis
Expand Down Expand Up @@ -128,15 +128,13 @@ public int compare(
* Constructor. * Constructor.
* *
* @param compiler Compiler instance. * @param compiler Compiler instance.
* @param shouldTraverseFunctions Whether functions should be traversed (true * @param shouldTraverseFunctions Whether functions should be traversed
* by default). * @param edgeAnnotations Whether to allow edge annotations.
* @param edgeAnnotations Whether to allow edge annotations. By default,
* only node annotations are allowed.
*/ */
ControlFlowAnalysis(AbstractCompiler compiler, ControlFlowAnalysis(AbstractCompiler compiler,
boolean shouldTraverseFunctions, boolean edgeAnnotations) { boolean shouldTraverseFunctionsAndClasses, boolean edgeAnnotations) {
this.compiler = compiler; this.compiler = compiler;
this.shouldTraverseFunctions = shouldTraverseFunctions; this.shouldTraverseFunctionsAndClasses = shouldTraverseFunctionsAndClasses;
this.edgeAnnotations = edgeAnnotations; this.edgeAnnotations = edgeAnnotations;
} }


Expand All @@ -163,7 +161,7 @@ public void process(Node externs, Node root) {
DiGraphNode<Node, Branch> entry = cfg.getEntry(); DiGraphNode<Node, Branch> entry = cfg.getEntry();
prioritizeFromEntryNode(entry); prioritizeFromEntryNode(entry);


if (shouldTraverseFunctions) { if (shouldTraverseFunctionsAndClasses) {
// If we're traversing inner functions, we need to rank the // If we're traversing inner functions, we need to rank the
// priority of them too. // priority of them too.
for (DiGraphNode<Node, Branch> candidate : cfg.getDirectedGraphNodes()) { for (DiGraphNode<Node, Branch> candidate : cfg.getDirectedGraphNodes()) {
Expand Down Expand Up @@ -219,8 +217,10 @@ public boolean shouldTraverse(
astPosition.put(n, astPositionCounter++); astPosition.put(n, astPositionCounter++);


switch (n.getType()) { switch (n.getType()) {
case Token.CLASS:
return shouldTraverseFunctionsAndClasses;
case Token.FUNCTION: case Token.FUNCTION:
if (shouldTraverseFunctions || n == cfg.getEntry().getValue()) { if (shouldTraverseFunctionsAndClasses || n == cfg.getEntry().getValue()) {
exceptionHandler.push(n); exceptionHandler.push(n);
return true; return true;
} }
Expand Down
15 changes: 15 additions & 0 deletions test/com/google/javascript/jscomp/CheckUnreachableCodeTest.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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) { private void assertUnreachable(String js) {
test(js, js, null, CheckUnreachableCode.UNREACHABLE_CODE); test(js, js, null, CheckUnreachableCode.UNREACHABLE_CODE);
Expand Down

0 comments on commit 0098149

Please sign in to comment.