Package com.google.javascript.jscomp.GlobalNamespace

Examples of com.google.javascript.jscomp.GlobalNamespace.Name


* @author nicksantos@google.com (Nick Santos)
*/
public class GlobalNamespaceTest extends TestCase {

  public void testRemoveDeclaration1() {
    Name n = new Name("a", null, false);
    Ref set1 = createNodelessRef(Ref.Type.SET_FROM_GLOBAL);
    Ref set2 = createNodelessRef(Ref.Type.SET_FROM_GLOBAL);

    n.addRef(set1);
    n.addRef(set2);

    assertEquals(set1, n.getDeclaration());
    assertEquals(2, n.globalSets);
    assertEquals(2, n.getRefs().size());

    n.removeRef(set1);

    assertEquals(set2, n.getDeclaration());
    assertEquals(1, n.globalSets);
    assertEquals(1, n.getRefs().size());
  }
View Full Code Here


    assertEquals(1, n.globalSets);
    assertEquals(1, n.getRefs().size());
  }

  public void testRemoveDeclaration2() {
    Name n = new Name("a", null, false);
    Ref set1 = createNodelessRef(Ref.Type.SET_FROM_GLOBAL);
    Ref set2 = createNodelessRef(Ref.Type.SET_FROM_LOCAL);

    n.addRef(set1);
    n.addRef(set2);

    assertEquals(set1, n.getDeclaration());
    assertEquals(1, n.globalSets);
    assertEquals(1, n.localSets);
    assertEquals(2, n.getRefs().size());

    n.removeRef(set1);

    assertNull(n.getDeclaration());
    assertEquals(0, n.globalSets);
  }
View Full Code Here

  public void testSimpleReassign2() {
    test("/** @define {number|boolean} */ var DEF=false;DEF=true;DEF=3",
        "var DEF=3;true;3");

    Name def = namespace.getNameIndex().get("DEF");
    assertEquals(1, def.getRefs().size());
    assertEquals(1, def.globalSets);
    assertNotNull(def.getDeclaration());
  }
View Full Code Here

    test(
        "/** @define {boolean} */var DEF=true;" +
        "var x=function(){var y=DEF}; DEF=false",
        "var DEF=false;var x=function(){var y=DEF};false");

    Name def = namespace.getNameIndex().get("DEF");
    assertEquals(2, def.getRefs().size());
    assertEquals(1, def.globalSets);
    assertNotNull(def.getDeclaration());
  }
View Full Code Here

  public void testNamespacedDefine1() {
    test("var a = {}; /** @define {boolean} */ a.B = false; a.B = true;",
         "var a = {}; a.B = true; true;");

    Name aDotB = namespace.getNameIndex().get("a.B");
    assertEquals(1, aDotB.getRefs().size());
    assertEquals(1, aDotB.globalSets);
    assertNotNull(aDotB.getDeclaration());
  }
View Full Code Here

      checkDescendantNames(name, name.globalSets + name.localSets > 0);
    }
  }

  private void findPrototypeProps(String type, Set<String> props) {
    Name slot = namespace.getSlot(type);
    if (slot != null) {
      for (Ref ref : slot.getRefs()) {
        if (ref.type == Ref.Type.PROTOTYPE_GET) {
          Node fullName = ref.getNode().getParent().getParent();
          if (fullName.isGetProp()) {
            props.add(fullName.getLastChild().getString());
          }
View Full Code Here

  private void validateName(Name name, boolean isDefined) {
    // If the name is not defined, emit warnings for each reference. While
    // we're looking through each reference, check all the module dependencies.
    Ref declaration = name.getDeclaration();
    Name parent = name.parent;

    JSModuleGraph moduleGraph = compiler.getModuleGraph();
    for (Ref ref : name.getRefs()) {
      // Don't worry about global exprs.
      boolean isGlobalExpr = ref.getNode().getParent().isExprResult();

      if (!isDefined && !isTypedef(ref)) {
        if (!isGlobalExpr) {
          reportRefToUndefinedName(name, ref);
        }
      } else if (declaration != null &&
          ref.getModule() != declaration.getModule() &&
          !moduleGraph.dependsOn(
              ref.getModule(), declaration.getModule())) {
        reportBadModuleReference(name, ref);
      } else {
        // Check for late references.
        if (ref.scope.isGlobal()) {
          // Prototype references are special, because in our reference graph,
          // A.prototype counts as a reference to A.
          boolean isPrototypeGet = (ref.type == Ref.Type.PROTOTYPE_GET);
          Name owner = isPrototypeGet ? name : parent;
          boolean singleGlobalParentDecl =
              owner != null &&
              owner.getDeclaration() != null &&
              owner.localSets == 0;

          if (singleGlobalParentDecl &&
              owner.getDeclaration().preOrderIndex > ref.preOrderIndex) {
            String refName = isPrototypeGet
                ? name.getFullName() + ".prototype"
                : name.getFullName();
            compiler.report(
                JSError.make(ref.node,
                    NAME_DEFINED_LATE_WARNING,
                    refName,
                    owner.getFullName(),
                    owner.getDeclaration().source.getName(),
                    String.valueOf(owner.getDeclaration().node.getLineno())));
          }
        }
      }
    }
  }
View Full Code Here

    @Override
    public void visit(NodeTraversal t, Node n, Node parent) {
      RefInfo refInfo = allRefInfo.get(n);
      if (refInfo != null) {
        Ref ref = refInfo.ref;
        Name name = refInfo.name;
        String fullName = name.getFullName();
        switch (ref.type) {
          case SET_FROM_GLOBAL:
          case SET_FROM_LOCAL:
            Node valParent = getValueParent(ref);
            Node val = valParent.getLastChild();
            if (valParent.isAssign() && name.isSimpleName() &&
                name.getDeclaration() == ref) {
              // For defines, it's an error if a simple name is assigned
              // before it's declared
              compiler.report(
                  t.makeError(val, INVALID_DEFINE_INIT_ERROR, fullName));
            } else if (processDefineAssignment(t, fullName, val, valParent)) {
View Full Code Here

  private void inlineAliases(GlobalNamespace namespace) {
    // Invariant: All the names in the worklist meet condition (a).
    Deque<Name> workList = new ArrayDeque<>(namespace.getNameForest());

    while (!workList.isEmpty()) {
      Name name = workList.pop();

      // Don't attempt to inline a getter or setter property as a variable.
      if (name.type == Name.Type.GET || name.type == Name.Type.SET) {
        continue;
      }

      if (!name.inExterns && name.globalSets == 1 && name.localSets == 0 &&
          name.aliasingGets > 0) {
        // {@code name} meets condition (b). Find all of its local aliases
        // and try to inline them.
        List<Ref> refs = Lists.newArrayList(name.getRefs());
        for (Ref ref : refs) {
          if (ref.type == Type.ALIASING_GET && ref.scope.isLocal()) {
            // {@code name} meets condition (c). Try to inline it.
            // TODO(johnlenz): consider picking up new aliases at the end
            // of the pass instead of immediately like we do for global
            // inlines.
            if (inlineAliasIfPossible(ref, namespace)) {
              name.removeRef(ref);
            }
          } else if (ref.type == Type.ALIASING_GET
              && ref.scope.isGlobal()
              && ref.getTwin() == null) {  // ignore aliases in chained assignments
            if (inlineGlobalAliasIfPossible(ref, namespace)) {
              name.removeRef(ref);
            }
          }
        }
      }
View Full Code Here

    // declaration.

    Node aliasParent = alias.node.getParent();
    if (aliasParent.isAssign() && NodeUtil.isExecutedExactlyOnce(aliasParent)) {
      if (aliasParent.getFirstChild().isQualifiedName()) {
        Name name = namespace.getSlot(aliasParent.getFirstChild().getQualifiedName());
        if (name != null && isInlinableGlobalAlias(name)) {
          List<AstChange> newNodes = Lists.newArrayList();

          List<Ref> refs = Lists.newArrayList(name.getRefs());
          for (Ref ref : refs) {
            switch (ref.type) {
              case SET_FROM_GLOBAL:
                continue;
              case DIRECT_GET:
              case ALIASING_GET:
                Node newNode = alias.node.cloneTree();
                Node node = ref.node;
                node.getParent().replaceChild(node, newNode);
                newNodes.add(new AstChange(ref.module, ref.scope, newNode));
                name.removeRef(ref);
                break;
              default:
                throw new IllegalStateException();
            }
          }
View Full Code Here

TOP

Related Classes of com.google.javascript.jscomp.GlobalNamespace.Name

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.