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

The following examples show how to use com.google.javascript.rhino.Node#isAssign() . 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: SimpleDefinitionFinder.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @return the node defining the name for this function (if any).
 */
static Node getNameNodeFromFunctionNode(Node function) {
  Preconditions.checkState(function.isFunction());
  if (NodeUtil.isFunctionDeclaration(function)) {
    return function.getFirstChild();
  } else {
    Node parent = function.getParent();
    if (NodeUtil.isVarDeclaration(parent)) {
      return parent;
    } else if (parent.isAssign()) {
      return parent.getFirstChild();
    } else if (NodeUtil.isObjectLitKey(parent, parent.getParent())) {
      return parent;
    }
  }
  return null;
}
 
Example 2
Source File: NameReferenceGraphConstruction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private Name recordStaticNameDefinition(NodeTraversal t, String name,
    JSType type, Node n, Node parent, Node gParent, Node rValue) {
  if (getNamedContainingFunction() != graph.MAIN) {
    // TODO(user): if A.B() defines A.C(), there is a dependence from
    // A.C() -> A.B(). However, this is not important in module code motion
    // and will be ignored (for now).
  }
  if (type.isConstructor()) {
    return recordClassConstructorOrInterface(
        name, type.toMaybeFunctionType(),
        n, parent, parent.getParent(), rValue);
  } else {
    Name symbol = graph.defineNameIfNotExists(name, isExtern);
    symbol.setType(type);
    if (n.isAssign()) {
      symbol.addAssignmentDeclaration(n);
    } else {
      symbol.addFunctionDeclaration(n);
    }
    return symbol;
  }
}
 
Example 3
Source File: Cardumen_00200_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Get the JSDocInfo for a function.
 */
public static JSDocInfo getFunctionJSDocInfo(Node n) {
  Preconditions.checkState(n.isFunction());
  JSDocInfo fnInfo = n.getJSDocInfo();
  if (fnInfo == null && NodeUtil.isFunctionExpression(n)) {
    // Look for the info on other nodes.
    Node parent = n.getParent();
    if (parent.isAssign()) {
      // on ASSIGNs
      fnInfo = parent.getJSDocInfo();
    } else if (parent.isName()) {
      // on var NAME = function() { ... };
      fnInfo = parent.getParent().getJSDocInfo();
    }
  }
  return fnInfo;
}
 
Example 4
Source File: Closure_17_TypedScopeCreator_s.java    From coming with MIT License 6 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node node, Node parent) {
  Node nameNode = null;
  switch (node.getType()) {
    case Token.VAR:
      for (Node child = node.getFirstChild();
           child != null; child = child.getNext()) {
        identifyNameNode(
            child, child.getFirstChild(),
            NodeUtil.getBestJSDocInfo(child));
      }
      break;
    case Token.EXPR_RESULT:
      Node firstChild = node.getFirstChild();
      if (firstChild.isAssign()) {
        identifyNameNode(
            firstChild.getFirstChild(), firstChild.getLastChild(),
            firstChild.getJSDocInfo());
      } else {
        identifyNameNode(
            firstChild, null, firstChild.getJSDocInfo());
      }
      break;
  }
}
 
Example 5
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get the JSDocInfo for a function.
 */
public static JSDocInfo getFunctionJSDocInfo(Node n) {
  Preconditions.checkState(n.isFunction());
  JSDocInfo fnInfo = n.getJSDocInfo();
  if (fnInfo == null && NodeUtil.isFunctionExpression(n)) {
    // Look for the info on other nodes.
    Node parent = n.getParent();
    if (parent.isAssign()) {
      // on ASSIGNs
      fnInfo = parent.getJSDocInfo();
    } else if (parent.isName()) {
      // on var NAME = function() { ... };
      fnInfo = parent.getParent().getJSDocInfo();
    }
  }
  return fnInfo;
}
 
Example 6
Source File: TypeConversionPass.java    From clutz with MIT License 6 votes vote down vote up
private void replaceExpressionOrAssignment(Node n, Node parent, Node newNode) {
  if (parent.isExprResult()) {
    // Handles case: Myclass.Type;
    // AST:
    // EXPR_RESULT
    //     GETPROP
    //         NAME MyClass
    //         STRING Type
    parent.replaceWith(newNode);
  } else if (parent.isAssign()) {
    // Handles case: Myclass.Type = {};
    // AST:
    // ASSIGN
    //     GETPROP
    //         NAME MyClass
    //         STRING Type
    //     OBJECTLIST
    if (parent.getGrandparent() != null) {
      parent.getGrandparent().replaceChild(parent.getParent(), newNode);
    }
  } else {
    parent.replaceChild(n, newNode);
  }
}
 
Example 7
Source File: Closure_43_TypedScopeCreator_t.java    From coming with MIT License 6 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node node, Node parent) {
  Node nameNode = null;
  switch (node.getType()) {
    case Token.VAR:
      for (Node child = node.getFirstChild();
           child != null; child = child.getNext()) {
        identifyNameNode(
            child, child.getFirstChild(),
            NodeUtil.getBestJSDocInfo(child));
      }
      break;
    case Token.EXPR_RESULT:
      Node firstChild = node.getFirstChild();
      if (firstChild.isAssign()) {
        identifyNameNode(
            firstChild.getFirstChild(), firstChild.getLastChild(),
            firstChild.getJSDocInfo());
      } else {
        identifyNameNode(
            firstChild, null, firstChild.getJSDocInfo());
      }
      break;
  }
}
 
Example 8
Source File: jMutRepair_003_t.java    From coming with MIT License 6 votes vote down vote up
/** Find the l-value that the given r-value is being assigned to. */
static Node getBestLValue(Node n) {
  Node parent = n.getParent();
  boolean isFunctionDeclaration = isFunctionDeclaration(n);
  if (isFunctionDeclaration) {
    return n.getFirstChild();
  } else if (parent.isName()) {
    return parent;
  } else if (parent.isAssign()) {
    return parent.getFirstChild();
  } else if (isObjectLitKey(parent, parent.getParent())) {
    return parent;
  } else if (
      (parent.isHook() && parent.getFirstChild() != n) ||
      parent.isOr() ||
      parent.isAnd() ||
      (parent.isComma() && parent.getFirstChild() != n)) {
    return getBestLValue(parent);
  }
  return null;
}
 
Example 9
Source File: Closure_40_NameAnalyzer_s.java    From coming with MIT License 6 votes vote down vote up
private void addSimplifiedExpression(Node n, Node parent) {
  if (parent.isVar()) {
    Node value = n.getFirstChild();
    if (value != null) {
      addSimplifiedChildren(value);
    }
  } else if (n.isAssign() &&
      (parent.isExprResult() ||
       parent.isFor() ||
       parent.isReturn())) {
    for (Node child : n.children()) {
      addSimplifiedChildren(child);
    }
  } else if (n.isCall() &&
             parent.isExprResult()) {
    addSimplifiedChildren(n);
  } else {
    addAllChildren(n);
  }
}
 
Example 10
Source File: Closure_124_ExploitAssigns_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Try to collapse the given assign into subsequent expressions.
 */
private void collapseAssign(Node assign, Node expr,
    Node exprParent) {
  Node leftValue = assign.getFirstChild();
  Node rightValue = leftValue.getNext();
  if (isCollapsibleValue(leftValue, true) &&
      collapseAssignEqualTo(expr, exprParent, leftValue)) {
    reportCodeChange();
  } else if (isCollapsibleValue(rightValue, false) &&
      collapseAssignEqualTo(expr, exprParent, rightValue)) {
    reportCodeChange();
  } else if (rightValue.isAssign()) {
    // Recursively deal with nested assigns.
    collapseAssign(rightValue, expr, exprParent);
  }
}
 
Example 11
Source File: ExpandJqueryAliases.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void maybeReplaceJqueryPrototypeAlias(Node n) {
  // Check to see if this is the assignment of the original alias.
  // If so, leave it intact.
  if(NodeUtil.isLValue(n)) {
    Node maybeAssign = n.getParent();
    while (!NodeUtil.isStatement(maybeAssign) && !maybeAssign.isAssign()) {
      maybeAssign = maybeAssign.getParent();
    }

    if (maybeAssign.isAssign()) {
      maybeAssign = maybeAssign.getParent();
      if (maybeAssign.isBlock() || maybeAssign.isScript() ||
          NodeUtil.isStatement(maybeAssign)) {
        return;
      }
    }
  }

  Node fn = n.getLastChild();
  if (fn != null) {
    n.replaceChild(fn, IR.string("prototype"));
    compiler.reportCodeChange();
  }
}
 
Example 12
Source File: Closure_114_NameAnalyzer_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @return Whether the alias was recorded.
 */
private boolean maybeRecordAlias(
    String name, Node parent,
    NameInformation referring, String referringName) {
  // A common type of reference is
  // function F() {}
  // F.prototype.bar = goog.nullFunction;
  //
  // In this specific case, we do not want a reference to goog.nullFunction
  // to preserve F.
  //
  // In the general case, the user could do something like
  // function F() {}
  // F.prototype.bar = goog.nullFunction;
  // F.prototype.bar.baz = 3;
  // where it would not be safe to remove F.
  //
  // So we do not treat this alias as a backdoor for people to mutate the
  // original object. We think that this heuristic will always be
  // OK in real code.
  boolean isPrototypePropAssignment =
      parent.isAssign()
      && NodeUtil.isPrototypeProperty(parent.getFirstChild());

  if ((parent.isName() ||
      parent.isAssign()) &&
      !isPrototypePropAssignment &&
      referring != null &&
      scopes.get(parent).contains(referring)) {
    recordAlias(referringName, name);
    return true;
  }
  return false;
}
 
Example 13
Source File: Cardumen_0020_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replace n with a simpler expression, while preserving program
 * behavior.
 *
 * If the n's value is used, replace it with its rhs; otherwise
 * replace it with the subexpressions that have side effects.
 */
private void replaceWithRhs(Node parent, Node n) {
  if (valueConsumedByParent(n, parent)) {
    // parent reads from n directly; replace it with n's rhs + lhs
    // subexpressions with side effects.
    List<Node> replacements = getRhsSubexpressions(n);
    List<Node> newReplacements = Lists.newArrayList();
    for (int i = 0; i < replacements.size() - 1; i++) {
      newReplacements.addAll(getSideEffectNodes(replacements.get(i)));
    }
    Node valueExpr = replacements.get(replacements.size() - 1);
    valueExpr.detachFromParent();
    newReplacements.add(valueExpr);
    changeProxy.replaceWith(
        parent, n, collapseReplacements(newReplacements));
  } else if (n.isAssign() && !parent.isFor()) {
    // assignment appears in a RHS expression.  we have already
    // considered names in the assignment's RHS as being referenced;
    // replace the assignment with its RHS.
    // TODO(user) make the pass smarter about these cases and/or run
    // this pass and RemoveConstantExpressions together in a loop.
    Node replacement = n.getLastChild();
    replacement.detachFromParent();
    changeProxy.replaceWith(parent, n, replacement);
  } else {
    replaceTopLevelExpressionWithRhs(parent, n);
  }
}
 
Example 14
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Find the best JSDoc for the given node. */
static JSDocInfo getBestJSDocInfo(Node n) {
  JSDocInfo info = n.getJSDocInfo();
  if (info == null) {
    Node parent = n.getParent();
    if (parent == null) {
      return null;
    }

    if (parent.isName()) {
      return getBestJSDocInfo(parent);
    } else if (parent.isAssign()) {
      return parent.getJSDocInfo();
    } else if (isObjectLitKey(parent, parent.getParent())) {
      return parent.getJSDocInfo();
    } else if (parent.isFunction()) {
      return parent.getJSDocInfo();
    } else if (parent.isVar() && parent.hasOneChild()) {
      return parent.getJSDocInfo();
    } else if ((parent.isHook() && parent.getFirstChild() != n) ||
               parent.isOr() ||
               parent.isAnd() ||
               (parent.isComma() && parent.getFirstChild() != n)) {
      return getBestJSDocInfo(parent);
    } else if (parent.isCast()) {
      return parent.getJSDocInfo();
    }
  }
  return info;
}
 
Example 15
Source File: Cardumen_00200_s.java    From coming with MIT License 5 votes vote down vote up
/** Find the best JSDoc for the given node. */
static JSDocInfo getBestJSDocInfo(Node n) {
  JSDocInfo info = n.getJSDocInfo();
  if (info == null) {
    Node parent = n.getParent();
    if (parent == null) {
      return null;
    }

    if (parent.isName()) {
      return getBestJSDocInfo(parent);
    } else if (parent.isAssign()) {
      return parent.getJSDocInfo();
    } else if (isObjectLitKey(parent, parent.getParent())) {
      return parent.getJSDocInfo();
    } else if (parent.isFunction()) {
      return parent.getJSDocInfo();
    } else if (parent.isVar() && parent.hasOneChild()) {
      return parent.getJSDocInfo();
    } else if ((parent.isHook() && parent.getFirstChild() != n) ||
               parent.isOr() ||
               parent.isAnd() ||
               (parent.isComma() && parent.getFirstChild() != n)) {
      return getBestJSDocInfo(parent);
    }
  }
  return info;
}
 
Example 16
Source File: Closure_129_PrepareAst_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Translate dispatcher info into the property expected node.
 */
private void annotateDispatchers(Node n, Node parent) {
  Preconditions.checkState(n.isFunction());
  if (parent.getJSDocInfo() != null
      && parent.getJSDocInfo().isJavaDispatch()) {
    if (parent.isAssign()) {
      Preconditions.checkState(parent.getLastChild() == n);
      n.putBooleanProp(Node.IS_DISPATCHER, true);
    }
  }
}
 
Example 17
Source File: PureFunctionIdentifier.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the doc info associated with the function.
 */
private JSDocInfo getJSDocInfoForFunction(
    Node node, Node parent, Node gramp) {
  JSDocInfo info = node.getJSDocInfo();
  if (info != null) {
    return info;
  } else if (parent.isName()) {
    return gramp.hasOneChild() ? gramp.getJSDocInfo() : null;
  } else if (parent.isAssign()) {
    return parent.getJSDocInfo();
  } else {
    return null;
  }
}
 
Example 18
Source File: MaybeReachingVariableUse.java    From astor with GNU General Public License v2.0 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 19
Source File: jMutRepair_003_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Determines whether this node is strictly on the left hand side of an assign
 * or var initialization. Notably, this does not include all L-values, only
 * statements where the node is used only as an L-value.
 *
 * @param n The node
 * @param parent Parent of the node
 * @return True if n is the left hand of an assign
 */
static boolean isVarOrSimpleAssignLhs(Node n, Node parent) {
  return (parent.isAssign() && parent.getFirstChild() == n) ||
         parent.isVar();
}
 
Example 20
Source File: Closure_119_GlobalNamespace_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Determines whether an assignment is nested (i.e. whether its return
 * value is used).
 *
 * @param parent The parent of the current traversal node (not null)
 * @return Whether it appears that the return value of the assignment is
 *     used
 */
boolean isNestedAssign(Node parent) {
  return parent.isAssign() &&
         !parent.getParent().isExprResult();
}