Java Code Examples for com.google.javascript.rhino.Token#GET

The following examples show how to use com.google.javascript.rhino.Token#GET . 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: 1_NodeUtil.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Gets the function's name. This method recognizes the forms:
 * <ul>
 * <li>{@code &#123;'name': function() ...&#125;}</li>
 * <li>{@code &#123;name: function() ...&#125;}</li>
 * <li>{@code function name() ...}</li>
 * <li>{@code var name = function() ...}</li>
 * <li>{@code qualified.name = function() ...}</li>
 * <li>{@code var name2 = function name1() ...}</li>
 * <li>{@code qualified.name2 = function name1() ...}</li>
 * </ul>
 *
 * @param n a node whose type is {@link Token#FUNCTION}
 * @return the function's name, or {@code null} if it has no name
 */
static String getNearestFunctionName(Node n) {
  String name = getFunctionName(n);
  if (name != null) {
    return name;
  }

  // Check for the form { 'x' : function() { } }
  Node parent = n.getParent();
  switch (parent.getType()) {
    case Token.SET:
    case Token.GET:
    case Token.STRING:
      // Return the name of the literal's key.
      return parent.getString();
    case Token.NUMBER:
      return getStringValue(parent);
  }

  return null;
}
 
Example 2
Source File: Closure_80_NodeUtil_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets the function's name. This method recognizes the forms:
 * <ul>
 * <li>{@code &#123;'name': function() ...&#125;}</li>
 * <li>{@code &#123;name: function() ...&#125;}</li>
 * <li>{@code function name() ...}</li>
 * <li>{@code var name = function() ...}</li>
 * <li>{@code qualified.name = function() ...}</li>
 * <li>{@code var name2 = function name1() ...}</li>
 * <li>{@code qualified.name2 = function name1() ...}</li>
 * </ul>
 *
 * @param n a node whose type is {@link Token#FUNCTION}
 * @return the function's name, or {@code null} if it has no name
 */
static String getNearestFunctionName(Node n) {
  String name = getFunctionName(n);
  if (name != null) {
    return name;
  }

  // Check for the form { 'x' : function() { } }
  Node parent = n.getParent();
  switch (parent.getType()) {
    case Token.SET:
    case Token.GET:
    case Token.STRING:
      // Return the name of the literal's key.
      return parent.getString();
    case Token.NUMBER:
      return getStringValue(parent);
  }

  return null;
}
 
Example 3
Source File: Closure_75_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether a node represents an object literal get or set key
 * (e.g. key1 in {get key1() {}, set key2(a){}).
 *
 * @param node A node
 */
static boolean isGetOrSetKey(Node node) {
  switch (node.getType()) {
    case Token.GET:
    case Token.SET:
      return true;
  }
  return false;
}
 
Example 4
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 5
Source File: Closure_80_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether a node represents an object literal get or set key
 * (e.g. key1 in {get key1() {}, set key2(a){}).
 *
 * @param node A node
 */
static boolean isGetOrSetKey(Node node) {
  switch (node.getType()) {
    case Token.GET:
    case Token.SET:
      return true;
  }
  return false;
}
 
Example 6
Source File: Closure_80_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Get the name of an object literal key.
 *
 * @param key A node
 */
static String getObjectLitKeyName(Node key) {
  switch (key.getType()) {
    case Token.NUMBER:
      return NodeUtil.getStringValue(key);
    case Token.STRING:
    case Token.GET:
    case Token.SET:
      return key.getString();
  }
  throw new IllegalStateException("Unexpected node type: " + key);
}
 
Example 7
Source File: Closure_80_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether a node represents an object literal key
 * (e.g. key1 in {key1: value1, key2: value2}).
 *
 * @param node A node
 * @param parent The node's parent
 */
static boolean isObjectLitKey(Node node, Node parent) {
  switch (node.getType()) {
    case Token.NUMBER:
    case Token.STRING:
      return parent.getType() == Token.OBJECTLIT;
    case Token.GET:
    case Token.SET:
      return true;
  }
  return false;
}
 
Example 8
Source File: Closure_61_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether a node represents an object literal key
 * (e.g. key1 in {key1: value1, key2: value2}).
 *
 * @param node A node
 * @param parent The node's parent
 */
static boolean isObjectLitKey(Node node, Node parent) {
  switch (node.getType()) {
    case Token.STRING:
      return parent.getType() == Token.OBJECTLIT;
    case Token.GET:
    case Token.SET:
      return true;
  }
  return false;
}
 
Example 9
Source File: Closure_80_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether a node represents an object literal get or set key
 * (e.g. key1 in {get key1() {}, set key2(a){}).
 *
 * @param node A node
 */
static boolean isGetOrSetKey(Node node) {
  switch (node.getType()) {
    case Token.GET:
    case Token.SET:
      return true;
  }
  return false;
}
 
Example 10
Source File: Closure_61_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Get the name of an object literal key.
 *
 * @param key A node
 */
static String getObjectLitKeyName(Node key) {
  switch (key.getType()) {
    case Token.STRING:
    case Token.GET:
    case Token.SET:
      return key.getString();
  }
  throw new IllegalStateException("Unexpected node type: " + key);
}
 
Example 11
Source File: Closure_80_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether a node represents an object literal key
 * (e.g. key1 in {key1: value1, key2: value2}).
 *
 * @param node A node
 * @param parent The node's parent
 */
static boolean isObjectLitKey(Node node, Node parent) {
  switch (node.getType()) {
    case Token.NUMBER:
    case Token.STRING:
      return parent.getType() == Token.OBJECTLIT;
    case Token.GET:
    case Token.SET:
      return true;
  }
  return false;
}
 
Example 12
Source File: Closure_86_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether a node represents an object literal get or set key
 * (e.g. key1 in {get key1() {}, set key2(a){}).
 *
 * @param node A node
 */
static boolean isGetOrSetKey(Node node) {
  switch (node.getType()) {
    case Token.GET:
    case Token.SET:
      return true;
  }
  return false;
}
 
Example 13
Source File: Closure_60_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether a node represents an object literal key
 * (e.g. key1 in {key1: value1, key2: value2}).
 *
 * @param node A node
 * @param parent The node's parent
 */
static boolean isObjectLitKey(Node node, Node parent) {
  switch (node.getType()) {
    case Token.STRING:
      return parent.getType() == Token.OBJECTLIT;
    case Token.GET:
    case Token.SET:
      return true;
  }
  return false;
}
 
Example 14
Source File: Closure_60_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether a node represents an object literal get or set key
 * (e.g. key1 in {get key1() {}, set key2(a){}).
 *
 * @param node A node
 */
static boolean isGetOrSetKey(Node node) {
  switch (node.getType()) {
    case Token.GET:
    case Token.SET:
      return true;
  }
  return false;
}
 
Example 15
Source File: Closure_86_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether a node represents an object literal key
 * (e.g. key1 in {key1: value1, key2: value2}).
 *
 * @param node A node
 * @param parent The node's parent
 */
static boolean isObjectLitKey(Node node, Node parent) {
  switch (node.getType()) {
    case Token.NUMBER:
    case Token.STRING:
      return parent.getType() == Token.OBJECTLIT;
    case Token.GET:
    case Token.SET:
      return true;
  }
  return false;
}
 
Example 16
Source File: Closure_61_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether a node represents an object literal get or set key
 * (e.g. key1 in {get key1() {}, set key2(a){}).
 *
 * @param node A node
 */
static boolean isGetOrSetKey(Node node) {
  switch (node.getType()) {
    case Token.GET:
    case Token.SET:
      return true;
  }
  return false;
}
 
Example 17
Source File: 1_NodeUtil.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determines whether a node represents an object literal get or set key
 * (e.g. key1 in {get key1() {}, set key2(a){}).
 *
 * @param node A node
 */
static boolean isGetOrSetKey(Node node) {
  switch (node.getType()) {
    case Token.GET:
    case Token.SET:
      return true;
  }
  return false;
}
 
Example 18
Source File: 1_NodeUtil.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the name of an object literal key.
 *
 * @param key A node
 */
static String getObjectLitKeyName(Node key) {
  switch (key.getType()) {
    case Token.NUMBER:
      return NodeUtil.getStringValue(key);
    case Token.STRING:
    case Token.GET:
    case Token.SET:
      return key.getString();
  }
  throw new IllegalStateException("Unexpected node type: " + key);
}
 
Example 19
Source File: Closure_75_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Get the name of an object literal key.
 *
 * @param key A node
 */
static String getObjectLitKeyName(Node key) {
  switch (key.getType()) {
    case Token.NUMBER:
      return NodeUtil.getStringValue(key);
    case Token.STRING:
    case Token.GET:
    case Token.SET:
      return key.getString();
  }
  throw new IllegalStateException("Unexpected node type: " + key);
}
 
Example 20
Source File: Closure_53_InlineObjectLiterals_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Counts the number of direct (full) references to an object.
 * Specifically we check for references of the following type:
 * <pre>
 *   x;
 *   x.fn();
 * </pre>
 */
private boolean isInlinableObject(List<Reference> refs) {
  boolean ret = false;
  for (Reference ref : refs) {
    Node name = ref.getNode();
    Node parent = ref.getParent();
    Node gramps = ref.getGrandparent();

    // Ignore indirect references, like x.y (except x.y(), since
    // the function referenced by y might reference 'this').
    //
    if (parent.getType() == Token.GETPROP) {
      Preconditions.checkState(parent.getFirstChild() == name);
      // A call target maybe using the object as a 'this' value.
      if (gramps.getType() == Token.CALL
          && gramps.getFirstChild() == parent) {
        return false;
      }
      continue;
    }

    // Only rewrite VAR declarations or simple assignment statements
    if (!isVarOrAssignExprLhs(name)) {
       return false;
    }

    Node val = ref.getAssignedValue();
    if (val == null) {
      // A var with no assignment.
      continue;
    }

    // We're looking for object literal assignments only.
    if (val.getType() != Token.OBJECTLIT) {
      return false;
    }

    // Make sure that the value is not self-refential. IOW,
    // disallow things like x = {b: x.a}.
    //
    // TODO: Only exclude unorderable self-referential
    // assignments. i.e. x = {a: x.b, b: x.a} is not orderable,
    // but x = {a: 1, b: x.a} is.
    //
    // Also, ES5 getters/setters aren't handled by this pass.
    for (Node child = val.getFirstChild(); child != null;
         child = child.getNext()) {
      if (child.getType() == Token.GET ||
          child.getType() == Token.SET) {
        // ES5 get/set not supported.
        return false;
      }
      Node childVal = child.getFirstChild();
      // Check if childVal is the parent of any of the passed in
      // references, as that is how self-referential assignments
      // will happen.
      for (Reference t : refs) {
        Node refNode = t.getParent();
        while (!NodeUtil.isStatementBlock(refNode)) {
          if (refNode == childVal) {
            // There's a self-referential assignment
            return false;
          }
          refNode = refNode.getParent();
        }
      }
    }


    // We have found an acceptable object literal assignment. As
    // long as there are no other assignments that mess things up,
    // we can inline.
    ret = true;
  }
  return ret;
}