Java Code Examples for com.google.javascript.rhino.IR#add()

The following examples show how to use com.google.javascript.rhino.IR#add() . 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 6 votes vote down vote up
private Node tryFoldSimpleFunctionCall(Node n) {
  Preconditions.checkState(n.isCall());
  Node callTarget = n.getFirstChild();
  if (callTarget != null && callTarget.isName() &&
        callTarget.getString().equals("String")) {
    // Fold String(a) to '' + (a) on immutable literals,
    // which allows further optimizations
    //
    // We can't do this in the general case, because String(a) has
    // slightly different semantics than '' + (a). See
    // http://code.google.com/p/closure-compiler/issues/detail?id=759
    Node value = callTarget.getNext();
    if (value != null && value.getNext() == null &&
        NodeUtil.isImmutableValue(value)) {
      Node addition = IR.add(
          IR.string("").srcref(callTarget),
          value.detachFromParent());
      n.getParent().replaceChild(n, addition);
      reportCodeChange();
      return addition;
    }
  }
  return n;
}
 
Example 2
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 6 votes vote down vote up
private Node tryFoldSimpleFunctionCall(Node n) {
  Preconditions.checkState(n.isCall());
  Node callTarget = n.getFirstChild();
  if (callTarget != null && callTarget.isName() &&
        callTarget.getString().equals("String")) {
    // Fold String(a) to '' + (a) on immutable literals,
    // which allows further optimizations
    //
    // We can't do this in the general case, because String(a) has
    // slightly different semantics than '' + (a). See
    // http://code.google.com/p/closure-compiler/issues/detail?id=759
    Node value = callTarget.getNext();
    if (value != null && value.getNext() == null &&
        NodeUtil.isImmutableValue(value)) {
      Node addition = IR.add(
          IR.string("").srcref(callTarget),
          value.detachFromParent());
      n.getParent().replaceChild(n, addition);
      reportCodeChange();
      return addition;
    }
  }
  return n;
}
 
Example 3
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 6 votes vote down vote up
private Node tryFoldSimpleFunctionCall(Node n) {
  Preconditions.checkState(n.isCall());
  Node callTarget = n.getFirstChild();
  if (callTarget != null && callTarget.isName() &&
        callTarget.getString().equals("String")) {
    // Fold String(a) to '' + (a) on immutable literals,
    // which allows further optimizations
    //
    // We can't do this in the general case, because String(a) has
    // slightly different semantics than '' + (a). See
    // http://code.google.com/p/closure-compiler/issues/detail?id=759
    Node value = callTarget.getNext();
    if (value != null) {
      Node addition = IR.add(
          IR.string("").srcref(callTarget),
          value.detachFromParent());
      n.getParent().replaceChild(n, addition);
      reportCodeChange();
      return addition;
    }
  }
  return n;
}
 
Example 4
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 6 votes vote down vote up
private Node tryFoldSimpleFunctionCall(Node n) {
  Preconditions.checkState(n.isCall());
  Node callTarget = n.getFirstChild();
  if (callTarget != null && callTarget.isName() &&
        callTarget.getString().equals("String")) {
    // Fold String(a) to '' + (a) on immutable literals,
    // which allows further optimizations
    //
    // We can't do this in the general case, because String(a) has
    // slightly different semantics than '' + (a). See
    // http://code.google.com/p/closure-compiler/issues/detail?id=759
    Node value = callTarget.getNext();
    if (value != null && value.getNext() == null &&
        NodeUtil.isImmutableValue(value)) {
      Node addition = IR.add(
          IR.string("").srcref(callTarget),
          value.detachFromParent());
      n.getParent().replaceChild(n, addition);
      reportCodeChange();
      return addition;
    }
  }
  return n;
}
 
Example 5
Source File: ReplaceStrings.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Builds a replacement abstract syntax tree for the string expression {@code
 * expr}. Appends any string literal values that are encountered to
 * {@code keyBuilder}, to build the expression's replacement key.
 *
 * @param expr A JS expression that evaluates to a string value
 * @param prefix The JS expression to which {@code expr}'s replacement is
 *        logically being concatenated. It is a partial solution to the
 *        problem at hand and will either be this method's return value or a
 *        descendant of it.
 * @param keyBuilder A builder of the string expression's replacement key
 * @return The abstract syntax tree that should replace {@code expr}
 */
private Node buildReplacement(
    Node expr, Node prefix, StringBuilder keyBuilder) {
  switch (expr.getType()) {
    case Token.ADD:
      Node left = expr.getFirstChild();
      Node right = left.getNext();
      prefix = buildReplacement(left, prefix, keyBuilder);
      return buildReplacement(right, prefix, keyBuilder);
    case Token.STRING:
      keyBuilder.append(expr.getString());
      return prefix;
    default:
      keyBuilder.append(placeholderToken);
      prefix = IR.add(prefix, IR.string(placeholderToken));
      return IR.add(prefix, expr.cloneTree());
  }
}
 
Example 6
Source File: PeepholeSubstituteAlternateSyntax.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private Node tryFoldSimpleFunctionCall(Node n) {
  Preconditions.checkState(n.isCall());
  Node callTarget = n.getFirstChild();
  if (callTarget != null && callTarget.isName() &&
        callTarget.getString().equals("String")) {
    // Fold String(a) to '' + (a) on immutable literals,
    // which allows further optimizations
    //
    // We can't do this in the general case, because String(a) has
    // slightly different semantics than '' + (a). See
    // http://code.google.com/p/closure-compiler/issues/detail?id=759
    Node value = callTarget.getNext();
    if (value != null && value.getNext() == null &&
        NodeUtil.isImmutableValue(value)) {
      Node addition = IR.add(
          IR.string("").srcref(callTarget),
          value.detachFromParent());
      n.getParent().replaceChild(n, addition);
      reportCodeChange();
      return addition;
    }
  }
  return n;
}
 
Example 7
Source File: ReplaceMessages.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a parse tree corresponding to the remaining message parts in
 * an iteration. The result will contain only STRING nodes, NAME nodes
 * (corresponding to placeholder references), and/or ADD nodes used to
 * combine the other two types.
 *
 * @param partsIterator  an iterator over message parts
 * @param argListNode  an LP node whose children are valid placeholder names
 * @return the root of the constructed parse tree
 *
 * @throws MalformedException if {@code partsIterator} contains a
 *   placeholder reference that does not correspond to a valid argument in
 *   the arg list
 */
private Node constructAddOrStringNode(Iterator<CharSequence> partsIterator,
                                      Node argListNode)
    throws MalformedException {
  CharSequence part = partsIterator.next();
  Node partNode = null;
  if (part instanceof JsMessage.PlaceholderReference) {
    JsMessage.PlaceholderReference phRef =
        (JsMessage.PlaceholderReference) part;

    for (Node node : argListNode.children()) {
      if (node.isName()) {
        String arg = node.getString();

        // We ignore the case here because the transconsole only supports
        // uppercase placeholder names, but function arguments in JavaScript
        // code can have mixed case.
        if (arg.equalsIgnoreCase(phRef.getName())) {
          partNode = IR.name(arg);
        }
      }
    }

    if (partNode == null) {
      throw new MalformedException(
          "Unrecognized message placeholder referenced: " + phRef.getName(),
          argListNode);
    }
  } else {
    // The part is just a string literal.
    partNode = IR.string(part.toString());
  }

  if (partsIterator.hasNext()) {
    return IR.add(partNode,
                    constructAddOrStringNode(partsIterator, argListNode));
  } else {
    return partNode;
  }
}
 
Example 8
Source File: ReplaceMessages.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a parse tree corresponding to the remaining message parts in an
 * iteration. The result consists of one or more STRING nodes, placeholder
 * replacement value nodes (which can be arbitrary expressions), and ADD
 * nodes.
 *
 * @param parts  an iterator over message parts
 * @param objLitNode  an OBJLIT node mapping placeholder names to values
 * @return the root of the constructed parse tree
 *
 * @throws MalformedException if {@code parts} contains a placeholder
 *   reference that does not correspond to a valid placeholder name
 */
private Node constructStringExprNode(Iterator<CharSequence> parts,
    Node objLitNode) throws MalformedException {

  CharSequence part = parts.next();
  Node partNode = null;
  if (part instanceof JsMessage.PlaceholderReference) {
    JsMessage.PlaceholderReference phRef =
        (JsMessage.PlaceholderReference) part;

    // The translated message is null
    if (objLitNode == null) {
      throw new MalformedException("Empty placeholder value map " +
          "for a translated message with placeholders.", objLitNode);
    }

    for (Node key = objLitNode.getFirstChild(); key != null;
         key = key.getNext()) {
      if (key.getString().equals(phRef.getName())) {
        Node valueNode = key.getFirstChild();
        partNode = valueNode.cloneTree();
      }
    }

    if (partNode == null) {
      throw new MalformedException(
          "Unrecognized message placeholder referenced: " + phRef.getName(),
          objLitNode);
    }
  } else {
    // The part is just a string literal.
    partNode = IR.string(part.toString());
  }

  if (parts.hasNext()) {
    return IR.add(partNode,
        constructStringExprNode(parts, objLitNode));
  } else {
    return partNode;
  }
}