public void visit(NodeTraversal t, Node n, Node parent) {
      if (n.getType() != Token.FUNCTION) {
        return;
      }

      int id = functionNames.getFunctionId(n);
      if (id < 0) {
        // Function node was added during compilation; don't instrument.
        return;
      }

      if (reportFunctionName.length() != 0) {
        Node body = n.getFirstChild().getNext().getNext();
        Node call =
            new Node(
                Token.CALL, Node.newString(Token.NAME, reportFunctionName), Node.newNumber(id));
        Node expr = new Node(Token.EXPR_RESULT, call);
        body.addChildToFront(expr);
        compiler.reportCodeChange();
      }

      if (reportFunctionExitName.length() != 0) {
        Node body = n.getFirstChild().getNext().getNext();
        (new InstrumentReturns(id)).process(body);
      }

      if (definedFunctionName.length() != 0) {
        Node call =
            new Node(
                Token.CALL, Node.newString(Token.NAME, definedFunctionName), Node.newNumber(id));
        Node expr = NodeUtil.newExpr(call);

        Node addingRoot = null;
        if (NodeUtil.isFunctionDeclaration(n)) {
          JSModule module = t.getModule();
          addingRoot = compiler.getNodeForCodeInsertion(module);
          addingRoot.addChildToFront(expr);
        } else {
          Node beforeChild = n;
          for (Node ancestor : n.getAncestors()) {
            int type = ancestor.getType();
            if (type == Token.BLOCK || type == Token.SCRIPT) {
              addingRoot = ancestor;
              break;
            }
            beforeChild = ancestor;
          }
          addingRoot.addChildBefore(expr, beforeChild);
        }
        compiler.reportCodeChange();
      }
    }
 private Node createTemplateParameterNode(int index, JSType type) {
   Preconditions.checkState(index >= 0);
   Preconditions.checkNotNull(type);
   Node n = Node.newNumber(index);
   n.setType(TEMPLATE_TYPE_PARAM);
   n.setJSType(type);
   return n;
 }
 /** Applies optimizations to all previously marked nodes. */
 public void applyChanges() {
   for (Node n : toRemove) {
     n.getParent().removeChild(n);
     compiler.reportCodeChange();
   }
   for (Node n : toReplaceWithZero) {
     n.getParent().replaceChild(n, Node.newNumber(0).copyInformationFrom(n));
     compiler.reportCodeChange();
   }
 }
Пример #4
0
  @SuppressWarnings("unchecked")
  public void testParse() {
    Node a = Node.newString(Token.NAME, "a");
    a.addChildToFront(Node.newString(Token.NAME, "b"));
    List<ParserResult> testCases =
        ImmutableList.of(
            new ParserResult("3;", createScript(new Node(Token.EXPR_RESULT, Node.newNumber(3.0)))),
            new ParserResult("var a = b;", createScript(new Node(Token.VAR, a))),
            new ParserResult(
                "\"hell\\\no\\ world\\\n\\\n!\"",
                createScript(
                    new Node(Token.EXPR_RESULT, Node.newString(Token.STRING, "hello world!")))));

    for (ParserResult testCase : testCases) {
      assertNodeEquality(testCase.node, parse(testCase.code));
    }
  }
 private Node createTemplateLocalNameNode(int index) {
   Preconditions.checkState(index >= 0);
   Node n = Node.newNumber(index);
   n.setType(TEMPLATE_LOCAL_NAME);
   return n;
 }
Пример #6
0
 /* (non-Javadoc)
  * @see com.google.javascript.jscomp.fuzzing.AbstractFuzzer#generate(int)
  */
 @Override
 protected Node generate(int budget, Set<Type> types) {
   return Node.newNumber(context.random.nextInt(getOwnConfig().get("max").getAsInt()));
 }
 private Node newReportFunctionExitNode() {
   return new Node(
       Token.CALL,
       Node.newString(Token.NAME, reportFunctionExitName),
       Node.newNumber(functionId));
 }