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

The following examples show how to use com.google.javascript.rhino.Node#getLastChild() . 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_40_NameAnalyzer_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determine if the parent reads the value of a child expression
 * directly.  This is true children used in predicates, RETURN
 * statements and, rhs of variable declarations and assignments.
 *
 * In the case of:
 * if (a) b else c
 *
 * This method returns true for "a", and false for "b" and "c": the
 * IF expression does something special based on "a"'s value.  "b"
 * and "c" are effectivelly outputs.  Same logic applies to FOR,
 * WHILE and DO loop predicates.  AND/OR/HOOK expressions are
 * syntactic sugar for IF statements; therefore this method returns
 * true for the predicate and false otherwise.
 */
private boolean valueConsumedByParent(Node n, Node parent) {
  if (NodeUtil.isAssignmentOp(parent)) {
    return parent.getLastChild() == n;
  }

  switch (parent.getType()) {
    case Token.NAME:
    case Token.RETURN:
      return true;
    case Token.AND:
    case Token.OR:
    case Token.HOOK:
      return parent.getFirstChild() == n;
    case Token.FOR:
      return parent.getFirstChild().getNext() == n;
    case Token.IF:
    case Token.WHILE:
      return parent.getFirstChild() == n;
    case Token.DO:
      return parent.getLastChild() == n;
    default:
      return false;
  }
}
 
Example 2
Source File: Cardumen_00200_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: Closure_114_NameAnalyzer_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determine if the parent reads the value of a child expression
 * directly.  This is true children used in predicates, RETURN
 * statements and, RHS of variable declarations and assignments.
 *
 * In the case of:
 * if (a) b else c
 *
 * This method returns true for "a", and false for "b" and "c": the
 * IF expression does something special based on "a"'s value.  "b"
 * and "c" are effectively outputs.  Same logic applies to FOR,
 * WHILE and DO loop predicates.  AND/OR/HOOK expressions are
 * syntactic sugar for IF statements; therefore this method returns
 * true for the predicate and false otherwise.
 */
private boolean valueConsumedByParent(Node n, Node parent) {
  if (NodeUtil.isAssignmentOp(parent)) {
    return parent.getLastChild() == n;
  }

  switch (parent.getType()) {
    case Token.NAME:
    case Token.RETURN:
      return true;
    case Token.AND:
    case Token.OR:
    case Token.HOOK:
      return parent.getFirstChild() == n;
    case Token.FOR:
      return parent.getFirstChild().getNext() == n;
    case Token.IF:
    case Token.WHILE:
      return parent.getFirstChild() == n;
    case Token.DO:
      return parent.getLastChild() == n;
    default:
      return false;
  }
}
 
Example 4
Source File: jMutRepair_0032_s.java    From coming with MIT License 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isCall()) {
    Node target = n.getFirstChild();
    // TODO(johnlenz): add this to the coding convention
    // so we can remove goog.reflect.sinkValue as well.
    if (target.isName() && target.getString().equals(PROTECTOR_FN)) {
      Node expr = n.getLastChild();
      n.detachChildren();
      parent.replaceChild(n, expr);
    }
  }
}
 
Example 5
Source File: NodeUtilTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testMergeBlock2() {
  Compiler compiler = new Compiler();

  // Test removing the initializer.
  Node actual = parse("foo:{a();}");

  Node parentLabel = actual.getFirstChild();
  Node childBlock = parentLabel.getLastChild();

  assertFalse(NodeUtil.tryMergeBlock(childBlock));
}
 
Example 6
Source File: Closure_74_PeepholeFoldConstants_s.java    From coming with MIT License 5 votes vote down vote up
private void tryReduceOperandsForOp(Node n) {
  switch (n.getType()) {
    case Token.ADD:
      Node left = n.getFirstChild();
      Node right = n.getLastChild();
      if (!NodeUtil.mayBeString(left) && !NodeUtil.mayBeString(right)) {
        tryConvertOperandsToNumber(n);
      }
      break;
    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
      // TODO(johnlenz): convert these to integers.
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_MOD:
    case Token.ASSIGN_DIV:
      tryConvertToNumber(n.getLastChild());
      break;
    case Token.BITNOT:
    case Token.BITOR:
    case Token.BITXOR:
    case Token.BITAND:
    case Token.LSH:
    case Token.RSH:
    case Token.URSH:
    case Token.SUB:
    case Token.MUL:
    case Token.MOD:
    case Token.DIV:
    case Token.POS:
    case Token.NEG:
      tryConvertOperandsToNumber(n);
      break;
  }
}
 
Example 7
Source File: TightenTypes.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Finds assignments and variables from the function body. */
void initForScopeRoot(Node decl) {
  Preconditions.checkNotNull(decl);
  if (decl.isFunction()) {
    decl = decl.getLastChild();
  }
  Preconditions.checkArgument(decl.isBlock());

  NodeTraversal.traverse(compiler, decl, new CreateScope(this, false));
}
 
Example 8
Source File: Cardumen_0020_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * A value whose result is the return value of a function call
 * can be an alias to global object. The dependency on the call target will
 * prevent the removal of the function and its dependent values, but won't
 * prevent the alias' removal.
 */
private boolean maybeHiddenAlias(String name, Node n) {
  Node parent = n.getParent();
  if (NodeUtil.isVarOrSimpleAssignLhs(n, parent)) {
    Node rhs = (parent.isVar())
        ? n.getFirstChild() : parent.getLastChild();
    return (rhs != null && !NodeUtil.evaluatesToLocalValue(
        rhs, NON_LOCAL_RESULT_PREDICATE));
  }
  return false;
}
 
Example 9
Source File: Cardumen_00150_s.java    From coming with MIT License 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isCall()) {
    Node target = n.getFirstChild();
    // TODO(johnlenz): add this to the coding convention
    // so we can remove goog.reflect.sinkValue as well.
    if (target.isName() && target.getString().equals(PROTECTOR_FN)) {
      Node expr = n.getLastChild();
      n.detachChildren();
      parent.replaceChild(n, expr);
    }
  }
}
 
Example 10
Source File: Cardumen_00202_s.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 11
Source File: Closure_72_FunctionToBlockMutator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replace the 'return' statement with its child expression.
 *   "return foo()" becomes "foo()" or "resultName = foo()"
 *   "return" is removed or becomes "resultName = void 0".
 *
 * @param block
 * @param resultName
 */
private static void convertLastReturnToStatement(
    Node block, String resultName) {
  Node ret = block.getLastChild();
  Preconditions.checkArgument(ret.getType() == Token.RETURN);
  Node resultNode = getReplacementReturnStatement(ret, resultName);

  if (resultNode == null) {
    block.removeChild(ret);
  } else {
    resultNode.copyInformationFromForTree(ret);
    block.replaceChild(ret, resultNode);
  }
}
 
Example 12
Source File: Closure_60_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/** Whether the given name is constant by coding convention. */
static boolean isConstantByConvention(
    CodingConvention convention, Node node, Node parent) {
  String name = node.getString();
  if (parent.getType() == Token.GETPROP &&
      node == parent.getLastChild()) {
    return convention.isConstantKey(name);
  } else if (isObjectLitKey(node, parent)) {
    return convention.isConstantKey(name);
  } else {
    return convention.isConstant(name);
  }
}
 
Example 13
Source File: jMutRepair_0047_t.java    From coming with MIT License 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isCall()) {
    Node target = n.getFirstChild();
    // TODO(johnlenz): add this to the coding convention
    // so we can remove goog.reflect.sinkValue as well.
    if (target.isName() && target.getString().equals(PROTECTOR_FN)) {
      Node expr = n.getLastChild();
      n.detachChildren();
      parent.replaceChild(n, expr);
    }
  }
}
 
Example 14
Source File: Cardumen_0020_s.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 15
Source File: JGenProg2017_0053_t.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  // VOID nodes appear when there are extra semicolons at the BLOCK level.
  // I've been unable to think of any cases where this indicates a bug,
  // and apparently some people like keeping these semicolons around,
  // so we'll allow it.
  if (n.isEmpty() ||
      n.isComma()) {
    return;
  }

  if (parent == null) {
    return;
  }

  // Do not try to remove a block or an expr result. We already handle
  // these cases when we visit the child, and the peephole passes will
  // fix up the tree in more clever ways when these are removed.
  if (parent.getType() == Token.COMMA) {
    Node gramps = parent.getParent();
    if (gramps.isCall() && parent == gramps.getFirstChild()) {
      if (n == parent.getFirstChild() && parent.getChildCount() == 2 && n.getNext().isName() && "eval".equals(n.getNext().getString())) {
    return;
      }
  }

  // This no-op statement was there so that JSDoc information could
  // be attached to the name. This check should not complain about it.
    if (n == parent.getLastChild()) {
      for (Node an : parent.getAncestors()) {
        int ancestorType = an.getType();
        if (ancestorType == Token.COMMA)
          continue;
        if (ancestorType != Token.EXPR_RESULT && ancestorType != Token.BLOCK){
        }
        else
          break;
      }
    }
  } else if (parent.getType() != Token.EXPR_RESULT && parent.getType() != Token.BLOCK) {
    if (parent.getType() == Token.FOR && parent.getChildCount() == 4 && (n == parent.getFirstChild() ||
         n == parent.getFirstChild().getNext().getNext())) {
    } else {
    return;
    }
  }

  boolean isResultUsed = NodeUtil.isExpressionResultUsed(n);
  boolean isSimpleOp = NodeUtil.isSimpleOperatorType(n.getType());
  if (!isResultUsed &&
      (isSimpleOp || !NodeUtil.mayHaveSideEffects(n, t.getCompiler()))) {
    if (n.isQualifiedName() && n.getJSDocInfo() != null) {
      return;
    } else if (n.isExprResult()) {
      return;
    }
    String msg = "This code lacks side-effects. Is there a bug?";
    if (n.isString()) {
      msg = "Is there a missing '+' on the previous line?";
    } else if (isSimpleOp) {
      msg = "The result of the '" + Token.name(n.getType()).toLowerCase() +
          "' operator is not being used.";
    }

    t.getCompiler().report(
        t.makeError(n, level, USELESS_CODE_ERROR, msg));
    // TODO(johnlenz): determine if it is necessary to
    // try to protect side-effect free statements as well.
    if (!NodeUtil.isStatement(n)) {
      problemNodes.add(n);
    }
  }
}
 
Example 16
Source File: jKali_0043_s.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  // VOID nodes appear when there are extra semicolons at the BLOCK level.
  // I've been unable to think of any cases where this indicates a bug,
  // and apparently some people like keeping these semicolons around,
  // so we'll allow it.
  if (n.isEmpty() ||
      n.isComma()) {
    return;
  }

  if (parent == null) {
    return;
  }

  // Do not try to remove a block or an expr result. We already handle
  // these cases when we visit the child, and the peephole passes will
  // fix up the tree in more clever ways when these are removed.
  if (parent.getType() == Token.COMMA) {
    Node gramps = parent.getParent();
    if (gramps.isCall() && parent == gramps.getFirstChild()) {
      if (n == parent.getFirstChild() && parent.getChildCount() == 2 && n.getNext().isName() && "eval".equals(n.getNext().getString())) {
    return;
      }
  }

  // This no-op statement was there so that JSDoc information could
  // be attached to the name. This check should not complain about it.
    if (n == parent.getLastChild()) {
      for (Node an : parent.getAncestors()) {
        int ancestorType = an.getType();
        if (ancestorType == Token.COMMA)
          continue;
        if (ancestorType != Token.EXPR_RESULT && ancestorType != Token.BLOCK)
          return;
        else
          break;
      }
    }
  } else if (parent.getType() != Token.EXPR_RESULT && parent.getType() != Token.BLOCK) {
    if (parent.getType() == Token.FOR && parent.getChildCount() == 4 && (n == parent.getFirstChild() ||
         n == parent.getFirstChild().getNext().getNext())) {
    } else {
    return;
    }
  }

  boolean isResultUsed = NodeUtil.isExpressionResultUsed(n);
  boolean isSimpleOp = NodeUtil.isSimpleOperatorType(n.getType());
  if (!isResultUsed &&
      (isSimpleOp || !NodeUtil.mayHaveSideEffects(n, t.getCompiler()))) {
    if (n.isQualifiedName() && n.getJSDocInfo() != null) {
      return;
    } else if (n.isExprResult()) {
      return;
    }
    String msg = "This code lacks side-effects. Is there a bug?";
    if (n.isString()) {
      msg = "Is there a missing '+' on the previous line?";
    } else if (isSimpleOp) {
      msg = "The result of the '" + Token.name(n.getType()).toLowerCase() +
          "' operator is not being used.";
    }

    t.getCompiler().report(
        t.makeError(n, level, USELESS_CODE_ERROR, msg));
    // TODO(johnlenz): determine if it is necessary to
    // try to protect side-effect free statements as well.
    if (!NodeUtil.isStatement(n)) {
      problemNodes.add(n);
    }
  }
}
 
Example 17
Source File: Closure_75_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
/** Whether the child node is the FINALLY block of a try. */
static boolean isTryFinallyNode(Node parent, Node child) {
  return parent.getType() == Token.TRY && parent.getChildCount() == 3
      && child == parent.getLastChild();
}
 
Example 18
Source File: Nopol2017_0031_s.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  // VOID nodes appear when there are extra semicolons at the BLOCK level.
  // I've been unable to think of any cases where this indicates a bug,
  // and apparently some people like keeping these semicolons around,
  // so we'll allow it.
  if (n.isEmpty() ||
      n.isComma()) {
    return;
  }

  if (parent == null) {
    return;
  }

  // Do not try to remove a block or an expr result. We already handle
  // these cases when we visit the child, and the peephole passes will
  // fix up the tree in more clever ways when these are removed.
  if (n.isExprResult()) {
    return;
  }

  // This no-op statement was there so that JSDoc information could
  // be attached to the name. This check should not complain about it.
  if (n.isQualifiedName() && n.getJSDocInfo() != null) {
    return;
  }

  boolean isResultUsed = NodeUtil.isExpressionResultUsed(n);
  boolean isSimpleOp = NodeUtil.isSimpleOperatorType(n.getType());
  if (parent.getType() == Token.COMMA) {
    if (isResultUsed) {
      return;
    }
    if (n == parent.getLastChild()) {
      for (Node an : parent.getAncestors()) {
        int ancestorType = an.getType();
        if (ancestorType == Token.COMMA) continue;
        if (ancestorType != Token.EXPR_RESULT && ancestorType != Token.BLOCK) return;
        else break;
      }
    }
  } else if (parent.getType() != Token.EXPR_RESULT && parent.getType() != Token.BLOCK) {
    if (! (parent.getType() == Token.FOR && parent.getChildCount() == 4 && (n == parent.getFirstChild() || n == parent.getFirstChild().getNext().getNext()))) {
      return;
    }
  }
  if (
      (isSimpleOp || !NodeUtil.mayHaveSideEffects(n, t.getCompiler()))) {
    String msg = "This code lacks side-effects. Is there a bug?";
    if (n.isString()) {
      msg = "Is there a missing '+' on the previous line?";
    } else if (isSimpleOp) {
      msg = "The result of the '" + Token.name(n.getType()).toLowerCase() +
          "' operator is not being used.";
    }

    t.getCompiler().report(
        t.makeError(n, level, USELESS_CODE_ERROR, msg));
    // TODO(johnlenz): determine if it is necessary to
    // try to protect side-effect free statements as well.
    if (!NodeUtil.isStatement(n)) {
      problemNodes.add(n);
    }
  }
}
 
Example 19
Source File: Closure_103_ControlFlowAnalysis_s.java    From coming with MIT License 4 votes vote down vote up
@Override
public boolean shouldTraverse(
    NodeTraversal nodeTraversal, Node n, Node parent) {
  astPosition.put(n, astPositionCounter++);

  switch (n.getType()) {
    case Token.FUNCTION:
      if (shouldTraverseFunctions || n == cfg.getEntry().getValue()) {
        exceptionHandler.push(n);
        return true;
      }
      return false;
    case Token.TRY:
      exceptionHandler.push(n);
      return true;
  }

  /*
   * We are going to stop the traversal depending on what the node's parent
   * is.
   *
   * We are only interested in adding edges between nodes that change control
   * flow. The most obvious ones are loops and IF-ELSE's. A statement
   * transfers control to its next sibling.
   *
   * In case of an expression tree, there is no control flow within the tree
   * even when there are short circuited operators and conditionals. When we
   * are doing data flow analysis, we will simply synthesize lattices up the
   * expression tree by finding the meet at each expression node.
   *
   * For example: within a Token.SWITCH, the expression in question does not
   * change the control flow and need not to be considered.
   */
  if (parent != null) {
    switch (parent.getType()) {
      case Token.FOR:
        // Only traverse the body of the for loop.
        return n == parent.getLastChild();

      // Skip the conditions.
      case Token.IF:
      case Token.WHILE:
      case Token.WITH:
        return n != parent.getFirstChild();
      case Token.DO:
        return n != parent.getFirstChild().getNext();
      // Only traverse the body of the cases
      case Token.SWITCH:
      case Token.CASE:
      case Token.CATCH:
      case Token.LABEL:
        return n != parent.getFirstChild();
      case Token.FUNCTION:
        return n == parent.getFirstChild().getNext().getNext();
      case Token.CONTINUE:
      case Token.BREAK:
      case Token.EXPR_RESULT:
      case Token.VAR:
      case Token.RETURN:
      case Token.THROW:
        return false;
      case Token.TRY:
        /* Just before we are about to visit the second child of the TRY node,
         * we know that we will be visiting either the CATCH or the FINALLY.
         * In other words, we know that the post order traversal of the TRY
         * block has been finished, no more exceptions can be caught by the
         * handler at this TRY block and should be taken out of the stack.
         */
        if (n == parent.getFirstChild().getNext()) {
          Preconditions.checkState(exceptionHandler.peek() == parent);
          exceptionHandler.pop();
        }
    }
  }
  return true;
}
 
Example 20
Source File: Cardumen_00149_s.java    From coming with MIT License 4 votes vote down vote up
/** Whether the child node is the FINALLY block of a try. */
static boolean isTryFinallyNode(Node parent, Node child) {
  return parent.isTry() && parent.getChildCount() == 3
      && child == parent.getLastChild();
}