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

The following examples show how to use com.google.javascript.rhino.Node#isIf() . 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_132_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
private void tryJoinForCondition(Node n) {
  if (!late) {
    return;
  }

  Node block = n.getLastChild();
  Node maybeIf = block.getFirstChild();
  if (maybeIf != null && maybeIf.isIf()) {
    Node maybeBreak = maybeIf.getChildAtIndex(1).getFirstChild();
    if (maybeBreak != null && maybeBreak.isBreak()
        && !maybeBreak.hasChildren()) {

      // Preserve the IF ELSE expression is there is one.
      if (maybeIf.getChildCount() == 3) {
        block.replaceChild(maybeIf,
            maybeIf.getLastChild().detachFromParent());
      } else {
        block.removeFirstChild();
      }

      Node ifCondition = maybeIf.removeFirstChild();
      Node fixedIfCondition = IR.not(ifCondition)
          .srcref(ifCondition);

      // OK, join the IF expression with the FOR expression
      Node forCondition = NodeUtil.getConditionExpression(n);
      if (forCondition.isEmpty()) {
        n.replaceChild(forCondition, fixedIfCondition);
      } else {
        Node replacement = new Node(Token.AND);
        n.replaceChild(forCondition, replacement);
        replacement.addChildToBack(forCondition);
        replacement.addChildToBack(fixedIfCondition);
      }

      reportCodeChange();
    }
  }
}
 
Example 2
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
private void tryJoinForCondition(Node n) {
  if (!late) {
    return;
  }

  Node block = n.getLastChild();
  Node maybeIf = block.getFirstChild();
  if (maybeIf != null && maybeIf.isIf()) {
    Node maybeBreak = maybeIf.getChildAtIndex(1).getFirstChild();
    if (maybeBreak != null && maybeBreak.isBreak()
        && !maybeBreak.hasChildren()) {

      // Preserve the IF ELSE expression is there is one.
      if (maybeIf.getChildCount() == 3) {
        block.replaceChild(maybeIf,
            maybeIf.getLastChild().detachFromParent());
      } else {
        block.removeFirstChild();
      }

      Node ifCondition = maybeIf.removeFirstChild();
      Node fixedIfCondition = IR.not(ifCondition)
          .srcref(ifCondition);

      // OK, join the IF expression with the FOR expression
      Node forCondition = NodeUtil.getConditionExpression(n);
      if (forCondition.isEmpty()) {
        n.replaceChild(forCondition, fixedIfCondition);
      } else {
        Node replacement = new Node(Token.AND);
        n.replaceChild(forCondition, replacement);
        replacement.addChildToBack(forCondition);
        replacement.addChildToBack(fixedIfCondition);
      }

      reportCodeChange();
    }
  }
}
 
Example 3
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
private void tryJoinForCondition(Node n) {
  if (!late) {
    return;
  }

  Node block = n.getLastChild();
  Node maybeIf = block.getFirstChild();
  if (maybeIf != null && maybeIf.isIf()) {
    Node maybeBreak = maybeIf.getChildAtIndex(1).getFirstChild();
    if (maybeBreak != null && maybeBreak.isBreak()
        && !maybeBreak.hasChildren()) {

      // Preserve the IF ELSE expression is there is one.
      if (maybeIf.getChildCount() == 3) {
        block.replaceChild(maybeIf,
            maybeIf.getLastChild().detachFromParent());
      } else {
        block.removeFirstChild();
      }

      Node ifCondition = maybeIf.removeFirstChild();
      Node fixedIfCondition = IR.not(ifCondition)
          .srcref(ifCondition);

      // OK, join the IF expression with the FOR expression
      Node forCondition = NodeUtil.getConditionExpression(n);
      if (forCondition.isEmpty()) {
        n.replaceChild(forCondition, fixedIfCondition);
      } else {
        Node replacement = new Node(Token.AND);
        n.replaceChild(forCondition, replacement);
        replacement.addChildToBack(forCondition);
        replacement.addChildToBack(fixedIfCondition);
      }

      reportCodeChange();
    }
  }
}
 
Example 4
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
private void tryJoinForCondition(Node n) {
  if (!late) {
    return;
  }

  Node block = n.getLastChild();
  Node maybeIf = block.getFirstChild();
  if (maybeIf != null && maybeIf.isIf()) {
    Node maybeBreak = maybeIf.getChildAtIndex(1).getFirstChild();
    if (maybeBreak != null && maybeBreak.isBreak()
        && !maybeBreak.hasChildren()) {

      // Preserve the IF ELSE expression is there is one.
      if (maybeIf.getChildCount() == 3) {
        block.replaceChild(maybeIf,
            maybeIf.getLastChild().detachFromParent());
      } else {
        block.removeFirstChild();
      }

      Node ifCondition = maybeIf.removeFirstChild();
      Node fixedIfCondition = IR.not(ifCondition)
          .srcref(ifCondition);

      // OK, join the IF expression with the FOR expression
      Node forCondition = NodeUtil.getConditionExpression(n);
      if (forCondition.isEmpty()) {
        n.replaceChild(forCondition, fixedIfCondition);
      } else {
        Node replacement = new Node(Token.AND);
        n.replaceChild(forCondition, replacement);
        replacement.addChildToBack(forCondition);
        replacement.addChildToBack(fixedIfCondition);
      }

      reportCodeChange();
    }
  }
}
 
Example 5
Source File: PeepholeRemoveDeadCode.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return The condition of a conditional statement.
 */
private Node getConditionalStatementCondition(Node n) {
  if (n.isIf()) {
    return NodeUtil.getConditionExpression(n);
  } else {
    Preconditions.checkState(isExprConditional(n));
    return n.getFirstChild().getFirstChild();
  }
}
 
Example 6
Source File: PeepholeSubstituteAlternateSyntax.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void tryJoinForCondition(Node n) {
  if (!late) {
    return;
  }

  Node block = n.getLastChild();
  Node maybeIf = block.getFirstChild();
  if (maybeIf != null && maybeIf.isIf()) {
    Node maybeBreak = maybeIf.getChildAtIndex(1).getFirstChild();
    if (maybeBreak != null && maybeBreak.isBreak()
        && !maybeBreak.hasChildren()) {

      // Preserve the IF ELSE expression is there is one.
      if (maybeIf.getChildCount() == 3) {
        block.replaceChild(maybeIf,
            maybeIf.getLastChild().detachFromParent());
      } else {
        block.removeFirstChild();
      }

      Node ifCondition = maybeIf.removeFirstChild();
      Node fixedIfCondition = IR.not(ifCondition)
          .srcref(ifCondition);

      // OK, join the IF expression with the FOR expression
      Node forCondition = NodeUtil.getConditionExpression(n);
      if (forCondition.isEmpty()) {
        n.replaceChild(forCondition, fixedIfCondition);
      } else {
        Node replacement = new Node(Token.AND);
        n.replaceChild(forCondition, replacement);
        replacement.addChildToBack(forCondition);
        replacement.addChildToBack(fixedIfCondition);
      }

      reportCodeChange();
    }
  }
}
 
Example 7
Source File: Closure_8_CollapseVariableDeclarations_s.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isVar()) {
    blacklistStubVars(t, n);
  }

  // Only care about var nodes
  if (!n.isVar() && !canBeRedeclared(n, t.getScope())) return;

  // If we've already looked at this node, skip it
  if (nodesToCollapse.contains(n)) return;

  // Adjacent VAR children of an IF node are the if and else parts and can't
  // be collapsed
  if (parent.isIf()) return;

  Node varNode = n;

  boolean hasVar = n.isVar();

  // Find variable declarations that follow this one (if any)
  n = n.getNext();

  boolean hasNodesToCollapse = false;

  while (n != null &&
      (n.isVar() || canBeRedeclared(n, t.getScope()))) {

    if (n.isVar()) {
      blacklistStubVars(t, n);
      hasVar = true;
    }

    nodesToCollapse.add(n);
    hasNodesToCollapse = true;

    n = n.getNext();
  }

  if (hasNodesToCollapse && hasVar) {
    nodesToCollapse.add(varNode);
    collapses.add(new Collapse(varNode, n, parent));
  }
}
 
Example 8
Source File: Closure_8_CollapseVariableDeclarations_t.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isVar()) {
    blacklistStubVars(t, n);
  }

  // Only care about var nodes
  if (!n.isVar() && !canBeRedeclared(n, t.getScope())) return;

  // If we've already looked at this node, skip it
  if (nodesToCollapse.contains(n)) return;

  // Adjacent VAR children of an IF node are the if and else parts and can't
  // be collapsed
  if (parent.isIf()) return;

  Node varNode = n;

  boolean hasVar = n.isVar();

  // Find variable declarations that follow this one (if any)
  n = n.getNext();

  boolean hasNodesToCollapse = false;

  while (n != null &&
      (n.isVar() || canBeRedeclared(n, t.getScope()))) {

    if (n.isVar()) {
      blacklistStubVars(t, n);
      hasVar = true;
    }

    nodesToCollapse.add(n);
    hasNodesToCollapse = true;

    n = n.getNext();
  }

  if (hasNodesToCollapse && hasVar) {
    nodesToCollapse.add(varNode);
    collapses.add(new Collapse(varNode, n, parent));
  }
}
 
Example 9
Source File: PeepholeRemoveDeadCode.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return Whether the node is a conditional statement.
 */
private boolean isConditionalStatement(Node n) {
  // We defined a conditional statement to be a IF or EXPR_RESULT rooted with
  // a HOOK, AND, or OR node.
  return n != null && (n.isIf() || isExprConditional(n));
}
 
Example 10
Source File: CollapseVariableDeclarations.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isVar()) {
    blacklistStubVars(t, n);
  }

  // Only care about var nodes
  if (!n.isVar() && !canBeRedeclared(n, t.getScope())) return;

  // If we've already looked at this node, skip it
  if (nodesToCollapse.contains(n)) return;

  // Adjacent VAR children of an IF node are the if and else parts and can't
  // be collapsed
  if (parent.isIf()) return;

  Node varNode = n;

  boolean hasVar = n.isVar();

  // Find variable declarations that follow this one (if any)
  n = n.getNext();

  boolean hasNodesToCollapse = false;

  while (n != null &&
      (n.isVar() || canBeRedeclared(n, t.getScope()))) {

    if (n.isVar()) {
      blacklistStubVars(t, n);
      hasVar = true;
    }

    nodesToCollapse.add(n);
    hasNodesToCollapse = true;

    n = n.getNext();
  }

  if (hasNodesToCollapse && hasVar) {
    nodesToCollapse.add(varNode);
    collapses.add(new Collapse(varNode, n, parent));
  }
}