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

The following examples show how to use com.google.javascript.rhino.IR#arraylit() . 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: RuntimeTypeCheck.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a function call to check that the given expression matches the
 * given type at runtime.
 *
 * <p>For example, if the type is {@code (string|Foo)}, the function call is
 * {@code checkType(expr, [valueChecker('string'), classChecker('Foo')])}.
 *
 * @return the function call node or {@code null} if the type is not checked
 */
private Node createCheckTypeCallNode(JSType type, Node expr) {
  Node arrayNode = IR.arraylit();
  Collection<JSType> alternates;
  if (type.isUnionType()) {
    alternates = Sets.newTreeSet(ALPHA);
    Iterables.addAll(alternates, type.toMaybeUnionType().getAlternates());
  } else {
    alternates = ImmutableList.of(type);
  }
  for (JSType alternate : alternates) {
    Node checkerNode = createCheckerNode(alternate);
    if (checkerNode == null) {
      return null;
    }
    arrayNode.addChildToBack(checkerNode);
  }
  return IR.call(jsCode("checkType"), expr, arrayNode);
}
 
Example 2
Source File: ReplaceMessagesForChrome.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private Node getNewValueNode(Node origNode, JsMessage message)
    throws MalformedException {
  Node newValueNode = getChromeI18nGetMessageNode(message.getId());

  if (!message.placeholders().isEmpty()) {
    Node placeholderValues = origNode.getLastChild();
    checkNode(placeholderValues, Token.OBJECTLIT);

    // Output the placeholders, sorted alphabetically by placeholder name,
    // regardless of what order they appear in the original message.
    List<String> placeholderNames = Lists.newArrayList();
    for (CharSequence cs : message.parts()) {
      if (cs instanceof PlaceholderReference) {
        String placeholderName = ((PlaceholderReference) cs).getName();
        placeholderNames.add(placeholderName);
      }
    }
    Collections.sort(placeholderNames);

    Node placeholderValueArray = IR.arraylit();
    for (String name : placeholderNames) {
      Node value = getPlaceholderValue(placeholderValues, name);
      if (value == null) {
        throw new MalformedException(
            "No value was provided for placeholder " + name,
            origNode);
      }
      placeholderValueArray.addChildToBack(value);
    }
    newValueNode.addChildToBack(placeholderValueArray);
  }

  newValueNode.copyInformationFromForTree(origNode);
  return newValueNode;
}
 
Example 3
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 4
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 5
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 6
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 7
Source File: PeepholeReplaceKnownMethods.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Try to fold .split() calls on strings
 */
private Node tryFoldStringSplit(Node n, Node stringNode, Node arg1) {
  if (late) {
    return n;
  }

  Preconditions.checkArgument(n.isCall());
  Preconditions.checkArgument(stringNode.isString());

  String separator = null;
  String stringValue = stringNode.getString();

  // Maximum number of possible splits
  int limit = stringValue.length() + 1;

  if (arg1 != null) {
    if (arg1.isString()) {
      separator = arg1.getString();
    } else if (!arg1.isNull()) {
      return n;
    }

    Node arg2 = arg1.getNext();
    if (arg2 != null) {
      if (arg2.isNumber()) {
        limit = Math.min((int) arg2.getDouble(), limit);
        if (limit < 0) {
          return n;
        }
      } else {
        return n;
      }
    }
  }

  // Split the string and convert the returned array into JS nodes
  String[] stringArray = jsSplit(stringValue, separator, limit);
  Node arrayOfStrings = IR.arraylit();
  for (int i = 0; i < stringArray.length; i++) {
    arrayOfStrings.addChildToBack(
        IR.string(stringArray[i]).srcref(stringNode));
  }

  Node parent = n.getParent();
  parent.replaceChild(n, arrayOfStrings);
  reportCodeChange();
  return arrayOfStrings;
}
 
Example 8
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;
}