| 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.base.Predicate; |
| 21 |
import com.google.common.collect.HashMultimap; |
| 22 |
import com.google.common.collect.ImmutableList; |
| 23 |
import com.google.common.collect.ImmutableMap; |
| 24 |
import com.google.common.collect.ImmutableSet; |
| 25 |
import com.google.common.collect.Iterables; |
| 26 |
import com.google.common.collect.Lists; |
| 27 |
import com.google.common.collect.Maps; |
| 28 |
import com.google.common.collect.Multimap; |
| 29 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 30 |
import com.google.javascript.rhino.IR; |
| 31 |
import com.google.javascript.rhino.Node; |
| 32 |
import com.google.javascript.rhino.Token; |
| 33 |
import com.google.javascript.rhino.jstype.JSType; |
| 34 |
import com.google.javascript.rhino.jstype.JSTypeRegistry; |
| 35 |
|
| 36 |
import java.util.Collection; |
| 37 |
import java.util.List; |
| 38 |
import java.util.Map; |
| 39 |
import java.util.Set; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
class ReplaceStrings extends AbstractPostOrderCallback |
| 51 |
implements CompilerPass { |
| 52 |
static final DiagnosticType BAD_REPLACEMENT_CONFIGURATION = |
| 53 |
DiagnosticType.warning( |
| 54 |
"JSC_BAD_REPLACEMENT_CONFIGURATION", |
| 55 |
"Bad replacement configuration."); |
| 56 |
|
| 57 |
private final String DEFAULT_PLACEHOLDER_TOKEN = "`"; |
| 58 |
private final String placeholderToken; |
| 59 |
private static final String REPLACE_ONE_MARKER = "?"; |
| 60 |
private static final String REPLACE_ALL_MARKER = "*"; |
| 61 |
|
| 62 |
private final AbstractCompiler compiler; |
| 63 |
private final JSTypeRegistry registry; |
| 64 |
|
| 65 |
|
| 66 |
private final Map<String, Config> functions = Maps.newHashMap(); |
| 67 |
private final Multimap<String, String> methods = HashMultimap.create(); |
| 68 |
private final NameGenerator nameGenerator; |
| 69 |
private final Map<String, Result> results = Maps.newLinkedHashMap(); |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
private class Config { |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
final String name; |
| 79 |
final int parameter; |
| 80 |
static final int REPLACE_ALL_VALUE = 0; |
| 81 |
|
| 82 |
Config(String name, int parameter) { |
| 83 |
this.name = name; |
| 84 |
this.parameter = parameter; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
class Result { |
| 92 |
|
| 93 |
|
| 94 |
public final String original; |
| 95 |
public final String replacement; |
| 96 |
public final List<Location> replacementLocations = Lists.newLinkedList(); |
| 97 |
|
| 98 |
Result(String original, String replacement) { |
| 99 |
this.original = original; |
| 100 |
this.replacement = replacement; |
| 101 |
} |
| 102 |
|
| 103 |
void addLocation(Node n) { |
| 104 |
replacementLocations.add(new Location( |
| 105 |
n.getSourceFileName(), |
| 106 |
n.getLineno(), n.getCharno())); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
class Location { |
| 112 |
public final String sourceFile; |
| 113 |
public final int line; |
| 114 |
public final int column; |
| 115 |
Location(String sourceFile, int line, int column) { |
| 116 |
this.sourceFile = sourceFile; |
| 117 |
this.line = line; |
| 118 |
this.column = column; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
ReplaceStrings( |
| 135 |
AbstractCompiler compiler, String placeholderToken, |
| 136 |
List<String> functionsToInspect, |
| 137 |
Set<String> blacklisted, |
| 138 |
VariableMap previousMappings) { |
| 139 |
this.compiler = compiler; |
| 140 |
this.placeholderToken = placeholderToken.isEmpty() |
| 141 |
? DEFAULT_PLACEHOLDER_TOKEN : placeholderToken; |
| 142 |
this.registry = compiler.getTypeRegistry(); |
| 143 |
|
| 144 |
Iterable<String> reservedNames = blacklisted; |
| 145 |
if (previousMappings != null) { |
| 146 |
Set<String> previous = |
| 147 |
previousMappings.getOriginalNameToNewNameMap().keySet(); |
| 148 |
reservedNames = Iterables.concat(blacklisted, previous); |
| 149 |
initMapping(previousMappings, blacklisted); |
| 150 |
} |
| 151 |
this.nameGenerator = createNameGenerator(reservedNames); |
| 152 |
|
| 153 |
|
| 154 |
parseConfiguration(functionsToInspect); |
| 155 |
} |
| 156 |
|
| 157 |
private void initMapping( |
| 158 |
VariableMap previousVarMap, Set<String> reservedNames) { |
| 159 |
Map<String,String> previous = previousVarMap.getOriginalNameToNewNameMap(); |
| 160 |
for (Map.Entry<String,String> entry : previous.entrySet()) { |
| 161 |
String key = entry.getKey(); |
| 162 |
if (!reservedNames.contains(key)) { |
| 163 |
String value = entry.getValue(); |
| 164 |
results.put(value, new Result(value, key)); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
static final Predicate<Result> USED_RESULTS = new Predicate<Result>() { |
| 170 |
@Override |
| 171 |
public boolean apply(Result result) { |
| 172 |
|
| 173 |
|
| 174 |
return !result.replacementLocations.isEmpty(); |
| 175 |
} |
| 176 |
}; |
| 177 |
|
| 178 |
|
| 179 |
List<Result> getResult() { |
| 180 |
return ImmutableList.copyOf( |
| 181 |
Iterables.filter(results.values(), USED_RESULTS)); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
VariableMap getStringMap() { |
| 186 |
ImmutableMap.Builder<String, String> map = ImmutableMap.builder(); |
| 187 |
for (Result result : Iterables.filter(results.values(), USED_RESULTS)) { |
| 188 |
map.put(result.replacement, result.original); |
| 189 |
} |
| 190 |
|
| 191 |
VariableMap stringMap = new VariableMap(map.build()); |
| 192 |
return stringMap; |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
public void process(Node externs, Node root) { |
| 197 |
NodeTraversal.traverse(compiler, root, this); |
| 198 |
} |
| 199 |
|
| 200 |
@Override |
| 201 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 202 |
|
| 203 |
|
| 204 |
switch (n.getType()) { |
| 205 |
case Token.NEW: |
| 206 |
case Token.CALL: |
| 207 |
Node calledFn = n.getFirstChild(); |
| 208 |
|
| 209 |
|
| 210 |
String name = calledFn.getQualifiedName(); |
| 211 |
if (name != null) { |
| 212 |
Config config = findMatching(name); |
| 213 |
if (config != null) { |
| 214 |
doSubstitutions(t, config, n); |
| 215 |
return; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
if (NodeUtil.isGet(calledFn)) { |
| 221 |
Node rhs = calledFn.getLastChild(); |
| 222 |
if (rhs.isName() || rhs.isString()) { |
| 223 |
String methodName = rhs.getString(); |
| 224 |
Collection<String> classes = methods.get(methodName); |
| 225 |
if (classes != null) { |
| 226 |
Node lhs = calledFn.getFirstChild(); |
| 227 |
if (lhs.getJSType() != null) { |
| 228 |
JSType type = lhs.getJSType().restrictByNotNullOrUndefined(); |
| 229 |
Config config = findMatchingClass(type, classes); |
| 230 |
if (config != null) { |
| 231 |
doSubstitutions(t, config, n); |
| 232 |
return; |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
break; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
private Config findMatching(String name) { |
| 247 |
Config config = functions.get(name); |
| 248 |
if (config == null) { |
| 249 |
name = name.replace('$', '.'); |
| 250 |
config = functions.get(name); |
| 251 |
} |
| 252 |
return config; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
private Config findMatchingClass( |
| 260 |
JSType callClassType, Collection<String> declarationNames) { |
| 261 |
if (!callClassType.isNoObjectType() && !callClassType.isUnknownType()) { |
| 262 |
for (String declarationName : declarationNames) { |
| 263 |
String className = getClassFromDeclarationName(declarationName); |
| 264 |
JSType methodClassType = registry.getType(className); |
| 265 |
if (methodClassType != null |
| 266 |
&& callClassType.isSubtype(methodClassType)) { |
| 267 |
return functions.get(declarationName); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
return null; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
private void doSubstitutions(NodeTraversal t, Config config, Node n) { |
| 278 |
Preconditions.checkState( |
| 279 |
n.isNew() || n.isCall()); |
| 280 |
|
| 281 |
if (config.parameter != Config.REPLACE_ALL_VALUE) { |
| 282 |
|
| 283 |
Node arg = n.getChildAtIndex(config.parameter); |
| 284 |
if (arg != null) { |
| 285 |
replaceExpression(t, arg, n); |
| 286 |
} |
| 287 |
} else { |
| 288 |
|
| 289 |
Node firstParam = n.getFirstChild().getNext(); |
| 290 |
for (Node arg = firstParam; arg != null; arg = arg.getNext()) { |
| 291 |
arg = replaceExpression(t, arg, n); |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
private Node replaceExpression(NodeTraversal t, Node expr, Node parent) { |
| 306 |
Node replacement; |
| 307 |
String key = null; |
| 308 |
String replacementString; |
| 309 |
switch (expr.getType()) { |
| 310 |
case Token.STRING: |
| 311 |
key = expr.getString(); |
| 312 |
replacementString = getReplacement(key); |
| 313 |
replacement = IR.string(replacementString); |
| 314 |
break; |
| 315 |
case Token.ADD: |
| 316 |
StringBuilder keyBuilder = new StringBuilder(); |
| 317 |
Node keyNode = IR.string(""); |
| 318 |
replacement = buildReplacement(expr, keyNode, keyBuilder); |
| 319 |
key = keyBuilder.toString(); |
| 320 |
replacementString = getReplacement(key); |
| 321 |
keyNode.setString(replacementString); |
| 322 |
break; |
| 323 |
case Token.NAME: |
| 324 |
|
| 325 |
Scope.Var var = t.getScope().getVar(expr.getString()); |
| 326 |
if (var != null && var.isConst()) { |
| 327 |
Node value = var.getInitialValue(); |
| 328 |
if (value != null && value.isString()) { |
| 329 |
key = value.getString(); |
| 330 |
replacementString = getReplacement(key); |
| 331 |
replacement = IR.string(replacementString); |
| 332 |
break; |
| 333 |
} |
| 334 |
} |
| 335 |
return expr; |
| 336 |
default: |
| 337 |
|
| 338 |
|
| 339 |
return expr; |
| 340 |
} |
| 341 |
|
| 342 |
Preconditions.checkNotNull(key); |
| 343 |
Preconditions.checkNotNull(replacementString); |
| 344 |
recordReplacement(expr, key); |
| 345 |
|
| 346 |
parent.replaceChild(expr, replacement); |
| 347 |
compiler.reportCodeChange(); |
| 348 |
return replacement; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
private String getReplacement(String key) { |
| 355 |
Result result = results.get(key); |
| 356 |
if (result != null) { |
| 357 |
return result.replacement; |
| 358 |
} |
| 359 |
|
| 360 |
String replacement = nameGenerator.generateNextName(); |
| 361 |
result = new Result(key, replacement); |
| 362 |
results.put(key, result); |
| 363 |
return replacement; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
private void recordReplacement(Node n, String key) { |
| 370 |
Result result = results.get(key); |
| 371 |
Preconditions.checkState(result != null); |
| 372 |
|
| 373 |
result.addLocation(n); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
private Node buildReplacement( |
| 390 |
Node expr, Node prefix, StringBuilder keyBuilder) { |
| 391 |
switch (expr.getType()) { |
| 392 |
case Token.ADD: |
| 393 |
Node left = expr.getFirstChild(); |
| 394 |
Node right = left.getNext(); |
| 395 |
prefix = buildReplacement(left, prefix, keyBuilder); |
| 396 |
return buildReplacement(right, prefix, keyBuilder); |
| 397 |
case Token.STRING: |
| 398 |
keyBuilder.append(expr.getString()); |
| 399 |
return prefix; |
| 400 |
default: |
| 401 |
keyBuilder.append(placeholderToken); |
| 402 |
prefix = IR.add(prefix, IR.string(placeholderToken)); |
| 403 |
return IR.add(prefix, expr.cloneTree()); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
private String getMethodFromDeclarationName(String fullDeclarationName) { |
| 411 |
String[] parts = fullDeclarationName.split("\\.prototype\\."); |
| 412 |
Preconditions.checkState(parts.length == 1 || parts.length == 2); |
| 413 |
if (parts.length == 2) { |
| 414 |
return parts[1]; |
| 415 |
} |
| 416 |
return null; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
private String getClassFromDeclarationName(String fullDeclarationName) { |
| 423 |
String[] parts = fullDeclarationName.split("\\.prototype\\."); |
| 424 |
Preconditions.checkState(parts.length == 1 || parts.length == 2); |
| 425 |
if (parts.length == 2) { |
| 426 |
return parts[0]; |
| 427 |
} |
| 428 |
return null; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
private void parseConfiguration(List<String> functionsToInspect) { |
| 436 |
for (String function : functionsToInspect) { |
| 437 |
Config config = parseConfiguration(function); |
| 438 |
functions.put(config.name, config); |
| 439 |
|
| 440 |
String method = getMethodFromDeclarationName(config.name); |
| 441 |
if (method != null) { |
| 442 |
methods.put(method, config.name); |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
private Config parseConfiguration(String function) { |
| 457 |
|
| 458 |
int first = function.indexOf('('); |
| 459 |
int last = function.indexOf(')'); |
| 460 |
|
| 461 |
|
| 462 |
Preconditions.checkState(first != -1 && last != -1); |
| 463 |
|
| 464 |
String name = function.substring(0, first); |
| 465 |
String params = function.substring(first+1, last); |
| 466 |
|
| 467 |
int paramCount = 0; |
| 468 |
int replacementParameter = -1; |
| 469 |
String[] parts = params.split(","); |
| 470 |
for (String param : parts) { |
| 471 |
paramCount++; |
| 472 |
if (param.equals(REPLACE_ALL_MARKER)) { |
| 473 |
Preconditions.checkState(paramCount == 1 && parts.length == 1); |
| 474 |
replacementParameter = Config.REPLACE_ALL_VALUE; |
| 475 |
} else if (param.equals(REPLACE_ONE_MARKER)) { |
| 476 |
|
| 477 |
Preconditions.checkState(replacementParameter == -1); |
| 478 |
replacementParameter = paramCount; |
| 479 |
} else { |
| 480 |
|
| 481 |
Preconditions.checkState(param.isEmpty(), "Unknown marker", param); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
Preconditions.checkState(replacementParameter != -1); |
| 486 |
return new Config(name, replacementParameter); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
private static NameGenerator createNameGenerator(Iterable<String> reserved) { |
| 494 |
final String namePrefix = ""; |
| 495 |
final char[] reservedChars = new char[0]; |
| 496 |
return new NameGenerator( |
| 497 |
ImmutableSet.copyOf(reserved), namePrefix, reservedChars); |
| 498 |
} |
| 499 |
} |