Skip to content

Commit

Permalink
Delete InstrumentationOption.ALL, there is no way to configure the co…
Browse files Browse the repository at this point in the history
…mpiler to use this option (and there happens to be a bug in one of the code branches related to this option).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=202414606
  • Loading branch information
lukesandberg authored and brad4d committed Jun 28, 2018
1 parent 43ec254 commit 563cb7e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
43 changes: 28 additions & 15 deletions src/com/google/javascript/jscomp/CoverageInstrumentationPass.java
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkState;

import com.google.common.annotations.GwtIncompatible;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node;
import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -38,7 +39,6 @@ class CoverageInstrumentationPass implements CompilerPass {
private final InstrumentOption instrumentOption;

public enum InstrumentOption {
ALL, // Instrument to collect both line coverage and branch coverage.
LINE_ONLY, // Collect coverage for every executable statement.
BRANCH_ONLY // Collect coverage for control-flow branches.
}
Expand Down Expand Up @@ -101,24 +101,37 @@ public void process(Node externsNode, Node rootNode) {

private Node createConditionalObjectDecl(String name, Node srcref) {
// Make sure to quote properties so they are not renamed.
String jscovData;
if (instrumentOption == InstrumentOption.BRANCH_ONLY) {
jscovData = "{'fileNames':[], 'branchPresent':[], 'branchesInLine': [], 'branchesTaken': []}";
} else if (instrumentOption == InstrumentOption.LINE_ONLY) {
jscovData = "{'fileNames':[], 'instrumentedLines': [], 'executedLines': []}";
} else {
jscovData =
"{'fileNames:[], 'instrumentedLines: [], 'executedLines': [],"
+ " 'branchPresent':[], 'branchesInLine': [], 'branchesTaken': []}";
Node jscovData;
switch (instrumentOption) {
case BRANCH_ONLY:
jscovData =
IR.objectlit(
IR.quotedStringKey("fileNames", IR.arraylit()),
IR.quotedStringKey("branchPresent", IR.arraylit()),
IR.quotedStringKey("branchesInLine", IR.arraylit()),
IR.quotedStringKey("branchesTaken", IR.arraylit()));
break;
case LINE_ONLY:
jscovData =
IR.objectlit(
IR.quotedStringKey("fileNames", IR.arraylit()),
IR.quotedStringKey("instrumentedLines", IR.arraylit()),
IR.quotedStringKey("executedLines", IR.arraylit()));
break;
default:
throw new AssertionError("Unexpected option: " + instrumentOption);
}

// Add the __jscov var to the window as a quoted key so it can be found even if property
// renaming is enabled.
String jscovDecl =
" var " + name + " = window.top['__jscov'] || (window.top['__jscov'] = " + jscovData + ");";

Node script = compiler.parseSyntheticCode(jscovDecl);
Node var = script.removeFirstChild();
Node var =
IR.var(
IR.name(name),
IR.or(
IR.getelem(IR.getprop(IR.name("window"), "top"), IR.string("__jscov")),
IR.assign(
IR.getelem(IR.getprop(IR.name("window"), "top"), IR.string("__jscov")),
jscovData)));
return var.useSourceInfoIfMissingFromForTree(srcref);
}
}
8 changes: 6 additions & 2 deletions src/com/google/javascript/rhino/IR.java
Expand Up @@ -573,8 +573,6 @@ public static Node computedProp(Node key, Node value) {
return new Node(Token.COMPUTED_PROP, key, value);
}

// TODO(johnlenz): quoted props

public static Node propdef(Node string, Node value) {
checkState(string.isStringKey());
checkState(!string.hasChildren());
Expand Down Expand Up @@ -618,6 +616,12 @@ public static Node stringKey(String s, Node value) {
return stringKey;
}

public static Node quotedStringKey(String s, Node value) {
Node k = stringKey(s, value);
k.putBooleanProp(Node.QUOTED_PROP, true);
return k;
}

public static Node rest(Node target) {
checkState(target.isValidAssignmentTarget(), target);
return new Node(Token.REST, target);
Expand Down

0 comments on commit 563cb7e

Please sign in to comment.