| 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.base.Preconditions; |
| 20 |
import com.google.common.collect.BiMap; |
| 21 |
import com.google.common.collect.HashBiMap; |
| 22 |
import com.google.common.collect.ImmutableBiMap; |
| 23 |
import com.google.common.collect.Maps; |
| 24 |
import com.google.debugging.sourcemap.Base64; |
| 25 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 26 |
import com.google.javascript.rhino.IR; |
| 27 |
import com.google.javascript.rhino.JSDocInfo; |
| 28 |
import com.google.javascript.rhino.Node; |
| 29 |
|
| 30 |
import java.io.BufferedReader; |
| 31 |
import java.io.IOException; |
| 32 |
import java.io.StringReader; |
| 33 |
import java.util.Collections; |
| 34 |
import java.util.Map; |
| 35 |
import java.util.Set; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class ReplaceIdGenerators implements CompilerPass { |
| 44 |
static final DiagnosticType NON_GLOBAL_ID_GENERATOR_CALL = |
| 45 |
DiagnosticType.error( |
| 46 |
"JSC_NON_GLOBAL_ID_GENERATOR_CALL", |
| 47 |
"Id generator call must be in the global scope"); |
| 48 |
|
| 49 |
static final DiagnosticType CONDITIONAL_ID_GENERATOR_CALL = |
| 50 |
DiagnosticType.error( |
| 51 |
"JSC_CONDITIONAL_ID_GENERATOR_CALL", |
| 52 |
"Id generator call must be unconditional"); |
| 53 |
|
| 54 |
static final DiagnosticType CONFLICTING_GENERATOR_TYPE = |
| 55 |
DiagnosticType.error( |
| 56 |
"JSC_CONFLICTING_ID_GENERATOR_TYPE", |
| 57 |
"Id generator can only be one of consistent, inconsistent, or stable."); |
| 58 |
|
| 59 |
static final DiagnosticType INVALID_GENERATOR_ID_MAPPING = |
| 60 |
DiagnosticType.error( |
| 61 |
"JSC_INVALID_GENERATOR_ID_MAPPING", |
| 62 |
"Invalid generator id mapping. {0}"); |
| 63 |
|
| 64 |
private final AbstractCompiler compiler; |
| 65 |
private final Map<String, NameSupplier> nameGenerators; |
| 66 |
private final Map<String, Map<String, String>> consistNameMap; |
| 67 |
|
| 68 |
private final Map<String, Map<String, String>> idGeneratorMaps; |
| 69 |
private final Map<String, BiMap<String, String>> previousMap; |
| 70 |
|
| 71 |
private final boolean generatePseudoNames; |
| 72 |
|
| 73 |
public ReplaceIdGenerators( |
| 74 |
AbstractCompiler compiler, Set<String> idGens, |
| 75 |
boolean generatePseudoNames, |
| 76 |
String previousMapSerialized) { |
| 77 |
this.compiler = compiler; |
| 78 |
this.generatePseudoNames = generatePseudoNames; |
| 79 |
nameGenerators = Maps.newLinkedHashMap(); |
| 80 |
idGeneratorMaps = Maps.newLinkedHashMap(); |
| 81 |
consistNameMap = Maps.newLinkedHashMap(); |
| 82 |
|
| 83 |
Map<String, BiMap<String, String>> previousMap; |
| 84 |
previousMap = parsePreviousResults(previousMapSerialized); |
| 85 |
this.previousMap = previousMap; |
| 86 |
|
| 87 |
if (idGens != null) { |
| 88 |
for (String gen : idGens) { |
| 89 |
nameGenerators.put( |
| 90 |
gen, createNameSupplier(RenameStrategy.INCONSISTENT, previousMap.get(gen))); |
| 91 |
idGeneratorMaps.put(gen, Maps.<String, String>newLinkedHashMap()); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
private enum RenameStrategy { |
| 97 |
CONSISTENT, |
| 98 |
INCONSISTENT, |
| 99 |
STABLE |
| 100 |
} |
| 101 |
|
| 102 |
private static interface NameSupplier { |
| 103 |
String getName(String id, String name); |
| 104 |
RenameStrategy getRenameStrategy(); |
| 105 |
} |
| 106 |
|
| 107 |
private static class ObfuscatedNameSuppier implements NameSupplier { |
| 108 |
private final NameGenerator generator; |
| 109 |
private final Map<String, String> previousMappings; |
| 110 |
private RenameStrategy renameStrategy; |
| 111 |
|
| 112 |
public ObfuscatedNameSuppier( |
| 113 |
RenameStrategy renameStrategy, BiMap<String, String> previousMappings) { |
| 114 |
this.previousMappings = previousMappings.inverse(); |
| 115 |
this.generator = |
| 116 |
new NameGenerator(previousMappings.keySet(), "", null); |
| 117 |
this.renameStrategy = renameStrategy; |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public String getName(String id, String name) { |
| 122 |
String newName = previousMappings.get(id); |
| 123 |
if (newName == null) { |
| 124 |
newName = generator.generateNextName(); |
| 125 |
} |
| 126 |
return newName; |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public RenameStrategy getRenameStrategy() { |
| 131 |
return renameStrategy; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
private static class PseudoNameSuppier implements NameSupplier { |
| 136 |
private int counter = 0; |
| 137 |
private RenameStrategy renameStrategy; |
| 138 |
|
| 139 |
public PseudoNameSuppier(RenameStrategy renameStrategy) { |
| 140 |
this.renameStrategy = renameStrategy; |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
public String getName(String id, String name) { |
| 145 |
if (renameStrategy == RenameStrategy.INCONSISTENT) { |
| 146 |
return name + "$" + counter++; |
| 147 |
} |
| 148 |
return name + "$0"; |
| 149 |
} |
| 150 |
|
| 151 |
@Override |
| 152 |
public RenameStrategy getRenameStrategy() { |
| 153 |
return renameStrategy; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
private static class StableNameSupplier implements NameSupplier { |
| 158 |
@Override |
| 159 |
public String getName(String id, String name) { |
| 160 |
return Base64.base64EncodeInt(name.hashCode()); |
| 161 |
} |
| 162 |
@Override |
| 163 |
public RenameStrategy getRenameStrategy() { |
| 164 |
return RenameStrategy.STABLE; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
private NameSupplier createNameSupplier( |
| 169 |
RenameStrategy renameStrategy, BiMap<String, String> previousMappings) { |
| 170 |
previousMappings = previousMappings != null ? |
| 171 |
previousMappings : |
| 172 |
ImmutableBiMap.<String, String>of(); |
| 173 |
if (renameStrategy == RenameStrategy.STABLE) { |
| 174 |
return new StableNameSupplier(); |
| 175 |
} else if (generatePseudoNames) { |
| 176 |
return new PseudoNameSuppier(renameStrategy); |
| 177 |
} else { |
| 178 |
return new ObfuscatedNameSuppier(renameStrategy, previousMappings); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
private class GatherGenerators extends AbstractPostOrderCallback { |
| 183 |
|
| 184 |
@Override |
| 185 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 186 |
JSDocInfo doc = n.getJSDocInfo(); |
| 187 |
if (doc == null) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
int numGeneratorAnnotations = |
| 192 |
(doc.isConsistentIdGenerator() ? 1 : 0) + |
| 193 |
(doc.isIdGenerator() ? 1 : 0) + |
| 194 |
(doc.isStableIdGenerator() ? 1 : 0); |
| 195 |
if (numGeneratorAnnotations == 0) { |
| 196 |
return; |
| 197 |
} else if (numGeneratorAnnotations > 1) { |
| 198 |
compiler.report(t.makeError(n, CONFLICTING_GENERATOR_TYPE)); |
| 199 |
} |
| 200 |
|
| 201 |
String name = null; |
| 202 |
if (n.isAssign()) { |
| 203 |
name = n.getFirstChild().getQualifiedName(); |
| 204 |
} else if (n.isVar()) { |
| 205 |
name = n.getFirstChild().getString(); |
| 206 |
} else if (n.isFunction()){ |
| 207 |
name = n.getFirstChild().getString(); |
| 208 |
if (name.isEmpty()) { |
| 209 |
return; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
if (doc.isConsistentIdGenerator()) { |
| 214 |
consistNameMap.put(name, Maps.<String, String>newLinkedHashMap()); |
| 215 |
nameGenerators.put( |
| 216 |
name, createNameSupplier(RenameStrategy.CONSISTENT, previousMap.get(name))); |
| 217 |
} else if (doc.isStableIdGenerator()) { |
| 218 |
nameGenerators.put( |
| 219 |
name, createNameSupplier(RenameStrategy.STABLE, previousMap.get(name))); |
| 220 |
} else { |
| 221 |
nameGenerators.put( |
| 222 |
name, createNameSupplier(RenameStrategy.INCONSISTENT, previousMap.get(name))); |
| 223 |
} |
| 224 |
idGeneratorMaps.put(name, Maps.<String, String>newLinkedHashMap()); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
@Override |
| 229 |
public void process(Node externs, Node root) { |
| 230 |
NodeTraversal.traverse(compiler, root, new GatherGenerators()); |
| 231 |
if (!nameGenerators.isEmpty()) { |
| 232 |
NodeTraversal.traverse(compiler, root, new ReplaceGenerators()); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
private class ReplaceGenerators extends AbstractPostOrderCallback { |
| 237 |
@Override |
| 238 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 239 |
if (!n.isCall()) { |
| 240 |
return; |
| 241 |
} |
| 242 |
|
| 243 |
String callName = n.getFirstChild().getQualifiedName(); |
| 244 |
NameSupplier nameGenerator = nameGenerators.get(callName); |
| 245 |
if (nameGenerator == null) { |
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
if (!t.inGlobalScope() && |
| 250 |
nameGenerator.getRenameStrategy() == RenameStrategy.INCONSISTENT) { |
| 251 |
|
| 252 |
compiler.report(t.makeError(n, NON_GLOBAL_ID_GENERATOR_CALL)); |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
if (nameGenerator.getRenameStrategy() == RenameStrategy.INCONSISTENT) { |
| 257 |
for (Node ancestor : n.getAncestors()) { |
| 258 |
if (NodeUtil.isControlStructure(ancestor)) { |
| 259 |
|
| 260 |
compiler.report(t.makeError(n, CONDITIONAL_ID_GENERATOR_CALL)); |
| 261 |
return; |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
Node id = n.getFirstChild().getNext(); |
| 267 |
|
| 268 |
|
| 269 |
if (!id.isString()) { |
| 270 |
return; |
| 271 |
} |
| 272 |
|
| 273 |
Map<String, String> idGeneratorMap = idGeneratorMaps.get(callName); |
| 274 |
String rename = null; |
| 275 |
|
| 276 |
String name = id.getString(); |
| 277 |
String instanceId = getIdForGeneratorNode( |
| 278 |
nameGenerator.getRenameStrategy() == RenameStrategy.CONSISTENT, id); |
| 279 |
if (nameGenerator.getRenameStrategy() == RenameStrategy.CONSISTENT) { |
| 280 |
Map<String, String> entry = consistNameMap.get(callName); |
| 281 |
rename = entry.get(instanceId); |
| 282 |
if (rename == null) { |
| 283 |
rename = nameGenerator.getName(instanceId, name); |
| 284 |
entry.put(instanceId, rename); |
| 285 |
} |
| 286 |
} else { |
| 287 |
rename = nameGenerator.getName(instanceId, name); |
| 288 |
} |
| 289 |
|
| 290 |
parent.replaceChild(n, IR.string(rename)); |
| 291 |
idGeneratorMap.put(rename, instanceId); |
| 292 |
|
| 293 |
compiler.reportCodeChange(); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
public String getSerializedIdMappings() { |
| 302 |
StringBuilder sb = new StringBuilder(); |
| 303 |
for (Map.Entry<String, Map<String, String>> replacements : |
| 304 |
idGeneratorMaps.entrySet()) { |
| 305 |
if (!replacements.getValue().isEmpty()) { |
| 306 |
sb.append("["); |
| 307 |
sb.append(replacements.getKey()); |
| 308 |
sb.append("]\n\n"); |
| 309 |
for (Map.Entry<String, String> replacement : |
| 310 |
replacements.getValue().entrySet()) { |
| 311 |
sb.append(replacement.getKey()); |
| 312 |
sb.append(':'); |
| 313 |
sb.append(replacement.getValue()); |
| 314 |
sb.append("\n"); |
| 315 |
} |
| 316 |
sb.append("\n"); |
| 317 |
} |
| 318 |
} |
| 319 |
return sb.toString(); |
| 320 |
} |
| 321 |
|
| 322 |
private Map<String, BiMap<String, String>> parsePreviousResults( |
| 323 |
String serializedMap) { |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
if (serializedMap == null || serializedMap.isEmpty()) { |
| 334 |
return Collections.emptyMap(); |
| 335 |
} |
| 336 |
|
| 337 |
Map<String, BiMap<String, String>> resultMap = Maps.newHashMap(); |
| 338 |
BufferedReader reader = new BufferedReader(new StringReader(serializedMap)); |
| 339 |
BiMap<String, String> currentSectionMap = null; |
| 340 |
|
| 341 |
String line; |
| 342 |
int lineIndex = 0; |
| 343 |
try { |
| 344 |
while ((line = reader.readLine()) != null) { |
| 345 |
lineIndex++; |
| 346 |
if (line.isEmpty()) { |
| 347 |
continue; |
| 348 |
} |
| 349 |
if (line.charAt(0) == '[') { |
| 350 |
String currentSection = line.substring(1, line.length() - 1); |
| 351 |
currentSectionMap = resultMap.get(currentSection); |
| 352 |
if (currentSectionMap == null) { |
| 353 |
currentSectionMap = HashBiMap.create(); |
| 354 |
resultMap.put(currentSection, currentSectionMap); |
| 355 |
} else { |
| 356 |
reportInvalidLine(line, lineIndex); |
| 357 |
return Collections.emptyMap(); |
| 358 |
} |
| 359 |
} else { |
| 360 |
int split = line.indexOf(':'); |
| 361 |
if (split != -1) { |
| 362 |
String name = line.substring(0, split); |
| 363 |
String location = line.substring(split + 1, line.length()); |
| 364 |
currentSectionMap.put(name, location); |
| 365 |
} else { |
| 366 |
reportInvalidLine(line, lineIndex); |
| 367 |
return Collections.emptyMap(); |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
} catch (IOException e) { |
| 372 |
JSError.make(INVALID_GENERATOR_ID_MAPPING, e.getMessage()); |
| 373 |
} |
| 374 |
return resultMap; |
| 375 |
} |
| 376 |
|
| 377 |
private void reportInvalidLine(String line, int lineIndex) { |
| 378 |
JSError.make(INVALID_GENERATOR_ID_MAPPING, |
| 379 |
"line(" + line + "): " + lineIndex); |
| 380 |
} |
| 381 |
|
| 382 |
String getIdForGeneratorNode(boolean consistent, Node n) { |
| 383 |
Preconditions.checkState(n.isString()); |
| 384 |
if (consistent) { |
| 385 |
return n.getString(); |
| 386 |
} else { |
| 387 |
return n.getSourceFileName() + ':' + n.getLineno() + ":" + n.getCharno(); |
| 388 |
} |
| 389 |
} |
| 390 |
} |