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

The following examples show how to use com.google.javascript.rhino.Node#setType() . 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: ReorderConstantExpression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
Node optimizeSubtree(Node subtree) {
  // if the operator is symmetric
  if (NodeUtil.isSymmetricOperation(subtree)
      || NodeUtil.isRelationalOperation(subtree)) {
    // right value is immutable and left is not
    if (NodeUtil.isImmutableValue(subtree.getLastChild())
        && !NodeUtil.isImmutableValue(subtree.getFirstChild())) {

      // if relational, get the inverse operator.
      if (NodeUtil.isRelationalOperation(subtree)){
        int inverseOperator = NodeUtil.getInverseOperator(subtree.getType());
        subtree.setType(inverseOperator);
      }

      // swap them
      Node firstNode = subtree.getFirstChild().detachFromParent();
      Node lastNode = subtree.getLastChild().detachFromParent();

      subtree.addChildrenToFront(lastNode);
      subtree.addChildrenToBack(firstNode);
      reportCodeChange();
    }
  }
  return subtree;
}
 
Example 2
Source File: IRFactory.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private Node transformBlock(AstNode node) {
  Node irNode = transform(node);
  if (!irNode.isBlock()) {
    if (irNode.isEmpty()) {
      irNode.setType(Token.BLOCK);
      irNode.setWasEmptyNode(true);
    } else {
      Node newBlock = newNode(Token.BLOCK, irNode);
      newBlock.setLineno(irNode.getLineno());
      newBlock.setCharno(irNode.getCharno());
      maybeSetLengthFrom(newBlock, node);
      irNode = newBlock;
    }
  }
  return irNode;
}
 
Example 3
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Fold "new Object()" to "Object()".
 */
private Node tryFoldStandardConstructors(Node n) {
  Preconditions.checkState(n.isNew());

  // If name normalization has been run then we know that
  // new Object() does in fact refer to what we think it is
  // and not some custom-defined Object().
  if (isASTNormalized()) {
    if (n.getFirstChild().isName()) {
      String className = n.getFirstChild().getString();
      if (STANDARD_OBJECT_CONSTRUCTORS.contains(className)) {
        n.setType(Token.CALL);
        n.putBooleanProp(Node.FREE_CALL, true);
        reportCodeChange();
      }
    }
  }

  return n;
}
 
Example 4
Source File: PeepholeSubstituteAlternateSyntax.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Fold "new Object()" to "Object()".
 */
private Node tryFoldStandardConstructors(Node n) {
  Preconditions.checkState(n.isNew());

  // If name normalization has been run then we know that
  // new Object() does in fact refer to what we think it is
  // and not some custom-defined Object().
  if (isASTNormalized()) {
    if (n.getFirstChild().isName()) {
      String className = n.getFirstChild().getString();
      if (STANDARD_OBJECT_CONSTRUCTORS.contains(className)) {
        n.setType(Token.CALL);
        n.putBooleanProp(Node.FREE_CALL, true);
        reportCodeChange();
      }
    }
  }

  return n;
}
 
Example 5
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Fold "new Object()" to "Object()".
 */
private Node tryFoldStandardConstructors(Node n) {
  Preconditions.checkState(n.isNew());

  // If name normalization has been run then we know that
  // new Object() does in fact refer to what we think it is
  // and not some custom-defined Object().
  if (isASTNormalized()) {
    if (n.getFirstChild().isName()) {
      String className = n.getFirstChild().getString();
      if (STANDARD_OBJECT_CONSTRUCTORS.contains(className)) {
        n.setType(Token.CALL);
        n.putBooleanProp(Node.FREE_CALL, true);
        reportCodeChange();
      }
    }
  }

  return n;
}
 
Example 6
Source File: Closure_42_IRFactory_s.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processObjectLiteral(ObjectLiteral literalNode) {
  if (literalNode.isDestructuring()) {
    reportDestructuringAssign(literalNode);
  }

  Node node = newNode(Token.OBJECTLIT);
  for (ObjectProperty el : literalNode.getElements()) {
    if (config.languageMode == LanguageMode.ECMASCRIPT3) {
      if (el.isGetter()) {
        reportGetter(el);
        continue;
      } else if (el.isSetter()) {
        reportSetter(el);
        continue;
      }
    }

    Node key = transformAsString(el.getLeft());
    Node value = transform(el.getRight());
    if (el.isGetter()) {
      key.setType(Token.GETTER_DEF);
      Preconditions.checkState(value.isFunction());
      if (getFnParamNode(value).hasChildren()) {
        reportGetterParam(el.getLeft());
      }
    } else if (el.isSetter()) {
      key.setType(Token.SETTER_DEF);
      Preconditions.checkState(value.isFunction());
      if (!getFnParamNode(value).hasOneChild()) {
        reportSetterParam(el.getLeft());
      }
    }
    key.addChildToFront(value);
    node.addChildToBack(key);
  }
  return node;
}
 
Example 7
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Try to minimize NOT nodes such as !(x==y).
 *
 * Returns the replacement for n or the original if no change was made
 */
private Node tryMinimizeNot(Node n) {
  Node parent = n.getParent();

  Node notChild = n.getFirstChild();
  // negative operator of the current one : == -> != for instance.
  int complementOperator;
  switch (notChild.getType()) {
    case Token.EQ:
      complementOperator = Token.NE;
      break;
    case Token.NE:
      complementOperator = Token.EQ;
      break;
    case Token.SHEQ:
      complementOperator = Token.SHNE;
      break;
    case Token.SHNE:
      complementOperator = Token.SHEQ;
      break;
    // GT, GE, LT, LE are not handled in this because !(x<NaN) != x>=NaN.
    default:
      return n;
  }
  Node newOperator = n.removeFirstChild();
  newOperator.setType(complementOperator);
  parent.replaceChild(n, newOperator);
  reportCodeChange();
  return newOperator;
}
 
Example 8
Source File: Closure_122_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processBreakStatement(BreakStatement statementNode) {
  Node node = newNode(Token.BREAK);
  if (statementNode.getBreakLabel() != null) {
    Node labelName = transform(statementNode.getBreakLabel());
    // Change the NAME to LABEL_NAME
    labelName.setType(Token.LABEL_NAME);
    node.addChildToBack(labelName);
  }
  return node;
}
 
Example 9
Source File: Closure_84_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processObjectLiteral(ObjectLiteral literalNode) {
  if (literalNode.isDestructuring()) {
    reportDestructuringAssign(literalNode);
  }

  Node node = newNode(Token.OBJECTLIT);
  for (ObjectProperty el : literalNode.getElements()) {
    if (!config.acceptES5) {
      if (el.isGetter()) {
        reportGetter(el);
        continue;
      } else if (el.isSetter()) {
        reportSetter(el);
        continue;
      }
    }

    Node key = transformAsString(el.getLeft());
    if (el.isGetter()) {
      key.setType(Token.GET);
    } else if (el.isSetter()) {
      key.setType(Token.SET);
    }
    key.addChildToFront(transform(el.getRight()));
    node.addChildToBack(key);
  }
  return node;
}
 
Example 10
Source File: IRFactory.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
Node processContinueStatement(ContinueStatement statementNode) {
  Node node = newNode(Token.CONTINUE);
  if (statementNode.getLabel() != null) {
    Node labelName = transform(statementNode.getLabel());
    // Change the NAME to LABEL_NAME
    labelName.setType(Token.LABEL_NAME);
    node.addChildToBack(labelName);
  }
  return node;
}
 
Example 11
Source File: Closure_81_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 12
Source File: ClosureOptimizePrimitives.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts all of the given call nodes to object literals that are safe to
 * do so.
 */
private void processObjectCreateCalls(List<Node> callNodes) {
  for (Node callNode : callNodes) {
    Node curParam = callNode.getFirstChild().getNext();
    if (canOptimizeObjectCreate(curParam)) {
      Node objNode = IR.objectlit().srcref(callNode);
      while (curParam != null) {
        Node keyNode = curParam;
        Node valueNode = curParam.getNext();
        curParam = valueNode.getNext();

        callNode.removeChild(keyNode);
        callNode.removeChild(valueNode);

        if (!keyNode.isString()) {
          keyNode = IR.string(NodeUtil.getStringValue(keyNode))
              .srcref(keyNode);
        }
        keyNode.setType(Token.STRING_KEY);
        keyNode.setQuotedString();
        objNode.addChildToBack(IR.propdef(keyNode, valueNode));
      }
      callNode.getParent().replaceChild(callNode, objNode);
      compiler.reportCodeChange();
    }
  }
}
 
Example 13
Source File: Closure_81_IRFactory_s.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processContinueStatement(ContinueStatement statementNode) {
  Node node = newNode(Token.CONTINUE);
  if (statementNode.getLabel() != null) {
    Node labelName = transform(statementNode.getLabel());
    // Change the NAME to LABEL_NAME
    labelName.setType(Token.LABEL_NAME);
    node.addChildToBack(labelName);
  }
  return node;
}
 
Example 14
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 15
Source File: IRFactory.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
Node processObjectLiteral(ObjectLiteral literalNode) {
  if (literalNode.isDestructuring()) {
    reportDestructuringAssign(literalNode);
  }

  Node node = newNode(Token.OBJECTLIT);
  for (ObjectProperty el : literalNode.getElements()) {
    if (config.languageMode == LanguageMode.ECMASCRIPT3) {
      if (el.isGetter()) {
        reportGetter(el);
        continue;
      } else if (el.isSetter()) {
        reportSetter(el);
        continue;
      }
    }

    Node key = transformAsString(el.getLeft());
    key.setType(Token.STRING_KEY);

    Node value = transform(el.getRight());
    if (el.isGetter()) {
      key.setType(Token.GETTER_DEF);
      Preconditions.checkState(value.isFunction());
      if (getFnParamNode(value).hasChildren()) {
        reportGetterParam(el.getLeft());
      }
    } else if (el.isSetter()) {
      key.setType(Token.SETTER_DEF);
      Preconditions.checkState(value.isFunction());
      if (!getFnParamNode(value).hasOneChild()) {
        reportSetterParam(el.getLeft());
      }
    }
    key.addChildToFront(value);
    node.addChildToBack(key);
  }
  return node;
}
 
Example 16
Source File: Closure_79_Normalize_t.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 17
Source File: Closure_42_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processContinueStatement(ContinueStatement statementNode) {
  Node node = newNode(Token.CONTINUE);
  if (statementNode.getLabel() != null) {
    Node labelName = transform(statementNode.getLabel());
    // Change the NAME to LABEL_NAME
    labelName.setType(Token.LABEL_NAME);
    node.addChildToBack(labelName);
  }
  return node;
}
 
Example 18
Source File: Closure_37_IRFactory_s.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processObjectLiteral(ObjectLiteral literalNode) {
  if (literalNode.isDestructuring()) {
    reportDestructuringAssign(literalNode);
  }

  Node node = newNode(Token.OBJECTLIT);
  for (ObjectProperty el : literalNode.getElements()) {
    if (config.languageMode == LanguageMode.ECMASCRIPT3) {
      if (el.isGetter()) {
        reportGetter(el);
        continue;
      } else if (el.isSetter()) {
        reportSetter(el);
        continue;
      }
    }

    Node key = transformAsString(el.getLeft());
    Node value = transform(el.getRight());
    if (el.isGetter()) {
      key.setType(Token.GETTER_DEF);
      Preconditions.checkState(value.isFunction());
      if (getFnParamNode(value).hasChildren()) {
        reportGetterParam(el.getLeft());
      }
    } else if (el.isSetter()) {
      key.setType(Token.SETTER_DEF);
      Preconditions.checkState(value.isFunction());
      if (!getFnParamNode(value).hasOneChild()) {
        reportSetterParam(el.getLeft());
      }
    }
    key.addChildToFront(value);
    node.addChildToBack(key);
  }
  return node;
}
 
Example 19
Source File: Closure_37_IRFactory_s.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processContinueStatement(ContinueStatement statementNode) {
  Node node = newNode(Token.CONTINUE);
  if (statementNode.getLabel() != null) {
    Node labelName = transform(statementNode.getLabel());
    // Change the NAME to LABEL_NAME
    labelName.setType(Token.LABEL_NAME);
    node.addChildToBack(labelName);
  }
  return node;
}
 
Example 20
Source File: Normalize.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) {
  switch (n.getType()) {
    case Token.WHILE:
      if (CONVERT_WHILE_TO_FOR) {
        Node expr = n.getFirstChild();
        n.setType(Token.FOR);
        Node empty = IR.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.STRING_KEY:
    case Token.GETTER_DEF:
    case Token.SETTER_DEF:
      if (!compiler.getLifeCycleStage().isNormalizedObfuscated()) {
        annotateConstantsByConvention(n, parent);
      }
      break;

    case Token.CAST:
      parent.replaceChild(n, n.removeFirstChild());
      break;
  }
}