Java Code Examples for com.google.javascript.rhino.Token#CAST

The following examples show how to use com.google.javascript.rhino.Token#CAST . 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: NodeUtil.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true if this is an immutable value.
 */
static boolean isImmutableValue(Node n) {
  switch (n.getType()) {
    case Token.STRING:
    case Token.NUMBER:
    case Token.NULL:
    case Token.TRUE:
    case Token.FALSE:
      return true;
    case Token.CAST:
    case Token.NOT:
      return isImmutableValue(n.getFirstChild());
    case Token.VOID:
    case Token.NEG:
      return isImmutableValue(n.getFirstChild());
    case Token.NAME:
      String name = n.getString();
      // We assume here that programs don't change the value of the keyword
      // undefined to something other than the value undefined.
      return "undefined".equals(name)
          || "Infinity".equals(name)
          || "NaN".equals(name);
  }

  return false;
}
 
Example 2
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Apply the supplied predicate against
 * all possible result Nodes of the expression.
 */
static boolean allResultsMatch(Node n, Predicate<Node> p) {
  switch (n.getType()) {
    case Token.CAST:
      return allResultsMatch(n.getFirstChild(), p);
    case Token.ASSIGN:
    case Token.COMMA:
      return allResultsMatch(n.getLastChild(), p);
    case Token.AND:
    case Token.OR:
      return allResultsMatch(n.getFirstChild(), p)
          && allResultsMatch(n.getLastChild(), p);
    case Token.HOOK:
      return allResultsMatch(n.getFirstChild().getNext(), p)
          && allResultsMatch(n.getLastChild(), p);
    default:
      return p.apply(n);
  }
}
 
Example 3
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Apply the supplied predicate against
 * all possible result Nodes of the expression.
 */
static boolean anyResultsMatch(Node n, Predicate<Node> p) {
  switch (n.getType()) {
    case Token.CAST:
      return anyResultsMatch(n.getFirstChild(), p);
    case Token.ASSIGN:
    case Token.COMMA:
      return anyResultsMatch(n.getLastChild(), p);
    case Token.AND:
    case Token.OR:
      return anyResultsMatch(n.getFirstChild(), p)
          || anyResultsMatch(n.getLastChild(), p);
    case Token.HOOK:
      return anyResultsMatch(n.getFirstChild().getNext(), p)
          || anyResultsMatch(n.getLastChild(), p);
    default:
      return p.apply(n);
  }
}
 
Example 4
Source File: Closure_125_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether this node is testing for the existence of a property.
 * If true, we will not emit warnings about a missing property.
 *
 * @param getProp The GETPROP being tested.
 */
private boolean isPropertyTest(Node getProp) {
  Node parent = getProp.getParent();
  switch (parent.getType()) {
    case Token.CALL:
      return parent.getFirstChild() != getProp &&
          compiler.getCodingConvention().isPropertyTestFunction(parent);

    case Token.IF:
    case Token.WHILE:
    case Token.DO:
    case Token.FOR:
      return NodeUtil.getConditionExpression(parent) == getProp;

    case Token.INSTANCEOF:
    case Token.TYPEOF:
      return true;

    case Token.AND:
    case Token.HOOK:
      return parent.getFirstChild() == getProp;

    case Token.NOT:
      return parent.getParent().isOr() &&
          parent.getParent().getFirstChild() == parent;

    case Token.CAST:
      return isPropertyTest(parent);
  }
  return false;
}
 
Example 5
Source File: Closure_125_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether this node is testing for the existence of a property.
 * If true, we will not emit warnings about a missing property.
 *
 * @param getProp The GETPROP being tested.
 */
private boolean isPropertyTest(Node getProp) {
  Node parent = getProp.getParent();
  switch (parent.getType()) {
    case Token.CALL:
      return parent.getFirstChild() != getProp &&
          compiler.getCodingConvention().isPropertyTestFunction(parent);

    case Token.IF:
    case Token.WHILE:
    case Token.DO:
    case Token.FOR:
      return NodeUtil.getConditionExpression(parent) == getProp;

    case Token.INSTANCEOF:
    case Token.TYPEOF:
      return true;

    case Token.AND:
    case Token.HOOK:
      return parent.getFirstChild() == getProp;

    case Token.NOT:
      return parent.getParent().isOr() &&
          parent.getParent().getFirstChild() == parent;

    case Token.CAST:
      return isPropertyTest(parent);
  }
  return false;
}
 
Example 6
Source File: Closure_122_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
private boolean validAssignmentTarget(Node target) {
  switch (target.getType()) {
    case Token.CAST: // CAST is a bit weird, but syntactically valid.
    case Token.NAME:
    case Token.GETPROP:
    case Token.GETELEM:
      return true;
  }
  return false;
}
 
Example 7
Source File: Closure_122_IRFactory_s.java    From coming with MIT License 5 votes vote down vote up
private boolean validAssignmentTarget(Node target) {
  switch (target.getType()) {
    case Token.CAST: // CAST is a bit weird, but syntactically valid.
    case Token.NAME:
    case Token.GETPROP:
    case Token.GETELEM:
      return true;
  }
  return false;
}
 
Example 8
Source File: Normalize.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  switch (n.getType()) {
    case Token.WHILE:
      if (CONVERT_WHILE_TO_FOR) {
        Node expr = n.getFirstChild();
        n.setType(Token.FOR);
        Node empty = IR.empty();
        empty.copyInformationFrom(n);
        n.addChildBefore(empty, expr);
        n.addChildAfter(empty.cloneNode(), expr);
        reportCodeChange("WHILE node");
      }
      break;

    case Token.FUNCTION:
      normalizeFunctionDeclaration(n);
      break;

    case Token.NAME:
    case Token.STRING:
    case Token.STRING_KEY:
    case Token.GETTER_DEF:
    case Token.SETTER_DEF:
      if (!compiler.getLifeCycleStage().isNormalizedObfuscated()) {
        annotateConstantsByConvention(n, parent);
      }
      break;

    case Token.CAST:
      parent.replaceChild(n, n.removeFirstChild());
      break;
  }
}
 
Example 9
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns true if this is a literal value. We define a literal value
 * as any node that evaluates to the same thing regardless of when or
 * where it is evaluated. So /xyz/ and [3, 5] are literals, but
 * the name a is not.
 *
 * Function literals do not meet this definition, because they
 * lexically capture variables. For example, if you have
 * <code>
 * function() { return a; }
 * </code>
 * If it is evaluated in a different scope, then it
 * captures a different variable. Even if the function did not read
 * any captured variables directly, it would still fail this definition,
 * because it affects the lifecycle of variables in the enclosing scope.
 *
 * However, a function literal with respect to a particular scope is
 * a literal.
 *
 * @param includeFunctions If true, all function expressions will be
 *     treated as literals.
 */
static boolean isLiteralValue(Node n, boolean includeFunctions) {
  switch (n.getType()) {
    case Token.CAST:
      return isLiteralValue(n.getFirstChild(), includeFunctions);

    case Token.ARRAYLIT:
      for (Node child = n.getFirstChild(); child != null;
           child = child.getNext()) {
        if ((!child.isEmpty()) && !isLiteralValue(child, includeFunctions)) {
          return false;
        }
      }
      return true;

    case Token.REGEXP:
      // Return true only if all children are const.
      for (Node child = n.getFirstChild(); child != null;
           child = child.getNext()) {
        if (!isLiteralValue(child, includeFunctions)) {
          return false;
        }
      }
      return true;

    case Token.OBJECTLIT:
      // Return true only if all values are const.
      for (Node child = n.getFirstChild(); child != null;
           child = child.getNext()) {
        if (!isLiteralValue(child.getFirstChild(), includeFunctions)) {
          return false;
        }
      }
      return true;

    case Token.FUNCTION:
      return includeFunctions && !NodeUtil.isFunctionDeclaration(n);

    default:
      return isImmutableValue(n);
  }
}
 
Example 10
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
static int precedence(int type) {
  switch (type) {
    case Token.COMMA:  return 0;
    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_ADD:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_DIV:
    case Token.ASSIGN_MOD:
    case Token.ASSIGN: return 1;
    case Token.HOOK:   return 2;  // ?: operator
    case Token.OR:     return 3;
    case Token.AND:    return 4;
    case Token.BITOR:  return 5;
    case Token.BITXOR: return 6;
    case Token.BITAND: return 7;
    case Token.EQ:
    case Token.NE:
    case Token.SHEQ:
    case Token.SHNE:   return 8;
    case Token.LT:
    case Token.GT:
    case Token.LE:
    case Token.GE:
    case Token.INSTANCEOF:
    case Token.IN:     return 9;
    case Token.LSH:
    case Token.RSH:
    case Token.URSH:   return 10;
    case Token.SUB:
    case Token.ADD:    return 11;
    case Token.MUL:
    case Token.MOD:
    case Token.DIV:    return 12;
    case Token.INC:
    case Token.DEC:
    case Token.NEW:
    case Token.DELPROP:
    case Token.TYPEOF:
    case Token.VOID:
    case Token.NOT:
    case Token.BITNOT:
    case Token.POS:
    case Token.NEG:    return 13;

    case Token.CALL:
    case Token.GETELEM:
    case Token.GETPROP:
    // Data values
    case Token.ARRAYLIT:
    case Token.EMPTY:  // TODO(johnlenz): remove this.
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.STRING:
    case Token.STRING_KEY:
    case Token.THIS:
    case Token.TRUE:
      return 15;
    case Token.CAST:
      return 16;

    default: throw new Error("Unknown precedence for " +
                             Token.name(type) +
                             " (type " + type + ")");
  }
}
 
Example 11
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param locals A predicate to apply to unknown local values.
 * @return Whether the node is known to be a value that is not a reference
 *     outside the expression scope.
 */
static boolean evaluatesToLocalValue(Node value, Predicate<Node> locals) {
  switch (value.getType()) {
    case Token.CAST:
      return evaluatesToLocalValue(value.getFirstChild(), locals);
    case Token.ASSIGN:
      // A result that is aliased by a non-local name, is the effectively the
      // same as returning a non-local name, but this doesn't matter if the
      // value is immutable.
      return NodeUtil.isImmutableValue(value.getLastChild())
          || (locals.apply(value)
              && evaluatesToLocalValue(value.getLastChild(), locals));
    case Token.COMMA:
      return evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.AND:
    case Token.OR:
      return evaluatesToLocalValue(value.getFirstChild(), locals)
         && evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.HOOK:
      return evaluatesToLocalValue(value.getFirstChild().getNext(), locals)
         && evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.INC:
    case Token.DEC:
      if (value.getBooleanProp(Node.INCRDECR_PROP)) {
        return evaluatesToLocalValue(value.getFirstChild(), locals);
      } else {
        return true;
      }
    case Token.THIS:
      return locals.apply(value);
    case Token.NAME:
      return isImmutableValue(value) || locals.apply(value);
    case Token.GETELEM:
    case Token.GETPROP:
      // There is no information about the locality of object properties.
      return locals.apply(value);
    case Token.CALL:
      return callHasLocalResult(value)
          || isToStringMethodCall(value)
          || locals.apply(value);
    case Token.NEW:
      return newHasLocalResult(value)
             || locals.apply(value);
    case Token.FUNCTION:
    case Token.REGEXP:
    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      // Literals objects with non-literal children are allowed.
      return true;
    case Token.DELPROP:
    case Token.IN:
      // TODO(johnlenz): should IN operator be included in #isSimpleOperator?
      return true;
    default:
      // Other op force a local value:
      //  x = '' + g (x is now an local string)
      //  x -= g (x is now an local number)
      if (isAssignmentOp(value)
          || isSimpleOperator(value)
          || isImmutableValue(value)) {
        return true;
      }

      throw new IllegalStateException(
          "Unexpected expression node" + value +
          "\n parent:" + value.getParent());
  }
}
 
Example 12
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @returns false iff the result of the expression is not consumed.
 */
static boolean isExpressionResultUsed(Node expr) {
  // TODO(johnlenz): consider sharing some code with trySimpleUnusedResult.
  Node parent = expr.getParent();
  switch (parent.getType()) {
    case Token.BLOCK:
    case Token.EXPR_RESULT:
      return false;
    case Token.CAST:
      return isExpressionResultUsed(parent);
    case Token.HOOK:
    case Token.AND:
    case Token.OR:
      return (expr == parent.getFirstChild())
          ? true : isExpressionResultUsed(parent);
    case Token.COMMA:
      Node gramps = parent.getParent();
      if (gramps.isCall() &&
          parent == gramps.getFirstChild()) {
        // Semantically, a direct call to eval is different from an indirect
        // call to an eval. See ECMA-262 S15.1.2.1. So it's OK for the first
        // expression to a comma to be a no-op if it's used to indirect
        // an eval. This we pretend that this is "used".
        if (expr == parent.getFirstChild() &&
            parent.getChildCount() == 2 &&
            expr.getNext().isName() &&
            "eval".equals(expr.getNext().getString())) {
          return true;
        }
      }

      return (expr == parent.getFirstChild())
          ? false : isExpressionResultUsed(parent);
    case Token.FOR:
      if (!NodeUtil.isForIn(parent)) {
        // Only an expression whose result is in the condition part of the
        // expression is used.
        return (parent.getChildAtIndex(1) == expr);
      }
      break;
  }
  return true;
}