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

The following examples show how to use com.google.javascript.rhino.IR#objectlit() . 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: ExternExportsPass.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given an object literal to export, create an object lit with all its
 * string properties. We don't care what the values of those properties
 * are because they are not checked.
 */
private Node createExternObjectLit(Node exportedObjectLit) {
  Node lit = IR.objectlit();
  lit.setJSType(exportedObjectLit.getJSType());

  // This is an indirect way of telling the typed code generator
  // "print the type of this"
  lit.setJSDocInfo(new JSDocInfo());

  int index = 1;
  for (Node child = exportedObjectLit.getFirstChild();
       child != null;
       child = child.getNext()) {
    // TODO: handle getters or setters?
    if (child.isStringKey()) {
      lit.addChildToBack(
          IR.propdef(
              IR.stringKey(child.getString()),
              IR.number(index++)));
    }
  }
  return lit;
}
 
Example 2
Source File: Nopol2017_0010_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * There are some special cases where clients of the compiler
 * do not run TypedScopeCreator after running this pass.
 * So always give the namespace literal a type.
 */
private Node createNamespaceLiteral() {
  Node objlit = IR.objectlit();
  objlit.setJSType(
      compiler.getTypeRegistry().createAnonymousObjectType(null));
  return objlit;
}
 
Example 3
Source File: Nopol2017_0010_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * There are some special cases where clients of the compiler
 * do not run TypedScopeCreator after running this pass.
 * So always give the namespace literal a type.
 */
private Node createNamespaceLiteral() {
  Node objlit = IR.objectlit();
  objlit.setJSType(
      compiler.getTypeRegistry().createAnonymousObjectType(null));
  return objlit;
}
 
Example 4
Source File: Closure_113_ProcessClosurePrimitives_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * There are some special cases where clients of the compiler
 * do not run TypedScopeCreator after running this pass.
 * So always give the namespace literal a type.
 */
private Node createNamespaceLiteral() {
  Node objlit = IR.objectlit();
  objlit.setJSType(
      compiler.getTypeRegistry().createAnonymousObjectType(null));
  return objlit;
}
 
Example 5
Source File: Closure_113_ProcessClosurePrimitives_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * There are some special cases where clients of the compiler
 * do not run TypedScopeCreator after running this pass.
 * So always give the namespace literal a type.
 */
private Node createNamespaceLiteral() {
  Node objlit = IR.objectlit();
  objlit.setJSType(
      compiler.getTypeRegistry().createAnonymousObjectType(null));
  return objlit;
}
 
Example 6
Source File: ProcessClosurePrimitives.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * There are some special cases where clients of the compiler
 * do not run TypedScopeCreator after running this pass.
 * So always give the namespace literal a type.
 */
private Node createNamespaceLiteral() {
  Node objlit = IR.objectlit();
  objlit.setJSType(
      compiler.getTypeRegistry().createAnonymousObjectType(null));
  return objlit;
}
 
Example 7
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Replaces a new Array or Object node with an object literal, unless the
 * call to Array or Object is to a local function with the same name.
 */
private Node tryFoldLiteralConstructor(Node n) {
  Preconditions.checkArgument(n.isCall()
      || n.isNew());

  Node constructorNameNode = n.getFirstChild();

  Node newLiteralNode = null;

  // We require the AST to be normalized to ensure that, say,
  // Object() really refers to the built-in Object constructor
  // and not a user-defined constructor with the same name.

  if (isASTNormalized() && Token.NAME == constructorNameNode.getType()) {

    String className = constructorNameNode.getString();

    if ("RegExp".equals(className)) {
      // "RegExp("boo", "g")" --> /boo/g
      return tryFoldRegularExpressionConstructor(n);
    } else {
      boolean constructorHasArgs = constructorNameNode.getNext() != null;

      if ("Object".equals(className) && !constructorHasArgs) {
        // "Object()" --> "{}"
        newLiteralNode = IR.objectlit();
      } else if ("Array".equals(className)) {
        // "Array(arg0, arg1, ...)" --> "[arg0, arg1, ...]"
        Node arg0 = constructorNameNode.getNext();
        FoldArrayAction action = isSafeToFoldArrayConstructor(arg0);

        if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS ||
            action == FoldArrayAction.SAFE_TO_FOLD_WITHOUT_ARGS) {
          newLiteralNode = IR.arraylit();
          n.removeChildren();
          if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS) {
            newLiteralNode.addChildrenToFront(arg0);
          }
        }
      }

      if (newLiteralNode != null) {
        n.getParent().replaceChild(n, newLiteralNode);
        reportCodeChange();
        return newLiteralNode;
      }
    }
  }
  return n;
}
 
Example 8
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Replaces a new Array or Object node with an object literal, unless the
 * call to Array or Object is to a local function with the same name.
 */
private Node tryFoldLiteralConstructor(Node n) {
  Preconditions.checkArgument(n.isCall()
      || n.isNew());

  Node constructorNameNode = n.getFirstChild();

  Node newLiteralNode = null;

  // We require the AST to be normalized to ensure that, say,
  // Object() really refers to the built-in Object constructor
  // and not a user-defined constructor with the same name.

  if (isASTNormalized() && Token.NAME == constructorNameNode.getType()) {

    String className = constructorNameNode.getString();

    if ("RegExp".equals(className)) {
      // "RegExp("boo", "g")" --> /boo/g
      return tryFoldRegularExpressionConstructor(n);
    } else {
      boolean constructorHasArgs = constructorNameNode.getNext() != null;

      if ("Object".equals(className) && !constructorHasArgs) {
        // "Object()" --> "{}"
        newLiteralNode = IR.objectlit();
      } else if ("Array".equals(className)) {
        // "Array(arg0, arg1, ...)" --> "[arg0, arg1, ...]"
        Node arg0 = constructorNameNode.getNext();
        FoldArrayAction action = isSafeToFoldArrayConstructor(arg0);

        if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS ||
            action == FoldArrayAction.SAFE_TO_FOLD_WITHOUT_ARGS) {
          newLiteralNode = IR.arraylit();
          n.removeChildren();
          if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS) {
            newLiteralNode.addChildrenToFront(arg0);
          }
        }
      }

      if (newLiteralNode != null) {
        n.getParent().replaceChild(n, newLiteralNode);
        reportCodeChange();
        return newLiteralNode;
      }
    }
  }
  return n;
}
 
Example 9
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Replaces a new Array or Object node with an object literal, unless the
 * call to Array or Object is to a local function with the same name.
 */
private Node tryFoldLiteralConstructor(Node n) {
  Preconditions.checkArgument(n.isCall()
      || n.isNew());

  Node constructorNameNode = n.getFirstChild();

  Node newLiteralNode = null;

  // We require the AST to be normalized to ensure that, say,
  // Object() really refers to the built-in Object constructor
  // and not a user-defined constructor with the same name.

  if (isASTNormalized() && Token.NAME == constructorNameNode.getType()) {

    String className = constructorNameNode.getString();

    if ("RegExp".equals(className)) {
      // "RegExp("boo", "g")" --> /boo/g
      return tryFoldRegularExpressionConstructor(n);
    } else {
      boolean constructorHasArgs = constructorNameNode.getNext() != null;

      if ("Object".equals(className) && !constructorHasArgs) {
        // "Object()" --> "{}"
        newLiteralNode = IR.objectlit();
      } else if ("Array".equals(className)) {
        // "Array(arg0, arg1, ...)" --> "[arg0, arg1, ...]"
        Node arg0 = constructorNameNode.getNext();
        FoldArrayAction action = isSafeToFoldArrayConstructor(arg0);

        if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS ||
            action == FoldArrayAction.SAFE_TO_FOLD_WITHOUT_ARGS) {
          newLiteralNode = IR.arraylit();
          n.removeChildren();
          if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS) {
            newLiteralNode.addChildrenToFront(arg0);
          }
        }
      }

      if (newLiteralNode != null) {
        n.getParent().replaceChild(n, newLiteralNode);
        reportCodeChange();
        return newLiteralNode;
      }
    }
  }
  return n;
}
 
Example 10
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Replaces a new Array or Object node with an object literal, unless the
 * call to Array or Object is to a local function with the same name.
 */
private Node tryFoldLiteralConstructor(Node n) {
  Preconditions.checkArgument(n.isCall()
      || n.isNew());

  Node constructorNameNode = n.getFirstChild();

  Node newLiteralNode = null;

  // We require the AST to be normalized to ensure that, say,
  // Object() really refers to the built-in Object constructor
  // and not a user-defined constructor with the same name.

  if (isASTNormalized() && Token.NAME == constructorNameNode.getType()) {

    String className = constructorNameNode.getString();

    if ("RegExp".equals(className)) {
      // "RegExp("boo", "g")" --> /boo/g
      return tryFoldRegularExpressionConstructor(n);
    } else {
      boolean constructorHasArgs = constructorNameNode.getNext() != null;

      if ("Object".equals(className) && !constructorHasArgs) {
        // "Object()" --> "{}"
        newLiteralNode = IR.objectlit();
      } else if ("Array".equals(className)) {
        // "Array(arg0, arg1, ...)" --> "[arg0, arg1, ...]"
        Node arg0 = constructorNameNode.getNext();
        FoldArrayAction action = isSafeToFoldArrayConstructor(arg0);

        if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS ||
            action == FoldArrayAction.SAFE_TO_FOLD_WITHOUT_ARGS) {
          newLiteralNode = IR.arraylit();
          n.removeChildren();
          if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS) {
            newLiteralNode.addChildrenToFront(arg0);
          }
        }
      }

      if (newLiteralNode != null) {
        n.getParent().replaceChild(n, newLiteralNode);
        reportCodeChange();
        return newLiteralNode;
      }
    }
  }
  return n;
}
 
Example 11
Source File: ClosureRewriteClass.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Validates the class definition and if valid, destructively extracts
 * the class definition from the AST.
 */
private ClassDefinition extractClassDefinition(
    Node targetName, Node callNode) {

  // name = goog.defineClass(superClass, {...}, [modifier, ...])
  Node superClass = NodeUtil.getArgumentForCallOrNew(callNode, 0);
  if (superClass == null ||
      (!superClass.isNull() && !superClass.isQualifiedName())) {
    compiler.report(JSError.make(callNode, GOOG_CLASS_SUPER_CLASS_NOT_VALID));
    return null;
  }
  if (NodeUtil.isNullOrUndefined(superClass)) {
    superClass = null;
  }

  Node description = NodeUtil.getArgumentForCallOrNew(callNode, 1);
  if (description == null
      || !description.isObjectLit()
      || !validateObjLit(description)) {
    // report bad class definition
    compiler.report(JSError.make(callNode, GOOG_CLASS_DESCRIPTOR_NOT_VALID));
    return null;
  }

  int paramCount = callNode.getChildCount() -1;
  if (paramCount > 2) {
    compiler.report(JSError.make(callNode, GOOG_CLASS_UNEXPECTED_PARAMS));
    return null;
  }

  Node constructor = extractProperty(description, "constructor");
  if (constructor == null) {
    // report missing constructor
    compiler.report(JSError.make(description, GOOG_CLASS_CONSTRUCTOR_MISING));
    return null;
  }
  JSDocInfo info = NodeUtil.getBestJSDocInfo(constructor);

  Node classModifier = null;
  Node statics = null;
  Node staticsProp = extractProperty(description, "statics");
  if (staticsProp != null) {
    if (staticsProp.isObjectLit() && validateObjLit(staticsProp)) {
      statics = staticsProp;
    } else if (staticsProp.isFunction()) {
      classModifier = staticsProp;
    } else {
      compiler.report(
          JSError.make(staticsProp, GOOG_CLASS_STATICS_NOT_VALID));
      return null;
    }
  }

  if (statics == null) {
    statics = IR.objectlit();
  }

  // Ok, now rip apart the definition into its component pieces.
  // Remove the "special" property key nodes.
  maybeDetach(constructor.getParent());
  maybeDetach(statics.getParent());
  if (classModifier != null) {
    maybeDetach(classModifier.getParent());
  }
  ClassDefinition def = new ClassDefinition(
      targetName,
      maybeDetach(superClass),
      new MemberDefinition(info, null, maybeDetach(constructor)),
      objectLitToList(maybeDetach(statics)),
      objectLitToList(description),
      maybeDetach(classModifier));
  return def;
}
 
Example 12
Source File: PeepholeSubstituteAlternateSyntax.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Replaces a new Array or Object node with an object literal, unless the
 * call to Array or Object is to a local function with the same name.
 */
private Node tryFoldLiteralConstructor(Node n) {
  Preconditions.checkArgument(n.isCall()
      || n.isNew());

  Node constructorNameNode = n.getFirstChild();

  Node newLiteralNode = null;

  // We require the AST to be normalized to ensure that, say,
  // Object() really refers to the built-in Object constructor
  // and not a user-defined constructor with the same name.

  if (isASTNormalized() && Token.NAME == constructorNameNode.getType()) {

    String className = constructorNameNode.getString();

    if ("RegExp".equals(className)) {
      // "RegExp("boo", "g")" --> /boo/g
      return tryFoldRegularExpressionConstructor(n);
    } else {
      boolean constructorHasArgs = constructorNameNode.getNext() != null;

      if ("Object".equals(className) && !constructorHasArgs) {
        // "Object()" --> "{}"
        newLiteralNode = IR.objectlit();
      } else if ("Array".equals(className)) {
        // "Array(arg0, arg1, ...)" --> "[arg0, arg1, ...]"
        Node arg0 = constructorNameNode.getNext();
        FoldArrayAction action = isSafeToFoldArrayConstructor(arg0);

        if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS ||
            action == FoldArrayAction.SAFE_TO_FOLD_WITHOUT_ARGS) {
          newLiteralNode = IR.arraylit();
          n.removeChildren();
          if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS) {
            newLiteralNode.addChildrenToFront(arg0);
          }
        }
      }

      if (newLiteralNode != null) {
        n.getParent().replaceChild(n, newLiteralNode);
        reportCodeChange();
        return newLiteralNode;
      }
    }
  }
  return n;
}