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

The following examples show how to use com.google.javascript.rhino.Token#FUNCTION . 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_60_NodeUtil_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * @param nameNode A name node
 * @return The JSDocInfo for the name node
 */
static JSDocInfo getInfoForNameNode(Node nameNode) {
  JSDocInfo info = null;
  Node parent = null;
  if (nameNode != null) {
    info = nameNode.getJSDocInfo();
    parent = nameNode.getParent();
  }

  if (info == null && parent != null &&
      ((parent.getType() == Token.VAR && parent.hasOneChild()) ||
        parent.getType() == Token.FUNCTION)) {
    info = parent.getJSDocInfo();
  }
  return info;
}
 
Example 2
Source File: jMutRepair_003_t.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: Closure_80_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 4
Source File: Cardumen_00200_t.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 5
Source File: CheckAccessControls.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  switch (n.getType()) {
    case Token.NAME:
      checkNameDeprecation(t, n, parent);
      checkNameVisibility(t, n, parent);
      break;
    case Token.GETPROP:
      checkPropertyDeprecation(t, n, parent);
      checkPropertyVisibility(t, n, parent);
      checkConstantProperty(t, n);
      break;
    case Token.NEW:
      checkConstructorDeprecation(t, n, parent);
      break;
    case Token.FUNCTION:
      checkFinalClassOverrides(t, n, parent);
      break;
  }
}
 
Example 6
Source File: Closure_2_TypeCheck_t.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 7
Source File: Closure_103_ControlFlowAnalysis_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determines if the subtree might throw an exception.
 */
private static boolean mayThrowException(Node n) {
  switch (n.getType()) {
    case Token.CALL:
    case Token.GETPROP:
    case Token.GETELEM:
    case Token.THROW:
    case Token.NEW:
    case Token.ASSIGN:
    case Token.INC:
    case Token.DEC:
    case Token.INSTANCEOF:
      return true;
    case Token.FUNCTION:
      return false;
  }
  for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
    if (!ControlFlowGraph.isEnteringNewCfgNode(c) && mayThrowException(c)) {
      return true;
    }
  }
  return false;
}
 
Example 8
Source File: Closure_80_NodeUtil_t.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 9
Source File: Closure_80_NodeUtil_t.java    From coming with MIT License 5 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 vairables 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.ARRAYLIT:
    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: Closure_89_GlobalNamespace_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether a set operation is a constructor or enumeration
 * declaration. The set operation may either be an assignment to a name,
 * a variable declaration, or an object literal key mapping.
 *
 * @param n The node that represents the name being set
 * @param parent Parent node of {@code n} (an ASSIGN, VAR, or OBJLIT node)
 * @return Whether the set operation is either a constructor or enum
 *     declaration
 */
private boolean isConstructorOrEnumDeclaration(Node n, Node parent) {
  // NOTE(nicksantos): This does not handle named constructors
  // function a() {}
  // For legacy reasons, we should not fix this, because we do not
  // know who's depending on the current behavior.

  JSDocInfo info;
  int valueNodeType;
  switch (parent.getType()) {
    case Token.ASSIGN:
      info = parent.getJSDocInfo();
      valueNodeType = n.getNext().getType();
      break;
    case Token.VAR:
      info = n.getJSDocInfo();
      if (info == null) {
        info = parent.getJSDocInfo();
      }
      Node valueNode = n.getFirstChild();
      valueNodeType = valueNode != null ? valueNode.getType() : Token.VOID;
      break;
    default:
      return false;
  }
  // Heed the annotations only if they're sensibly used.
  return info != null &&
         (info.isConstructor() && valueNodeType == Token.FUNCTION ||
          info.hasEnumParameterType() && valueNodeType == Token.OBJECTLIT);
}
 
Example 11
Source File: Closure_60_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/** Creates function name(params_0, ..., params_n) { body }. */
public static Node newFunctionNode(String name, List<Node> params,
    Node body, int lineno, int charno) {
  Node parameterParen = new Node(Token.LP, lineno, charno);
  for (Node param : params) {
    parameterParen.addChildToBack(param);
  }
  Node function = new Node(Token.FUNCTION, lineno, charno);
  function.addChildrenToBack(
      Node.newString(Token.NAME, name, lineno, charno));
  function.addChildToBack(parameterParen);
  function.addChildToBack(body);
  return function;
}
 
Example 12
Source File: Cardumen_00149_t.java    From coming with MIT License 5 votes vote down vote up
/** Gets the r-value of a node returned by getBestLValue. */
static Node getRValueOfLValue(Node n) {
  Node parent = n.getParent();
  switch (parent.getType()) {
    case Token.ASSIGN:
      return n.getNext();
    case Token.VAR:
      return n.getFirstChild();
    case Token.FUNCTION:
      return parent;
  }
  return null;
}
 
Example 13
Source File: Cardumen_00200_t.java    From coming with MIT License 5 votes vote down vote up
/** Gets the r-value of a node returned by getBestLValue. */
static Node getRValueOfLValue(Node n) {
  Node parent = n.getParent();
  switch (parent.getType()) {
    case Token.ASSIGN:
      return n.getNext();
    case Token.VAR:
      return n.getFirstChild();
    case Token.FUNCTION:
      return parent;
  }
  return null;
}
 
Example 14
Source File: Cardumen_0014_s.java    From coming with MIT License 5 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.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 15
Source File: Closure_94_NodeUtil_t.java    From coming with MIT License 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.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) || locals.apply(value);
    case Token.NEW:
      return true;
    case Token.FUNCTION:
    case Token.REGEXP:
    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      // Literals objects with non-literal children are allowed.
      return true;
    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 16
Source File: Closure_75_NodeUtil_s.java    From coming with MIT License 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.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 17
Source File: jMutRepair_003_t.java    From coming with MIT License 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;

    default: throw new Error("Unknown precedence for " +
                             Token.name(type) +
                             " (type " + type + ")");
  }
}
 
Example 18
Source File: Closure_14_ControlFlowAnalysis_s.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  switch (n.getType()) {
    case Token.IF:
      handleIf(n);
      return;
    case Token.WHILE:
      handleWhile(n);
      return;
    case Token.DO:
      handleDo(n);
      return;
    case Token.FOR:
      handleFor(n);
      return;
    case Token.SWITCH:
      handleSwitch(n);
      return;
    case Token.CASE:
      handleCase(n);
      return;
    case Token.DEFAULT_CASE:
      handleDefault(n);
      return;
    case Token.BLOCK:
    case Token.SCRIPT:
      handleStmtList(n);
      return;
    case Token.FUNCTION:
      handleFunction(n);
      return;
    case Token.EXPR_RESULT:
      handleExpr(n);
      return;
    case Token.THROW:
      handleThrow(n);
      return;
    case Token.TRY:
      handleTry(n);
      return;
    case Token.CATCH:
      handleCatch(n);
      return;
    case Token.BREAK:
      handleBreak(n);
      return;
    case Token.CONTINUE:
      handleContinue(n);
      return;
    case Token.RETURN:
      handleReturn(n);
      return;
    case Token.WITH:
      handleWith(n);
      return;
    case Token.LABEL:
      return;
    default:
      handleStmt(n);
      return;
  }
}
 
Example 19
Source File: Closure_86_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 20
Source File: Closure_80_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Is this node a hoisted function declaration? A function declaration in the
 * scope root is hoisted to the top of the scope.
 * See {@link #isFunctionDeclaration}).
 */
static boolean isHoistedFunctionDeclaration(Node n) {
  return isFunctionDeclaration(n)
      && (n.getParent().getType() == Token.SCRIPT
          || n.getParent().getParent().getType() == Token.FUNCTION);
}