| 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.base.Preconditions; |
| 19 |
import com.google.common.collect.Lists; |
| 20 |
import com.google.common.collect.Maps; |
| 21 |
import com.google.common.collect.Sets; |
| 22 |
import com.google.javascript.jscomp.AbstractCompiler.LifeCycleStage; |
| 23 |
import com.google.javascript.jscomp.MakeDeclaredNamesUnique.BoilerplateRenamer; |
| 24 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 25 |
import com.google.javascript.jscomp.NodeTraversal.Callback; |
| 26 |
import com.google.javascript.jscomp.Scope.Var; |
| 27 |
import com.google.javascript.rhino.IR; |
| 28 |
import com.google.javascript.rhino.JSDocInfo; |
| 29 |
import com.google.javascript.rhino.Node; |
| 30 |
import com.google.javascript.rhino.Token; |
| 31 |
|
| 32 |
import java.util.Map; |
| 33 |
import java.util.Set; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
class Normalize implements CompilerPass { |
| 66 |
|
| 67 |
private final AbstractCompiler compiler; |
| 68 |
private final boolean assertOnChange; |
| 69 |
private static final boolean CONVERT_WHILE_TO_FOR = true; |
| 70 |
static final boolean MAKE_LOCAL_NAMES_UNIQUE = true; |
| 71 |
|
| 72 |
public static final DiagnosticType CATCH_BLOCK_VAR_ERROR = |
| 73 |
DiagnosticType.error( |
| 74 |
"JSC_CATCH_BLOCK_VAR_ERROR", |
| 75 |
"The use of scope variable {0} is not allowed within a catch block " + |
| 76 |
"with a catch exception of the same name."); |
| 77 |
|
| 78 |
|
| 79 |
Normalize(AbstractCompiler compiler, boolean assertOnChange) { |
| 80 |
this.compiler = compiler; |
| 81 |
this.assertOnChange = assertOnChange; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
static Node parseAndNormalizeSyntheticCode( |
| 88 |
AbstractCompiler compiler, String code, String prefix) { |
| 89 |
Node js = compiler.parseSyntheticCode(code); |
| 90 |
NodeTraversal.traverse(compiler, js, |
| 91 |
new Normalize.NormalizeStatements(compiler, false)); |
| 92 |
NodeTraversal.traverse( |
| 93 |
compiler, js, |
| 94 |
new MakeDeclaredNamesUnique( |
| 95 |
new BoilerplateRenamer( |
| 96 |
compiler.getCodingConvention(), |
| 97 |
compiler.getUniqueNameIdSupplier(), |
| 98 |
prefix))); |
| 99 |
return js; |
| 100 |
} |
| 101 |
|
| 102 |
static Node parseAndNormalizeTestCode( |
| 103 |
AbstractCompiler compiler, String code) { |
| 104 |
Node js = compiler.parseTestCode(code); |
| 105 |
NodeTraversal.traverse(compiler, js, |
| 106 |
new Normalize.NormalizeStatements(compiler, false)); |
| 107 |
NodeTraversal.traverse( |
| 108 |
compiler, js, |
| 109 |
new MakeDeclaredNamesUnique()); |
| 110 |
return js; |
| 111 |
} |
| 112 |
|
| 113 |
private void reportCodeChange(String changeDescription) { |
| 114 |
if (assertOnChange) { |
| 115 |
throw new IllegalStateException( |
| 116 |
"Normalize constraints violated:\n" + changeDescription); |
| 117 |
} |
| 118 |
compiler.reportCodeChange(); |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public void process(Node externs, Node root) { |
| 123 |
new NodeTraversal( |
| 124 |
compiler, new NormalizeStatements(compiler, assertOnChange)) |
| 125 |
.traverseRoots(externs, root); |
| 126 |
if (MAKE_LOCAL_NAMES_UNIQUE) { |
| 127 |
MakeDeclaredNamesUnique renamer = new MakeDeclaredNamesUnique(); |
| 128 |
NodeTraversal t = new NodeTraversal(compiler, renamer); |
| 129 |
t.traverseRoots(externs, root); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
removeDuplicateDeclarations(externs, root); |
| 146 |
new PropagateConstantAnnotationsOverVars(compiler, assertOnChange) |
| 147 |
.process(externs, root); |
| 148 |
|
| 149 |
FindExposeAnnotations findExposeAnnotations = new FindExposeAnnotations(); |
| 150 |
NodeTraversal.traverse(compiler, root, findExposeAnnotations); |
| 151 |
if (!findExposeAnnotations.exposedProperties.isEmpty()) { |
| 152 |
NodeTraversal.traverse(compiler, root, |
| 153 |
new RewriteExposedProperties( |
| 154 |
findExposeAnnotations.exposedProperties)); |
| 155 |
} |
| 156 |
|
| 157 |
if (!compiler.getLifeCycleStage().isNormalized()) { |
| 158 |
compiler.setLifeCycleStage(LifeCycleStage.NORMALIZED); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
private static class FindExposeAnnotations extends AbstractPostOrderCallback { |
| 166 |
private final Set<String> exposedProperties = Sets.newHashSet(); |
| 167 |
|
| 168 |
@Override public void visit(NodeTraversal t, Node n, Node parent) { |
| 169 |
if (NodeUtil.isExprAssign(n)) { |
| 170 |
Node assign = n.getFirstChild(); |
| 171 |
Node lhs = assign.getFirstChild(); |
| 172 |
if (lhs.isGetProp() && isMarkedExpose(assign)) { |
| 173 |
exposedProperties.add(lhs.getLastChild().getString()); |
| 174 |
} |
| 175 |
} else if (n.isStringKey() && isMarkedExpose(n)) { |
| 176 |
exposedProperties.add(n.getString()); |
| 177 |
} else if (n.isGetProp() && n.getParent().isExprResult() |
| 178 |
&& isMarkedExpose(n)) { |
| 179 |
exposedProperties.add(n.getLastChild().getString()); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
private boolean isMarkedExpose(Node n) { |
| 184 |
JSDocInfo info = n.getJSDocInfo(); |
| 185 |
return info != null && info.isExpose(); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
private class RewriteExposedProperties |
| 193 |
extends AbstractPostOrderCallback { |
| 194 |
private final Set<String> exposedProperties; |
| 195 |
|
| 196 |
RewriteExposedProperties(Set<String> exposedProperties) { |
| 197 |
this.exposedProperties = exposedProperties; |
| 198 |
} |
| 199 |
|
| 200 |
@Override public void visit(NodeTraversal t, Node n, Node parent) { |
| 201 |
if (n.isGetProp()) { |
| 202 |
String propName = n.getLastChild().getString(); |
| 203 |
if (exposedProperties.contains(propName)) { |
| 204 |
Node obj = n.removeFirstChild(); |
| 205 |
Node prop = n.removeFirstChild(); |
| 206 |
n.getParent().replaceChild(n, IR.getelem(obj, prop)); |
| 207 |
compiler.reportCodeChange(); |
| 208 |
} |
| 209 |
} else if (n.isStringKey()) { |
| 210 |
String propName = n.getString(); |
| 211 |
if (exposedProperties.contains(propName)) { |
| 212 |
n.setQuotedString(); |
| 213 |
compiler.reportCodeChange(); |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
static class PropagateConstantAnnotationsOverVars |
| 223 |
extends AbstractPostOrderCallback |
| 224 |
implements CompilerPass { |
| 225 |
private final AbstractCompiler compiler; |
| 226 |
private final boolean assertOnChange; |
| 227 |
|
| 228 |
PropagateConstantAnnotationsOverVars( |
| 229 |
AbstractCompiler compiler, boolean forbidChanges) { |
| 230 |
this.compiler = compiler; |
| 231 |
this.assertOnChange = forbidChanges; |
| 232 |
} |
| 233 |
|
| 234 |
@Override |
| 235 |
public void process(Node externs, Node root) { |
| 236 |
new NodeTraversal(compiler, this).traverseRoots(externs, root); |
| 237 |
} |
| 238 |
|
| 239 |
@Override |
| 240 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 241 |
|
| 242 |
if (n.isName()) { |
| 243 |
if (n.getString().isEmpty()) { |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
JSDocInfo info = null; |
| 248 |
|
| 249 |
Var var = t.getScope().getVar(n.getString()); |
| 250 |
if (var != null) { |
| 251 |
info = var.getJSDocInfo(); |
| 252 |
} |
| 253 |
|
| 254 |
boolean shouldBeConstant = |
| 255 |
(info != null && info.isConstant()) || |
| 256 |
NodeUtil.isConstantByConvention( |
| 257 |
compiler.getCodingConvention(), n, parent); |
| 258 |
boolean isMarkedConstant = n.getBooleanProp(Node.IS_CONSTANT_NAME); |
| 259 |
if (shouldBeConstant && !isMarkedConstant) { |
| 260 |
if (assertOnChange) { |
| 261 |
String name = n.getString(); |
| 262 |
throw new IllegalStateException( |
| 263 |
"Unexpected const change.\n" + |
| 264 |
" name: "+ name + "\n" + |
| 265 |
" parent:" + n.getParent().toStringTree()); |
| 266 |
} |
| 267 |
n.putBooleanProp(Node.IS_CONSTANT_NAME, true); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
static class VerifyConstants extends AbstractPostOrderCallback |
| 277 |
implements CompilerPass { |
| 278 |
|
| 279 |
final private AbstractCompiler compiler; |
| 280 |
final private boolean checkUserDeclarations; |
| 281 |
|
| 282 |
VerifyConstants(AbstractCompiler compiler, boolean checkUserDeclarations) { |
| 283 |
this.compiler = compiler; |
| 284 |
this.checkUserDeclarations = checkUserDeclarations; |
| 285 |
} |
| 286 |
|
| 287 |
@Override |
| 288 |
public void process(Node externs, Node root) { |
| 289 |
Node externsAndJs = root.getParent(); |
| 290 |
Preconditions.checkState(externsAndJs != null); |
| 291 |
Preconditions.checkState(externsAndJs.hasChild(externs)); |
| 292 |
|
| 293 |
NodeTraversal.traverseRoots( |
| 294 |
compiler, Lists.newArrayList(externs, root), this); |
| 295 |
} |
| 296 |
|
| 297 |
private Map<String, Boolean> constantMap = Maps.newHashMap(); |
| 298 |
|
| 299 |
@Override |
| 300 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 301 |
if (n.isName()) { |
| 302 |
String name = n.getString(); |
| 303 |
if (n.getString().isEmpty()) { |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
boolean isConst = n.getBooleanProp(Node.IS_CONSTANT_NAME); |
| 308 |
if (checkUserDeclarations) { |
| 309 |
boolean expectedConst = false; |
| 310 |
CodingConvention convention = compiler.getCodingConvention(); |
| 311 |
if (NodeUtil.isConstantName(n) |
| 312 |
|| NodeUtil.isConstantByConvention(convention, n, parent)) { |
| 313 |
expectedConst = true; |
| 314 |
} else { |
| 315 |
expectedConst = false; |
| 316 |
|
| 317 |
JSDocInfo info = null; |
| 318 |
Var var = t.getScope().getVar(n.getString()); |
| 319 |
if (var != null) { |
| 320 |
info = var.getJSDocInfo(); |
| 321 |
} |
| 322 |
|
| 323 |
if (info != null && info.isConstant()) { |
| 324 |
expectedConst = true; |
| 325 |
} else { |
| 326 |
expectedConst = false; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
if (expectedConst) { |
| 331 |
Preconditions.checkState(expectedConst == isConst, |
| 332 |
"The name %s is not annotated as constant.", name); |
| 333 |
} else { |
| 334 |
Preconditions.checkState(expectedConst == isConst, |
| 335 |
"The name %s should not be annotated as constant.", name); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
Boolean value = constantMap.get(name); |
| 340 |
if (value == null) { |
| 341 |
constantMap.put(name, isConst); |
| 342 |
} else { |
| 343 |
Preconditions.checkState(value.booleanValue() == isConst, |
| 344 |
"The name %s is not consistently annotated as constant.", name); |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
static class NormalizeStatements implements Callback { |
| 361 |
private final AbstractCompiler compiler; |
| 362 |
private final boolean assertOnChange; |
| 363 |
|
| 364 |
NormalizeStatements(AbstractCompiler compiler, boolean assertOnChange) { |
| 365 |
this.compiler = compiler; |
| 366 |
this.assertOnChange = assertOnChange; |
| 367 |
} |
| 368 |
|
| 369 |
private void reportCodeChange(String changeDescription) { |
| 370 |
if (assertOnChange) { |
| 371 |
throw new IllegalStateException( |
| 372 |
"Normalize constraints violated:\n" + changeDescription); |
| 373 |
} |
| 374 |
compiler.reportCodeChange(); |
| 375 |
} |
| 376 |
|
| 377 |
@Override |
| 378 |
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) { |
| 379 |
doStatementNormalizations(n); |
| 380 |
|
| 381 |
return true; |
| 382 |
} |
| 383 |
|
| 384 |
@Override |
| 385 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 386 |
switch (n.getType()) { |
| 387 |
case Token.WHILE: |
| 388 |
if (CONVERT_WHILE_TO_FOR) { |
| 389 |
Node expr = n.getFirstChild(); |
| 390 |
n.setType(Token.FOR); |
| 391 |
Node empty = IR.empty(); |
| 392 |
empty.copyInformationFrom(n); |
| 393 |
n.addChildBefore(empty, expr); |
| 394 |
n.addChildAfter(empty.cloneNode(), expr); |
| 395 |
reportCodeChange("WHILE node"); |
| 396 |
} |
| 397 |
break; |
| 398 |
|
| 399 |
case Token.FUNCTION: |
| 400 |
normalizeFunctionDeclaration(n); |
| 401 |
break; |
| 402 |
|
| 403 |
case Token.NAME: |
| 404 |
case Token.STRING: |
| 405 |
case Token.STRING_KEY: |
| 406 |
case Token.GETTER_DEF: |
| 407 |
case Token.SETTER_DEF: |
| 408 |
if (!compiler.getLifeCycleStage().isNormalizedObfuscated()) { |
| 409 |
annotateConstantsByConvention(n, parent); |
| 410 |
} |
| 411 |
break; |
| 412 |
|
| 413 |
case Token.CAST: |
| 414 |
parent.replaceChild(n, n.removeFirstChild()); |
| 415 |
break; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
private void annotateConstantsByConvention(Node n, Node parent) { |
| 423 |
Preconditions.checkState( |
| 424 |
n.isName() |
| 425 |
|| n.isString() |
| 426 |
|| n.isStringKey() |
| 427 |
|| n.isGetterDef() |
| 428 |
|| n.isSetterDef()); |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
boolean isObjLitKey = NodeUtil.isObjectLitKey(n); |
| 434 |
boolean isProperty = isObjLitKey || |
| 435 |
(parent.isGetProp() && |
| 436 |
parent.getLastChild() == n); |
| 437 |
if (n.isName() || isProperty) { |
| 438 |
boolean isMarkedConstant = n.getBooleanProp(Node.IS_CONSTANT_NAME); |
| 439 |
if (!isMarkedConstant && |
| 440 |
NodeUtil.isConstantByConvention( |
| 441 |
compiler.getCodingConvention(), n, parent)) { |
| 442 |
if (assertOnChange) { |
| 443 |
String name = n.getString(); |
| 444 |
throw new IllegalStateException( |
| 445 |
"Unexpected const change.\n" + |
| 446 |
" name: "+ name + "\n" + |
| 447 |
" parent:" + n.getParent().toStringTree()); |
| 448 |
} |
| 449 |
n.putBooleanProp(Node.IS_CONSTANT_NAME, true); |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
private void normalizeFunctionDeclaration(Node n) { |
| 463 |
Preconditions.checkState(n.isFunction()); |
| 464 |
if (!NodeUtil.isFunctionExpression(n) |
| 465 |
&& !NodeUtil.isHoistedFunctionDeclaration(n)) { |
| 466 |
rewriteFunctionDeclaration(n); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
private void rewriteFunctionDeclaration(Node n) { |
| 487 |
|
| 488 |
Node oldNameNode = n.getFirstChild(); |
| 489 |
Node fnNameNode = oldNameNode.cloneNode(); |
| 490 |
Node var = IR.var(fnNameNode).srcref(n); |
| 491 |
|
| 492 |
|
| 493 |
oldNameNode.setString(""); |
| 494 |
|
| 495 |
|
| 496 |
Node parent = n.getParent(); |
| 497 |
parent.replaceChild(n, var); |
| 498 |
fnNameNode.addChildToFront(n); |
| 499 |
|
| 500 |
reportCodeChange("Function declaration"); |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
private void doStatementNormalizations(Node n) { |
| 507 |
if (n.isLabel()) { |
| 508 |
normalizeLabels(n); |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
if (NodeUtil.isStatementBlock(n) || n.isLabel()) { |
| 514 |
extractForInitializer(n, null, null); |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
if (NodeUtil.isStatementBlock(n)) { |
| 520 |
splitVarDeclarations(n); |
| 521 |
} |
| 522 |
|
| 523 |
if (n.isFunction()) { |
| 524 |
moveNamedFunctions(n.getLastChild()); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
private void normalizeLabels(Node n) { |
| 536 |
Preconditions.checkArgument(n.isLabel()); |
| 537 |
|
| 538 |
Node last = n.getLastChild(); |
| 539 |
switch (last.getType()) { |
| 540 |
case Token.LABEL: |
| 541 |
case Token.BLOCK: |
| 542 |
case Token.FOR: |
| 543 |
case Token.WHILE: |
| 544 |
case Token.DO: |
| 545 |
return; |
| 546 |
default: |
| 547 |
Node block = IR.block(); |
| 548 |
block.copyInformationFrom(last); |
| 549 |
n.replaceChild(last, block); |
| 550 |
block.addChildToFront(last); |
| 551 |
reportCodeChange("LABEL normalization"); |
| 552 |
return; |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
private void extractForInitializer( |
| 568 |
Node n, Node before, Node beforeParent) { |
| 569 |
|
| 570 |
for (Node next, c = n.getFirstChild(); c != null; c = next) { |
| 571 |
next = c.getNext(); |
| 572 |
Node insertBefore = (before == null) ? c : before; |
| 573 |
Node insertBeforeParent = (before == null) ? n : beforeParent; |
| 574 |
switch (c.getType()) { |
| 575 |
case Token.LABEL: |
| 576 |
extractForInitializer(c, insertBefore, insertBeforeParent); |
| 577 |
break; |
| 578 |
case Token.FOR: |
| 579 |
if (NodeUtil.isForIn(c)) { |
| 580 |
Node first = c.getFirstChild(); |
| 581 |
if (first.isVar()) { |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
Node newStatement = first; |
| 587 |
|
| 588 |
Node name = newStatement.getFirstChild().cloneNode(); |
| 589 |
first.getParent().replaceChild(first, name); |
| 590 |
insertBeforeParent.addChildBefore(newStatement, insertBefore); |
| 591 |
reportCodeChange("FOR-IN var declaration"); |
| 592 |
} |
| 593 |
} else if (!c.getFirstChild().isEmpty()) { |
| 594 |
Node init = c.getFirstChild(); |
| 595 |
Node empty = IR.empty(); |
| 596 |
empty.copyInformationFrom(c); |
| 597 |
c.replaceChild(init, empty); |
| 598 |
|
| 599 |
Node newStatement; |
| 600 |
|
| 601 |
|
| 602 |
if (init.isVar()) { |
| 603 |
newStatement = init; |
| 604 |
} else { |
| 605 |
newStatement = NodeUtil.newExpr(init); |
| 606 |
} |
| 607 |
|
| 608 |
insertBeforeParent.addChildBefore(newStatement, insertBefore); |
| 609 |
reportCodeChange("FOR initializer"); |
| 610 |
} |
| 611 |
break; |
| 612 |
} |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
private void splitVarDeclarations(Node n) { |
| 625 |
for (Node next, c = n.getFirstChild(); c != null; c = next) { |
| 626 |
next = c.getNext(); |
| 627 |
if (c.isVar()) { |
| 628 |
if (assertOnChange && !c.hasChildren()) { |
| 629 |
throw new IllegalStateException("Empty VAR node."); |
| 630 |
} |
| 631 |
|
| 632 |
while (c.getFirstChild() != c.getLastChild()) { |
| 633 |
Node name = c.getFirstChild(); |
| 634 |
c.removeChild(name); |
| 635 |
Node newVar = IR.var(name).srcref(n); |
| 636 |
n.addChildBefore(newVar, c); |
| 637 |
reportCodeChange("VAR with multiple children"); |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
private void moveNamedFunctions(Node functionBody) { |
| 648 |
Preconditions.checkState( |
| 649 |
functionBody.getParent().isFunction()); |
| 650 |
Node previous = null; |
| 651 |
Node current = functionBody.getFirstChild(); |
| 652 |
|
| 653 |
|
| 654 |
while (current != null && NodeUtil.isFunctionDeclaration(current)) { |
| 655 |
previous = current; |
| 656 |
current = current.getNext(); |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
Node insertAfter = previous; |
| 661 |
while (current != null) { |
| 662 |
|
| 663 |
Node next = current.getNext(); |
| 664 |
if (NodeUtil.isFunctionDeclaration(current)) { |
| 665 |
|
| 666 |
Preconditions.checkNotNull(previous); |
| 667 |
functionBody.removeChildAfter(previous); |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
insertAfter = addToFront(functionBody, current, insertAfter); |
| 672 |
reportCodeChange("Move function declaration not at top of function"); |
| 673 |
} else { |
| 674 |
|
| 675 |
previous = current; |
| 676 |
} |
| 677 |
current = next; |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
private Node addToFront(Node parent, Node newChild, Node after) { |
| 687 |
if (after == null) { |
| 688 |
parent.addChildToFront(newChild); |
| 689 |
} else { |
| 690 |
parent.addChildAfter(newChild, after); |
| 691 |
} |
| 692 |
return newChild; |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
private void removeDuplicateDeclarations(Node externs, Node root) { |
| 700 |
Callback tickler = new ScopeTicklingCallback(); |
| 701 |
ScopeCreator scopeCreator = new SyntacticScopeCreator( |
| 702 |
compiler, new DuplicateDeclarationHandler()); |
| 703 |
NodeTraversal t = new NodeTraversal(compiler, tickler, scopeCreator); |
| 704 |
t.traverseRoots(externs, root); |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
private final class DuplicateDeclarationHandler implements |
| 711 |
SyntacticScopeCreator.RedeclarationHandler { |
| 712 |
|
| 713 |
private Set<Var> hasOkDuplicateDeclaration = Sets.newHashSet(); |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
@Override |
| 720 |
public void onRedeclaration( |
| 721 |
Scope s, String name, Node n, CompilerInput input) { |
| 722 |
Preconditions.checkState(n.isName()); |
| 723 |
Node parent = n.getParent(); |
| 724 |
Var v = s.getVar(name); |
| 725 |
|
| 726 |
if (v != null && s.isGlobal()) { |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
if (v.isExtern() && !input.isExtern()) { |
| 732 |
if (hasOkDuplicateDeclaration.add(v)) { |
| 733 |
return; |
| 734 |
} |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
if (v != null && v.getParentNode().isCatch()) { |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
name = MakeDeclaredNamesUnique.ContextualRenameInverter.getOrginalName( |
| 756 |
name); |
| 757 |
compiler.report( |
| 758 |
JSError.make( |
| 759 |
input.getName(), n, |
| 760 |
CATCH_BLOCK_VAR_ERROR, name)); |
| 761 |
} else if (v != null && parent.isFunction()) { |
| 762 |
if (v.getParentNode().isVar()) { |
| 763 |
s.undeclare(v); |
| 764 |
s.declare(name, n, n.getJSType(), v.input); |
| 765 |
replaceVarWithAssignment(v.getNameNode(), v.getParentNode(), |
| 766 |
v.getParentNode().getParent()); |
| 767 |
} |
| 768 |
} else if (parent.isVar()) { |
| 769 |
Preconditions.checkState(parent.hasOneChild()); |
| 770 |
|
| 771 |
replaceVarWithAssignment(n, parent, parent.getParent()); |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
private void replaceVarWithAssignment(Node n, Node parent, Node gramps) { |
| 792 |
if (n.hasChildren()) { |
| 793 |
|
| 794 |
parent.removeChild(n); |
| 795 |
|
| 796 |
Node value = n.getFirstChild(); |
| 797 |
n.removeChild(value); |
| 798 |
Node replacement = IR.assign(n, value); |
| 799 |
replacement.copyInformationFrom(parent); |
| 800 |
gramps.replaceChild(parent, NodeUtil.newExpr(replacement)); |
| 801 |
} else { |
| 802 |
|
| 803 |
if (NodeUtil.isStatementBlock(gramps)) { |
| 804 |
gramps.removeChild(parent); |
| 805 |
} else if (gramps.isFor()) { |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
parent.removeChild(n); |
| 810 |
gramps.replaceChild(parent, n); |
| 811 |
} else { |
| 812 |
Preconditions.checkState(gramps.isLabel()); |
| 813 |
|
| 814 |
|
| 815 |
throw new IllegalStateException("Unexpected LABEL"); |
| 816 |
} |
| 817 |
} |
| 818 |
reportCodeChange("Duplicate VAR declaration"); |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
private final class ScopeTicklingCallback |
| 826 |
implements NodeTraversal.ScopedCallback { |
| 827 |
@Override |
| 828 |
public void enterScope(NodeTraversal t) { |
| 829 |
|
| 830 |
|
| 831 |
t.getScope(); |
| 832 |
} |
| 833 |
|
| 834 |
@Override |
| 835 |
public void exitScope(NodeTraversal t) { |
| 836 |
|
| 837 |
} |
| 838 |
|
| 839 |
@Override |
| 840 |
public boolean shouldTraverse( |
| 841 |
NodeTraversal nodeTraversal, Node n, Node parent) { |
| 842 |
return true; |
| 843 |
} |
| 844 |
|
| 845 |
@Override |
| 846 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 847 |
|
| 848 |
} |
| 849 |
} |
| 850 |
} |