Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2008 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.common.collect.Maps;
20 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
21 import com.google.javascript.rhino.Node;
22 import java.io.Serializable;
23 import java.util.*;
24
25 /**
26 * Extract a list of all function nodes defined in a JavaScript
27 * program, assigns them globally unique ids and computes their fully
28 * qualified names. Function names are derived from the property they
29 * are assigned to and the scope they are defined in. For instance,
30 * the following code
31 *
32 * goog.widget = function(str) {
33 * this.member_fn = function() {}
34 * local_fn = function() {}
35 * goog.array.map(arr, function(){});
36 * }
37 *
38 * defines the following functions
39 *
40 * goog.widget
41 * goog.widget.member_fn
42 * goog.widget::local_fn
43 * goog.widget::<anonymous>
44 *
45 */
46
47 class FunctionNames implements CompilerPass, Serializable {
48 private static final long serialVersionUID = 1L;
49
50 private final transient AbstractCompiler compiler;
51 private final Map<Node, FunctionRecord> functionMap = Maps.newLinkedHashMap();
52 private final transient FunctionListExtractor functionListExtractor;
53
54 FunctionNames(AbstractCompiler compiler) {
55 this.compiler = compiler;
56 this.functionListExtractor = new FunctionListExtractor(functionMap);
57 }
58
59 @Override
60 public void process(Node externs, Node root) {
61 NodeTraversal.traverse(compiler, root, functionListExtractor);
62 FunctionExpressionNamer namer = new FunctionExpressionNamer(functionMap);
63 AnonymousFunctionNamingCallback namingCallback =
64 new AnonymousFunctionNamingCallback(namer);
65 NodeTraversal.traverse(compiler, root, namingCallback);
66 }
67
68 public Iterable<Node> getFunctionNodeList() {
69 return functionMap.keySet();
70 }
71
72 public int getFunctionId(Node f) {
73 FunctionRecord record = functionMap.get(f);
74 if (record != null) {
75 return record.id;
76 } else {
77 return -1;
78 }
79 }
80
81 public String getFunctionName(Node f) {
82 FunctionRecord record = functionMap.get(f);
83 if (record == null) {
84 // Function node was added during compilation and has no name.
85 return null;
86 }
87
88 String str = record.name;
89 if (str.isEmpty()) {
90 str = "<anonymous>";
91 }
92
93 Node parent = record.parent;
94 if (parent != null) {
95 str = getFunctionName(parent) + "::" + str;
96 }
97
98 // this.foo -> foo
99 str = str.replaceAll("::this\\.", ".");
100 // foo.prototype.bar -> foo.bar
101 // AnonymousFunctionNamingCallback already replaces ".prototype."
102 // with "..", just remove the extra dot.
103 str = str.replaceAll("\\.\\.", ".");
104 // remove toplevel anonymous blocks, if they exists.
105 str = str.replaceFirst("^(<anonymous>::)*", "");
106 return str;
107 }
108
109 private static class FunctionRecord implements Serializable {
110 private static final long serialVersionUID = 1L;
111
112 public final int id;
113 public final Node parent;
114 public String name;
115
116 FunctionRecord(int id, Node parent, String name) {
117 this.id = id;
118 this.parent = parent;
119 this.name = name;
120 }
121 }
122
123 private static class FunctionListExtractor extends AbstractPostOrderCallback {
124 private final Map<Node, FunctionRecord> functionMap;
125 private int nextId = 0;
126
127 FunctionListExtractor(Map<Node, FunctionRecord> functionMap) {
128 this.functionMap = functionMap;
129 }
130
131 @Override
132 public void visit(NodeTraversal t, Node n, Node parent) {
133 if (n.isFunction()) {
134 Node functionNameNode = n.getFirstChild();
135 String functionName = functionNameNode.getString();
136
137 Node enclosingFunction = t.getEnclosingFunction();
138
139 functionMap.put(n,
140 new FunctionRecord(nextId, enclosingFunction, functionName));
141 nextId++;
142 }
143 }
144 }
145
146 private static class FunctionExpressionNamer
147 implements AnonymousFunctionNamingCallback.FunctionNamer {
148 private static final char DELIMITER = '.';
149 private static final NodeNameExtractor extractor =
150 new NodeNameExtractor(DELIMITER);
151 private final Map<Node, FunctionRecord> functionMap;
152
153 FunctionExpressionNamer(Map<Node, FunctionRecord> functionMap) {
154 this.functionMap = functionMap;
155 }
156
157 @Override
158 public final String getName(Node node) {
159 return extractor.getName(node);
160 }
161
162 @Override
163 public final void setFunctionName(String name, Node fnNode) {
164 FunctionRecord record = functionMap.get(fnNode);
165 assert(record != null);
166 assert(record.name.isEmpty());
167 record.name = name;
168 }
169
170 @Override
171 public final String getCombinedName(String lhs, String rhs) {
172 return lhs + DELIMITER + rhs;
173 }
174 }
175 }