Skip to content

Commit d0438e8

Browse files
committed
Addressed PR Comments
1 parent 5457f24 commit d0438e8

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

src/com/google/javascript/jscomp/ProductionCoverageInstrumentationCallback.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,9 @@ final class ProductionCoverageInstrumentationCallback
6565
boolean visitedInstrumentCodeFile = false;
6666

6767
private enum Type {
68-
FUNCTION("Type.FUNCTION"),
69-
BRANCH("Type.BRANCH"),
70-
BRANCH_DEFAULT("Type.BRANCH_DEFAULT");
71-
72-
private String value;
73-
74-
Type(String s){
75-
this.value = s;
76-
}
68+
FUNCTION(),
69+
BRANCH(),
70+
BRANCH_DEFAULT();
7771
}
7872

7973
/**
@@ -130,29 +124,34 @@ public void visit(NodeTraversal traversal, Node node, Node parent) {
130124
// If the function node has been visited by visit() then we can be assured that all its
131125
// children nodes have been visited and properly instrumented.
132126
functionNameStack.pop();
133-
instrumentBlockNode(node.getLastChild(), fileName, functionName, Type.FUNCTION.value);
127+
instrumentBlockNode(node.getLastChild(), fileName, functionName, Type.FUNCTION);
134128
break;
135129
case IF:
136130
Node ifTrueNode = node.getSecondChild();
137-
instrumentBlockNode(ifTrueNode, sourceFileName, functionName, Type.BRANCH.value);
131+
instrumentBlockNode(ifTrueNode, sourceFileName, functionName, Type.BRANCH);
138132
if (node.getChildCount() == 2) {
139133
addElseBlock(node);
140134
}
141135
Node ifFalseNode = node.getLastChild();
136+
// The compiler converts all sets of If-Else if-Else blocks into a combination of nested
137+
// If-Else blocks. This case checks if the else blocks first child is an If statement, and
138+
// if it is it will not instrument. This avoids adding multiple instrumentation calls.
139+
// Since we also make sure an Else case is added to every If statement, we are still
140+
// assured that the else statement is being reached through a later instrumentation call.
142141
if (NodeUtil.isEmptyBlock(ifFalseNode)
143142
|| (ifFalseNode.getFirstChild() != null && !ifFalseNode.getFirstChild().isIf())) {
144-
instrumentBlockNode(ifFalseNode, sourceFileName, functionName, Type.BRANCH_DEFAULT.value);
143+
instrumentBlockNode(ifFalseNode, sourceFileName, functionName, Type.BRANCH_DEFAULT);
145144
}
146145
break;
147146
case SWITCH:
148147
boolean hasDefaultCase = false;
149148
for (Node c = node.getSecondChild(); c != null; c = c.getNext()) {
150149
if (c.isDefaultCase()) {
151150
instrumentBlockNode(
152-
c.getLastChild(), sourceFileName, functionName, Type.BRANCH_DEFAULT.value);
151+
c.getLastChild(), sourceFileName, functionName, Type.BRANCH_DEFAULT);
153152
hasDefaultCase = true;
154153
} else {
155-
instrumentBlockNode(c.getLastChild(), sourceFileName, functionName, Type.BRANCH.value);
154+
instrumentBlockNode(c.getLastChild(), sourceFileName, functionName, Type.BRANCH);
156155
}
157156
}
158157
if (!hasDefaultCase) {
@@ -161,17 +160,17 @@ public void visit(NodeTraversal traversal, Node node, Node parent) {
161160
Node defaultCase = IR.defaultCase(defaultBlock).useSourceInfoIfMissingFromForTree(node);
162161
node.addChildToBack(defaultCase);
163162
instrumentBlockNode(
164-
defaultBlock, sourceFileName, functionName, Type.BRANCH_DEFAULT.value);
163+
defaultBlock, sourceFileName, functionName, Type.BRANCH_DEFAULT);
165164
}
166165
break;
167166
case HOOK:
168167
Node ifTernaryIsTrueExpression = node.getSecondChild();
169168
Node ifTernaryIsFalseExpression = node.getLastChild();
170169

171170
addInstrumentationNodeWithComma(
172-
ifTernaryIsTrueExpression, sourceFileName, functionName, Type.BRANCH.value);
171+
ifTernaryIsTrueExpression, sourceFileName, functionName, Type.BRANCH);
173172
addInstrumentationNodeWithComma(
174-
ifTernaryIsFalseExpression, sourceFileName, functionName, Type.BRANCH.value);
173+
ifTernaryIsFalseExpression, sourceFileName, functionName, Type.BRANCH);
175174

176175
compiler.reportChangeToEnclosingScope(node);
177176
break;
@@ -183,15 +182,15 @@ public void visit(NodeTraversal traversal, Node node, Node parent) {
183182
// already been instrumented.
184183
Node secondExpression = node.getLastChild();
185184
addInstrumentationNodeWithComma(
186-
secondExpression, sourceFileName, functionName, Type.BRANCH.value);
185+
secondExpression, sourceFileName, functionName, Type.BRANCH);
187186

188187
compiler.reportChangeToEnclosingScope(node);
189188
break;
190189
default:
191190
if (NodeUtil.isLoopStructure(node)) {
192191
Node blockNode = NodeUtil.getLoopCodeBlock(node);
193192
checkNotNull(blockNode);
194-
instrumentBlockNode(blockNode, sourceFileName, functionName, Type.BRANCH.value);
193+
instrumentBlockNode(blockNode, sourceFileName, functionName, Type.BRANCH);
195194
}
196195
}
197196
}
@@ -201,7 +200,7 @@ public void visit(NodeTraversal traversal, Node node, Node parent) {
201200
* original node using a COMMA node.
202201
*/
203202
private void addInstrumentationNodeWithComma(
204-
Node originalNode, String fileName, String functionName, String type) {
203+
Node originalNode, String fileName, String functionName, Type type) {
205204
Node parentNode = originalNode.getParent();
206205
Node cloneOfOriginal = originalNode.cloneTree();
207206
Node newInstrumentationNode =
@@ -224,7 +223,7 @@ private void addInstrumentationNodeWithComma(
224223
* @param fnName The function name of the node being instrumented.
225224
* @param type The type of the node being instrumented.
226225
*/
227-
private void instrumentBlockNode(Node block, String fileName, String fnName, String type) {
226+
private void instrumentBlockNode(Node block, String fileName, String fnName, Type type) {
228227
block.addChildToFront(newInstrumentationNode(block, fileName, fnName, type));
229228
compiler.reportChangeToEnclosingScope(block);
230229
}
@@ -242,7 +241,7 @@ private void instrumentBlockNode(Node block, String fileName, String fnName, Str
242241
* @param type The type of the node being instrumented.
243242
* @return The newly constructed function call node.
244243
*/
245-
private Node newInstrumentationNode(Node node, String fileName, String fnName, String type) {
244+
private Node newInstrumentationNode(Node node, String fileName, String fnName, Type type) {
246245

247246
String encodedParam = parameterMapping.getEncodedParam(fileName, fnName, type);
248247

@@ -312,18 +311,18 @@ private static final class ParameterMapping {
312311
typeToIndex = new LinkedHashMap<>();
313312
}
314313

315-
private String getEncodedParam(String fileName, String functionName, String type) {
314+
private String getEncodedParam(String fileName, String functionName, Type type) {
316315

317316
fileNameToIndex.putIfAbsent(fileName, fileNameToIndex.size());
318317
functionNameToIndex.putIfAbsent(functionName, functionNameToIndex.size());
319-
typeToIndex.putIfAbsent(type, typeToIndex.size());
318+
typeToIndex.putIfAbsent(type.name(), typeToIndex.size());
320319

321320
StringBuilder sb = new StringBuilder();
322321

323322
try {
324323
Base64VLQ.encode(sb, fileNameToIndex.get(fileName));
325324
Base64VLQ.encode(sb, functionNameToIndex.get(functionName));
326-
Base64VLQ.encode(sb, typeToIndex.get(type));
325+
Base64VLQ.encode(sb, typeToIndex.get(type.name()));
327326
} catch (IOException e) {
328327
throw new AssertionError(e);
329328
}

0 commit comments

Comments
 (0)