| 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.Predicates; |
| 21 |
import com.google.common.collect.Iterables; |
| 22 |
import com.google.common.collect.Lists; |
| 23 |
import com.google.common.collect.Sets; |
| 24 |
import com.google.javascript.jscomp.GlobalNamespace.Name; |
| 25 |
import com.google.javascript.jscomp.GlobalNamespace.Ref; |
| 26 |
import com.google.javascript.jscomp.GlobalNamespace.Ref.Type; |
| 27 |
import com.google.javascript.jscomp.ReferenceCollectingCallback; |
| 28 |
import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceCollection; |
| 29 |
import com.google.javascript.jscomp.Scope; |
| 30 |
import com.google.javascript.jscomp.Scope.Var; |
| 31 |
import com.google.javascript.rhino.IR; |
| 32 |
import com.google.javascript.rhino.JSDocInfo; |
| 33 |
import com.google.javascript.rhino.Node; |
| 34 |
import com.google.javascript.rhino.Token; |
| 35 |
import com.google.javascript.rhino.TokenStream; |
| 36 |
import com.google.javascript.rhino.jstype.JSType; |
| 37 |
|
| 38 |
import java.util.ArrayDeque; |
| 39 |
import java.util.Deque; |
| 40 |
import java.util.List; |
| 41 |
import java.util.Map; |
| 42 |
import java.util.Set; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
class CollapseProperties implements CompilerPass { |
| 75 |
|
| 76 |
|
| 77 |
static final DiagnosticType UNSAFE_NAMESPACE_WARNING = |
| 78 |
DiagnosticType.warning( |
| 79 |
"JSC_UNSAFE_NAMESPACE", |
| 80 |
"incomplete alias created for namespace {0}"); |
| 81 |
|
| 82 |
static final DiagnosticType NAMESPACE_REDEFINED_WARNING = |
| 83 |
DiagnosticType.warning( |
| 84 |
"JSC_NAMESPACE_REDEFINED", |
| 85 |
"namespace {0} should not be redefined"); |
| 86 |
|
| 87 |
static final DiagnosticType UNSAFE_THIS = DiagnosticType.warning( |
| 88 |
"JSC_UNSAFE_THIS", |
| 89 |
"dangerous use of 'this' in static method {0}"); |
| 90 |
|
| 91 |
private AbstractCompiler compiler; |
| 92 |
|
| 93 |
|
| 94 |
private List<Name> globalNames; |
| 95 |
|
| 96 |
|
| 97 |
private Map<String, Name> nameMap; |
| 98 |
|
| 99 |
private final boolean collapsePropertiesOnExternTypes; |
| 100 |
private final boolean inlineAliases; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
CollapseProperties(AbstractCompiler compiler, |
| 112 |
boolean collapsePropertiesOnExternTypes, boolean inlineAliases) { |
| 113 |
this.compiler = compiler; |
| 114 |
this.collapsePropertiesOnExternTypes = collapsePropertiesOnExternTypes; |
| 115 |
this.inlineAliases = inlineAliases; |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public void process(Node externs, Node root) { |
| 120 |
GlobalNamespace namespace; |
| 121 |
if (collapsePropertiesOnExternTypes) { |
| 122 |
namespace = new GlobalNamespace(compiler, externs, root); |
| 123 |
} else { |
| 124 |
namespace = new GlobalNamespace(compiler, root); |
| 125 |
} |
| 126 |
|
| 127 |
if (inlineAliases) { |
| 128 |
inlineAliases(namespace); |
| 129 |
} |
| 130 |
nameMap = namespace.getNameIndex(); |
| 131 |
globalNames = namespace.getNameForest(); |
| 132 |
checkNamespaces(); |
| 133 |
|
| 134 |
for (Name n : globalNames) { |
| 135 |
flattenReferencesToCollapsibleDescendantNames(n, n.getBaseName()); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
for (Name n : globalNames) { |
| 142 |
collapseDeclarationOfNameAndDescendants(n, n.getBaseName()); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
private void inlineAliases(GlobalNamespace namespace) { |
| 162 |
|
| 163 |
Deque<Name> workList = new ArrayDeque<Name>(namespace.getNameForest()); |
| 164 |
while (!workList.isEmpty()) { |
| 165 |
Name name = workList.pop(); |
| 166 |
|
| 167 |
|
| 168 |
if (name.type == Name.Type.GET || name.type == Name.Type.SET) { |
| 169 |
continue; |
| 170 |
} |
| 171 |
|
| 172 |
if (name.globalSets == 1 && name.localSets == 0 && |
| 173 |
name.aliasingGets > 0) { |
| 174 |
|
| 175 |
|
| 176 |
List<Ref> refs = Lists.newArrayList(name.getRefs()); |
| 177 |
for (Ref ref : refs) { |
| 178 |
if (ref.type == Type.ALIASING_GET && ref.scope.isLocal()) { |
| 179 |
|
| 180 |
if (inlineAliasIfPossible(ref, namespace)) { |
| 181 |
name.removeRef(ref); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
if ((name.type == Name.Type.OBJECTLIT || |
| 190 |
name.type == Name.Type.FUNCTION) && |
| 191 |
name.aliasingGets == 0 && name.props != null) { |
| 192 |
|
| 193 |
|
| 194 |
workList.addAll(name.props); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
private boolean inlineAliasIfPossible(Ref alias, GlobalNamespace namespace) { |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
Node aliasParent = alias.node.getParent(); |
| 205 |
if (aliasParent.isName()) { |
| 206 |
|
| 207 |
Scope scope = alias.scope; |
| 208 |
Var aliasVar = scope.getVar(aliasParent.getString()); |
| 209 |
ReferenceCollectingCallback collector = |
| 210 |
new ReferenceCollectingCallback(compiler, |
| 211 |
ReferenceCollectingCallback.DO_NOTHING_BEHAVIOR, |
| 212 |
Predicates.<Var>equalTo(aliasVar)); |
| 213 |
(new NodeTraversal(compiler, collector)).traverseAtScope(scope); |
| 214 |
|
| 215 |
ReferenceCollection aliasRefs = collector.getReferences(aliasVar); |
| 216 |
if (aliasRefs.isWellDefined() |
| 217 |
&& aliasRefs.firstReferenceIsAssigningDeclaration() |
| 218 |
&& aliasRefs.isAssignedOnceInLifetime()) { |
| 219 |
|
| 220 |
int size = aliasRefs.references.size(); |
| 221 |
Set<Node> newNodes = Sets.newHashSetWithExpectedSize(size - 1); |
| 222 |
for (int i = 1; i < size; i++) { |
| 223 |
ReferenceCollectingCallback.Reference aliasRef = |
| 224 |
aliasRefs.references.get(i); |
| 225 |
|
| 226 |
Node newNode = alias.node.cloneTree(); |
| 227 |
aliasRef.getParent().replaceChild(aliasRef.getNode(), newNode); |
| 228 |
newNodes.add(newNode); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
aliasParent.replaceChild(alias.node, IR.nullNode()); |
| 233 |
compiler.reportCodeChange(); |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
namespace.scanNewNodes(alias.scope, newNodes); |
| 238 |
return true; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
private void checkNamespaces() { |
| 250 |
for (Name name : nameMap.values()) { |
| 251 |
if (name.isNamespace() && |
| 252 |
(name.aliasingGets > 0 || name.localSets + name.globalSets > 1 || |
| 253 |
name.deleteProps > 0)) { |
| 254 |
boolean initialized = name.getDeclaration() != null; |
| 255 |
for (Ref ref : name.getRefs()) { |
| 256 |
if (ref == name.getDeclaration()) { |
| 257 |
continue; |
| 258 |
} |
| 259 |
|
| 260 |
if (ref.type == Ref.Type.DELETE_PROP) { |
| 261 |
if (initialized) { |
| 262 |
warnAboutNamespaceRedefinition(name, ref); |
| 263 |
} |
| 264 |
} else if ( |
| 265 |
ref.type == Ref.Type.SET_FROM_GLOBAL || |
| 266 |
ref.type == Ref.Type.SET_FROM_LOCAL) { |
| 267 |
if (initialized) { |
| 268 |
warnAboutNamespaceRedefinition(name, ref); |
| 269 |
} |
| 270 |
|
| 271 |
initialized = true; |
| 272 |
} else if (ref.type == Ref.Type.ALIASING_GET) { |
| 273 |
warnAboutNamespaceAliasing(name, ref); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
private void warnAboutNamespaceAliasing(Name nameObj, Ref ref) { |
| 287 |
compiler.report( |
| 288 |
JSError.make(ref.getSourceName(), ref.node, |
| 289 |
UNSAFE_NAMESPACE_WARNING, nameObj.getFullName())); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
private void warnAboutNamespaceRedefinition(Name nameObj, Ref ref) { |
| 299 |
compiler.report( |
| 300 |
JSError.make(ref.getSourceName(), ref.node, |
| 301 |
NAMESPACE_REDEFINED_WARNING, nameObj.getFullName())); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
private void flattenReferencesToCollapsibleDescendantNames( |
| 312 |
Name n, String alias) { |
| 313 |
if (n.props == null) return; |
| 314 |
|
| 315 |
for (Name p : n.props) { |
| 316 |
String propAlias = appendPropForAlias(alias, p.getBaseName()); |
| 317 |
|
| 318 |
if (p.canCollapse()) { |
| 319 |
flattenReferencesTo(p, propAlias); |
| 320 |
} else if (p.isSimpleStubDeclaration()) { |
| 321 |
flattenSimpleStubDeclaration(p, propAlias); |
| 322 |
} |
| 323 |
|
| 324 |
flattenReferencesToCollapsibleDescendantNames(p, propAlias); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
private void flattenSimpleStubDeclaration(Name name, String alias) { |
| 334 |
Ref ref = Iterables.getOnlyElement(name.getRefs()); |
| 335 |
Node nameNode = NodeUtil.newName( |
| 336 |
compiler.getCodingConvention(), alias, ref.node, |
| 337 |
name.getFullName()); |
| 338 |
Node varNode = IR.var(nameNode).copyInformationFrom(nameNode); |
| 339 |
|
| 340 |
Preconditions.checkState( |
| 341 |
ref.node.getParent().isExprResult()); |
| 342 |
Node parent = ref.node.getParent(); |
| 343 |
Node gramps = parent.getParent(); |
| 344 |
gramps.replaceChild(parent, varNode); |
| 345 |
compiler.reportCodeChange(); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
private void flattenReferencesTo(Name n, String alias) { |
| 357 |
String originalName = n.getFullName(); |
| 358 |
for (Ref r : n.getRefs()) { |
| 359 |
if (r == n.getDeclaration()) { |
| 360 |
|
| 361 |
continue; |
| 362 |
} |
| 363 |
|
| 364 |
Node rParent = r.node.getParent(); |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
if (!NodeUtil.isObjectLitKey(r.node) && |
| 372 |
(r.getTwin() == null || r.isSet())) { |
| 373 |
flattenNameRef(alias, r.node, rParent, originalName); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
if (n.props != null) { |
| 381 |
for (Name p : n.props) { |
| 382 |
flattenPrefixes(alias, p, 1); |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
private void flattenPrefixes(String alias, Name n, int depth) { |
| 397 |
|
| 398 |
|
| 399 |
String originalName = n.getFullName(); |
| 400 |
Ref decl = n.getDeclaration(); |
| 401 |
if (decl != null && decl.node != null && |
| 402 |
decl.node.isGetProp()) { |
| 403 |
flattenNameRefAtDepth(alias, decl.node, depth, originalName); |
| 404 |
} |
| 405 |
|
| 406 |
for (Ref r : n.getRefs()) { |
| 407 |
if (r == decl) { |
| 408 |
|
| 409 |
continue; |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
if (r.getTwin() == null || r.isSet()) { |
| 415 |
flattenNameRefAtDepth(alias, r.node, depth, originalName); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
if (n.props != null) { |
| 420 |
for (Name p : n.props) { |
| 421 |
flattenPrefixes(alias, p, depth + 1); |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
private void flattenNameRefAtDepth(String alias, Node n, int depth, |
| 436 |
String originalName) { |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
int nType = n.getType(); |
| 441 |
boolean isQName = nType == Token.NAME || nType == Token.GETPROP; |
| 442 |
boolean isObjKey = NodeUtil.isObjectLitKey(n); |
| 443 |
Preconditions.checkState(isObjKey || isQName); |
| 444 |
if (isQName) { |
| 445 |
for (int i = 1; i < depth && n.hasChildren(); i++) { |
| 446 |
n = n.getFirstChild(); |
| 447 |
} |
| 448 |
if (n.hasChildren()) { |
| 449 |
flattenNameRef(alias, n.getFirstChild(), n, originalName); |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
private void flattenNameRef(String alias, Node n, Node parent, |
| 463 |
String originalName) { |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
Node ref = NodeUtil.newName( |
| 473 |
compiler.getCodingConvention(), alias, n, originalName); |
| 474 |
NodeUtil.copyNameAnnotations(n.getLastChild(), ref); |
| 475 |
if (parent.isCall() && n == parent.getFirstChild()) { |
| 476 |
|
| 477 |
|
| 478 |
parent.putBooleanProp(Node.FREE_CALL, true); |
| 479 |
} |
| 480 |
|
| 481 |
JSType type = n.getJSType(); |
| 482 |
if (type != null) { |
| 483 |
ref.setJSType(type); |
| 484 |
} |
| 485 |
parent.replaceChild(n, ref); |
| 486 |
compiler.reportCodeChange(); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
private void collapseDeclarationOfNameAndDescendants(Name n, String alias) { |
| 498 |
boolean canCollapseChildNames = n.canCollapseUnannotatedChildNames(); |
| 499 |
|
| 500 |
|
| 501 |
if (n.canCollapse()) { |
| 502 |
updateObjLitOrFunctionDeclaration(n, alias, canCollapseChildNames); |
| 503 |
} |
| 504 |
|
| 505 |
if (n.props != null) { |
| 506 |
for (Name p : n.props) { |
| 507 |
|
| 508 |
collapseDeclarationOfNameAndDescendants( |
| 509 |
p, appendPropForAlias(alias, p.getBaseName())); |
| 510 |
|
| 511 |
if (!p.inExterns && canCollapseChildNames && |
| 512 |
p.getDeclaration() != null && |
| 513 |
p.canCollapse() && |
| 514 |
p.getDeclaration().node != null && |
| 515 |
p.getDeclaration().node.getParent() != null && |
| 516 |
p.getDeclaration().node.getParent().isAssign()) { |
| 517 |
updateSimpleDeclaration( |
| 518 |
appendPropForAlias(alias, p.getBaseName()), p, p.getDeclaration()); |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
private void updateSimpleDeclaration(String alias, Name refName, Ref ref) { |
| 536 |
Node rvalue = ref.node.getNext(); |
| 537 |
Node parent = ref.node.getParent(); |
| 538 |
Node gramps = parent.getParent(); |
| 539 |
Node greatGramps = gramps.getParent(); |
| 540 |
|
| 541 |
if (rvalue != null && rvalue.isFunction()) { |
| 542 |
checkForHosedThisReferences(rvalue, refName.docInfo, refName); |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
Node nameNode = NodeUtil.newName( |
| 547 |
compiler.getCodingConvention(), alias, gramps.getFirstChild(), |
| 548 |
refName.getFullName()); |
| 549 |
NodeUtil.copyNameAnnotations(ref.node.getLastChild(), nameNode); |
| 550 |
|
| 551 |
if (gramps.isExprResult()) { |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
parent.removeChild(rvalue); |
| 568 |
nameNode.addChildToFront(rvalue); |
| 569 |
|
| 570 |
Node varNode = IR.var(nameNode); |
| 571 |
greatGramps.replaceChild(gramps, varNode); |
| 572 |
} else { |
| 573 |
|
| 574 |
Preconditions.checkNotNull(ref.getTwin()); |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
Node current = gramps; |
| 584 |
Node currentParent = gramps.getParent(); |
| 585 |
for (; !currentParent.isScript() && |
| 586 |
!currentParent.isBlock(); |
| 587 |
current = currentParent, |
| 588 |
currentParent = currentParent.getParent()) {} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
Node stubVar = IR.var(nameNode.cloneTree()) |
| 593 |
.copyInformationFrom(nameNode); |
| 594 |
currentParent.addChildBefore(stubVar, current); |
| 595 |
|
| 596 |
parent.replaceChild(ref.node, nameNode); |
| 597 |
} |
| 598 |
|
| 599 |
compiler.reportCodeChange(); |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
private void updateObjLitOrFunctionDeclaration( |
| 622 |
Name n, String alias, boolean canCollapseChildNames) { |
| 623 |
Ref decl = n.getDeclaration(); |
| 624 |
if (decl == null) { |
| 625 |
|
| 626 |
|
| 627 |
return; |
| 628 |
} |
| 629 |
|
| 630 |
if (decl.getTwin() != null) { |
| 631 |
|
| 632 |
|
| 633 |
return; |
| 634 |
} |
| 635 |
|
| 636 |
switch (decl.node.getParent().getType()) { |
| 637 |
case Token.ASSIGN: |
| 638 |
updateObjLitOrFunctionDeclarationAtAssignNode( |
| 639 |
n, alias, canCollapseChildNames); |
| 640 |
break; |
| 641 |
case Token.VAR: |
| 642 |
updateObjLitOrFunctionDeclarationAtVarNode(n, canCollapseChildNames); |
| 643 |
break; |
| 644 |
case Token.FUNCTION: |
| 645 |
updateFunctionDeclarationAtFunctionNode(n, canCollapseChildNames); |
| 646 |
break; |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
private void updateObjLitOrFunctionDeclarationAtAssignNode( |
| 659 |
Name n, String alias, boolean canCollapseChildNames) { |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
Ref ref = n.getDeclaration(); |
| 665 |
Node rvalue = ref.node.getNext(); |
| 666 |
Node varNode = new Node(Token.VAR); |
| 667 |
Node varParent = ref.node.getAncestor(3); |
| 668 |
Node gramps = ref.node.getAncestor(2); |
| 669 |
boolean isObjLit = rvalue.isObjectLit(); |
| 670 |
boolean insertedVarNode = false; |
| 671 |
|
| 672 |
if (isObjLit && n.canEliminate()) { |
| 673 |
|
| 674 |
varParent.replaceChild(gramps, varNode); |
| 675 |
ref.node = null; |
| 676 |
insertedVarNode = true; |
| 677 |
|
| 678 |
} else if (!n.isSimpleName()) { |
| 679 |
|
| 680 |
if (rvalue.isFunction()) { |
| 681 |
checkForHosedThisReferences(rvalue, n.docInfo, n); |
| 682 |
} |
| 683 |
|
| 684 |
ref.node.getParent().removeChild(rvalue); |
| 685 |
|
| 686 |
Node nameNode = NodeUtil.newName( |
| 687 |
compiler.getCodingConvention(), |
| 688 |
alias, ref.node.getAncestor(2), n.getFullName()); |
| 689 |
|
| 690 |
JSDocInfo info = ref.node.getParent().getJSDocInfo(); |
| 691 |
if (ref.node.getLastChild().getBooleanProp(Node.IS_CONSTANT_NAME) || |
| 692 |
(info != null && info.isConstant())) { |
| 693 |
nameNode.putBooleanProp(Node.IS_CONSTANT_NAME, true); |
| 694 |
} |
| 695 |
|
| 696 |
if (info != null) { |
| 697 |
varNode.setJSDocInfo(info); |
| 698 |
} |
| 699 |
varNode.addChildToBack(nameNode); |
| 700 |
nameNode.addChildToFront(rvalue); |
| 701 |
varParent.replaceChild(gramps, varNode); |
| 702 |
|
| 703 |
|
| 704 |
ref.node = nameNode; |
| 705 |
insertedVarNode = true; |
| 706 |
} |
| 707 |
|
| 708 |
if (canCollapseChildNames) { |
| 709 |
if (isObjLit) { |
| 710 |
declareVarsForObjLitValues( |
| 711 |
n, alias, rvalue, |
| 712 |
varNode, varParent.getChildBefore(varNode), varParent); |
| 713 |
} |
| 714 |
|
| 715 |
addStubsForUndeclaredProperties(n, alias, varParent, varNode); |
| 716 |
} |
| 717 |
|
| 718 |
if (insertedVarNode) { |
| 719 |
if (!varNode.hasChildren()) { |
| 720 |
varParent.removeChild(varNode); |
| 721 |
} |
| 722 |
compiler.reportCodeChange(); |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
private void checkForHosedThisReferences(Node function, JSDocInfo docInfo, |
| 731 |
final Name name) { |
| 732 |
|
| 733 |
|
| 734 |
if (docInfo == null || |
| 735 |
(!docInfo.isConstructor() && !docInfo.hasThisType())) { |
| 736 |
NodeTraversal.traverse(compiler, function.getLastChild(), |
| 737 |
new NodeTraversal.AbstractShallowCallback() { |
| 738 |
@Override |
| 739 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 740 |
if (n.isThis()) { |
| 741 |
compiler.report( |
| 742 |
JSError.make(name.getDeclaration().getSourceName(), n, |
| 743 |
UNSAFE_THIS, name.getFullName())); |
| 744 |
} |
| 745 |
} |
| 746 |
}); |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
private void updateObjLitOrFunctionDeclarationAtVarNode( |
| 758 |
Name n, boolean canCollapseChildNames) { |
| 759 |
if (!canCollapseChildNames) { |
| 760 |
return; |
| 761 |
} |
| 762 |
|
| 763 |
Ref ref = n.getDeclaration(); |
| 764 |
String name = ref.node.getString(); |
| 765 |
Node rvalue = ref.node.getFirstChild(); |
| 766 |
Node varNode = ref.node.getParent(); |
| 767 |
Node gramps = varNode.getParent(); |
| 768 |
|
| 769 |
boolean isObjLit = rvalue.isObjectLit(); |
| 770 |
int numChanges = 0; |
| 771 |
|
| 772 |
if (isObjLit) { |
| 773 |
numChanges += declareVarsForObjLitValues( |
| 774 |
n, name, rvalue, varNode, gramps.getChildBefore(varNode), |
| 775 |
gramps); |
| 776 |
} |
| 777 |
|
| 778 |
numChanges += addStubsForUndeclaredProperties(n, name, gramps, varNode); |
| 779 |
|
| 780 |
if (isObjLit && n.canEliminate()) { |
| 781 |
varNode.removeChild(ref.node); |
| 782 |
if (!varNode.hasChildren()) { |
| 783 |
gramps.removeChild(varNode); |
| 784 |
} |
| 785 |
numChanges++; |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
ref.node = null; |
| 790 |
} |
| 791 |
|
| 792 |
if (numChanges > 0) { |
| 793 |
compiler.reportCodeChange(); |
| 794 |
} |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
private void updateFunctionDeclarationAtFunctionNode( |
| 805 |
Name n, boolean canCollapseChildNames) { |
| 806 |
if (!canCollapseChildNames) { |
| 807 |
return; |
| 808 |
} |
| 809 |
|
| 810 |
Ref ref = n.getDeclaration(); |
| 811 |
String fnName = ref.node.getString(); |
| 812 |
addStubsForUndeclaredProperties( |
| 813 |
n, fnName, ref.node.getAncestor(2), ref.node.getParent()); |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
private int declareVarsForObjLitValues( |
| 830 |
Name objlitName, String alias, Node objlit, Node varNode, |
| 831 |
Node nameToAddAfter, Node varParent) { |
| 832 |
int numVars = 0; |
| 833 |
int arbitraryNameCounter = 0; |
| 834 |
boolean discardKeys = !objlitName.shouldKeepKeys(); |
| 835 |
|
| 836 |
for (Node key = objlit.getFirstChild(), nextKey; key != null; |
| 837 |
key = nextKey) { |
| 838 |
Node value = key.getFirstChild(); |
| 839 |
nextKey = key.getNext(); |
| 840 |
|
| 841 |
|
| 842 |
if (key.isGetterDef() || key.isSetterDef()) { |
| 843 |
continue; |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
boolean isJsIdentifier = !key.isNumber() && |
| 852 |
TokenStream.isJSIdentifier(key.getString()); |
| 853 |
String propName = isJsIdentifier ? |
| 854 |
key.getString() : String.valueOf(++arbitraryNameCounter); |
| 855 |
|
| 856 |
|
| 857 |
String qName = objlitName.getFullName() + '.' + propName; |
| 858 |
Name p = nameMap.get(qName); |
| 859 |
if (p != null && !p.canCollapse()) { |
| 860 |
continue; |
| 861 |
} |
| 862 |
|
| 863 |
String propAlias = appendPropForAlias(alias, propName); |
| 864 |
Node refNode = null; |
| 865 |
if (discardKeys) { |
| 866 |
objlit.removeChild(key); |
| 867 |
value.detachFromParent(); |
| 868 |
} else { |
| 869 |
|
| 870 |
refNode = IR.name(propAlias); |
| 871 |
if (key.getBooleanProp(Node.IS_CONSTANT_NAME)) { |
| 872 |
refNode.putBooleanProp(Node.IS_CONSTANT_NAME, true); |
| 873 |
} |
| 874 |
|
| 875 |
key.replaceChild(value, refNode); |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
Node nameNode = IR.name(propAlias); |
| 880 |
nameNode.addChildToFront(value); |
| 881 |
if (key.getBooleanProp(Node.IS_CONSTANT_NAME)) { |
| 882 |
nameNode.putBooleanProp(Node.IS_CONSTANT_NAME, true); |
| 883 |
} |
| 884 |
Node newVar = IR.var(nameNode) |
| 885 |
.copyInformationFromForTree(key); |
| 886 |
if (nameToAddAfter != null) { |
| 887 |
varParent.addChildAfter(newVar, nameToAddAfter); |
| 888 |
} else { |
| 889 |
varParent.addChildBefore(newVar, varNode); |
| 890 |
} |
| 891 |
compiler.reportCodeChange(); |
| 892 |
nameToAddAfter = newVar; |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
if (isJsIdentifier && p != null) { |
| 898 |
if (!discardKeys) { |
| 899 |
Ref newAlias = |
| 900 |
p.getDeclaration().cloneAndReclassify(Ref.Type.ALIASING_GET); |
| 901 |
newAlias.node = refNode; |
| 902 |
p.addRef(newAlias); |
| 903 |
} |
| 904 |
|
| 905 |
p.getDeclaration().node = nameNode; |
| 906 |
|
| 907 |
if (value.isFunction()) { |
| 908 |
checkForHosedThisReferences(value, value.getJSDocInfo(), p); |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
numVars++; |
| 913 |
} |
| 914 |
return numVars; |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
private int addStubsForUndeclaredProperties( |
| 931 |
Name n, String alias, Node parent, Node addAfter) { |
| 932 |
Preconditions.checkState(n.canCollapseUnannotatedChildNames()); |
| 933 |
Preconditions.checkArgument(NodeUtil.isStatementBlock(parent)); |
| 934 |
Preconditions.checkNotNull(addAfter); |
| 935 |
int numStubs = 0; |
| 936 |
if (n.props != null) { |
| 937 |
for (Name p : n.props) { |
| 938 |
if (p.needsToBeStubbed()) { |
| 939 |
String propAlias = appendPropForAlias(alias, p.getBaseName()); |
| 940 |
Node nameNode = IR.name(propAlias); |
| 941 |
Node newVar = IR.var(nameNode) |
| 942 |
.copyInformationFromForTree(addAfter); |
| 943 |
parent.addChildAfter(newVar, addAfter); |
| 944 |
addAfter = newVar; |
| 945 |
numStubs++; |
| 946 |
compiler.reportCodeChange(); |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
if (p.getRefs().get(0).node.getLastChild().getBooleanProp( |
| 951 |
Node.IS_CONSTANT_NAME)) { |
| 952 |
nameNode.putBooleanProp(Node.IS_CONSTANT_NAME, true); |
| 953 |
} |
| 954 |
} |
| 955 |
} |
| 956 |
} |
| 957 |
return numStubs; |
| 958 |
} |
| 959 |
|
| 960 |
private static String appendPropForAlias(String root, String prop) { |
| 961 |
if (prop.indexOf('$') != -1) { |
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
prop = prop.replace("$", "$0"); |
| 966 |
} |
| 967 |
return root + '$' + prop; |
| 968 |
} |
| 969 |
} |