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

The following examples show how to use com.google.javascript.rhino.Node#isLabel() . 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_128_CodeGenerator_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return Whether the Node is a DO or FUNCTION (with or without
 * labels).
 */
private boolean isOneExactlyFunctionOrDo(Node n) {
  if (n.isLabel()) {
    Node labeledStatement = n.getLastChild();
    if (!labeledStatement.isBlock()) {
      return isOneExactlyFunctionOrDo(labeledStatement);
    } else {
      // For labels with block children, we need to ensure that a
      // labeled FUNCTION or DO isn't generated when extraneous BLOCKs
      // are skipped.
      if (getNonEmptyChildCount(n, 2) == 1) {
        return isOneExactlyFunctionOrDo(getFirstNonEmptyChild(n));
      } else {
        // Either a empty statement or an block with more than one child,
        // way it isn't a FUNCTION or DO.
        return false;
      }
    }
  } else {
    return (n.isFunction() || n.isDo());
  }
}
 
Example 2
Source File: Normalize.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Do normalizations that introduce new siblings or parents.
 */
private void doStatementNormalizations(
    NodeTraversal t, Node n, Node parent) {
  if (n.isLabel()) {
    normalizeLabels(n);
  }

  // Only inspect the children of SCRIPTs, BLOCKs and LABELs, as all these
  // are the only legal place for VARs and FOR statements.
  if (NodeUtil.isStatementBlock(n) || n.isLabel()) {
    extractForInitializer(n, null, null);
  }

  // Only inspect the children of SCRIPTs, BLOCKs, as all these
  // are the only legal place for VARs.
  if (NodeUtil.isStatementBlock(n)) {
    splitVarDeclarations(n);
  }

  if (n.isFunction()) {
    moveNamedFunctions(n.getLastChild());
  }
}
 
Example 3
Source File: CodeGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @return Whether the Node is a DO or FUNCTION (with or without
 * labels).
 */
private boolean isOneExactlyFunctionOrDo(Node n) {
  if (n.isLabel()) {
    Node labeledStatement = n.getLastChild();
    if (!labeledStatement.isBlock()) {
      return isOneExactlyFunctionOrDo(labeledStatement);
    } else {
      // For labels with block children, we need to ensure that a
      // labeled FUNCTION or DO isn't generated when extraneous BLOCKs
      // are skipped.
      if (getNonEmptyChildCount(n, 2) == 1) {
        return isOneExactlyFunctionOrDo(getFirstNonEmptyChild(n));
      } else {
        // Either a empty statement or an block with more than one child,
        // way it isn't a FUNCTION or DO.
        return false;
      }
    }
  } else {
    return (n.isFunction() || n.isDo());
  }
}
 
Example 4
Source File: StrictModeCheck.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isFunction()) {
    checkFunctionUse(t, n);
  } else if (n.isName()) {
    if (!isDeclaration(n)) {
      checkNameUse(t, n);
    }
  } else if (n.isAssign()) {
    checkAssignment(t, n);
  } else if (n.isDelProp()) {
    checkDelete(t, n);
  } else if (n.isObjectLit()) {
    checkObjectLiteral(t, n);
  } else if (n.isLabel()) {
    checkLabel(t, n);
  }
}
 
Example 5
Source File: RenameLabels.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * shouldTraverse is call when descending into the Node tree, so it is used
 * here to build the context for label renames.
 *
 * {@inheritDoc}
 */
@Override
public boolean shouldTraverse(NodeTraversal nodeTraversal, Node node,
    Node parent) {
  if (node.isLabel()) {
    // Determine the new name for this label.
    LabelNamespace current = namespaceStack.peek();
    int currentDepth = current.renameMap.size() + 1;
    String name = node.getFirstChild().getString();

    // Store the context for this label name.
    LabelInfo li = new LabelInfo(currentDepth);
    Preconditions.checkState(!current.renameMap.containsKey(name));
    current.renameMap.put(name, li);

    // Create a new name, if needed, for this depth.
    if (names.size() < currentDepth) {
      names.add(nameSupplier.get());
    }

    String newName = getNameForId(currentDepth);
    compiler.addToDebugLog("label renamed: " + name + " => " + newName);
  }

  return true;
}
 
Example 6
Source File: PeepholeRemoveDeadCode.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Try folding EXPR_RESULT nodes by removing useless Ops and expressions.
 * @return the replacement node, if changed, or the original if not
 */
private Node tryFoldExpr(Node subtree) {
  Node result = trySimplifyUnusedResult(subtree.getFirstChild());
  if (result == null) {
    Node parent = subtree.getParent();
    // If the EXPR_RESULT no longer has any children, remove it as well.
    if (parent.isLabel()) {
      Node replacement = IR.block().srcref(subtree);
      parent.replaceChild(subtree, replacement);
      subtree = replacement;
    } else {
      subtree.detachFromParent();
      subtree = null;
    }
  }
  return subtree;
}
 
Example 7
Source File: PrepareAst.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Add blocks to IF, WHILE, DO, etc.
 */
private void normalizeBlocks(Node n) {
  if (NodeUtil.isControlStructure(n)
      && !n.isLabel()
      && !n.isSwitch()) {
    for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
      if (NodeUtil.isControlStructureCodeBlock(n,c) &&
          !c.isBlock()) {
        Node newBlock = IR.block().srcref(n);
        n.replaceChild(c, newBlock);
        if (!c.isEmpty()) {
          newBlock.addChildrenToFront(c);
        } else {
          newBlock.setWasEmptyNode(true);
        }
        c = newBlock;
        reportChange();
      }
    }
  }
}
 
Example 8
Source File: Closure_128_CodeGenerator_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return Whether the Node is a DO or FUNCTION (with or without
 * labels).
 */
private boolean isOneExactlyFunctionOrDo(Node n) {
  if (n.isLabel()) {
    Node labeledStatement = n.getLastChild();
    if (!labeledStatement.isBlock()) {
      return isOneExactlyFunctionOrDo(labeledStatement);
    } else {
      // For labels with block children, we need to ensure that a
      // labeled FUNCTION or DO isn't generated when extraneous BLOCKs
      // are skipped.
      if (getNonEmptyChildCount(n, 2) == 1) {
        return isOneExactlyFunctionOrDo(getFirstNonEmptyChild(n));
      } else {
        // Either a empty statement or an block with more than one child,
        // way it isn't a FUNCTION or DO.
        return false;
      }
    }
  } else {
    return (n.isFunction() || n.isDo());
  }
}
 
Example 9
Source File: Closure_129_PrepareAst_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Add blocks to IF, WHILE, DO, etc.
 */
private void normalizeBlocks(Node n) {
  if (NodeUtil.isControlStructure(n)
      && !n.isLabel()
      && !n.isSwitch()) {
    for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
      if (NodeUtil.isControlStructureCodeBlock(n,c) &&
          !c.isBlock()) {
        Node newBlock = IR.block().srcref(n);
        n.replaceChild(c, newBlock);
        if (!c.isEmpty()) {
          newBlock.addChildrenToFront(c);
        } else {
          newBlock.setWasEmptyNode(true);
        }
        c = newBlock;
        reportChange();
      }
    }
  }
}
 
Example 10
Source File: Closure_123_CodeGenerator_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return Whether the Node is a DO or FUNCTION (with or without
 * labels).
 */
private boolean isOneExactlyFunctionOrDo(Node n) {
  if (n.isLabel()) {
    Node labeledStatement = n.getLastChild();
    if (!labeledStatement.isBlock()) {
      return isOneExactlyFunctionOrDo(labeledStatement);
    } else {
      // For labels with block children, we need to ensure that a
      // labeled FUNCTION or DO isn't generated when extraneous BLOCKs
      // are skipped.
      if (getNonEmptyChildCount(n, 2) == 1) {
        return isOneExactlyFunctionOrDo(getFirstNonEmptyChild(n));
      } else {
        // Either a empty statement or an block with more than one child,
        // way it isn't a FUNCTION or DO.
        return false;
      }
    }
  } else {
    return (n.isFunction() || n.isDo());
  }
}
 
Example 11
Source File: Closure_123_CodeGenerator_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return Whether the Node is a DO or FUNCTION (with or without
 * labels).
 */
private boolean isOneExactlyFunctionOrDo(Node n) {
  if (n.isLabel()) {
    Node labeledStatement = n.getLastChild();
    if (!labeledStatement.isBlock()) {
      return isOneExactlyFunctionOrDo(labeledStatement);
    } else {
      // For labels with block children, we need to ensure that a
      // labeled FUNCTION or DO isn't generated when extraneous BLOCKs
      // are skipped.
      if (getNonEmptyChildCount(n, 2) == 1) {
        return isOneExactlyFunctionOrDo(getFirstNonEmptyChild(n));
      } else {
        // Either a empty statement or an block with more than one child,
        // way it isn't a FUNCTION or DO.
        return false;
      }
    }
  } else {
    return (n.isFunction() || n.isDo());
  }
}
 
Example 12
Source File: Closure_34_CodeGenerator_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return Whether the Node is a DO or FUNCTION (with or without
 * labels).
 */
private boolean isOneExactlyFunctionOrDo(Node n) {
  if (n.isLabel()) {
    Node labeledStatement = n.getLastChild();
    if (!labeledStatement.isBlock()) {
      return isOneExactlyFunctionOrDo(labeledStatement);
    } else {
      // For labels with block children, we need to ensure that a
      // labeled FUNCTION or DO isn't generated when extraneous BLOCKs
      // are skipped.
      if (getNonEmptyChildCount(n, 2) == 1) {
        return isOneExactlyFunctionOrDo(getFirstNonEmptyChild(n));
      } else {
        // Either a empty statement or an block with more than one child,
        // way it isn't a FUNCTION or DO.
        return false;
      }
    }
  } else {
    return (n.isFunction() || n.isDo());
  }
}
 
Example 13
Source File: Closure_34_CodeGenerator_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return Whether the Node is a DO or FUNCTION (with or without
 * labels).
 */
private boolean isOneExactlyFunctionOrDo(Node n) {
  if (n.isLabel()) {
    Node labeledStatement = n.getLastChild();
    if (!labeledStatement.isBlock()) {
      return isOneExactlyFunctionOrDo(labeledStatement);
    } else {
      // For labels with block children, we need to ensure that a
      // labeled FUNCTION or DO isn't generated when extraneous BLOCKs
      // are skipped.
      if (getNonEmptyChildCount(n, 2) == 1) {
        return isOneExactlyFunctionOrDo(getFirstNonEmptyChild(n));
      } else {
        // Either a empty statement or an block with more than one child,
        // way it isn't a FUNCTION or DO.
        return false;
      }
    }
  } else {
    return (n.isFunction() || n.isDo());
  }
}
 
Example 14
Source File: SideEffectsAnalysisTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isLabel() &&
      target.equals(n.getFirstChild().getString())) {

    found = n.getLastChild();
  }
}
 
Example 15
Source File: 1_ControlFlowAnalysis.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check if label is actually referencing the target control structure. If
 * label is null, it always returns true.
 */
private static boolean matchLabel(Node target, String label) {
  if (label == null) {
    return true;
  }
  while (target.isLabel()) {
    if (target.getFirstChild().getString().equals(label)) {
      return true;
    }
    target = target.getParent();
  }
  return false;
}
 
Example 16
Source File: MaybeReachingVariableUseTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isLabel()) {
    if (n.getFirstChild().getString().equals("D")) {
      def = n.getLastChild();
    } else if (n.getFirstChild().getString().startsWith("U")) {
      uses.add(n.getLastChild());
    }
  }
}
 
Example 17
Source File: Closure_14_ControlFlowAnalysis_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Check if label is actually referencing the target control structure. If
 * label is null, it always returns true.
 */
private static boolean matchLabel(Node target, String label) {
  if (label == null) {
    return true;
  }
  while (target.isLabel()) {
    if (target.getFirstChild().getString().equals(label)) {
      return true;
    }
    target = target.getParent();
  }
  return false;
}
 
Example 18
Source File: MustBeReachingVariableDefTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isLabel()) {
    if (n.getFirstChild().getString().equals("D")) {
      def = n.getLastChild();
    } else if (n.getFirstChild().getString().equals("U")) {
      use = n.getLastChild();
    }
  }
}
 
Example 19
Source File: ControlFlowAnalysis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check if label is actually referencing the target control structure. If
 * label is null, it always returns true.
 */
private static boolean matchLabel(Node target, String label) {
  if (label == null) {
    return true;
  }
  while (target.isLabel()) {
    if (target.getFirstChild().getString().equals(label)) {
      return true;
    }
    target = target.getParent();
  }
  return false;
}
 
Example 20
Source File: 1_ControlFlowAnalysis.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check if label is actually referencing the target control structure. If
 * label is null, it always returns true.
 */
private static boolean matchLabel(Node target, String label) {
  if (label == null) {
    return true;
  }
  while (target.isLabel()) {
    if (target.getFirstChild().getString().equals(label)) {
      return true;
    }
    target = target.getParent();
  }
  return false;
}