| 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.ImmutableMap; |
| 20 |
import com.google.common.collect.Maps; |
| 21 |
import com.google.javascript.rhino.Node; |
| 22 |
import com.google.javascript.rhino.Token; |
| 23 |
|
| 24 |
import java.util.*; |
| 25 |
import java.util.logging.*; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class NameAnonymousFunctionsMapped implements CompilerPass { |
| 44 |
|
| 45 |
private static Logger logger = Logger.getLogger( |
| 46 |
NameAnonymousFunctionsMapped.class.getName()); |
| 47 |
|
| 48 |
static final char PREFIX = '$'; |
| 49 |
static final String PREFIX_STRING = "$"; |
| 50 |
|
| 51 |
private final AbstractCompiler compiler; |
| 52 |
private final NameGenerator nameGenerator; |
| 53 |
private final VariableMap previousMap; |
| 54 |
private final Map<String, String> renameMap; |
| 55 |
|
| 56 |
private int namedCount = 0; |
| 57 |
private int bytesUsed = 0; |
| 58 |
|
| 59 |
NameAnonymousFunctionsMapped( |
| 60 |
AbstractCompiler compiler, VariableMap previousMap) { |
| 61 |
this.compiler = compiler; |
| 62 |
Set<String> reserved = |
| 63 |
previousMap != null ? |
| 64 |
previousMap.getNewNameToOriginalNameMap().keySet() : |
| 65 |
Collections.<String>emptySet(); |
| 66 |
this.nameGenerator = new NameGenerator(reserved, PREFIX_STRING, null); |
| 67 |
this.previousMap = previousMap; |
| 68 |
this.renameMap = Maps.newHashMap(); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void process(Node externs, Node root) { |
| 73 |
AnonymousFunctionNamingCallback namingCallback = |
| 74 |
new AnonymousFunctionNamingCallback(new MappedFunctionNamer()); |
| 75 |
NodeTraversal.traverse(compiler, root, namingCallback); |
| 76 |
logger.fine("Named " + namedCount + " anon functions using " + |
| 77 |
bytesUsed + " bytes"); |
| 78 |
if (namedCount > 0) { |
| 79 |
compiler.reportCodeChange(); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
private class MappedFunctionNamer |
| 90 |
implements AnonymousFunctionNamingCallback.FunctionNamer { |
| 91 |
static final char DELIMITER = '.'; |
| 92 |
|
| 93 |
@Override |
| 94 |
public final String getName(Node node) { |
| 95 |
switch (node.getType()) { |
| 96 |
case Token.NAME: |
| 97 |
case Token.STRING: |
| 98 |
case Token.STRING_KEY: |
| 99 |
return node.getString(); |
| 100 |
default: |
| 101 |
return new CodePrinter.Builder(node).build(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public final void setFunctionName(String name, Node fnNode) { |
| 107 |
Node fnNameNode = fnNode.getFirstChild(); |
| 108 |
String newName = getAlternateName(name); |
| 109 |
fnNameNode.setString(newName); |
| 110 |
namedCount++; |
| 111 |
bytesUsed += newName.length(); |
| 112 |
} |
| 113 |
|
| 114 |
String getAlternateName(String name) { |
| 115 |
String newName = renameMap.get(name); |
| 116 |
if (newName == null) { |
| 117 |
|
| 118 |
if (previousMap != null) { |
| 119 |
newName = previousMap.lookupNewName(name); |
| 120 |
} |
| 121 |
if (newName == null) { |
| 122 |
|
| 123 |
newName = nameGenerator.generateNextName(); |
| 124 |
} |
| 125 |
renameMap.put(name, newName); |
| 126 |
} |
| 127 |
return newName; |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public final String getCombinedName(String lhs, String rhs) { |
| 132 |
return lhs + DELIMITER + rhs; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
VariableMap getFunctionMap() { |
| 142 |
return new VariableMap(ImmutableMap.copyOf(renameMap)); |
| 143 |
} |
| 144 |
} |