| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.google.javascript.jscomp; |
| 17 |
|
| 18 |
import com.google.common.collect.Iterables; |
| 19 |
import com.google.common.collect.Sets; |
| 20 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 21 |
import com.google.javascript.rhino.Node; |
| 22 |
import java.util.Comparator; |
| 23 |
import java.util.TreeSet; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class RecordFunctionInformation extends AbstractPostOrderCallback |
| 30 |
implements CompilerPass { |
| 31 |
private final Compiler compiler; |
| 32 |
private final FunctionNames functionNames; |
| 33 |
private final JSModuleGraph moduleGraph; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
private final FunctionInformationMap.Builder mapBuilder; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
RecordFunctionInformation(Compiler compiler, |
| 47 |
FunctionNames functionNames) { |
| 48 |
this.compiler = compiler; |
| 49 |
this.moduleGraph = compiler.getModuleGraph(); |
| 50 |
this.functionNames = functionNames; |
| 51 |
this.mapBuilder = FunctionInformationMap.newBuilder(); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
FunctionInformationMap getMap() { |
| 58 |
return mapBuilder.build(); |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void process(Node externs, Node root) { |
| 63 |
NodeTraversal.traverse(compiler, root, this); |
| 64 |
|
| 65 |
if (moduleGraph == null) { |
| 66 |
addModuleInformation(null); |
| 67 |
} else { |
| 68 |
|
| 69 |
TreeSet<JSModule> modules = Sets.newTreeSet(new Comparator<JSModule>() { |
| 70 |
@Override |
| 71 |
public int compare(JSModule o1, JSModule o2) { |
| 72 |
return o1.getName().compareTo(o2.getName()); |
| 73 |
} |
| 74 |
}); |
| 75 |
Iterables.addAll(modules, moduleGraph.getAllModules()); |
| 76 |
for (JSModule m : modules) { |
| 77 |
addModuleInformation(m); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 84 |
if (!n.isFunction()) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
int id = functionNames.getFunctionId(n); |
| 89 |
if (id < 0) { |
| 90 |
|
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
String compiledSource = compiler.toSource(n); |
| 95 |
JSModule module = t.getModule(); |
| 96 |
mapBuilder.addEntry(FunctionInformationMap.Entry.newBuilder() |
| 97 |
.setId(id) |
| 98 |
.setSourceName(NodeUtil.getSourceName(n)) |
| 99 |
.setLineNumber(n.getLineno()) |
| 100 |
.setModuleName(moduleGraph == null ? "" : module.getName()) |
| 101 |
.setSize(compiledSource.length()) |
| 102 |
.setName(functionNames.getFunctionName(n)) |
| 103 |
.setCompiledSource(compiledSource).build()); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
private void addModuleInformation(JSModule module) { |
| 118 |
String name; |
| 119 |
String source; |
| 120 |
if (module != null) { |
| 121 |
name = module.getName(); |
| 122 |
source = compiler.toSource(module); |
| 123 |
} else { |
| 124 |
name = ""; |
| 125 |
source = compiler.toSource(); |
| 126 |
} |
| 127 |
|
| 128 |
mapBuilder.addModule(FunctionInformationMap.Module.newBuilder() |
| 129 |
.setName(name) |
| 130 |
.setCompiledSource(source).build()); |
| 131 |
} |
| 132 |
} |