Java Code Examples for com.google.javascript.rhino.Node#getType()

The following examples show how to use com.google.javascript.rhino.Node#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Closure_61_NodeUtil_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a Node at the top of the current scope where we can add new var
 * declarations as children.
 */
private static Node getAddingRoot(Node n) {
  Node addingRoot = null;
  Node ancestor = n;
  while (null != (ancestor = ancestor.getParent())) {
    int type = ancestor.getType();
    if (type == Token.SCRIPT) {
      addingRoot = ancestor;
      break;
    } else if (type == Token.FUNCTION) {
      addingRoot = ancestor.getLastChild();
      break;
    }
  }

  // make sure that the adding root looks ok
  Preconditions.checkState(addingRoot.getType() == Token.BLOCK ||
      addingRoot.getType() == Token.SCRIPT);
  Preconditions.checkState(addingRoot.getFirstChild() == null ||
      addingRoot.getFirstChild().getType() != Token.SCRIPT);
  return addingRoot;
}
 
Example 2
Source File: Cardumen_00149_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a Node at the top of the current scope where we can add new var
 * declarations as children.
 */
private static Node getAddingRoot(Node n) {
  Node addingRoot = null;
  Node ancestor = n;
  while (null != (ancestor = ancestor.getParent())) {
    int type = ancestor.getType();
    if (type == Token.SCRIPT) {
      addingRoot = ancestor;
      break;
    } else if (type == Token.FUNCTION) {
      addingRoot = ancestor.getLastChild();
      break;
    }
  }

  // make sure that the adding root looks ok
  Preconditions.checkState(addingRoot.isBlock() ||
      addingRoot.isScript());
  Preconditions.checkState(addingRoot.getFirstChild() == null ||
      !addingRoot.getFirstChild().isScript());
  return addingRoot;
}
 
Example 3
Source File: CrossModuleCodeMotion.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * return true if the node has any form of conditional in its ancestry
 * TODO(nicksantos) keep track of the conditionals in the ancestry, so
 * that we don't have to recrawl it.
 */
private boolean hasConditionalAncestor(Node n) {
  for (Node ancestor : n.getAncestors()) {
    switch (ancestor.getType()) {
      case Token.DO:
      case Token.FOR:
      case Token.HOOK:
      case Token.IF:
      case Token.SWITCH:
      case Token.WHILE:
      case Token.FUNCTION:
        return true;
    }
  }
  return false;
}
 
Example 4
Source File: Closure_103_DisambiguateProperties_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Processes a OBJECTLIT node.
 */
private void handleObjectLit(NodeTraversal t, Node n) {
  Node child = n.getFirstChild();
  while (child != null) {
    if (child.getType() == Token.STRING) {
      // We should never see a mix of numbers and strings.
      String name = child.getString();
      T type = typeSystem.getType(getScope(), n, name);

      Property prop = getProperty(name);
      if (!prop.scheduleRenaming(child,
                                 processProperty(t, prop, type, null))) {
        if (showInvalidationWarnings) {
          compiler.report(JSError.make(
              t.getSourceName(), child, INVALIDATION, name,
              (type == null ? "null" : type.toString()), n.toString()));
        }
      }
    }

    child = child.getNext().getNext();
  }
}
 
Example 5
Source File: Closure_66_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
private void checkNoTypeCheckSection(Node n, boolean enterSection) {
  switch (n.getType()) {
    case Token.SCRIPT:
    case Token.BLOCK:
    case Token.VAR:
    case Token.FUNCTION:
    case Token.ASSIGN:
      JSDocInfo info = n.getJSDocInfo();
      if (info != null && info.isNoTypeCheck()) {
        if (enterSection) {
          noTypeCheckSection++;
        } else {
          noTypeCheckSection--;
        }
      }
      validator.setShouldReport(noTypeCheckSection == 0);
      break;
  }
}
 
Example 6
Source File: ReferenceCollectingCallback.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
boolean isLvalue() {
  Node parent = getParent();
  int parentType = parent.getType();
  return (parentType == Token.VAR && nameNode.getFirstChild() != null)
      || parentType == Token.INC
      || parentType == Token.DEC
      || (NodeUtil.isAssignmentOp(parent)
          && parent.getFirstChild() == nameNode)
      || isLhsOfForInExpression(nameNode);
}
 
Example 7
Source File: jMutRepair_003_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns true if the operator on this node is relational.
 * the returned set does not include the equalities.
 */
static boolean isRelationalOperation(Node n) {
  switch (n.getType()) {
    case Token.GT: // equal
    case Token.GE: // not equal
    case Token.LT: // exactly equal
    case Token.LE: // exactly not equal
      return true;
  }
  return false;
}
 
Example 8
Source File: Closure_96_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Checks the initializer of an enum. An enum can be initialized with an
 * object literal whose values must be subtypes of the declared enum element
 * type, or by copying another enum.</p>
 *
 * <p>In the case of an enum copy, we verify that the enum element type of the
 * enum used for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Examples:</p>
 * <pre>var myEnum = {FOO: ..., BAR: ...};
 * var myEnum = myOtherEnum;</pre>
 *
 * @param value the value used for initialization of the enum
 * @param primitiveType The type of each element of the enum.
 */
private void checkEnumInitializer(
    NodeTraversal t, Node value, JSType primitiveType) {
  if (value.getType() == Token.OBJECTLIT) {
    // re-using value as the value of the object literal and advancing twice
    value = value.getFirstChild();
    value = (value == null) ? null : value.getNext();
    while (value != null) {
      // the value's type must be assignable to the enum's primitive type
      validator.expectCanAssignTo(t, value, getJSType(value), primitiveType,
          "element type must match enum's type");

      // advancing twice
      value = value.getNext();
      value = (value == null) ? null : value.getNext();
    }
  } else if (value.getJSType() instanceof EnumType) {
    // TODO(user): Remove the instanceof check in favor
    // of a type.isEnumType() predicate. Currently, not all enum types are
    // implemented by the EnumClass, e.g. the unknown type and the any
    // type. The types need to be defined by interfaces such that an
    // implementation can implement multiple types interface.
    EnumType valueEnumType = (EnumType) value.getJSType();
    JSType valueEnumPrimitiveType =
        valueEnumType.getElementsType().getPrimitiveType();
    validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
        primitiveType, "incompatible enum element types");
  } else {
    // The error condition is handled in TypedScopeCreator.
  }
}
 
Example 9
Source File: Closure_48_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
private void attachLiteralTypes(NodeTraversal t, Node n) {
  switch (n.getType()) {
    case Token.NULL:
      n.setJSType(getNativeType(NULL_TYPE));
      break;

    case Token.VOID:
      n.setJSType(getNativeType(VOID_TYPE));
      break;

    case Token.STRING:
      // Defer keys to the Token.OBJECTLIT case
      if (!NodeUtil.isObjectLitKey(n, n.getParent())) {
        n.setJSType(getNativeType(STRING_TYPE));
      }
      break;

    case Token.NUMBER:
      n.setJSType(getNativeType(NUMBER_TYPE));
      break;

    case Token.TRUE:
    case Token.FALSE:
      n.setJSType(getNativeType(BOOLEAN_TYPE));
      break;

    case Token.REGEXP:
      n.setJSType(getNativeType(REGEXP_TYPE));
      break;

    case Token.OBJECTLIT:
      defineObjectLiteral(t, n);
      break;

      // NOTE(nicksantos): If we ever support Array tuples,
      // we will need to put ARRAYLIT here as well.
  }
}
 
Example 10
Source File: 1_NodeUtil.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determines whether a node represents an object literal key
 * (e.g. key1 in {key1: value1, key2: value2}).
 *
 * @param node A node
 * @param parent The node's parent
 */
static boolean isObjectLitKey(Node node, Node parent) {
  switch (node.getType()) {
    case Token.NUMBER:
    case Token.STRING:
      return parent.getType() == Token.OBJECTLIT;
    case Token.GET:
    case Token.SET:
      return true;
  }
  return false;
}
 
Example 11
Source File: Closure_12_MaybeReachingVariableUse_s.java    From coming with MIT License 4 votes vote down vote up
private void computeMayUse(
    Node n, Node cfgNode, ReachingUses output, boolean conditional) {
  switch (n.getType()) {

    case Token.BLOCK:
    case Token.FUNCTION:
      return;

    case Token.NAME:
      addToUseIfLocal(n.getString(), cfgNode, output);
      return;

    case Token.WHILE:
    case Token.DO:
    case Token.IF:
      computeMayUse(
          NodeUtil.getConditionExpression(n), cfgNode, output, conditional);
      return;

    case Token.FOR:
      if (!NodeUtil.isForIn(n)) {
        computeMayUse(
            NodeUtil.getConditionExpression(n), cfgNode, output, conditional);
      } else {
        // for(x in y) {...}
        Node lhs = n.getFirstChild();
        Node rhs = lhs.getNext();
        if (lhs.isVar()) {
          lhs = lhs.getLastChild(); // for(var x in y) {...}
        }
        if (lhs.isName() && !conditional) {
          removeFromUseIfLocal(lhs.getString(), output);
        }
        computeMayUse(rhs, cfgNode, output, conditional);
      }
      return;

    case Token.AND:
    case Token.OR:
      computeMayUse(n.getLastChild(), cfgNode, output, true);
      computeMayUse(n.getFirstChild(), cfgNode, output, conditional);
      return;

    case Token.HOOK:
      computeMayUse(n.getLastChild(), cfgNode, output, true);
      computeMayUse(n.getFirstChild().getNext(), cfgNode, output, true);
      computeMayUse(n.getFirstChild(), cfgNode, output, conditional);
      return;

    case Token.VAR:
      Node varName = n.getFirstChild();
      Preconditions.checkState(n.hasChildren(), "AST should be normalized");

      if (varName.hasChildren()) {
        computeMayUse(varName.getFirstChild(), cfgNode, output, conditional);
        if (!conditional) {
          removeFromUseIfLocal(varName.getString(), output);
        }
      }
      return;

    default:
      if (NodeUtil.isAssignmentOp(n) && n.getFirstChild().isName()) {
        Node name = n.getFirstChild();
        if (!conditional) {
          removeFromUseIfLocal(name.getString(), output);
        }

        // In case of a += "Hello". There is a read of a.
        if (!n.isAssign()) {
          addToUseIfLocal(name.getString(), cfgNode, output);
        }

        computeMayUse(name.getNext(), cfgNode, output, conditional);
      } else {
        /*
         * We want to traverse in reverse order because we want the LAST
         * definition in the sub-tree....
         * But we have no better way to traverse in reverse other :'(
         */
        for (Node c = n.getLastChild(); c != null; c = n.getChildBefore(c)) {
          computeMayUse(c, cfgNode, output, conditional);
        }
      }
  }
}
 
Example 12
Source File: Closure_60_NodeUtil_t.java    From coming with MIT License 4 votes vote down vote up
static boolean isNull(Node n) {
  return n.getType() == Token.NULL;
}
 
Example 13
Source File: Closure_86_NodeUtil_t.java    From coming with MIT License 4 votes vote down vote up
public boolean apply(Node n) {
  return n.getType() == Token.NAME
      && n.getString().equals(name);
}
 
Example 14
Source File: Closure_75_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
public boolean apply(Node n) {
  return isFunctionDeclaration(n) || n.getType() == Token.VAR;
}
 
Example 15
Source File: Closure_80_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Is this a NAME node?
 */
static boolean isName(Node n) {
  return n.getType() == Token.NAME;
}
 
Example 16
Source File: Closure_80_NodeUtil_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Is this a GETPROP node?
 */
static boolean isGetProp(Node n) {
  return n.getType() == Token.GETPROP;
}
 
Example 17
Source File: Closure_94_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Is this a FUNCTION node?
 */
static boolean isFunction(Node n) {
  return n.getType() == Token.FUNCTION;
}
 
Example 18
Source File: Closure_60_NodeUtil_s.java    From coming with MIT License 2 votes vote down vote up
/**
 * When converting arrays to string using Array.prototype.toString or
 * Array.prototype.join, the rules for conversion to String are different
 * than converting each element individually.  Specifically, "null" and
 * "undefined" are converted to an empty string.
 * @param n A node that is a member of an Array.
 * @return The string representation.
 */
static String getArrayElementStringValue(Node n) {
  return (NodeUtil.isNullOrUndefined(n) || n.getType() == Token.EMPTY)
      ? "" : getStringValue(n);
}
 
Example 19
Source File: 1_ClosureCodingConvention.java    From SimFix with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Given a qualified name node, returns whether "prototype" is at the end.
 * For example:
 * a.b.c => false
 * a.b.c.prototype => true
 */
private boolean endsWithPrototype(Node qualifiedName) {
  return qualifiedName.getType() == Token.GETPROP &&
      qualifiedName.getLastChild().getString().equals("prototype");
}
 
Example 20
Source File: Closure_61_NodeUtil_s.java    From coming with MIT License 2 votes vote down vote up
/**
 * Is this node a call expression statement?
 *
 * @param n The node
 * @return True if {@code n} is EXPR_RESULT and {@code n}'s
 *     first child is CALL
 */
static boolean isExprCall(Node n) {
  return n.getType() == Token.EXPR_RESULT
      && n.getFirstChild().getType() == Token.CALL;
}