Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2006 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
20 import com.google.javascript.rhino.IR;
21 import com.google.javascript.rhino.JSDocInfo;
22 import com.google.javascript.rhino.Node;
23 import com.google.javascript.rhino.Token;
24
25 import java.util.HashSet;
26 import java.util.Set;
27
28 /**
29 * Removes try catch finally blocks from a parse tree for easier debugging
30 * (these statements impact both debugging in IE and sometimes even in FF).
31 *
32 */
33 class RemoveTryCatch implements CompilerPass {
34 private final AbstractCompiler compiler;
35 private final Set<Node> tryNodesContainingReturnStatements;
36
37 RemoveTryCatch(AbstractCompiler compiler) {
38 this.compiler = compiler;
39 this.tryNodesContainingReturnStatements = new HashSet<Node>();
40 }
41
42 /**
43 * Do all processing on the root node.
44 */
45 @Override
46 public void process(Node externs, Node root) {
47 NodeTraversal.traverse(compiler, root, new RemoveTryCatchCode());
48 }
49
50 private class RemoveTryCatchCode extends AbstractPostOrderCallback {
51 @Override
52 public void visit(NodeTraversal t, Node n, Node parent) {
53 switch (n.getType()) {
54 case Token.TRY:
55 // Ignore the try statement if it has the @preserveTry annotation
56 // (for expected exceptions).
57 JSDocInfo info = n.getJSDocInfo();
58 if (info != null && info.shouldPreserveTry()) {
59 return;
60 }
61
62 Node tryBlock = n.getFirstChild();
63 Node catchBlock = tryBlock.getNext(); // may be null or empty
64 Node finallyBlock = catchBlock != null ? catchBlock.getNext() : null;
65
66 // Ignore the try statement if it has a finally part and the try
67 // block contains an early return.
68 if (finallyBlock != null &&
69 tryNodesContainingReturnStatements.contains(n)) {
70 return;
71 }
72
73 // Redeclare vars declared in the catch node to be removed.
74 if (catchBlock.hasOneChild()) {
75 NodeUtil.redeclareVarsInsideBranch(catchBlock);
76 }
77
78 // Disconnect the try/catch/finally nodes from the parent
79 // and each other.
80 n.detachChildren();
81
82 // try node
83 Node block;
84 if (!NodeUtil.isStatementBlock(parent)) {
85 block = IR.block();
86 parent.replaceChild(n, block);
87 block.addChildToFront(tryBlock);
88 } else {
89 parent.replaceChild(n, tryBlock);
90 block = parent;
91 }
92
93 // finally node
94 if (finallyBlock != null) {
95 block.addChildAfter(finallyBlock, tryBlock);
96 }
97 compiler.reportCodeChange();
98 break;
99
100 case Token.RETURN:
101 for (Node anc = parent;
102 anc != null && !anc.isFunction();
103 anc = anc.getParent()) {
104 if (anc.isTry()) {
105 tryNodesContainingReturnStatements.add(anc);
106 break;
107 }
108 }
109 break;
110 }
111 }
112 }
113 }