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

The following examples show how to use com.google.javascript.rhino.Node#isCase() . 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: ReferenceCollectingCallback.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return true if this node marks the start of a new basic block
 */
private static boolean isBlockBoundary(Node n, Node parent) {
  if (parent != null) {
    switch (parent.getType()) {
      case Token.DO:
      case Token.FOR:
      case Token.TRY:
      case Token.WHILE:
      case Token.WITH:
        // NOTE: TRY has up to 3 child blocks:
        // TRY
        //   BLOCK
        //   BLOCK
        //     CATCH
        //   BLOCK
        // Note that there is an explicit CATCH token but no explicit
        // FINALLY token. For simplicity, we consider each BLOCK
        // a separate basic BLOCK.
        return true;
      case Token.AND:
      case Token.HOOK:
      case Token.IF:
      case Token.OR:
        // The first child of a conditional is not a boundary,
        // but all the rest of the children are.
        return n != parent.getFirstChild();

    }
  }

  return n.isCase();
}
 
Example 2
Source File: Closure_120_ReferenceCollectingCallback_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @return true if this node marks the start of a new basic block
 */
private static boolean isBlockBoundary(Node n, Node parent) {
  if (parent != null) {
    switch (parent.getType()) {
      case Token.DO:
      case Token.FOR:
      case Token.TRY:
      case Token.WHILE:
      case Token.WITH:
        // NOTE: TRY has up to 3 child blocks:
        // TRY
        //   BLOCK
        //   BLOCK
        //     CATCH
        //   BLOCK
        // Note that there is an explicit CATCH token but no explicit
        // FINALLY token. For simplicity, we consider each BLOCK
        // a separate basic BLOCK.
        return true;
      case Token.AND:
      case Token.HOOK:
      case Token.IF:
      case Token.OR:
        // The first child of a conditional is not a boundary,
        // but all the rest of the children are.
        return n != parent.getFirstChild();

    }
  }

  return n.isCase();
}
 
Example 3
Source File: Closure_120_ReferenceCollectingCallback_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @return true if this node marks the start of a new basic block
 */
private static boolean isBlockBoundary(Node n, Node parent) {
  if (parent != null) {
    switch (parent.getType()) {
      case Token.DO:
      case Token.FOR:
      case Token.TRY:
      case Token.WHILE:
      case Token.WITH:
        // NOTE: TRY has up to 3 child blocks:
        // TRY
        //   BLOCK
        //   BLOCK
        //     CATCH
        //   BLOCK
        // Note that there is an explicit CATCH token but no explicit
        // FINALLY token. For simplicity, we consider each BLOCK
        // a separate basic BLOCK.
        return true;
      case Token.AND:
      case Token.HOOK:
      case Token.IF:
      case Token.OR:
        // The first child of a conditional is not a boundary,
        // but all the rest of the children are.
        return n != parent.getFirstChild();

    }
  }

  return n.isCase();
}
 
Example 4
Source File: jKali_003_s.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is part of a switch statement. */
static boolean isSwitchCase(Node n) {
  return n.isCase() || n.isDefaultCase();
}
 
Example 5
Source File: SideEffectsAnalysis.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns true if the two nodes have the same control flow properties,
 * that is, is node1 be executed every time node2 is executed and vice versa?
 */
private static boolean nodesHaveSameControlFlow(Node node1, Node node2) {
  /*
   * We conservatively approximate this with the following criteria:
   *
   * Define the "deepest control dependent block" for a node to be the
   * closest ancestor whose *parent* is a control structure and where that
   * ancestor may or may be executed depending on the parent.
   *
   * So, for example, in:
   * if (a) {
   *  b;
   * } else {
   *  c;
   * }
   *
   * a has not deepest control dependent block.
   * b's deepest control dependent block is the "then" block of the IF.
   * c's deepest control dependent block is the "else" block of the IF.
   *
   * We'll say two nodes have the same control flow if
   *
   * 1) they have the same deepest control dependent block
   * 2) that block is either a CASE (which can't have early exits) or it
   * doesn't have any early exits (e.g. breaks, continues, returns.)
   *
   */

  Node node1DeepestControlDependentBlock =
      closestControlDependentAncestor(node1);

  Node node2DeepestControlDependentBlock =
    closestControlDependentAncestor(node2);

  if (node1DeepestControlDependentBlock ==
      node2DeepestControlDependentBlock) {

    if (node2DeepestControlDependentBlock != null) {
      // CASE is complicated because we have to deal with fall through and
      // because some BREAKs are early exits and some are not.
      // For now, we don't allow movement within a CASE.
      //
      // TODO(dcc): be less conservative about movement within CASE
      if (node2DeepestControlDependentBlock.isCase()) {
        return false;
      }

      // Don't allow breaks, continues, returns in control dependent
      // block because we don't actually create a control-flow graph
      // and so don't know if early exits site between the source
      // and the destination.
      //
      // This is overly conservative as it doesn't allow, for example,
      // moving in the following case:
      // while (a) {
      //   source();
      //
      //   while(b) {
      //     break;
      //   }
      //
      //   destination();
      // }
      //
      // To fully support this kind of movement, we'll probably have to use
      // a CFG-based analysis rather than just looking at the AST.
      //
      // TODO(dcc): have nodesHaveSameControlFlow() use a CFG
      Predicate<Node> isEarlyExitPredicate = new Predicate<Node>() {
        @Override
        public boolean apply(Node input) {
          int nodeType = input.getType();

          return nodeType == Token.RETURN
              || nodeType == Token.BREAK
              || nodeType == Token.CONTINUE;
        }
      };

      return !NodeUtil.has(node2DeepestControlDependentBlock,
          isEarlyExitPredicate, NOT_FUNCTION_PREDICATE);
    } else {
      return true;
    }
  } else {
    return false;
  }
}
 
Example 6
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** Whether the node is part of a switch statement. */
static boolean isSwitchCase(Node n) {
  return n.isCase() || n.isDefaultCase();
}
 
Example 7
Source File: Closure_10_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is part of a switch statement. */
static boolean isSwitchCase(Node n) {
  return n.isCase() || n.isDefaultCase();
}
 
Example 8
Source File: Closure_10_NodeUtil_t.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is part of a switch statement. */
static boolean isSwitchCase(Node n) {
  return n.isCase() || n.isDefaultCase();
}
 
Example 9
Source File: jMutRepair_003_t.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is part of a switch statement. */
static boolean isSwitchCase(Node n) {
  return n.isCase() || n.isDefaultCase();
}
 
Example 10
Source File: jMutRepair_003_s.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is part of a switch statement. */
static boolean isSwitchCase(Node n) {
  return n.isCase() || n.isDefaultCase();
}
 
Example 11
Source File: Cardumen_00201_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 (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 (parent.isCase()) {
      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 12
Source File: jKali_003_t.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is part of a switch statement. */
static boolean isSwitchCase(Node n) {
  return n.isCase() || n.isDefaultCase();
}
 
Example 13
Source File: Cardumen_0087_t.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is part of a switch statement. */
static boolean isSwitchCase(Node n) {
  return n.isCase() || n.isDefaultCase();
}
 
Example 14
Source File: Cardumen_0087_s.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is part of a switch statement. */
static boolean isSwitchCase(Node n) {
  return n.isCase() || n.isDefaultCase();
}
 
Example 15
Source File: Cardumen_0014_s.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is part of a switch statement. */
static boolean isSwitchCase(Node n) {
  return n.isCase() || n.isDefaultCase();
}
 
Example 16
Source File: Cardumen_0014_t.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is part of a switch statement. */
static boolean isSwitchCase(Node n) {
  return n.isCase() || n.isDefaultCase();
}
 
Example 17
Source File: Cardumen_00149_t.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is part of a switch statement. */
static boolean isSwitchCase(Node n) {
  return n.isCase() || n.isDefaultCase();
}
 
Example 18
Source File: Cardumen_00149_s.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is part of a switch statement. */
static boolean isSwitchCase(Node n) {
  return n.isCase() || n.isDefaultCase();
}
 
Example 19
Source File: Cardumen_00200_t.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is part of a switch statement. */
static boolean isSwitchCase(Node n) {
  return n.isCase() || n.isDefaultCase();
}
 
Example 20
Source File: Cardumen_00200_s.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is part of a switch statement. */
static boolean isSwitchCase(Node n) {
  return n.isCase() || n.isDefaultCase();
}