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

The following examples show how to use com.google.javascript.rhino.IR#name() . 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: Cardumen_00200_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return An appropriate AST node for the double value.
 */
static Node numberNode(double value, Node srcref) {
  Node result;
  if (Double.isNaN(value)) {
    result = IR.name("NaN");
  } else if (value == Double.POSITIVE_INFINITY) {
    result = IR.name("Infinity");
  } else if (value == Double.NEGATIVE_INFINITY) {
    result = IR.neg(IR.name("Infinity"));
  } else {
    result = IR.number(value);
  }
  if (srcref != null) {
    result.srcrefTree(srcref);
  }
  return result;
}
 
Example 2
Source File: jMutRepair_0023_t.java    From coming with MIT License 5 votes vote down vote up
private void addExtern() {
  Node name = IR.name(PROTECTOR_FN);
  name.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  Node var = IR.var(name);
  // Add "@noalias" so we can strip the method when AliasExternals is enabled.
  JSDocInfoBuilder builder = new JSDocInfoBuilder(false);
  builder.recordNoAlias();
  var.setJSDocInfo(builder.build(var));
  CompilerInput input = compiler.getSynthesizedExternsInput();
  input.getAstRoot(compiler).addChildrenToBack(var);
  compiler.reportCodeChange();
}
 
Example 3
Source File: Cardumen_0014_s.java    From coming with MIT License 5 votes vote down vote up
private static Node newName(
    CodingConvention convention, String name) {
  Node nameNode = IR.name(name);
  if (convention.isConstant(name)) {
    nameNode.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  }
  return nameNode;
}
 
Example 4
Source File: Closure_10_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
private static Node newName(
    CodingConvention convention, String name) {
  Node nameNode = IR.name(name);
  if (convention.isConstant(name)) {
    nameNode.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  }
  return nameNode;
}
 
Example 5
Source File: Cardumen_00149_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a VAR node containing the given name and initial value expression.
 */
static Node newVarNode(String name, Node value) {
  Node nodeName = IR.name(name);
  if (value != null) {
    Preconditions.checkState(value.getNext() == null);
    nodeName.addChildToBack(value);
    nodeName.srcref(value);
  }
  Node var = IR.var(nodeName).srcref(nodeName);

  return var;
}
 
Example 6
Source File: patch1-Closure-22-Nopol2017_patch1-Closure-22-Nopol2017_t.java    From coming with MIT License 5 votes vote down vote up
private void addExtern() {
  Node name = IR.name(PROTECTOR_FN);
  name.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  Node var = IR.var(name);
  // Add "@noalias" so we can strip the method when AliasExternals is enabled.
  JSDocInfoBuilder builder = new JSDocInfoBuilder(false);
  builder.recordNoAlias();
  var.setJSDocInfo(builder.build(var));
  CompilerInput input = compiler.getSynthesizedExternsInput();
  input.getAstRoot(compiler).addChildrenToBack(var);
  compiler.reportCodeChange();
}
 
Example 7
Source File: jMutRepair_0047_t.java    From coming with MIT License 5 votes vote down vote up
private void addExtern() {
  Node name = IR.name(PROTECTOR_FN);
  name.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  Node var = IR.var(name);
  // Add "@noalias" so we can strip the method when AliasExternals is enabled.
  JSDocInfoBuilder builder = new JSDocInfoBuilder(false);
  builder.recordNoAlias();
  var.setJSDocInfo(builder.build(var));
  CompilerInput input = compiler.getSynthesizedExternsInput();
  input.getAstRoot(compiler).addChildrenToBack(var);
  compiler.reportCodeChange();
}
 
Example 8
Source File: jMutRepair_004_t.java    From coming with MIT License 5 votes vote down vote up
private void addExtern() {
  Node name = IR.name(PROTECTOR_FN);
  name.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  Node var = IR.var(name);
  // Add "@noalias" so we can strip the method when AliasExternals is enabled.
  JSDocInfoBuilder builder = new JSDocInfoBuilder(false);
  builder.recordNoAlias();
  var.setJSDocInfo(builder.build(var));
  CompilerInput input = compiler.getSynthesizedExternsInput();
  input.getAstRoot(compiler).addChildrenToBack(var);
  compiler.reportCodeChange();
}
 
Example 9
Source File: CollapseProperties.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds global variable "stubs" for any properties of a global name that are
 * only set in a local scope or read but never set.
 *
 * @param n An object representing a global name (e.g. "a", "a.b.c")
 * @param alias The flattened name of the object whose properties we are
 *     adding stubs for (e.g. "a$b$c")
 * @param parent The node to which new global variables should be added
 *     as children
 * @param addAfter The child of after which new
 *     variables should be added (may be null)
 * @return The number of variables added
 */
private int addStubsForUndeclaredProperties(
    Name n, String alias, Node parent, Node addAfter) {
  Preconditions.checkState(n.canCollapseUnannotatedChildNames());
  Preconditions.checkArgument(NodeUtil.isStatementBlock(parent));
  Preconditions.checkNotNull(addAfter);
  int numStubs = 0;
  if (n.props != null) {
    for (Name p : n.props) {
      if (p.needsToBeStubbed()) {
        String propAlias = appendPropForAlias(alias, p.getBaseName());
        Node nameNode = IR.name(propAlias);
        Node newVar = IR.var(nameNode)
            .copyInformationFromForTree(addAfter);
        parent.addChildAfter(newVar, addAfter);
        addAfter = newVar;
        numStubs++;
        compiler.reportCodeChange();

        // Determine if this is a constant var by checking the first
        // reference to it. Don't check the declaration, as it might be null.
        if (p.getRefs().get(0).node.getLastChild().getBooleanProp(
                Node.IS_CONSTANT_NAME)) {
          nameNode.putBooleanProp(Node.IS_CONSTANT_NAME, true);
        }
      }
    }
  }
  return numStubs;
}
 
Example 10
Source File: Cardumen_00200_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a VAR node containing the given name and initial value expression.
 */
static Node newVarNode(String name, Node value) {
  Node nodeName = IR.name(name);
  if (value != null) {
    Preconditions.checkState(value.getNext() == null);
    nodeName.addChildToBack(value);
    nodeName.srcref(value);
  }
  Node var = IR.var(nodeName).srcref(nodeName);

  return var;
}
 
Example 11
Source File: Closure_21_CheckSideEffects_s.java    From coming with MIT License 5 votes vote down vote up
private void addExtern() {
  Node name = IR.name(PROTECTOR_FN);
  name.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  Node var = IR.var(name);
  // Add "@noalias" so we can strip the method when AliasExternals is enabled.
  JSDocInfoBuilder builder = new JSDocInfoBuilder(false);
  builder.recordNoAlias();
  var.setJSDocInfo(builder.build(var));
  CompilerInput input = compiler.getSynthesizedExternsInput();
  input.getAstRoot(compiler).addChildrenToBack(var);
  compiler.reportCodeChange();
}
 
Example 12
Source File: Cardumen_0087_s.java    From coming with MIT License 5 votes vote down vote up
private static Node newName(
    CodingConvention convention, String name) {
  Node nameNode = IR.name(name);
  if (convention.isConstant(name)) {
    nameNode.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  }
  return nameNode;
}
 
Example 13
Source File: Closure_22_CheckSideEffects_s.java    From coming with MIT License 5 votes vote down vote up
private void addExtern() {
  Node name = IR.name(PROTECTOR_FN);
  name.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  Node var = IR.var(name);
  // Add "@noalias" so we can strip the method when AliasExternals is enabled.
  JSDocInfoBuilder builder = new JSDocInfoBuilder(false);
  builder.recordNoAlias();
  var.setJSDocInfo(builder.build(var));
  CompilerInput input = compiler.getSynthesizedExternsInput();
  input.getAstRoot(compiler).addChildrenToBack(var);
  compiler.reportCodeChange();
}
 
Example 14
Source File: Reader.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void transformIdPatt(JsonML element, Node parent)
    throws JsonMLException {
  Node node = IR.name(
      getStringAttribute(element, TagAttr.NAME));
  setPosition(node);
  parent.addChildToBack(node);
}
 
Example 15
Source File: jMutRepair_0031_s.java    From coming with MIT License 5 votes vote down vote up
private void addExtern() {
  Node name = IR.name(PROTECTOR_FN);
  name.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  Node var = IR.var(name);
  // Add "@noalias" so we can strip the method when AliasExternals is enabled.
  JSDocInfoBuilder builder = new JSDocInfoBuilder(false);
  builder.recordNoAlias();
  var.setJSDocInfo(builder.build(var));
  CompilerInput input = compiler.getSynthesizedExternsInput();
  input.getAstRoot(compiler).addChildrenToBack(var);
  compiler.reportCodeChange();
}
 
Example 16
Source File: jMutRepair_003_t.java    From coming with MIT License 5 votes vote down vote up
private static Node newName(
    CodingConvention convention, String name) {
  Node nameNode = IR.name(name);
  if (convention.isConstant(name)) {
    nameNode.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  }
  return nameNode;
}
 
Example 17
Source File: Cardumen_00248_t.java    From coming with MIT License 5 votes vote down vote up
private void addExtern() {
  Node name = IR.name(PROTECTOR_FN);
  name.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  Node var = IR.var(name);
  // Add "@noalias" so we can strip the method when AliasExternals is enabled.
  JSDocInfoBuilder builder = new JSDocInfoBuilder(false);
  builder.recordNoAlias();
  var.setJSDocInfo(builder.build(var));
  CompilerInput input = compiler.getSynthesizedExternsInput();
  input.getAstRoot(compiler).addChildrenToBack(var);
  compiler.reportCodeChange();
}
 
Example 18
Source File: ModuleConversionPass.java    From clutz with MIT License 5 votes vote down vote up
private static Node createNamedImports(ModuleImport moduleImport) {
  Node importSpec = new Node(Token.IMPORT_SPECS);
  importSpec.setShorthandProperty(true);
  for (String fullLocalName : moduleImport.fullLocalNames) {
    Node spec = new Node(Token.IMPORT_SPEC, IR.name(fullLocalName));
    spec.setShorthandProperty(true);
    if (moduleImport.localNameAliases.containsKey(fullLocalName)) {
      spec.addChildToBack(IR.name(moduleImport.localNameAliases.get(fullLocalName)));
    }
    importSpec.addChildToBack(spec);
  }
  return importSpec;
}
 
Example 19
Source File: Closure_130_CollapseProperties_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Declares global variables to serve as aliases for the values in an object
 * literal, optionally removing all of the object literal's keys and values.
 *
 * @param alias The object literal's flattened name (e.g. "a$b$c")
 * @param objlit The OBJLIT node
 * @param varNode The VAR node to which new global variables should be added
 *     as children
 * @param nameToAddAfter The child of {@code varNode} after which new
 *     variables should be added (may be null)
 * @param varParent {@code varNode}'s parent
 * @return The number of variables added
 */
private int declareVarsForObjLitValues(
    Name objlitName, String alias, Node objlit, Node varNode,
    Node nameToAddAfter, Node varParent) {
  int numVars = 0;
  int arbitraryNameCounter = 0;
  boolean discardKeys = !objlitName.shouldKeepKeys();

  for (Node key = objlit.getFirstChild(), nextKey; key != null;
       key = nextKey) {
    Node value = key.getFirstChild();
    nextKey = key.getNext();

    // A get or a set can not be rewritten as a VAR.
    if (key.isGetterDef() || key.isSetterDef()) {
      continue;
    }

    // We generate arbitrary names for keys that aren't valid JavaScript
    // identifiers, since those keys are never referenced. (If they were,
    // this object literal's child names wouldn't be collapsible.) The only
    // reason that we don't eliminate them entirely is the off chance that
    // their values are expressions that have side effects.
    boolean isJsIdentifier = !key.isNumber() &&
                             TokenStream.isJSIdentifier(key.getString());
    String propName = isJsIdentifier ?
        key.getString() : String.valueOf(++arbitraryNameCounter);

    // If the name cannot be collapsed, skip it.
    String qName = objlitName.getFullName() + '.' + propName;
    Name p = nameMap.get(qName);
    if (p != null && !p.canCollapse()) {
      continue;
    }

    String propAlias = appendPropForAlias(alias, propName);
    Node refNode = null;
    if (discardKeys) {
      objlit.removeChild(key);
      value.detachFromParent();
    } else {
      // Substitute a reference for the value.
      refNode = IR.name(propAlias);
      if (key.getBooleanProp(Node.IS_CONSTANT_NAME)) {
        refNode.putBooleanProp(Node.IS_CONSTANT_NAME, true);
      }

      key.replaceChild(value, refNode);
    }

    // Declare the collapsed name as a variable with the original value.
    Node nameNode = IR.name(propAlias);
    nameNode.addChildToFront(value);
    if (key.getBooleanProp(Node.IS_CONSTANT_NAME)) {
      nameNode.putBooleanProp(Node.IS_CONSTANT_NAME, true);
    }
    Node newVar = IR.var(nameNode)
        .copyInformationFromForTree(key);
    if (nameToAddAfter != null) {
      varParent.addChildAfter(newVar, nameToAddAfter);
    } else {
      varParent.addChildBefore(newVar, varNode);
    }
    compiler.reportCodeChange();
    nameToAddAfter = newVar;

    // Update the global name's node ancestry if it hasn't already been
    // done. (Duplicate keys in an object literal can bring us here twice
    // for the same global name.)
    if (isJsIdentifier && p != null) {
      if (!discardKeys) {
        Ref newAlias =
            p.getDeclaration().cloneAndReclassify(Ref.Type.ALIASING_GET);
        newAlias.node = refNode;
        p.addRef(newAlias);
      }

      p.getDeclaration().node = nameNode;

      if (value.isFunction()) {
        checkForHosedThisReferences(value, value.getJSDocInfo(), p);
      }
    }

    numVars++;
  }
  return numVars;
}
 
Example 20
Source File: ExpressionDecomposer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * @param expr The conditional expression to extract.
 * @param injectionPoint The before which extracted expression, would be
 *     injected.
 * @param needResult  Whether the result of the expression is required.
 * @return The node that contains the logic of the expression after
 *     extraction.
 */
private Node extractConditional(
    Node expr, Node injectionPoint, boolean needResult) {
  Node parent = expr.getParent();
  String tempName = getTempValueName();

  // Break down the conditional.
  Node first = expr.getFirstChild();
  Node second = first.getNext();
  Node last = expr.getLastChild();

  // Isolate the children nodes.
  expr.detachChildren();

  // Transform the conditional to an IF statement.
  Node cond = null;
  Node trueExpr = IR.block().srcref(expr);
  Node falseExpr = IR.block().srcref(expr);
  switch (expr.getType()) {
    case Token.HOOK:
      // a = x?y:z --> if (x) {a=y} else {a=z}
      cond = first;
      trueExpr.addChildToFront(NodeUtil.newExpr(
          buildResultExpression(second, needResult, tempName)));
      falseExpr.addChildToFront(NodeUtil.newExpr(
          buildResultExpression(last, needResult, tempName)));
      break;
    case Token.AND:
      // a = x&&y --> if (a=x) {a=y} else {}
      cond = buildResultExpression(first, needResult, tempName);
      trueExpr.addChildToFront(NodeUtil.newExpr(
          buildResultExpression(last, needResult, tempName)));
      break;
    case Token.OR:
      // a = x||y --> if (a=x) {} else {a=y}
      cond = buildResultExpression(first, needResult, tempName);
      falseExpr.addChildToFront(NodeUtil.newExpr(
          buildResultExpression(last, needResult, tempName)));
      break;
    default:
      // With a valid tree we should never get here.
      throw new IllegalStateException("Unexpected.");
  }

  Node ifNode;
  if (falseExpr.hasChildren()) {
    ifNode = IR.ifNode(cond, trueExpr, falseExpr);
  } else {
    ifNode = IR.ifNode(cond, trueExpr);
  }
  ifNode.copyInformationFrom(expr);

  if (needResult) {
    Node tempVarNode = NodeUtil.newVarNode(tempName, null)
        .copyInformationFromForTree(expr);
    Node injectionPointParent = injectionPoint.getParent();
    injectionPointParent.addChildBefore(tempVarNode, injectionPoint);
    injectionPointParent.addChildAfter(ifNode, tempVarNode);

    // Replace the expression with the temporary name.
    Node replacementValueNode = IR.name(tempName);
    parent.replaceChild(expr, replacementValueNode);
  } else {
    // Only conditionals that are the direct child of an expression statement
    // don't need results, for those simply replace the expression statement.
    Preconditions.checkArgument(parent.isExprResult());
    Node gramps = parent.getParent();
    gramps.replaceChild(parent, ifNode);
  }

  return ifNode;
}