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

The following examples show how to use com.google.javascript.rhino.Node#isThrow() . 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: MinerrPass.java    From ng-closure-runner with MIT License 6 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isThrow()) {
    if (!isMinerrInstance(n.getFirstChild())) {
      compiler.report(t.makeError(n, THROW_IS_NOT_MINERR_ERROR_WARNING));
      unmarkInstancesBelow(n);
    }
  }
  if (isMinerrInstance(n)) {
    minerrInstances.add(n);
  }
  if (isMinerrDefinition(n)) {
    if (minerrDefNode == null) {
      minerrDefNode = n;
    } else {
      compiler.report(t.makeError(n, MULTIPLE_MINERR_DEFINITION_WARNING));
    }
  }
}
 
Example 2
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Remove duplicate exits.  If the node following the exit node expression
 * has the same effect as exit node, the node can be removed.
 * For example:
 *   "if (a) {return f()} return f();" ==> "if (a) {} return f();"
 *   "if (a) {throw 'ow'} throw 'ow';" ==> "if (a) {} throw 'ow';"
 *
 * @param n An follow control exit expression (a THROW or RETURN node)
 * @return The replacement for n, or the original if no change was made.
 */
private Node tryRemoveRedundantExit(Node n) {
  Node exitExpr = n.getFirstChild();

  Node follow = ControlFlowAnalysis.computeFollowNode(n);

  // Skip pass all the finally blocks because both the fall through and return
  // will also trigger all the finally blocks.
  Node prefinallyFollows = follow;
  follow = skipFinallyNodes(follow);
  if (prefinallyFollows != follow) {
    // There were finally clauses
    if (!isPure(exitExpr)) {
      // Can't replace the return
      return n;
    }
  }

  if (follow == null && (n.isThrow() || exitExpr != null)) {
    // Can't complete remove a throw here or a return with a result.
    return n;
  }

  // When follow is null, this mean the follow of a break target is the
  // end of a function. This means a break is same as return.
  if (follow == null || areMatchingExits(n, follow)) {
    n.detachFromParent();
    reportCodeChange();
    return null;
  }

  return n;
}
 
Example 3
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
boolean isExceptionPossible(Node n) {
  // TODO(johnlenz): maybe use ControlFlowAnalysis.mayThrowException?
  Preconditions.checkState(n.isReturn()
      || n.isThrow());
  return n.isThrow()
      || (n.hasChildren()
          && !NodeUtil.isLiteralValue(n.getLastChild(), true));
}
 
Example 4
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Remove duplicate exits.  If the node following the exit node expression
 * has the same effect as exit node, the node can be removed.
 * For example:
 *   "if (a) {return f()} return f();" ==> "if (a) {} return f();"
 *   "if (a) {throw 'ow'} throw 'ow';" ==> "if (a) {} throw 'ow';"
 *
 * @param n An follow control exit expression (a THROW or RETURN node)
 * @return The replacement for n, or the original if no change was made.
 */
private Node tryRemoveRedundantExit(Node n) {
  Node exitExpr = n.getFirstChild();

  Node follow = ControlFlowAnalysis.computeFollowNode(n);

  // Skip pass all the finally blocks because both the fall through and return
  // will also trigger all the finally blocks.
  Node prefinallyFollows = follow;
  follow = skipFinallyNodes(follow);
  if (prefinallyFollows != follow) {
    // There were finally clauses
    if (!isPure(exitExpr)) {
      // Can't replace the return
      return n;
    }
  }

  if (follow == null && (n.isThrow() || exitExpr != null)) {
    // Can't complete remove a throw here or a return with a result.
    return n;
  }

  // When follow is null, this mean the follow of a break target is the
  // end of a function. This means a break is same as return.
  if (follow == null || areMatchingExits(n, follow)) {
    n.detachFromParent();
    reportCodeChange();
    return null;
  }

  return n;
}
 
Example 5
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
boolean isExceptionPossible(Node n) {
  // TODO(johnlenz): maybe use ControlFlowAnalysis.mayThrowException?
  Preconditions.checkState(n.isReturn()
      || n.isThrow());
  return n.isThrow()
      || (n.hasChildren()
          && !NodeUtil.isLiteralValue(n.getLastChild(), true));
}
 
Example 6
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Remove duplicate exits.  If the node following the exit node expression
 * has the same effect as exit node, the node can be removed.
 * For example:
 *   "if (a) {return f()} return f();" ==> "if (a) {} return f();"
 *   "if (a) {throw 'ow'} throw 'ow';" ==> "if (a) {} throw 'ow';"
 *
 * @param n An follow control exit expression (a THROW or RETURN node)
 * @return The replacement for n, or the original if no change was made.
 */
private Node tryRemoveRedundantExit(Node n) {
  Node exitExpr = n.getFirstChild();

  Node follow = ControlFlowAnalysis.computeFollowNode(n);

  // Skip pass all the finally blocks because both the fall through and return
  // will also trigger all the finally blocks.
  Node prefinallyFollows = follow;
  follow = skipFinallyNodes(follow);
  if (prefinallyFollows != follow) {
    // There were finally clauses
    if (!isPure(exitExpr)) {
      // Can't replace the return
      return n;
    }
  }

  if (follow == null && (n.isThrow() || exitExpr != null)) {
    // Can't complete remove a throw here or a return with a result.
    return n;
  }

  // When follow is null, this mean the follow of a break target is the
  // end of a function. This means a break is same as return.
  if (follow == null || areMatchingExits(n, follow)) {
    n.detachFromParent();
    reportCodeChange();
    return null;
  }

  return n;
}
 
Example 7
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
boolean isExceptionPossible(Node n) {
  // TODO(johnlenz): maybe use ControlFlowAnalysis.mayThrowException?
  Preconditions.checkState(n.isReturn()
      || n.isThrow());
  return n.isThrow()
      || (n.hasChildren()
          && !NodeUtil.isLiteralValue(n.getLastChild(), true));
}
 
Example 8
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Remove duplicate exits.  If the node following the exit node expression
 * has the same effect as exit node, the node can be removed.
 * For example:
 *   "if (a) {return f()} return f();" ==> "if (a) {} return f();"
 *   "if (a) {throw 'ow'} throw 'ow';" ==> "if (a) {} throw 'ow';"
 *
 * @param n An follow control exit expression (a THROW or RETURN node)
 * @return The replacement for n, or the original if no change was made.
 */
private Node tryRemoveRedundantExit(Node n) {
  Node exitExpr = n.getFirstChild();

  Node follow = ControlFlowAnalysis.computeFollowNode(n);

  // Skip pass all the finally blocks because both the fall through and return
  // will also trigger all the finally blocks.
  Node prefinallyFollows = follow;
  follow = skipFinallyNodes(follow);
  if (prefinallyFollows != follow) {
    // There were finally clauses
    if (!isPure(exitExpr)) {
      // Can't replace the return
      return n;
    }
  }

  if (follow == null && (n.isThrow() || exitExpr != null)) {
    // Can't complete remove a throw here or a return with a result.
    return n;
  }

  // When follow is null, this mean the follow of a break target is the
  // end of a function. This means a break is same as return.
  if (follow == null || areMatchingExits(n, follow)) {
    n.detachFromParent();
    reportCodeChange();
    return null;
  }

  return n;
}
 
Example 9
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
boolean isExceptionPossible(Node n) {
  // TODO(johnlenz): maybe use ControlFlowAnalysis.mayThrowException?
  Preconditions.checkState(n.isReturn()
      || n.isThrow());
  return n.isThrow()
      || (n.hasChildren()
          && !NodeUtil.isLiteralValue(n.getLastChild(), true));
}
 
Example 10
Source File: PeepholeSubstituteAlternateSyntax.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Remove duplicate exits.  If the node following the exit node expression
 * has the same effect as exit node, the node can be removed.
 * For example:
 *   "if (a) {return f()} return f();" ==> "if (a) {} return f();"
 *   "if (a) {throw 'ow'} throw 'ow';" ==> "if (a) {} throw 'ow';"
 *
 * @param n An follow control exit expression (a THROW or RETURN node)
 * @return The replacement for n, or the original if no change was made.
 */
private Node tryRemoveRedundantExit(Node n) {
  Node exitExpr = n.getFirstChild();

  Node follow = ControlFlowAnalysis.computeFollowNode(n);

  // Skip pass all the finally blocks because both the fall through and return
  // will also trigger all the finally blocks.
  Node prefinallyFollows = follow;
  follow = skipFinallyNodes(follow);
  if (prefinallyFollows != follow) {
    // There were finally clauses
    if (!isPure(exitExpr)) {
      // Can't replace the return
      return n;
    }
  }

  if (follow == null && (n.isThrow() || exitExpr != null)) {
    // Can't complete remove a throw here or a return with a result.
    return n;
  }

  // When follow is null, this mean the follow of a break target is the
  // end of a function. This means a break is same as return.
  if (follow == null || areMatchingExits(n, follow)) {
    n.detachFromParent();
    reportCodeChange();
    return null;
  }

  return n;
}
 
Example 11
Source File: PeepholeSubstituteAlternateSyntax.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
boolean isExceptionPossible(Node n) {
  // TODO(johnlenz): maybe use ControlFlowAnalysis.mayThrowException?
  Preconditions.checkState(n.isReturn()
      || n.isThrow());
  return n.isThrow()
      || (n.hasChildren()
          && !NodeUtil.isLiteralValue(n.getLastChild(), true));
}
 
Example 12
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Replace duplicate exits in control structures.  If the node following
 * the exit node expression has the same effect as exit node, the node can
 * be replaced or removed.
 * For example:
 *   "while (a) {return f()} return f();" ==> "while (a) {break} return f();"
 *   "while (a) {throw 'ow'} throw 'ow';" ==> "while (a) {break} throw 'ow';"
 *
 * @param n An follow control exit expression (a THROW or RETURN node)
 * @return The replacement for n, or the original if no change was made.
 */
private Node tryReplaceExitWithBreak(Node n) {
  Node result = n.getFirstChild();

  // Find the enclosing control structure, if any, that a "break" would exit
  // from.
  Node breakTarget = n;
  for (;!ControlFlowAnalysis.isBreakTarget(breakTarget, null /* no label */);
      breakTarget = breakTarget.getParent()) {
    if (breakTarget.isFunction() || breakTarget.isScript()) {
      // No break target.
      return n;
    }
  }

  Node follow = ControlFlowAnalysis.computeFollowNode(breakTarget);

  // Skip pass all the finally blocks because both the break and return will
  // also trigger all the finally blocks. However, the order of execution is
  // slightly changed. Consider:
  //
  // return a() -> finally { b() } -> return a()
  //
  // which would call a() first. However, changing the first return to a
  // break will result in calling b().

  Node prefinallyFollows = follow;
  follow = skipFinallyNodes(follow);

  if (prefinallyFollows != follow) {
    // There were finally clauses
    if (!isPure(result)) {
      // Can't defer the exit
      return n;
    }
  }

  if (follow == null && (n.isThrow() || result != null)) {
    // Can't complete remove a throw here or a return with a result.
    return n;
  }

  // When follow is null, this mean the follow of a break target is the
  // end of a function. This means a break is same as return.
  if (follow == null || areMatchingExits(n, follow)) {
    Node replacement = IR.breakNode();
    n.getParent().replaceChild(n, replacement);
    this.reportCodeChange();
    return replacement;
  }

  return n;
}
 
Example 13
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Replace duplicate exits in control structures.  If the node following
 * the exit node expression has the same effect as exit node, the node can
 * be replaced or removed.
 * For example:
 *   "while (a) {return f()} return f();" ==> "while (a) {break} return f();"
 *   "while (a) {throw 'ow'} throw 'ow';" ==> "while (a) {break} throw 'ow';"
 *
 * @param n An follow control exit expression (a THROW or RETURN node)
 * @return The replacement for n, or the original if no change was made.
 */
private Node tryReplaceExitWithBreak(Node n) {
  Node result = n.getFirstChild();

  // Find the enclosing control structure, if any, that a "break" would exit
  // from.
  Node breakTarget = n;
  for (;!ControlFlowAnalysis.isBreakTarget(breakTarget, null /* no label */);
      breakTarget = breakTarget.getParent()) {
    if (breakTarget.isFunction() || breakTarget.isScript()) {
      // No break target.
      return n;
    }
  }

  Node follow = ControlFlowAnalysis.computeFollowNode(breakTarget);

  // Skip pass all the finally blocks because both the break and return will
  // also trigger all the finally blocks. However, the order of execution is
  // slightly changed. Consider:
  //
  // return a() -> finally { b() } -> return a()
  //
  // which would call a() first. However, changing the first return to a
  // break will result in calling b().

  Node prefinallyFollows = follow;
  follow = skipFinallyNodes(follow);

  if (prefinallyFollows != follow) {
    // There were finally clauses
    if (!isPure(result)) {
      // Can't defer the exit
      return n;
    }
  }

  if (follow == null && (n.isThrow() || result != null)) {
    // Can't complete remove a throw here or a return with a result.
    return n;
  }

  // When follow is null, this mean the follow of a break target is the
  // end of a function. This means a break is same as return.
  if (follow == null || areMatchingExits(n, follow)) {
    Node replacement = IR.breakNode();
    n.getParent().replaceChild(n, replacement);
    this.reportCodeChange();
    return replacement;
  }

  return n;
}
 
Example 14
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Replace duplicate exits in control structures.  If the node following
 * the exit node expression has the same effect as exit node, the node can
 * be replaced or removed.
 * For example:
 *   "while (a) {return f()} return f();" ==> "while (a) {break} return f();"
 *   "while (a) {throw 'ow'} throw 'ow';" ==> "while (a) {break} throw 'ow';"
 *
 * @param n An follow control exit expression (a THROW or RETURN node)
 * @return The replacement for n, or the original if no change was made.
 */
private Node tryReplaceExitWithBreak(Node n) {
  Node result = n.getFirstChild();

  // Find the enclosing control structure, if any, that a "break" would exit
  // from.
  Node breakTarget = n;
  for (;!ControlFlowAnalysis.isBreakTarget(breakTarget, null /* no label */);
      breakTarget = breakTarget.getParent()) {
    if (breakTarget.isFunction() || breakTarget.isScript()) {
      // No break target.
      return n;
    }
  }

  Node follow = ControlFlowAnalysis.computeFollowNode(breakTarget);

  // Skip pass all the finally blocks because both the break and return will
  // also trigger all the finally blocks. However, the order of execution is
  // slightly changed. Consider:
  //
  // return a() -> finally { b() } -> return a()
  //
  // which would call a() first. However, changing the first return to a
  // break will result in calling b().

  Node prefinallyFollows = follow;
  follow = skipFinallyNodes(follow);

  if (prefinallyFollows != follow) {
    // There were finally clauses
    if (!isPure(result)) {
      // Can't defer the exit
      return n;
    }
  }

  if (follow == null && (n.isThrow() || result != null)) {
    // Can't complete remove a throw here or a return with a result.
    return n;
  }

  // When follow is null, this mean the follow of a break target is the
  // end of a function. This means a break is same as return.
  if (follow == null || areMatchingExits(n, follow)) {
    Node replacement = IR.breakNode();
    n.getParent().replaceChild(n, replacement);
    this.reportCodeChange();
    return replacement;
  }

  return n;
}
 
Example 15
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Replace duplicate exits in control structures.  If the node following
 * the exit node expression has the same effect as exit node, the node can
 * be replaced or removed.
 * For example:
 *   "while (a) {return f()} return f();" ==> "while (a) {break} return f();"
 *   "while (a) {throw 'ow'} throw 'ow';" ==> "while (a) {break} throw 'ow';"
 *
 * @param n An follow control exit expression (a THROW or RETURN node)
 * @return The replacement for n, or the original if no change was made.
 */
private Node tryReplaceExitWithBreak(Node n) {
  Node result = n.getFirstChild();

  // Find the enclosing control structure, if any, that a "break" would exit
  // from.
  Node breakTarget = n;
  for (;!ControlFlowAnalysis.isBreakTarget(breakTarget, null /* no label */);
      breakTarget = breakTarget.getParent()) {
    if (breakTarget.isFunction() || breakTarget.isScript()) {
      // No break target.
      return n;
    }
  }

  Node follow = ControlFlowAnalysis.computeFollowNode(breakTarget);

  // Skip pass all the finally blocks because both the break and return will
  // also trigger all the finally blocks. However, the order of execution is
  // slightly changed. Consider:
  //
  // return a() -> finally { b() } -> return a()
  //
  // which would call a() first. However, changing the first return to a
  // break will result in calling b().

  Node prefinallyFollows = follow;
  follow = skipFinallyNodes(follow);

  if (prefinallyFollows != follow) {
    // There were finally clauses
    if (!isPure(result)) {
      // Can't defer the exit
      return n;
    }
  }

  if (follow == null && (n.isThrow() || result != null)) {
    // Can't complete remove a throw here or a return with a result.
    return n;
  }

  // When follow is null, this mean the follow of a break target is the
  // end of a function. This means a break is same as return.
  if (follow == null || areMatchingExits(n, follow)) {
    Node replacement = IR.breakNode();
    n.getParent().replaceChild(n, replacement);
    this.reportCodeChange();
    return replacement;
  }

  return n;
}
 
Example 16
Source File: PeepholeSubstituteAlternateSyntax.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Replace duplicate exits in control structures.  If the node following
 * the exit node expression has the same effect as exit node, the node can
 * be replaced or removed.
 * For example:
 *   "while (a) {return f()} return f();" ==> "while (a) {break} return f();"
 *   "while (a) {throw 'ow'} throw 'ow';" ==> "while (a) {break} throw 'ow';"
 *
 * @param n An follow control exit expression (a THROW or RETURN node)
 * @return The replacement for n, or the original if no change was made.
 */
private Node tryReplaceExitWithBreak(Node n) {
  Node result = n.getFirstChild();

  // Find the enclosing control structure, if any, that a "break" would exit
  // from.
  Node breakTarget = n;
  for (;!ControlFlowAnalysis.isBreakTarget(breakTarget, null /* no label */);
      breakTarget = breakTarget.getParent()) {
    if (breakTarget.isFunction() || breakTarget.isScript()) {
      // No break target.
      return n;
    }
  }

  Node follow = ControlFlowAnalysis.computeFollowNode(breakTarget);

  // Skip pass all the finally blocks because both the break and return will
  // also trigger all the finally blocks. However, the order of execution is
  // slightly changed. Consider:
  //
  // return a() -> finally { b() } -> return a()
  //
  // which would call a() first. However, changing the first return to a
  // break will result in calling b().

  Node prefinallyFollows = follow;
  follow = skipFinallyNodes(follow);

  if (prefinallyFollows != follow) {
    // There were finally clauses
    if (!isPure(result)) {
      // Can't defer the exit
      return n;
    }
  }

  if (follow == null && (n.isThrow() || result != null)) {
    // Can't complete remove a throw here or a return with a result.
    return n;
  }

  // When follow is null, this mean the follow of a break target is the
  // end of a function. This means a break is same as return.
  if (follow == null || areMatchingExits(n, follow)) {
    Node replacement = IR.breakNode();
    n.getParent().replaceChild(n, replacement);
    this.reportCodeChange();
    return replacement;
  }

  return n;
}