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

The following examples show how to use com.google.javascript.rhino.Node#copyInformationFromForTree() . 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_92_ProcessClosurePrimitives_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Creates a simple namespace variable declaration
 * (e.g. <code>var foo = {};</code>).
 *
 * @param namespace A simple namespace (must be a valid js identifier)
 * @param sourceNode The node to get source information from.
 */
private Node makeVarDeclNode(String namespace, Node sourceNode) {
  Node name = Node.newString(Token.NAME, namespace);
  name.addChildToFront(createNamespaceLiteral());

  Node decl = new Node(Token.VAR, name);
  decl.putBooleanProp(Node.IS_NAMESPACE, true);

  // TODO(nicksantos): ew ew ew. Create a mutator package.
  if (compiler.getCodingConvention().isConstant(namespace)) {
    name.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  }

  Preconditions.checkState(isNamespacePlaceholder(decl));
  decl.copyInformationFromForTree(sourceNode);
  return decl;
}
 
Example 2
Source File: Closure_93_ProcessClosurePrimitives_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Creates a simple namespace variable declaration
 * (e.g. <code>var foo = {};</code>).
 *
 * @param namespace A simple namespace (must be a valid js identifier)
 * @param sourceNode The node to get source information from.
 */
private Node makeVarDeclNode(String namespace, Node sourceNode) {
  Node name = Node.newString(Token.NAME, namespace);
  name.addChildToFront(createNamespaceLiteral());

  Node decl = new Node(Token.VAR, name);
  decl.putBooleanProp(Node.IS_NAMESPACE, true);

  // TODO(nicksantos): ew ew ew. Create a mutator package.
  if (compiler.getCodingConvention().isConstant(namespace)) {
    name.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  }

  Preconditions.checkState(isNamespacePlaceholder(decl));
  decl.copyInformationFromForTree(sourceNode);
  return decl;
}
 
Example 3
Source File: Closure_93_ProcessClosurePrimitives_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Processes the output of processed-provide from a previous pass.  This will
 * update our data structures in the same manner as if the provide had been
 * processed in this pass.
 */
private void processProvideFromPreviousPass(
    NodeTraversal t, String name, Node parent) {
  if (!providedNames.containsKey(name)) {
    // Record this provide created on a previous pass, and create a dummy
    // EXPR node as a placeholder to simulate an explicit provide.
    Node expr = new Node(Token.EXPR_RESULT);
    expr.copyInformationFromForTree(parent);
    parent.getParent().addChildBefore(expr, parent);
    compiler.reportCodeChange();

    JSModule module = t.getModule();
    registerAnyProvidedPrefixes(name, expr, module);

    ProvidedName provided = new ProvidedName(name, expr, module, true);
    providedNames.put(name, provided);
    provided.addDefinition(parent, module);
  } else {
    // Remove this provide if it came from a previous pass since we have an
    // replacement already.
    if (isNamespacePlaceholder(parent)) {
      parent.getParent().removeChild(parent);
      compiler.reportCodeChange();
    }
  }
}
 
Example 4
Source File: Closure_93_ProcessClosurePrimitives_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Processes the output of processed-provide from a previous pass.  This will
 * update our data structures in the same manner as if the provide had been
 * processed in this pass.
 */
private void processProvideFromPreviousPass(
    NodeTraversal t, String name, Node parent) {
  if (!providedNames.containsKey(name)) {
    // Record this provide created on a previous pass, and create a dummy
    // EXPR node as a placeholder to simulate an explicit provide.
    Node expr = new Node(Token.EXPR_RESULT);
    expr.copyInformationFromForTree(parent);
    parent.getParent().addChildBefore(expr, parent);
    compiler.reportCodeChange();

    JSModule module = t.getModule();
    registerAnyProvidedPrefixes(name, expr, module);

    ProvidedName provided = new ProvidedName(name, expr, module, true);
    providedNames.put(name, provided);
    provided.addDefinition(parent, module);
  } else {
    // Remove this provide if it came from a previous pass since we have an
    // replacement already.
    if (isNamespacePlaceholder(parent)) {
      parent.getParent().removeChild(parent);
      compiler.reportCodeChange();
    }
  }
}
 
Example 5
Source File: Nopol2017_0010_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Copy source info to the new node.
 */
private void setSourceInfo(Node newNode) {
  Node provideStringNode = getProvideStringNode();
  int offset = getSourceInfoOffset(provideStringNode);
  Node sourceInfoNode = provideStringNode == null
      ? firstNode : provideStringNode;
  newNode.copyInformationFromForTree(sourceInfoNode);
  if (offset != 0) {
    newNode.setSourceEncodedPositionForTree(
        sourceInfoNode.getSourcePosition() + offset);
  }
}
 
Example 6
Source File: ReplaceMessagesForChrome.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void processJsMessage(
    JsMessage message, JsMessageDefinition definition) {
  try {
    Node msgNode = definition.getMessageNode();
    Node newValue = getNewValueNode(msgNode, message);
    newValue.copyInformationFromForTree(msgNode);

    definition.getMessageParentNode().replaceChild(msgNode, newValue);
    compiler.reportCodeChange();
  } catch (MalformedException e) {
    compiler.report(JSError.make(message.getSourceName(), e.getNode(),
        MESSAGE_TREE_MALFORMED, e.getMessage()));
  }
}
 
Example 7
Source File: Closure_61_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a node for an empty result expression:
 *   "void 0"
 */
static Node newUndefinedNode(Node srcReferenceNode) {
  Node node = new Node(Token.VOID, Node.newNumber(0));
  if (srcReferenceNode != null) {
      node.copyInformationFromForTree(srcReferenceNode);
  }
  return node;
}
 
Example 8
Source File: Closure_113_ProcessClosurePrimitives_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Copy source info to the new node.
 */
private void setSourceInfo(Node newNode) {
  Node provideStringNode = getProvideStringNode();
  int offset = getSourceInfoOffset(provideStringNode);
  Node sourceInfoNode = provideStringNode == null
      ? firstNode : provideStringNode;
  newNode.copyInformationFromForTree(sourceInfoNode);
  if (offset != 0) {
    newNode.setSourceEncodedPositionForTree(
        sourceInfoNode.getSourcePosition() + offset);
  }
}
 
Example 9
Source File: Closure_72_FunctionToBlockMutator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replace the 'return' statement with its child expression.
 *   "return foo()" becomes "foo()" or "resultName = foo()"
 *   "return" is removed or becomes "resultName = void 0".
 *
 * @param block
 * @param resultName
 */
private static void convertLastReturnToStatement(
    Node block, String resultName) {
  Node ret = block.getLastChild();
  Preconditions.checkArgument(ret.getType() == Token.RETURN);
  Node resultNode = getReplacementReturnStatement(ret, resultName);

  if (resultNode == null) {
    block.removeChild(ret);
  } else {
    resultNode.copyInformationFromForTree(ret);
    block.replaceChild(ret, resultNode);
  }
}
 
Example 10
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
private Node tryMinimizeStringArrayLiteral(Node n) {
  if(!late) {
    return n;
  }

  int numElements = n.getChildCount();
  // We save two bytes per element.
  int saving = numElements * 2 - STRING_SPLIT_OVERHEAD;
  if (saving <= 0) {
    return n;
  }

  String[] strings = new String[n.getChildCount()];
  int idx = 0;
  for (Node cur = n.getFirstChild(); cur != null; cur = cur.getNext()) {
    strings[idx++] = cur.getString();
  }

  // These delimiters are chars that appears a lot in the program therefore
  // probably have a small Huffman encoding.
  String delimiter = pickDelimiter(strings);
  if (delimiter != null) {
    String template = Joiner.on(delimiter).join(strings);
    Node call = IR.call(
        IR.getprop(
            IR.string(template),
            IR.string("split")),
        IR.string("" + delimiter));
    call.copyInformationFromForTree(n);
    n.getParent().replaceChild(n, call);
    reportCodeChange();
    return call;
  }
  return n;
}
 
Example 11
Source File: FunctionToBlockMutator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Example:
 *   a = (void) 0;
 */
private static void addDummyAssignment(Node node, String resultName) {
  Preconditions.checkArgument(node.isBlock());

  // A result is needed create a dummy value.
  Node srcLocation = node;
  Node retVal = NodeUtil.newUndefinedNode(srcLocation);
  Node resultNode = createAssignStatementNode(resultName, retVal);
  resultNode.copyInformationFromForTree(node);

  node.addChildrenToBack(resultNode);
}
 
Example 12
Source File: Cardumen_00200_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a node for an empty result expression:
 *   "void 0"
 */
static Node newUndefinedNode(Node srcReferenceNode) {
  Node node = IR.voidNode(IR.number(0));
  if (srcReferenceNode != null) {
      node.copyInformationFromForTree(srcReferenceNode);
  }
  return node;
}
 
Example 13
Source File: jKali_003_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a node for an empty result expression:
 *   "void 0"
 */
static Node newUndefinedNode(Node srcReferenceNode) {
  Node node = IR.voidNode(IR.number(0));
  if (srcReferenceNode != null) {
      node.copyInformationFromForTree(srcReferenceNode);
  }
  return node;
}
 
Example 14
Source File: SpecializeModule.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates an AST that consists solely of copies of the input roots for the
 * passed in module.
 *
 * Also records a map in {@link #functionInfoBySpecializedFunctionNode}
 * of information about the original function keyed on the copies of the
 * functions to specialized.
 */
private Node copyModuleInputs(JSModule module) {

  specializedInputRootsByOriginal = Maps.newLinkedHashMap();

  functionInfoBySpecializedFunctionNode = Maps.newLinkedHashMap();

  Node syntheticModuleJsRoot = IR.block();
  syntheticModuleJsRoot.setIsSyntheticBlock(true);

  for (CompilerInput input : module.getInputs()) {
    Node originalInputRoot = input.getAstRoot(compiler);

    Node copiedInputRoot = originalInputRoot.cloneTree();
    copiedInputRoot.copyInformationFromForTree(originalInputRoot);

    specializedInputRootsByOriginal.put(originalInputRoot,
        copiedInputRoot);

    matchTopLevelFunctions(originalInputRoot, copiedInputRoot);

    syntheticModuleJsRoot.addChildToBack(copiedInputRoot);
  }

  // The jsRoot needs a parent (in a normal compilation this would be the
  // node that contains jsRoot and the externs).
  Node syntheticExternsAndJsRoot = IR.block();
  syntheticExternsAndJsRoot.addChildToBack(syntheticModuleJsRoot);

  return syntheticModuleJsRoot;
}
 
Example 15
Source File: Cardumen_00149_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Sets the debug information (source file info and original name)
 * on the given node.
 *
 * @param node The node on which to set the debug information.
 * @param basisNode The basis node from which to copy the source file info.
 * @param originalName The original name of the node.
 */
static void setDebugInformation(Node node, Node basisNode,
                                String originalName) {
  node.copyInformationFromForTree(basisNode);
  node.putProp(Node.ORIGINALNAME_PROP, originalName);
}
 
Example 16
Source File: Closure_75_NodeUtil_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Sets the debug information (source file info and orignal name)
 * on the given node.
 *
 * @param node The node on which to set the debug information.
 * @param basisNode The basis node from which to copy the source file info.
 * @param originalName The original name of the node.
 */
static void setDebugInformation(Node node, Node basisNode,
                                String originalName) {
  node.copyInformationFromForTree(basisNode);
  node.putProp(Node.ORIGINALNAME_PROP, originalName);
}
 
Example 17
Source File: Cardumen_00200_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Sets the debug information (source file info and original name)
 * on the given node.
 *
 * @param node The node on which to set the debug information.
 * @param basisNode The basis node from which to copy the source file info.
 * @param originalName The original name of the node.
 */
static void setDebugInformation(Node node, Node basisNode,
                                String originalName) {
  node.copyInformationFromForTree(basisNode);
  node.putProp(Node.ORIGINALNAME_PROP, originalName);
}
 
Example 18
Source File: Closure_61_NodeUtil_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Sets the debug information (source file info and orignal name)
 * on the given node.
 *
 * @param node The node on which to set the debug information.
 * @param basisNode The basis node from which to copy the source file info.
 * @param originalName The original name of the node.
 */
static void setDebugInformation(Node node, Node basisNode,
                                String originalName) {
  node.copyInformationFromForTree(basisNode);
  node.putProp(Node.ORIGINALNAME_PROP, originalName);
}
 
Example 19
Source File: Cardumen_0087_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Sets the debug information (source file info and original name)
 * on the given node.
 *
 * @param node The node on which to set the debug information.
 * @param basisNode The basis node from which to copy the source file info.
 * @param originalName The original name of the node.
 */
static void setDebugInformation(Node node, Node basisNode,
                                String originalName) {
  node.copyInformationFromForTree(basisNode);
  node.putProp(Node.ORIGINALNAME_PROP, originalName);
}
 
Example 20
Source File: Closure_60_NodeUtil_s.java    From coming with MIT License 2 votes vote down vote up
/**
 * Sets the debug information (source file info and orignal name)
 * on the given node.
 *
 * @param node The node on which to set the debug information.
 * @param basisNode The basis node from which to copy the source file info.
 * @param originalName The original name of the node.
 */
static void setDebugInformation(Node node, Node basisNode,
                                String originalName) {
  node.copyInformationFromForTree(basisNode);
  node.putProp(Node.ORIGINALNAME_PROP, originalName);
}