…lines 1–448 unchanged (448 lines)
449 * @param alias The flattened name for {@code n}
450 */
451 private void collapseDeclarationOfNameAndDescendants(Name n, String alias) {
452 boolean canCollapseChildNames = n.canCollapseUnannotatedChildNames();
453
454 // Handle this name first so that nested object literals get unrolled.
455 if (n.canCollapse()) {
456 updateObjLitOrFunctionDeclaration(n, alias, canCollapseChildNames);
457 }
458
459 if (n.props != null) {
460 for (Name p : n.props) {
461 // Recurse first so that saved node ancestries are intact when needed.
462 collapseDeclarationOfNameAndDescendants(
…lines 463–568 unchanged (106 lines)
569 * @param alias The flattened name for {@code n} (e.g. "a", "a$b$c")
570 * @param canCollapseChildNames Whether it's possible to collapse children of
571 * this name. (This is mostly passed for convenience; it's equivalent to
572 * n.canCollapseChildNames()).
573 */
574 private void updateObjLitOrFunctionDeclaration(
575 Name n, String alias, boolean canCollapseChildNames) {
576 if (n.declaration == null) {
577 // Some names do not have declarations, because they
578 // are only defined in local scopes.
579 return;
580 }
581
582 if (n.declaration.getTwin() != null) {
583 // Twin declarations will get handled when normal references
584 // are handled.
585 return;
586 }
587
588 switch (n.declaration.node.getParent().getType()) {
589 case Token.ASSIGN:
590 updateObjLitOrFunctionDeclarationAtAssignNode(
591 n, alias, canCollapseChildNames);
592 break;
593 case Token.VAR:
594 updateObjLitOrFunctionDeclarationAtVarNode(n, canCollapseChildNames);
595 break;
596 case Token.FUNCTION:
597 updateFunctionDeclarationAtFunctionNode(n, canCollapseChildNames);
598 break;
599 }
600 }
601
602 /**
603 * Updates the first initialization (a.k.a "declaration") of a global name
604 * that occurs at an ASSIGN node. See comment for
605 * {@link #updateObjLitOrFunctionDeclaration}.
606 *
607 * @param n An object representing a global name (e.g. "a", "a.b.c")
608 * @param alias The flattened name for {@code n} (e.g. "a", "a$b$c")
609 */
610 private void updateObjLitOrFunctionDeclarationAtAssignNode(
611 Name n, String alias, boolean canCollapseChildNames) {
612 // NOTE: It's important that we don't add additional nodes
613 // (e.g. a var node before the exprstmt) because the exprstmt might be
614 // the child of an if statement that's not inside a block).
615
616 Ref ref = n.declaration;
617 Node rvalue = ref.node.getNext();
…lines 618–648 unchanged (31 lines)
649
650 // Update the node ancestry stored in the reference.
651 ref.node = nameNode;
652 insertedVarNode = true;
653 }
654
655 if (canCollapseChildNames) {
656 if (isObjLit) {
657 declareVarsForObjLitValues(
658 n, alias, rvalue,
659 varNode, varParent.getChildBefore(varNode), varParent);
660 }
661
662 addStubsForUndeclaredProperties(n, alias, varParent, varNode);
663 }
664
665 if (insertedVarNode) {
666 if (!varNode.hasChildren()) {
667 varParent.removeChild(varNode);
668 }
669 compiler.reportCodeChange();
…lines 670–697 unchanged (28 lines)
698 * that occurs at a VAR node. See comment for
699 * {@link #updateObjLitOrFunctionDeclaration}.
700 *
701 * @param n An object representing a global name (e.g. "a")
702 */
703 private void updateObjLitOrFunctionDeclarationAtVarNode(
704 Name n, boolean canCollapseChildNames) {
705 if (!canCollapseChildNames) {
706 return;
707 }
708
709 Ref ref = n.declaration;
710 String name = ref.node.getString();
711 Node rvalue = ref.node.getFirstChild();
712 Node varNode = ref.node.getParent();
713 Node gramps = varNode.getParent();
…lines 714–744 unchanged (31 lines)
745 * that occurs at a FUNCTION node. See comment for
746 * {@link #updateObjLitOrFunctionDeclaration}.
747 *
748 * @param n An object representing a global name (e.g. "a")
749 */
750 private void updateFunctionDeclarationAtFunctionNode(
751 Name n, boolean canCollapseChildNames) {
752 if (!canCollapseChildNames) {
753 return;
754 }
755
756 Ref ref = n.declaration;
757 String fnName = ref.node.getString();
758 addStubsForUndeclaredProperties(
759 n, fnName, ref.node.getAncestor(2), ref.node.getParent());
760 }
…lines 761–913 unchanged (153 lines)