| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 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 |
|
| 99 |
str = str.replaceAll("::this\\.", "."); |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
str = str.replaceAll("\\.\\.", "."); |
| 104 |
|
| 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 |
} |