Java Code Examples for com.google.javascript.rhino.Token#EMPTY

The following examples show how to use com.google.javascript.rhino.Token#EMPTY . 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: LinkCommentsForOneFile.java    From clutz with MIT License 6 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (isDone()) {
    return;
  }
  if (n.isScript()) {
    // New dummy node at the end of the file
    Node dummy = new Node(Token.EMPTY);
    n.addChildToBack(dummy);

    while (hasRemainingComments()) {
      // If the new comment is separated from the current one by at least a line,
      // output the current group of comments.
      if (getFirstLineOfNextComment() - getLastLineOfCurrentComment() > 1) {
        dummy.getParent().addChildBefore(newFloatingCommentFromBuffer(), dummy);
      }
      addNextCommentToBuffer();
    }
    n.addChildBefore(newFloatingCommentFromBuffer(), dummy);
    addNextCommentToBuffer();
    n.removeChild(dummy);
  }
}
 
Example 2
Source File: Closure_75_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns true if this is a literal value. We define a literal value
 * as any node that evaluates to the same thing regardless of when or
 * where it is evaluated. So /xyz/ and [3, 5] are literals, but
 * the name a is not.
 *
 * Function literals do not meet this definition, because they
 * lexically capture variables. For example, if you have
 * <code>
 * function() { return a; }
 * </code>
 * If it is evaluated in a different scope, then it
 * captures a different variable. Even if the function did not read
 * any captured vairables directly, it would still fail this definition,
 * because it affects the lifecycle of variables in the enclosing scope.
 *
 * However, a function literal with respect to a particular scope is
 * a literal.
 *
 * @param includeFunctions If true, all function expressions will be
 *     treated as literals.
 */
static boolean isLiteralValue(Node n, boolean includeFunctions) {
  switch (n.getType()) {
    case Token.ARRAYLIT:
      for (Node child = n.getFirstChild(); child != null;
           child = child.getNext()) {
        if (child.getType() != Token.EMPTY
            && !isLiteralValue(child, includeFunctions)) {
          return false;
        }
      }
      return true;

    case Token.REGEXP:
      // Return true only if all children are const.
      for (Node child = n.getFirstChild(); child != null;
           child = child.getNext()) {
        if (!isLiteralValue(child, includeFunctions)) {
          return false;
        }
      }
      return true;

    case Token.OBJECTLIT:
      // Return true only if all values are const.
      for (Node child = n.getFirstChild(); child != null;
           child = child.getNext()) {
        if (!isLiteralValue(child.getFirstChild(), includeFunctions)) {
          return false;
        }
      }
      return true;

    case Token.FUNCTION:
      return includeFunctions && !NodeUtil.isFunctionDeclaration(n);

    default:
      return isImmutableValue(n);
  }
}
 
Example 3
Source File: Closure_94_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns whether this a BLOCK node with no children.
 *
 * @param block The node.
 */
static boolean isEmptyBlock(Node block) {
  if (block.getType() != Token.BLOCK) {
    return false;
  }

  for (Node n = block.getFirstChild(); n != null; n = n.getNext()) {
    if (n.getType() != Token.EMPTY) {
      return false;
    }
  }
  return true;
}
 
Example 4
Source File: Closure_79_Normalize_s.java    From coming with MIT License 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  switch (n.getType()) {
    case Token.WHILE:
      if (CONVERT_WHILE_TO_FOR) {
        Node expr = n.getFirstChild();
        n.setType(Token.FOR);
        Node empty = new Node(Token.EMPTY);
        empty.copyInformationFrom(n);
        n.addChildBefore(empty, expr);
        n.addChildAfter(empty.cloneNode(), expr);
        reportCodeChange("WHILE node");
      }
      break;

    case Token.FUNCTION:
      normalizeFunctionDeclaration(n);
      break;

    case Token.NAME:
    case Token.STRING:
    case Token.GET:
    case Token.SET:
      if (!compiler.getLifeCycleStage().isNormalizedObfuscated()) {
        annotateConstantsByConvention(n, parent);
      }
      break;
  }
}
 
Example 5
Source File: Closure_77_CodeGenerator_t.java    From coming with MIT License 5 votes vote down vote up
/** Gets the first non-empty child of the given node. */
private static Node getFirstNonEmptyChild(Node n) {
  for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
    if (c.getType() == Token.BLOCK) {
      Node result = getFirstNonEmptyChild(c);
      if (result != null) {
        return result;
      }
    } else if (c.getType() != Token.EMPTY) {
      return c;
    }
  }
  return null;
}
 
Example 6
Source File: Closure_86_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns whether this a BLOCK node with no children.
 *
 * @param block The node.
 */
static boolean isEmptyBlock(Node block) {
  if (block.getType() != Token.BLOCK) {
    return false;
  }

  for (Node n = block.getFirstChild(); n != null; n = n.getNext()) {
    if (n.getType() != Token.EMPTY) {
      return false;
    }
  }
  return true;
}
 
Example 7
Source File: Closure_86_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns whether this a BLOCK node with no children.
 *
 * @param block The node.
 */
static boolean isEmptyBlock(Node block) {
  if (block.getType() != Token.BLOCK) {
    return false;
  }

  for (Node n = block.getFirstChild(); n != null; n = n.getNext()) {
    if (n.getType() != Token.EMPTY) {
      return false;
    }
  }
  return true;
}
 
Example 8
Source File: Closure_73_CodeGenerator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * This function adds a comma-separated list as is specified by an ARRAYLIT
 * node with the associated skipIndexes array.  This is a space optimization
 * since we avoid creating a whole Node object for each empty array literal
 * slot.
 * @param firstInList The first in the node list (chained through the next
 * property).
 */
void addArrayList(Node firstInList) {
  boolean lastWasEmpty = false;
  for (Node n = firstInList; n != null; n = n.getNext()) {
    if (n != firstInList) {
      cc.listSeparator();
    }
    addExpr(n, 1);
    lastWasEmpty = n.getType() == Token.EMPTY;
  }

  if (lastWasEmpty) {
    cc.listSeparator();
  }
}
 
Example 9
Source File: Closure_84_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
private Node transformBlock(AstNode node) {
  Node irNode = transform(node);
  if (irNode.getType() != Token.BLOCK) {
    if (irNode.getType() == Token.EMPTY) {
      irNode.setType(Token.BLOCK);
      irNode.setWasEmptyNode(true);
    } else {
      Node newBlock = newNode(Token.BLOCK, irNode);
      newBlock.setLineno(irNode.getLineno());
      newBlock.setCharno(irNode.getCharno());
      irNode = newBlock;
    }
  }
  return irNode;
}
 
Example 10
Source File: Closure_80_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns whether this a BLOCK node with no children.
 *
 * @param block The node.
 */
static boolean isEmptyBlock(Node block) {
  if (block.getType() != Token.BLOCK) {
    return false;
  }

  for (Node n = block.getFirstChild(); n != null; n = n.getNext()) {
    if (n.getType() != Token.EMPTY) {
      return false;
    }
  }
  return true;
}
 
Example 11
Source File: Closure_52_CodeGenerator_s.java    From coming with MIT License 5 votes vote down vote up
/** Gets the first non-empty child of the given node. */
private static Node getFirstNonEmptyChild(Node n) {
  for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
    if (c.getType() == Token.BLOCK) {
      Node result = getFirstNonEmptyChild(c);
      if (result != null) {
        return result;
      }
    } else if (c.getType() != Token.EMPTY) {
      return c;
    }
  }
  return null;
}
 
Example 12
Source File: Closure_75_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
static int precedence(int type) {
  switch (type) {
    case Token.COMMA:  return 0;
    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_ADD:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_DIV:
    case Token.ASSIGN_MOD:
    case Token.ASSIGN: return 1;
    case Token.HOOK:   return 2;  // ?: operator
    case Token.OR:     return 3;
    case Token.AND:    return 4;
    case Token.BITOR:  return 5;
    case Token.BITXOR: return 6;
    case Token.BITAND: return 7;
    case Token.EQ:
    case Token.NE:
    case Token.SHEQ:
    case Token.SHNE:   return 8;
    case Token.LT:
    case Token.GT:
    case Token.LE:
    case Token.GE:
    case Token.INSTANCEOF:
    case Token.IN:     return 9;
    case Token.LSH:
    case Token.RSH:
    case Token.URSH:   return 10;
    case Token.SUB:
    case Token.ADD:    return 11;
    case Token.MUL:
    case Token.MOD:
    case Token.DIV:    return 12;
    case Token.INC:
    case Token.DEC:
    case Token.NEW:
    case Token.DELPROP:
    case Token.TYPEOF:
    case Token.VOID:
    case Token.NOT:
    case Token.BITNOT:
    case Token.POS:
    case Token.NEG:    return 13;

    case Token.ARRAYLIT:
    case Token.CALL:
    case Token.EMPTY:
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.GETELEM:
    case Token.GETPROP:
    case Token.GET_REF:
    case Token.IF:
    case Token.LP:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.STRING:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Node.tokenToName(type) +
                             " (type " + type + ")");
  }
}
 
Example 13
Source File: Closure_75_NodeUtil_t.java    From coming with MIT License 4 votes vote down vote up
static int precedence(int type) {
  switch (type) {
    case Token.COMMA:  return 0;
    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_ADD:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_DIV:
    case Token.ASSIGN_MOD:
    case Token.ASSIGN: return 1;
    case Token.HOOK:   return 2;  // ?: operator
    case Token.OR:     return 3;
    case Token.AND:    return 4;
    case Token.BITOR:  return 5;
    case Token.BITXOR: return 6;
    case Token.BITAND: return 7;
    case Token.EQ:
    case Token.NE:
    case Token.SHEQ:
    case Token.SHNE:   return 8;
    case Token.LT:
    case Token.GT:
    case Token.LE:
    case Token.GE:
    case Token.INSTANCEOF:
    case Token.IN:     return 9;
    case Token.LSH:
    case Token.RSH:
    case Token.URSH:   return 10;
    case Token.SUB:
    case Token.ADD:    return 11;
    case Token.MUL:
    case Token.MOD:
    case Token.DIV:    return 12;
    case Token.INC:
    case Token.DEC:
    case Token.NEW:
    case Token.DELPROP:
    case Token.TYPEOF:
    case Token.VOID:
    case Token.NOT:
    case Token.BITNOT:
    case Token.POS:
    case Token.NEG:    return 13;

    case Token.ARRAYLIT:
    case Token.CALL:
    case Token.EMPTY:
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.GETELEM:
    case Token.GETPROP:
    case Token.GET_REF:
    case Token.IF:
    case Token.LP:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.STRING:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Node.tokenToName(type) +
                             " (type " + type + ")");
  }
}
 
Example 14
Source File: Closure_65_CodeGenerator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Adds a block or expression, substituting a VOID with an empty statement.
 * This is used for "for (...);" and "if (...);" type statements.
 *
 * @param n The node to print.
 * @param context The context to determine how the node should be printed.
 */
private void addNonEmptyStatement(
    Node n, Context context, boolean allowNonBlockChild) {
  Node nodeToProcess = n;

  if (!allowNonBlockChild && n.getType() != Token.BLOCK) {
    throw new Error("Missing BLOCK child.");
  }

  // Strip unneeded blocks, that is blocks with <2 children unless
  // the CodePrinter specifically wants to keep them.
  if (n.getType() == Token.BLOCK) {
    int count = getNonEmptyChildCount(n, 2);
    if (count == 0) {
      if (cc.shouldPreserveExtraBlocks()) {
        cc.beginBlock();
        cc.endBlock(cc.breakAfterBlockFor(n, context == Context.STATEMENT));
      } else {
        cc.endStatement(true);
      }
      return;
    }

    if (count == 1) {
      // Hack around a couple of browser bugs:
      //   Safari needs a block around function declarations.
      //   IE6/7 needs a block around DOs.
      Node firstAndOnlyChild = getFirstNonEmptyChild(n);
      boolean alwaysWrapInBlock = cc.shouldPreserveExtraBlocks();
      if (alwaysWrapInBlock || isOneExactlyFunctionOrDo(firstAndOnlyChild)) {
        cc.beginBlock();
        add(firstAndOnlyChild, Context.STATEMENT);
        cc.maybeLineBreak();
        cc.endBlock(cc.breakAfterBlockFor(n, context == Context.STATEMENT));
        return;
      } else {
        // Continue with the only child.
        nodeToProcess = firstAndOnlyChild;
      }
    }

    if (count > 1) {
      context = Context.PRESERVE_BLOCK;
    }
  }

  if (nodeToProcess.getType() == Token.EMPTY) {
    cc.endStatement(true);
  } else {
    add(nodeToProcess, context);

    // VAR doesn't include ';' since it gets used in expressions - so any
    // VAR in a statement context needs a call to endStatement() here.
    if (nodeToProcess.getType() == Token.VAR) {
      cc.endStatement();
    }
  }
}
 
Example 15
Source File: Closure_86_NodeUtil_t.java    From coming with MIT License 4 votes vote down vote up
static int precedence(int type) {
  switch (type) {
    case Token.COMMA:  return 0;
    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_ADD:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_DIV:
    case Token.ASSIGN_MOD:
    case Token.ASSIGN: return 1;
    case Token.HOOK:   return 2;  // ?: operator
    case Token.OR:     return 3;
    case Token.AND:    return 4;
    case Token.BITOR:  return 5;
    case Token.BITXOR: return 6;
    case Token.BITAND: return 7;
    case Token.EQ:
    case Token.NE:
    case Token.SHEQ:
    case Token.SHNE:   return 8;
    case Token.LT:
    case Token.GT:
    case Token.LE:
    case Token.GE:
    case Token.INSTANCEOF:
    case Token.IN:     return 9;
    case Token.LSH:
    case Token.RSH:
    case Token.URSH:   return 10;
    case Token.SUB:
    case Token.ADD:    return 11;
    case Token.MUL:
    case Token.MOD:
    case Token.DIV:    return 12;
    case Token.INC:
    case Token.DEC:
    case Token.NEW:
    case Token.DELPROP:
    case Token.TYPEOF:
    case Token.VOID:
    case Token.NOT:
    case Token.BITNOT:
    case Token.POS:
    case Token.NEG:    return 13;

    case Token.ARRAYLIT:
    case Token.CALL:
    case Token.EMPTY:
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.GETELEM:
    case Token.GETPROP:
    case Token.GET_REF:
    case Token.IF:
    case Token.LP:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.RETURN:
    case Token.STRING:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Node.tokenToName(type) +
                             " (type " + type + ")");
  }
}
 
Example 16
Source File: Closure_73_CodeGenerator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Adds a block or expression, substituting a VOID with an empty statement.
 * This is used for "for (...);" and "if (...);" type statements.
 *
 * @param n The node to print.
 * @param context The context to determine how the node should be printed.
 */
private void addNonEmptyStatement(
    Node n, Context context, boolean allowNonBlockChild) {
  Node nodeToProcess = n;

  if (!allowNonBlockChild && n.getType() != Token.BLOCK) {
    throw new Error("Missing BLOCK child.");
  }

  // Strip unneeded blocks, that is blocks with <2 children unless
  // the CodePrinter specifically wants to keep them.
  if (n.getType() == Token.BLOCK) {
    int count = getNonEmptyChildCount(n, 2);
    if (count == 0) {
      if (cc.shouldPreserveExtraBlocks()) {
        cc.beginBlock();
        cc.endBlock(cc.breakAfterBlockFor(n, context == Context.STATEMENT));
      } else {
        cc.endStatement(true);
      }
      return;
    }

    if (count == 1) {
      // Hack around a couple of browser bugs:
      //   Safari needs a block around function declarations.
      //   IE6/7 needs a block around DOs.
      Node firstAndOnlyChild = getFirstNonEmptyChild(n);
      boolean alwaysWrapInBlock = cc.shouldPreserveExtraBlocks();
      if (alwaysWrapInBlock || isOneExactlyFunctionOrDo(firstAndOnlyChild)) {
        cc.beginBlock();
        add(firstAndOnlyChild, Context.STATEMENT);
        cc.maybeLineBreak();
        cc.endBlock(cc.breakAfterBlockFor(n, context == Context.STATEMENT));
        return;
      } else {
        // Continue with the only child.
        nodeToProcess = firstAndOnlyChild;
      }
    }

    if (count > 1) {
      context = Context.PRESERVE_BLOCK;
    }
  }

  if (nodeToProcess.getType() == Token.EMPTY) {
    cc.endStatement(true);
  } else {
    add(nodeToProcess, context);

    // VAR doesn't include ';' since it gets used in expressions - so any
    // VAR in a statement context needs a call to endStatement() here.
    if (nodeToProcess.getType() == Token.VAR) {
      cc.endStatement();
    }
  }
}
 
Example 17
Source File: Closure_61_NodeUtil_t.java    From coming with MIT License 4 votes vote down vote up
static int precedence(int type) {
  switch (type) {
    case Token.COMMA:  return 0;
    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_ADD:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_DIV:
    case Token.ASSIGN_MOD:
    case Token.ASSIGN: return 1;
    case Token.HOOK:   return 2;  // ?: operator
    case Token.OR:     return 3;
    case Token.AND:    return 4;
    case Token.BITOR:  return 5;
    case Token.BITXOR: return 6;
    case Token.BITAND: return 7;
    case Token.EQ:
    case Token.NE:
    case Token.SHEQ:
    case Token.SHNE:   return 8;
    case Token.LT:
    case Token.GT:
    case Token.LE:
    case Token.GE:
    case Token.INSTANCEOF:
    case Token.IN:     return 9;
    case Token.LSH:
    case Token.RSH:
    case Token.URSH:   return 10;
    case Token.SUB:
    case Token.ADD:    return 11;
    case Token.MUL:
    case Token.MOD:
    case Token.DIV:    return 12;
    case Token.INC:
    case Token.DEC:
    case Token.NEW:
    case Token.DELPROP:
    case Token.TYPEOF:
    case Token.VOID:
    case Token.NOT:
    case Token.BITNOT:
    case Token.POS:
    case Token.NEG:    return 13;

    case Token.CALL:
    case Token.GETELEM:
    case Token.GETPROP:
    // Data values
    case Token.ARRAYLIT:
    case Token.EMPTY:  // TODO(johnlenz): remove this.
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.STRING:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Node.tokenToName(type) +
                             " (type " + type + ")");
  }
}
 
Example 18
Source File: Cardumen_0087_t.java    From coming with MIT License 4 votes vote down vote up
static int precedence(int type) {
  switch (type) {
    case Token.COMMA:  return 0;
    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_ADD:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_DIV:
    case Token.ASSIGN_MOD:
    case Token.ASSIGN: return 1;
    case Token.HOOK:   return 2;  // ?: operator
    case Token.OR:     return 3;
    case Token.AND:    return 4;
    case Token.BITOR:  return 5;
    case Token.BITXOR: return 6;
    case Token.BITAND: return 7;
    case Token.EQ:
    case Token.NE:
    case Token.SHEQ:
    case Token.SHNE:   return 8;
    case Token.LT:
    case Token.GT:
    case Token.LE:
    case Token.GE:
    case Token.INSTANCEOF:
    case Token.IN:     return 9;
    case Token.LSH:
    case Token.RSH:
    case Token.URSH:   return 10;
    case Token.SUB:
    case Token.ADD:    return 11;
    case Token.MUL:
    case Token.MOD:
    case Token.DIV:    return 12;
    case Token.INC:
    case Token.DEC:
    case Token.NEW:
    case Token.DELPROP:
    case Token.TYPEOF:
    case Token.VOID:
    case Token.NOT:
    case Token.BITNOT:
    case Token.POS:
    case Token.NEG:    return 13;

    case Token.CALL:
    case Token.GETELEM:
    case Token.GETPROP:
    // Data values
    case Token.ARRAYLIT:
    case Token.EMPTY:  // TODO(johnlenz): remove this.
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.STRING:
    case Token.STRING_KEY:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Token.name(type) +
                             " (type " + type + ")");
  }
}
 
Example 19
Source File: FunctionToBlockMutatorTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void helperMutate(
    String code, final String expectedResult, final String fnName,
    final String resultName,
    final boolean needsDefaultResult,
    final boolean isCallInLoop) {
  final Compiler compiler = new Compiler();
  final FunctionToBlockMutator mutator = new FunctionToBlockMutator(
      compiler, compiler.getUniqueNameIdSupplier());
  Node expectedRoot = parse(compiler, expectedResult);
  Preconditions.checkState(compiler.getErrorCount() == 0);
  final Node expected = expectedRoot.getFirstChild();
  final Node tree = parse(compiler, code);
  Preconditions.checkState(compiler.getErrorCount() == 0);

  Node externsRoot = new Node(Token.EMPTY);
  Node mainRoot = tree;
  MarkNoSideEffectCalls mark = new MarkNoSideEffectCalls(compiler);
  mark.process(externsRoot, mainRoot);

  final Node fnNode = findFunction(tree, fnName);
  final Set<String> unsafe =
      FunctionArgumentInjector.findModifiedParameters(fnNode);

  // Fake precondition.
  compiler.setLifeCycleStage(LifeCycleStage.NORMALIZED);

  // inline tester
  Method tester = new Method() {
    @Override
    public boolean call(NodeTraversal t, Node n, Node parent) {

      Node result = mutator.mutate(
          fnName, fnNode, n, resultName,
          needsDefaultResult, isCallInLoop);
      validateSourceInfo(compiler, result);
      String explanation = expected.checkTreeEquals(result);
      assertNull("\nExpected: " + compiler.toSource(expected) +
          "\nResult: " + compiler.toSource(result) +
          "\n" + explanation, explanation);
      return true;
    }
  };

  compiler.resetUniqueNameId();
  TestCallback test = new TestCallback(fnName, tester);
  NodeTraversal.traverse(compiler, tree, test);
}
 
Example 20
Source File: Closure_60_NodeUtil_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * When converting arrays to string using Array.prototype.toString or
 * Array.prototype.join, the rules for conversion to String are different
 * than converting each element individually.  Specifically, "null" and
 * "undefined" are converted to an empty string.
 * @param n A node that is a member of an Array.
 * @return The string representation.
 */
static String getArrayElementStringValue(Node n) {
  return (NodeUtil.isNullOrUndefined(n) || n.getType() == Token.EMPTY)
      ? "" : getStringValue(n);
}