当前位置: 首页>>代码示例>>Java>>正文


Java Node.hasChildren方法代码示例

本文整理汇总了Java中com.google.javascript.rhino.Node.hasChildren方法的典型用法代码示例。如果您正苦于以下问题:Java Node.hasChildren方法的具体用法?Java Node.hasChildren怎么用?Java Node.hasChildren使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.javascript.rhino.Node的用法示例。


在下文中一共展示了Node.hasChildren方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: identifyTypeDefAssign

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
@Override
public String identifyTypeDefAssign(Node n) {
  Node firstChild = n.getFirstChild();
  int type = n.getType();
  if (type == Token.ASSIGN) {
    if (TYPEDEF_NAME.equals(n.getLastChild().getQualifiedName())) {
      return firstChild.getQualifiedName();
    }
  } else if (type == Token.VAR && firstChild.hasChildren()) {
    if (TYPEDEF_NAME.equals(
            firstChild.getFirstChild().getQualifiedName())) {
      return firstChild.getString();
    }
  }

  return null;
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:18,代码来源:ClosureCodingConvention.java

示例2: processVariableAssignment

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
private void processVariableAssignment(Component cmp, Node assignmentNode) {
    if (NodeUtil.isLiteralValue(assignmentNode, false)) {
        cmp.setCodeFragment(cmp.name() + " : " + NodeUtil.getStringValue(assignmentNode));
        cmp.setCodeFragment(declarationSnippet(assignmentNode.getToken()));
    } else if (assignmentNode.hasChildren() && assignmentNode.isNew()
            && (assignmentNode.getFirstChild().isName() || assignmentNode.getFirstChild().isGetProp())) {
        String invokedType;
        if (assignmentNode.getFirstChild().isGetProp()) {
            invokedType = assignmentNode.getFirstChild().getFirstChild().getString();
            cmp.insertComponentInvocation(new TypeDeclaration(invokedType));
        } else {
            invokedType = assignmentNode.getFirstChild().getString();
            cmp.insertComponentInvocation(new TypeInstantiation(invokedType));
        }
        cmp.setCodeFragment(invokedType);
    }
}
 
开发者ID:Zir0-93,项目名称:clarpse,代码行数:18,代码来源:JavaScriptListener.java

示例3: visit

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (parent == null) {
    return;
  }
  if (n.getType() == Token.FUNCTION || n.getType() == Token.SCRIPT) {
    return;
  }
  // Removes TRYs that had its CATCH removed and/or empty FINALLY.
  if (n.getType() == Token.TRY) {
    Node body = n.getFirstChild();
    Node catchOrFinallyBlock = body.getNext();
    Node finallyBlock = catchOrFinallyBlock.getNext();

    if (!catchOrFinallyBlock.hasChildren() &&
        (finallyBlock == null || !finallyBlock.hasChildren())) {
      n.removeChild(body);
      parent.replaceChild(n, body);
      compiler.reportCodeChange();
      n = body;
    }
  }
  GraphNode<Node, Branch> gNode = curCfg.getNode(n);
  if (gNode == null) { // Not in CFG.
    return;
  }
  if (gNode.getAnnotation() != GraphReachability.REACHABLE ||
      (removeNoOpStatements && !NodeUtil.mayHaveSideEffects(n))) {
    removeDeadExprStatementSafely(n, parent);
  }
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:32,代码来源:UnreachableCodeElimination.java

示例4: removeDeclaration

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Remove the given VAR declaration.
 */
private void removeDeclaration(Reference declaration) {
  Node varNode = declaration.getParent();
  varNode.removeChild(declaration.getNameNode());

  // Remove var node if empty
  if (!varNode.hasChildren()) {
    Preconditions.checkState(varNode.getType() == Token.VAR);

    Node grandparent = declaration.getGrandparent();
    NodeUtil.removeChild(grandparent, varNode);
  }

  compiler.reportCodeChange();
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:18,代码来源:InlineVariables.java

示例5: visit

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
public void visit(NodeTraversal t, Node n, Node parent) {
  Node child;
  switch (n.getType()) {
    case Token.IF:
      child = n.getFirstChild().getNext();  // skip the condition child
      break;

    case Token.WHILE:
    case Token.FOR:
      child = NodeUtil.getLoopCodeBlock(n);
      break;

    default:
      return;  // don't check other types
  }

  // semicolons cause VOID children. Empty blocks are allowed because
  // that's usually intentional, especially with loops.
  for (; child != null; child = child.getNext()) {
    if ((child.getType() == Token.BLOCK) && (!child.hasChildren())) {
      // Only warn on empty blocks that replaced EMPTY nodes.  BLOCKs with no
      // children are considered OK.
      if (child.wasEmptyNode()) {
        t.getCompiler().report(
            JSError.make(t, n, level, SUSPICIOUS_SEMICOLON));
      }
    }
  }
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:30,代码来源:CheckAccidentalSemicolon.java

示例6: inlineReturnValue

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Inline a function that fulfills the requirements of
 * canInlineReferenceDirectly into the call site, replacing only the CALL
 * node.
 */
private Node inlineReturnValue(Node callNode, Node fnNode) {
  Node block = fnNode.getLastChild();
  Node callParentNode = callNode.getParent();

  // NOTE: As the normalize pass guarantees globals aren't being
  // shadowed and an expression can't introduce new names, there is
  // no need to check for conflicts.

  // Create an argName -> expression map, checking for side effects.
  Map<String, Node> argMap =
      FunctionArgumentInjector.getFunctionCallParameterMap(
          fnNode, callNode, this.safeNameIdSupplier);

  Node newExpression;
  if (!block.hasChildren()) {
    newExpression = NodeUtil.newUndefinedNode();
  } else {
    Node returnNode = block.getFirstChild();
    Preconditions.checkArgument(returnNode.getType() == Token.RETURN);

    // Clone the return node first.
    Node safeReturnNode = returnNode.cloneTree();
    Node inlineResult = FunctionArgumentInjector.inject(
        safeReturnNode, null, argMap);
    Preconditions.checkArgument(safeReturnNode == inlineResult);
    newExpression = safeReturnNode.removeFirstChild();
  }

  callParentNode.replaceChild(callNode, newExpression);
  return newExpression;
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:37,代码来源:FunctionInjector.java

示例7: visit

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (NodeUtil.isVarDeclaration(n)) {
    if (removable.contains(n.getString())) {
      parent.removeChild(n);
      if (!parent.hasChildren()) {
        parent.getParent().removeChild(parent);
      }
    }
  }
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:12,代码来源:InstrumentFunctions.java

示例8: getReplacementReturnStatement

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Replace the 'return' statement with its child expression.
 * If the result is needed (resultName != null):
 *   "return foo()" becomes "resultName = foo()"
 *   "return" becomes "resultName = void 0".
 * Otherwise:
 *   "return foo()" becomes "foo()"
 *   "return", null is returned.
 */
private static Node getReplacementReturnStatement(
    Node node, String resultName) {
  Node resultNode = null;

  Node retVal = null;
  if (node.hasChildren()) {
    // Clone the child as the child hasn't been removed
    // from the node yet.
    retVal = node.getFirstChild().cloneTree();
  }

  if (resultName == null) {
    if (retVal != null) {
      resultNode = NodeUtil.newExpr(retVal); // maybe null.
    }
  } else {
    if (retVal == null) {
      // A result is needed create a dummy value.
      retVal = NodeUtil.newUndefinedNode();
    }
    // Create a "resultName = retVal;" statement.
    resultNode = createAssignStatementNode(resultName, retVal);
  }

  return resultNode;
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:36,代码来源:FunctionToBlockMutator.java

示例9: findNamedFunctions

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
public void findNamedFunctions(NodeTraversal t, Node n, Node parent) {
  if (!NodeUtil.isStatement(n)) {
    // There aren't any interesting functions here.
    return;
  }

  switch (n.getType()) {
    // Anonymous functions in the form of:
    //   var fooFn = function(x) { return ... }
    case Token.VAR:
      // TODO(johnlenz): Make this a Preconditions check.
      //     Currently this fails for some targets.
      if (n.hasOneChild()) {
        // Only look at declarations in the global scope.
        Node nameNode = n.getFirstChild();
        if (nameNode.getType() == Token.NAME && nameNode.hasChildren()
            && nameNode.getFirstChild().getType() == Token.FUNCTION) {
          maybeAddFunction(new FunctionVar(n), t.getModule());
        }
      }
      break;

    // Named functions
    // function Foo(x) { return ... }
    case Token.FUNCTION:
      Preconditions.checkState(NodeUtil.isStatementBlock(parent)
          || parent.getType() == Token.LABEL);
      Function fn = new NamedFunction(n);
      String name = fn.getName();
      if (!name.isEmpty()) {
        maybeAddFunction(fn, t.getModule());
      }
      break;
  }
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:36,代码来源:InlineFunctions.java

示例10: handleBreak

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
private void handleBreak(Node node) {
  String label = null;
  // See if it is a break with label.
  if (node.hasChildren()) {
    label = node.getFirstChild().getString();
  }
  Node cur;
  Node lastJump;
  Node parent = node.getParent();
  /*
   * Continuously look up the ancestor tree for the BREAK target or the target
   * with the corresponding label and connect to it. If along the path we
   * discover a FINALLY, we will connect the BREAK to that FINALLY. From then
   * on, we will just record the control flow changes in the finallyMap. This
   * is due to the fact that we need to connect any node that leaves its own
   * FINALLY block to the outer FINALLY or the BREAK's target but those nodes
   * are not known yet due to the way we traverse the nodes.
   */
  for (cur = node, lastJump = node;
      !isBreakTarget(cur, parent, label);
      cur = parent, parent = parent.getParent()) {
    if (cur.getType() == Token.TRY && NodeUtil.hasFinally(cur)) {
      if (lastJump == node) {
        createEdge(lastJump, Branch.UNCOND, computeFallThrough(
            cur.getLastChild()));
      } else {
        finallyMap.put(lastJump, computeFallThrough(cur.getLastChild()));
      }
      lastJump = cur;
    }
    Preconditions.checkState(parent != null, "Cannot find break target.");
  }
  if (lastJump == node) {
    createEdge(lastJump, Branch.UNCOND, computeFollowNode(cur));
  } else {
    finallyMap.put(lastJump, computeFollowNode(cur));
  }
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:39,代码来源:ControlFlowAnalysis.java

示例11: removeVarDeclaration

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Tries to remove variable declaration if the variable has been coalesced
 * with another variable that has already been declared.
 */
private void removeVarDeclaration(Node name) {
  Node var = name.getParent();
  Node parent = var.getParent();

  // Special case when we are in FOR-IN loop.
  if (NodeUtil.isForIn(parent)) {
    var.removeChild(name);
    parent.replaceChild(var, name);
  } else if (var.getChildCount() == 1) {
    // The removal is easy when there is only one variable in the VAR node.
    if (name.hasChildren()) {
      Node value = name.removeFirstChild();
      var.removeChild(name);
      Node assign = new Node(Token.ASSIGN, name, value);

      // We don't need to wrapped it with EXPR node if it is within a FOR.
      if (parent.getType() != Token.FOR) {
        assign = NodeUtil.newExpr(assign);
      }
      parent.replaceChild(var, assign);

    } else {
      // In a FOR( ; ; ) node, we must replace it with an EMPTY or else it
      // becomes a FOR-IN node.
      NodeUtil.removeChild(parent, var);
    }
  } else {
    if (!name.hasChildren()) {
      var.removeChild(name);
    }
    // We are going to leave duplicated declaration otherwise.
  }
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:38,代码来源:CoalesceVariableNames.java

示例12: onRedeclaration

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Remove duplicate VAR declarations encountered discovered during
 * scope creation.
 */
@Override
public void onRedeclaration(
    Scope s, String name, Node n, Node parent, Node gramps,
    Node nodeWithLineNumber) {
  Preconditions.checkState(n.getType() == Token.NAME);
  if (parent.getType() == Token.VAR) {
    Preconditions.checkState(parent.hasOneChild());

    //
    // Remove the parent VAR. There are three cases that need to be handled:
    //  1) "var a = b;" which is replaced with "a = b"
    //  2) "label:var a;" which is replaced with "label:;".  Ideally, the
    //     label itself would be removed but that is not possible in the
    //     context in which "onRedeclaration" is called.
    //  3) "for (var a in b) ..." which is replaced with "for (a in b)..."
    // Cases we don't need to handle are VARs with multiple children,
    // which have already been split into separate declarations, so there
    // is no need to handle that here, and "for (var a;;);", which has
    // been moved out of the loop.
    //
    // The result of this is that in each case the parent node is replaced
    // which is generally dangerous in a traversal but is fine here with
    // the scope creator, as the next node of interest is the parent's
    // next sibling.
    //
    if (n.hasChildren()) {
      // The var is being initialize, preserve the new value.
      parent.removeChild(n);
      // Convert "var name = value" to "name = value"
      Node value = n.getFirstChild();
      n.removeChild(value);
      Node replacement = new Node(Token.ASSIGN, n, value);
      gramps.replaceChild(parent, new Node(Token.EXPR_RESULT, replacement));
    } else {
      // It is an empty reference remove it.
      if (NodeUtil.isStatementBlock(gramps)) {
        gramps.removeChild(parent);
      } else if (gramps.getType() == Token.FOR) {
        // This is the "for (var a in b)..." case.  We don't need to worry
        // about initializers in "for (var a;;)..." as those are moved out
        // as part of the other normalizations.
        parent.removeChild(n);
        gramps.replaceChild(parent, n);
      } else {
        Preconditions.checkState(gramps.getType() == Token.LABEL);
        gramps.replaceChild(parent, new Node(Token.EMPTY));
      }
    }
    reportCodeChange("Duplicate VAR declaration");
  }
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:56,代码来源:Normalize.java

示例13: tryMinimizeIfBlockExits

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Look for exits (returns, breaks, or continues, depending on the context) at
 * the end of a block removes them by moving the if node's siblings, if any,
 * into the opposite condition block.
 *
 * @param srcBlock The block to inspect.
 * @param destBlock The block to move sibling nodes into.
 * @param ifNode The if node to work with.
 * @param exitType The type of exit to look for.
 * @param labelName The name associated with the exit, if any.
 * @nullable labelName null for anything excepted for named-break associated
 *           with a label.
 */
private void tryMinimizeIfBlockExits(Node srcBlock, Node destBlock,
    Node ifNode, int exitType, String labelName) {
  Node exitNodeParent = null;
  Node exitNode = null;

  // Pick an exit node candidate.
  if (srcBlock.getType() == Token.BLOCK) {
    if (!srcBlock.hasChildren()) {
      return;
    }
    exitNodeParent = srcBlock;
    exitNode = exitNodeParent.getLastChild();
  } else {
    // Just a single statement, if it isn't an exit bail.
    exitNodeParent = ifNode;
    exitNode = srcBlock;
  }

  // Verify the candidate.
  if (!matchingExitNode(exitNode, exitType, labelName)) {
    return;
  }

  // Take case of the if nodes siblings, if any.
  if (ifNode.getNext() != null) {
    // Move siblings of the if block into the opposite
    // logic block of the exit.
    Node newDestBlock = new Node(Token.BLOCK);
    if (destBlock == null) {
      // Only possible if this is the false block.
      ifNode.addChildToBack(newDestBlock);
    } else if (destBlock.getType() == Token.EMPTY) {
      // Use the new block.
      ifNode.replaceChild(destBlock, newDestBlock);
    } else if (destBlock.getType() == Token.BLOCK) {
      // Reuse the existing block.
      newDestBlock = destBlock;
    } else {
      // Add the existing statement to the new block.
      ifNode.replaceChild(destBlock, newDestBlock);
      newDestBlock.addChildToBack(destBlock);
    }

    // Move all the if node's following siblings.
    moveAllFollowing(ifNode, ifNode.getParent(), newDestBlock);
  }

  // Get rid of the "exit", replace with an empty item if needed.
  NodeUtil.removeChild(exitNodeParent, exitNode);

  compiler.reportCodeChange();
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:66,代码来源:MinimizeExitPoints.java

示例14: updateObjLitOrFunctionDeclarationAtAssignNode

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * Updates the first initialization (a.k.a "declaration") of a global name
 * that occurs at an ASSIGN node. See comment for
 * {@link #updateObjLitOrFunctionDeclaration}.
 *
 * @param n An object representing a global name (e.g. "a", "a.b.c")
 * @param alias The flattened name for {@code n} (e.g. "a", "a$b$c")
 */
private void updateObjLitOrFunctionDeclarationAtAssignNode(
    Name n, String alias) {
  // NOTE: It's important that we don't add additional nodes
  // (e.g. a var node before the exprstmt) because the exprstmt might be
  // the child of an if statement that's not inside a block).

  Ref ref = n.declaration;
  Node rvalue = ref.node.getNext();
  Node varNode = new Node(Token.VAR);
  Node varParent = ref.node.getAncestor(3);
  Node gramps = ref.node.getAncestor(2);
  boolean isObjLit = rvalue.getType() == Token.OBJECTLIT;

  if (isObjLit && n.canEliminate()) {
    // Eliminate the object literal altogether.
    varParent.replaceChild(gramps, varNode);
    ref.node = null;

  } else {
    if (rvalue.getType() == Token.FUNCTION) {
      checkForHosedThisReferences(rvalue, n.docInfo, n);
    }

    ref.node.getParent().removeChild(rvalue);

    Node nameNode = NodeUtil.newName(
        alias, ref.node.getAncestor(2), n.fullName());

    if (ref.node.getLastChild().getBooleanProp(Node.IS_CONSTANT_NAME)) {
      nameNode.putBooleanProp(Node.IS_CONSTANT_NAME, true);
    }

    varNode.addChildToBack(nameNode);
    nameNode.addChildToFront(rvalue);
    varParent.replaceChild(gramps, varNode);

    // Update the node ancestry stored in the reference.
    ref.node = nameNode;
  }

  if (isObjLit) {
    boolean discardKeys = n.aliasingGets == 0;
    declareVarsForObjLitValues(
        n, alias, rvalue,
        varNode, varParent.getChildBefore(varNode), varParent,
        discardKeys);
  }

  addStubsForUndeclaredProperties(n, alias, varParent, varNode);

  if (!varNode.hasChildren()) {
    varParent.removeChild(varNode);
  }

  compiler.reportCodeChange();
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:65,代码来源:CollapseProperties.java

示例15: hasCatchHandler

import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
 * @return Whether BLOCK (from a TRY node) contains a CATCH.
 * @see NodeUtil#getCatchBlock
 */
static boolean hasCatchHandler(Node n) {
  Preconditions.checkArgument(n.getType() == Token.BLOCK);
  return n.hasChildren() && n.getFirstChild().getType() == Token.CATCH;
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:9,代码来源:NodeUtil.java


注:本文中的com.google.javascript.rhino.Node.hasChildren方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。