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

The following examples show how to use com.google.javascript.rhino.Token#ASSIGN . 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_125_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 2
Source File: Cardumen_00200_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets the function's name. This method recognizes five forms:
 * <ul>
 * <li>{@code function name() ...}</li>
 * <li>{@code var name = function() ...}</li>
 * <li>{@code qualified.name = function() ...}</li>
 * <li>{@code var name2 = function name1() ...}</li>
 * <li>{@code qualified.name2 = function name1() ...}</li>
 * </ul>
 * In two last cases with named function expressions, the second name is
 * returned (the variable of qualified name).
 *
 * @param n a node whose type is {@link Token#FUNCTION}
 * @return the function's name, or {@code null} if it has no name
 */
static String getFunctionName(Node n) {
  Preconditions.checkState(n.isFunction());
  Node parent = n.getParent();
  switch (parent.getType()) {
    case Token.NAME:
      // var name = function() ...
      // var name2 = function name1() ...
      return parent.getQualifiedName();

    case Token.ASSIGN:
      // qualified.name = function() ...
      // qualified.name2 = function name1() ...
      return parent.getFirstChild().getQualifiedName();

    default:
      // function name() ...
      String name = n.getFirstChild().getQualifiedName();
      return name;
  }
}
 
Example 3
Source File: Closure_75_NodeUtil_t.java    From coming with MIT License 6 votes vote down vote up
static boolean isAssignmentOp(Node n) {
  switch (n.getType()){
    case Token.ASSIGN:
    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:
      return true;
  }
  return false;
}
 
Example 4
Source File: GatherSideEffectSubexpressionsCallbackTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private Node getSideEffectsHookNode() {
  Node hookNode = new Node(Token.HOOK);

  Node assign1 = new Node(Token.ASSIGN);
  assign1.addChildToBack(Node.newString(Token.NAME, "bar"));
  assign1.addChildToBack(Node.newNumber(0));

  Node assign2 = new Node(Token.ASSIGN);
  assign2.addChildToBack(Node.newString(Token.NAME, "baz"));
  assign2.addChildToBack(Node.newNumber(0));

  hookNode.addChildToBack(Node.newString(Token.NAME, "foo"));
  hookNode.addChildToBack(assign1);
  hookNode.addChildToBack(assign2);

  return hookNode;
}
 
Example 5
Source File: Cardumen_0014_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets the function's name. This method recognizes five forms:
 * <ul>
 * <li>{@code function name() ...}</li>
 * <li>{@code var name = function() ...}</li>
 * <li>{@code qualified.name = function() ...}</li>
 * <li>{@code var name2 = function name1() ...}</li>
 * <li>{@code qualified.name2 = function name1() ...}</li>
 * </ul>
 * In two last cases with named function expressions, the second name is
 * returned (the variable of qualified name).
 *
 * @param n a node whose type is {@link Token#FUNCTION}
 * @return the function's name, or {@code null} if it has no name
 */
static String getFunctionName(Node n) {
  Preconditions.checkState(n.isFunction());
  Node parent = n.getParent();
  switch (parent.getType()) {
    case Token.NAME:
      // var name = function() ...
      // var name2 = function name1() ...
      return parent.getQualifiedName();

    case Token.ASSIGN:
      // qualified.name = function() ...
      // qualified.name2 = function name1() ...
      return parent.getFirstChild().getQualifiedName();

    default:
      // function name() ...
      String name = n.getFirstChild().getQualifiedName();
      return name;
  }
}
 
Example 6
Source File: Cardumen_0014_t.java    From coming with MIT License 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.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 7
Source File: Closure_66_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 8
Source File: Closure_53_InlineObjectLiterals_t.java    From coming with MIT License 5 votes vote down vote up
private boolean isVarOrAssignExprLhs(Node n) {
  Node parent = n.getParent();
  return parent.getType() == Token.VAR ||
      (parent.getType() == Token.ASSIGN
          && parent.getFirstChild() == n
          && parent.getParent().getType() == Token.EXPR_RESULT);
}
 
Example 9
Source File: Cardumen_0020_t.java    From coming with MIT License 5 votes vote down vote up
@Override
public void remove() {
  // Setters have VAR, FUNCTION, or ASSIGN parent nodes. CALL parent
  // nodes are global refs, and are handled later in this function.
  Node containingNode = parent.getParent();
  switch (parent.getType()) {
    case Token.VAR:
      Preconditions.checkState(parent.hasOneChild());
      replaceWithRhs(containingNode, parent);
      break;
    case Token.FUNCTION:
      replaceWithRhs(containingNode, parent);
      break;
    case Token.ASSIGN:
      if (containingNode.isExprResult()) {
        replaceWithRhs(containingNode.getParent(), containingNode);
      } else {
        replaceWithRhs(containingNode, parent);
      }
      break;
    case Token.OBJECTLIT:
      // TODO(nicksantos): Come up with a way to remove this.
      // If we remove object lit keys, then we will need to also
      // create dependency scopes for them.
      break;
  }
}
 
Example 10
Source File: CrossModuleCodeMotion.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Determines whether the given NAME node belongs to a declaration that
 * can be moved across modules. If it is, registers it properly.
 *
 * There are four types of movable declarations:
 * 1) var NAME = [movable object];
 * 2) function NAME() {}
 * 3) NAME = [movable object];
 *    NAME.prop = [movable object];
 *    NAME.prop.prop2 = [movable object];
 *    etc.
 * 4) Class-defining function calls, like "inherits" and "mixin".
 *    NAME.inherits([some other name]);
 * where "movable object" is a literal or a function.
 */
private boolean maybeProcessDeclaration(NodeTraversal t, Node name,
    Node parent, NamedInfo info) {
  Node gramps = parent.getParent();
  switch (parent.getType()) {
    case Token.VAR:
      if (canMoveValue(name.getFirstChild())) {
        return info.addDeclaration(
            new Declaration(t.getModule(), name, parent, gramps));
      }
      return false;

    case Token.FUNCTION:
      if (NodeUtil.isFunctionDeclaration(parent)) {
        return info.addDeclaration(
            new Declaration(t.getModule(), name, parent, gramps));
      }
      return false;

    case Token.ASSIGN:
    case Token.GETPROP:
      Node child = name;

      // Look for assignment expressions where the name is the root
      // of a qualified name on the left hand side of the assignment.
      for (Node current : name.getAncestors()) {
        if (current.isGetProp()) {
          // fallthrough
        } else if (current.isAssign() &&
                   current.getFirstChild() == child) {
          Node currentParent = current.getParent();
          if (currentParent.isExprResult() &&
              canMoveValue(current.getLastChild())) {
            return info.addDeclaration(
                new Declaration(t.getModule(), current, currentParent,
                    currentParent.getParent()));
          }
        } else {
          return false;
        }

        child = current;
      }
      return false;

    case Token.CALL:
      if (NodeUtil.isExprCall(gramps)) {
        SubclassRelationship relationship =
            compiler.getCodingConvention().getClassesDefinedByCall(parent);
        if (relationship != null &&
            name.getString().equals(relationship.subclassName)) {
          return info.addDeclaration(
              new Declaration(t.getModule(), parent, gramps,
                  gramps.getParent()));
        }
      }
      return false;

    default:
      return false;
  }
}
 
Example 11
Source File: 1_NodeUtil.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts an operator's token value (see {@link Token}) to a string
 * representation.
 *
 * @param operator the operator's token value to convert
 * @return the string representation or {@code null} if the token value is
 * not an operator
 */
static String opToStr(int operator) {
  switch (operator) {
    case Token.BITOR: return "|";
    case Token.OR: return "||";
    case Token.BITXOR: return "^";
    case Token.AND: return "&&";
    case Token.BITAND: return "&";
    case Token.SHEQ: return "===";
    case Token.EQ: return "==";
    case Token.NOT: return "!";
    case Token.NE: return "!=";
    case Token.SHNE: return "!==";
    case Token.LSH: return "<<";
    case Token.IN: return "in";
    case Token.LE: return "<=";
    case Token.LT: return "<";
    case Token.URSH: return ">>>";
    case Token.RSH: return ">>";
    case Token.GE: return ">=";
    case Token.GT: return ">";
    case Token.MUL: return "*";
    case Token.DIV: return "/";
    case Token.MOD: return "%";
    case Token.BITNOT: return "~";
    case Token.ADD: return "+";
    case Token.SUB: return "-";
    case Token.POS: return "+";
    case Token.NEG: return "-";
    case Token.ASSIGN: return "=";
    case Token.ASSIGN_BITOR: return "|=";
    case Token.ASSIGN_BITXOR: return "^=";
    case Token.ASSIGN_BITAND: return "&=";
    case Token.ASSIGN_LSH: return "<<=";
    case Token.ASSIGN_RSH: return ">>=";
    case Token.ASSIGN_URSH: return ">>>=";
    case Token.ASSIGN_ADD: return "+=";
    case Token.ASSIGN_SUB: return "-=";
    case Token.ASSIGN_MUL: return "*=";
    case Token.ASSIGN_DIV: return "/=";
    case Token.ASSIGN_MOD: return "%=";
    case Token.VOID: return "void";
    case Token.TYPEOF: return "typeof";
    case Token.INSTANCEOF: return "instanceof";
    default: return null;
  }
}
 
Example 12
Source File: Closure_86_NodeUtil_s.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.ARRAYLIT:
    case Token.CALL:
    case Token.EMPTY:
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.GETELEM:
    case Token.GETPROP:
    case Token.GET_REF:
    case Token.IF:
    case Token.LP:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.RETURN:
    case Token.STRING:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Node.tokenToName(type) +
                             " (type " + type + ")");
  }
}
 
Example 13
Source File: Closure_48_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  inputId = t.getInputId();
  attachLiteralTypes(t, n);

  switch (n.getType()) {
    case Token.CALL:
      checkForClassDefiningCalls(t, n, parent);
      checkForCallingConventionDefiningCalls(n, delegateCallingConventions);
      break;

    case Token.FUNCTION:
      if (t.getInput() == null || !t.getInput().isExtern()) {
        nonExternFunctions.add(n);
      }

      // Hoisted functions are handled during pre-traversal.
      if (!NodeUtil.isHoistedFunctionDeclaration(n)) {
        defineFunctionLiteral(n, parent);
      }
      break;

    case Token.ASSIGN:
      // Handle initialization of properties.
      Node firstChild = n.getFirstChild();
      if (firstChild.isGetProp() &&
          firstChild.isQualifiedName()) {
        maybeDeclareQualifiedName(t, n.getJSDocInfo(),
            firstChild, n, firstChild.getNext());
      }
      break;

    case Token.CATCH:
      defineCatch(n, parent);
      break;

    case Token.VAR:
      defineVar(n, parent);
      break;

    case Token.GETPROP:
      // Handle stubbed properties.
      if (parent.isExprResult() &&
          n.isQualifiedName()) {
        maybeDeclareQualifiedName(t, n.getJSDocInfo(), n, parent, null);
      }
      break;
  }
}
 
Example 14
Source File: Closure_89_GlobalNamespace_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether the result of a hook (x?y:z) or boolean expression
 * (x||y) or (x&&y) is assigned to a specific global name.
 *
 * @param t The traversal
 * @param parent The parent of the current node in the traversal. This node
 *     should already be known to be a HOOK, AND, or OR node.
 * @param name A name that is already known to be global in the current
 *     scope (e.g. "a" or "a.b.c.d")
 * @return The expression's get type, either {@link Ref.Type#DIRECT_GET} or
 *     {@link Ref.Type#ALIASING_GET}
 */
Ref.Type determineGetTypeForHookOrBooleanExpr(
    NodeTraversal t, Node parent, String name) {
  Node prev = parent;
  for (Node anc : parent.getAncestors()) {
    switch (anc.getType()) {
      case Token.EXPR_RESULT:
      case Token.VAR:
      case Token.IF:
      case Token.WHILE:
      case Token.FOR:
      case Token.TYPEOF:
      case Token.VOID:
      case Token.NOT:
      case Token.BITNOT:
      case Token.POS:
      case Token.NEG:
        return Ref.Type.DIRECT_GET;
      case Token.HOOK:
        if (anc.getFirstChild() == prev) {
          return Ref.Type.DIRECT_GET;
        }
        break;
      case Token.ASSIGN:
        if (!name.equals(anc.getFirstChild().getQualifiedName())) {
          return Ref.Type.ALIASING_GET;
        }
        break;
      case Token.NAME:  // a variable declaration
        if (!name.equals(anc.getString())) {
          return Ref.Type.ALIASING_GET;
        }
        break;
      case Token.CALL:
        if (anc.getFirstChild() != prev) {
          return Ref.Type.ALIASING_GET;
        }
        break;
    }
    prev = anc;
  }
  return Ref.Type.ALIASING_GET;
}
 
Example 15
Source File: Cardumen_0014_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 16
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 17
Source File: Cardumen_00200_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)
          || 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 18
Source File: Closure_70_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  attachLiteralTypes(t, n);

  switch (n.getType()) {
    case Token.CALL:
      checkForClassDefiningCalls(t, n, parent);
      break;

    case Token.FUNCTION:
      if (t.getInput() == null || !t.getInput().isExtern()) {
        nonExternFunctions.add(n);
      }

      // Hoisted functions are handled during pre-traversal.
      if (!NodeUtil.isHoistedFunctionDeclaration(n)) {
        defineFunctionLiteral(n, parent);
      }
      break;

    case Token.ASSIGN:
      // Handle initialization of properties.
      Node firstChild = n.getFirstChild();
      if (firstChild.getType() == Token.GETPROP &&
          firstChild.isQualifiedName()) {
        maybeDeclareQualifiedName(t, n.getJSDocInfo(),
            firstChild, n, firstChild.getNext());
      }
      break;

    case Token.CATCH:
      defineCatch(n, parent);
      break;

    case Token.VAR:
      defineVar(n, parent);
      break;

    case Token.GETPROP:
      // Handle stubbed properties.
      if (parent.getType() == Token.EXPR_RESULT &&
          n.isQualifiedName()) {
        maybeDeclareQualifiedName(t, n.getJSDocInfo(), n, parent, null);
      }
      break;
  }
}
 
Example 19
Source File: Cardumen_0087_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 20
Source File: Closure_94_NodeUtil_s.java    From coming with MIT License 2 votes vote down vote up
/**
 * Is this node an assignment expression statement?
 *
 * @param n The node
 * @return True if {@code n} is EXPR_RESULT and {@code n}'s
 *     first child is ASSIGN
 */
static boolean isExprAssign(Node n) {
  return n.getType() == Token.EXPR_RESULT
      && n.getFirstChild().getType() == Token.ASSIGN;
}