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

The following examples show how to use com.google.javascript.rhino.Token#STRING . 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_55_FunctionRewriter_t.java    From coming with MIT License 6 votes vote down vote up
@Override
public Node reduce(Node node) {
  if (!isReduceableFunctionExpression(node)) {
    return node;
  }

  Node propName = getSetPropertyName(node);
  if (propName != null) {
    if (propName.getType() != Token.STRING) {
      throw new IllegalStateException(
          "Expected STRING, got " + Token.name(propName.getType()));
    }

    return buildCallNode(FACTORY_METHOD_NAME, propName,
                         node.getLineno(), node.getCharno());
  } else {
    return node;
  }
}
 
Example 2
Source File: Closure_103_DisambiguateProperties_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Processes a OBJECTLIT node.
 */
private void handleObjectLit(NodeTraversal t, Node n) {
  Node child = n.getFirstChild();
  while (child != null) {
    if (child.getType() == Token.STRING) {
      // We should never see a mix of numbers and strings.
      String name = child.getString();
      T type = typeSystem.getType(getScope(), n, name);

      Property prop = getProperty(name);
      if (!prop.scheduleRenaming(child,
                                 processProperty(t, prop, type, null))) {
        if (showInvalidationWarnings) {
          compiler.report(JSError.make(
              t.getSourceName(), child, INVALIDATION, name,
              (type == null ? "null" : type.toString()), n.toString()));
        }
      }
    }

    child = child.getNext().getNext();
  }
}
 
Example 3
Source File: Closure_60_NodeUtil_t.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
 */
public 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 4
Source File: Nopol2017_0047_t.java    From coming with MIT License 6 votes vote down vote up
@Override
public Node reduce(Node node) {
  if (!isReduceableFunctionExpression(node)) {
    return node;
  }

  Node propName = getSetPropertyName(node);
  if (propName != null) {
    if (propName.getType() != Token.STRING) {
      throw new IllegalStateException(
          "Expected STRING, got " + Token.name(propName.getType()));
    }

    return buildCallNode(FACTORY_METHOD_NAME, propName,
                         node.getLineno(), node.getCharno());
  } else {
    return node;
  }
}
 
Example 5
Source File: Closure_55_FunctionRewriter_s.java    From coming with MIT License 6 votes vote down vote up
@Override
public Node reduce(Node node) {
  if (!isReduceableFunctionExpression(node)) {
    return node;
  }

  Node propName = getGetPropertyName(node);
  if (propName != null) {
    if (propName.getType() != Token.STRING) {
      throw new IllegalStateException(
          "Expected STRING, got " + Token.name(propName.getType()));
    }

    return buildCallNode(FACTORY_METHOD_NAME, propName,
                         node.getLineno(), node.getCharno());
  } else {
    return node;
  }
}
 
Example 6
Source File: Closure_60_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @return Whether node is a call to methodName.
 *    a.f(...)
 *    a['f'](...)
 */
static boolean isObjectCallMethod(Node callNode, String methodName) {
  if (callNode.getType() == Token.CALL) {
    Node functionIndentifyingExpression = callNode.getFirstChild();
    if (isGet(functionIndentifyingExpression)) {
      Node last = functionIndentifyingExpression.getLastChild();
      if (last != null && last.getType() == Token.STRING) {
        String propName = last.getString();
        return (propName.equals(methodName));
      }
    }
  }
  return false;
}
 
Example 7
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 8
Source File: Closure_86_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @return Whether node is a call to methodName.
 *    a.f(...)
 *    a['f'](...)
 */
static boolean isObjectCallMethod(Node callNode, String methodName) {
  if (callNode.getType() == Token.CALL) {
    Node functionIndentifyingExpression = callNode.getFirstChild();
    if (isGet(functionIndentifyingExpression)) {
      Node last = functionIndentifyingExpression.getLastChild();
      if (last != null && last.getType() == Token.STRING) {
        String propName = last.getString();
        return (propName.equals(methodName));
      }
    }
  }
  return false;
}
 
Example 9
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 10
Source File: Closure_95_TypedScopeCreator_s.java    From coming with MIT License 4 votes vote down vote up
private void attachLiteralTypes(Node n) {
  switch (n.getType()) {
    case Token.NULL:
      n.setJSType(getNativeType(NULL_TYPE));
      break;

    case Token.VOID:
      n.setJSType(getNativeType(VOID_TYPE));
      break;

    case Token.STRING:
      n.setJSType(getNativeType(STRING_TYPE));
      break;

    case Token.NUMBER:
      n.setJSType(getNativeType(NUMBER_TYPE));
      break;

    case Token.TRUE:
    case Token.FALSE:
      n.setJSType(getNativeType(BOOLEAN_TYPE));
      break;

    case Token.REGEXP:
      n.setJSType(getNativeType(REGEXP_TYPE));
      break;

    case Token.REF_SPECIAL:
      n.setJSType(getNativeType(UNKNOWN_TYPE));
      break;

    case Token.OBJECTLIT:
      if (n.getJSType() == null) {
        n.setJSType(typeRegistry.createAnonymousObjectType());
      }
      break;

    // NOTE(nicksantos): If we ever support Array tuples,
    // we will need to put ARRAYLIT here as well.
  }
}
 
Example 11
Source File: Closure_106_GlobalNamespace_s.java    From coming with MIT License 4 votes vote down vote up
/** {@inheritDoc} */
public void visit(NodeTraversal t, Node n, Node parent) {
  if (nodeFilter != null && !nodeFilter.apply(n)) {
    return;
  }

  // If we are traversing the externs, then we save a pointer to the scope
  // generated by them, so that we can do lookups in it later.
  if (externsRoot != null && n == externsRoot) {
    externsScope = t.getScope();
  }

  String name;
  boolean isSet = false;
  Name.Type type = Name.Type.OTHER;
  boolean isPropAssign = false;

  switch (n.getType()) {
    case Token.STRING:
      // This may be a key in an object literal declaration.
      name = null;
      if (parent != null && parent.getType() == Token.OBJECTLIT) {
        name = getNameForObjLitKey(n);
      }
      if (name == null) return;
      isSet = true;
      type = getValueType(n.getNext());
      break;
    case Token.NAME:
      // This may be a variable get or set.
      if (parent != null) {
        switch (parent.getType()) {
          case Token.VAR:
            isSet = true;
            Node rvalue = n.getFirstChild();
            type = rvalue == null ? Name.Type.OTHER : getValueType(rvalue);
            break;
          case Token.ASSIGN:
            if (parent.getFirstChild() == n) {
              isSet = true;
              type = getValueType(n.getNext());
            }
            break;
          case Token.GETPROP:
            return;
          case Token.FUNCTION:
            Node gramps = parent.getParent();
            if (gramps == null ||
                NodeUtil.isFunctionAnonymous(parent)) return;
            isSet = true;
            type = Name.Type.FUNCTION;
            break;
        }
      }
      name = n.getString();
      break;
    case Token.GETPROP:
      // This may be a namespaced name get or set.
      if (parent != null) {
        switch (parent.getType()) {
          case Token.ASSIGN:
            if (parent.getFirstChild() == n) {
              isSet = true;
              type = getValueType(n.getNext());
              isPropAssign = true;
            }
            break;
          case Token.GETPROP:
            return;
        }
      }
      name = n.getQualifiedName();
      if (name == null) return;
      break;
    default:
      return;
  }

  // We are only interested in global names.
  Scope scope = t.getScope();
  if (!isGlobalNameReference(name, scope)) {
    return;
  }

  if (isSet) {
    if (isGlobalScope(scope)) {
      handleSetFromGlobal(t, n, parent, name, isPropAssign, type);
    } else {
      handleSetFromLocal(t, n, parent, name);
    }
  } else {
    handleGet(t, n, parent, name);
  }
}
 
Example 12
Source File: Closure_10_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Gets the value of a node as a Number, or null if it cannot be converted.
 * When it returns a non-null Double, this method effectively emulates the
 * <code>Number()</code> JavaScript cast function.
 */
static Double getNumberValue(Node n) {
  switch (n.getType()) {
    case Token.TRUE:
      return 1.0;

    case Token.FALSE:
    case Token.NULL:
      return 0.0;

    case Token.NUMBER:
      return n.getDouble();

    case Token.VOID:
      if (mayHaveSideEffects(n.getFirstChild())) {
        return null;
      } else {
        return Double.NaN;
      }

    case Token.NAME:
      // Check for known constants
      String name = n.getString();
      if (name.equals("undefined")) {
        return Double.NaN;
      }
      if (name.equals("NaN")) {
        return Double.NaN;
      }
      if (name.equals("Infinity")) {
        return Double.POSITIVE_INFINITY;
      }
      return null;

    case Token.NEG:
      if (n.getChildCount() == 1 && n.getFirstChild().isName()
          && n.getFirstChild().getString().equals("Infinity")) {
        return Double.NEGATIVE_INFINITY;
      }
      return null;

    case Token.NOT:
      TernaryValue child = getPureBooleanValue(n.getFirstChild());
      if (child != TernaryValue.UNKNOWN) {
        return child.toBoolean(true) ? 0.0 : 1.0; // reversed.
      }
      break;

    case Token.STRING:
      return getStringNumberValue(n.getString());

    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      String value = getStringValue(n);
      return value != null ? getStringNumberValue(value) : null;
  }

  return null;
}
 
Example 13
Source File: jMutRepair_003_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Gets the value of a node as a Number, or null if it cannot be converted.
 * When it returns a non-null Double, this method effectively emulates the
 * <code>Number()</code> JavaScript cast function.
 */
static Double getNumberValue(Node n) {
  switch (n.getType()) {
    case Token.TRUE:
      return 1.0;

    case Token.FALSE:
    case Token.NULL:
      return 0.0;

    case Token.NUMBER:
      return n.getDouble();

    case Token.VOID:
      if (mayHaveSideEffects(n.getFirstChild())) {
        return null;
      } else {
        return Double.NaN;
      }

    case Token.NAME:
      // Check for known constants
      String name = n.getString();
      if (name.equals("undefined")) {
        return Double.NaN;
      }
      if (name.equals("NaN")) {
        return Double.NaN;
      }
      if (name.equals("Infinity")) {
        return Double.POSITIVE_INFINITY;
      }
      return null;

    case Token.NEG:
      if (n.getChildCount() == 1 && n.getFirstChild().isName()
          && n.getFirstChild().getString().equals("Infinity")) {
        return Double.NEGATIVE_INFINITY;
      }
      return null;

    case Token.NOT:
      TernaryValue child = getPureBooleanValue(n.getFirstChild());
      if (child != TernaryValue.UNKNOWN) {
        return child.toBoolean(true) ? 0.0 : 1.0; // reversed.
      }
      break;

    case Token.STRING:
      return getStringNumberValue(n.getString());

    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      String value = getStringValue(n);
      return value != null ? getStringNumberValue(value) : null;
  }

  return null;
}
 
Example 14
Source File: Closure_106_GlobalNamespace_t.java    From coming with MIT License 4 votes vote down vote up
/** {@inheritDoc} */
public void visit(NodeTraversal t, Node n, Node parent) {
  if (nodeFilter != null && !nodeFilter.apply(n)) {
    return;
  }

  // If we are traversing the externs, then we save a pointer to the scope
  // generated by them, so that we can do lookups in it later.
  if (externsRoot != null && n == externsRoot) {
    externsScope = t.getScope();
  }

  String name;
  boolean isSet = false;
  Name.Type type = Name.Type.OTHER;
  boolean isPropAssign = false;

  switch (n.getType()) {
    case Token.STRING:
      // This may be a key in an object literal declaration.
      name = null;
      if (parent != null && parent.getType() == Token.OBJECTLIT) {
        name = getNameForObjLitKey(n);
      }
      if (name == null) return;
      isSet = true;
      type = getValueType(n.getNext());
      break;
    case Token.NAME:
      // This may be a variable get or set.
      if (parent != null) {
        switch (parent.getType()) {
          case Token.VAR:
            isSet = true;
            Node rvalue = n.getFirstChild();
            type = rvalue == null ? Name.Type.OTHER : getValueType(rvalue);
            break;
          case Token.ASSIGN:
            if (parent.getFirstChild() == n) {
              isSet = true;
              type = getValueType(n.getNext());
            }
            break;
          case Token.GETPROP:
            return;
          case Token.FUNCTION:
            Node gramps = parent.getParent();
            if (gramps == null ||
                NodeUtil.isFunctionAnonymous(parent)) return;
            isSet = true;
            type = Name.Type.FUNCTION;
            break;
        }
      }
      name = n.getString();
      break;
    case Token.GETPROP:
      // This may be a namespaced name get or set.
      if (parent != null) {
        switch (parent.getType()) {
          case Token.ASSIGN:
            if (parent.getFirstChild() == n) {
              isSet = true;
              type = getValueType(n.getNext());
              isPropAssign = true;
            }
            break;
          case Token.GETPROP:
            return;
        }
      }
      name = n.getQualifiedName();
      if (name == null) return;
      break;
    default:
      return;
  }

  // We are only interested in global names.
  Scope scope = t.getScope();
  if (!isGlobalNameReference(name, scope)) {
    return;
  }

  if (isSet) {
    if (isGlobalScope(scope)) {
      handleSetFromGlobal(t, n, parent, name, isPropAssign, type);
    } else {
      handleSetFromLocal(t, n, parent, name);
    }
  } else {
    handleGet(t, n, parent, name);
  }
}
 
Example 15
Source File: 1_NodeUtil.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
static int precedence(int type) {
  switch (type) {
    case Token.COMMA:  return 0;
    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_ADD:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_DIV:
    case Token.ASSIGN_MOD:
    case Token.ASSIGN: return 1;
    case Token.HOOK:   return 2;  // ?: operator
    case Token.OR:     return 3;
    case Token.AND:    return 4;
    case Token.BITOR:  return 5;
    case Token.BITXOR: return 6;
    case Token.BITAND: return 7;
    case Token.EQ:
    case Token.NE:
    case Token.SHEQ:
    case Token.SHNE:   return 8;
    case Token.LT:
    case Token.GT:
    case Token.LE:
    case Token.GE:
    case Token.INSTANCEOF:
    case Token.IN:     return 9;
    case Token.LSH:
    case Token.RSH:
    case Token.URSH:   return 10;
    case Token.SUB:
    case Token.ADD:    return 11;
    case Token.MUL:
    case Token.MOD:
    case Token.DIV:    return 12;
    case Token.INC:
    case Token.DEC:
    case Token.NEW:
    case Token.DELPROP:
    case Token.TYPEOF:
    case Token.VOID:
    case Token.NOT:
    case Token.BITNOT:
    case Token.POS:
    case Token.NEG:    return 13;

    case Token.ARRAYLIT:
    case Token.CALL:
    case Token.EMPTY:
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.GETELEM:
    case Token.GETPROP:
    case Token.GET_REF:
    case Token.IF:
    case Token.LP:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.RETURN:
    case Token.STRING:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Node.tokenToName(type) +
                             " (type " + type + ")");
  }
}
 
Example 16
Source File: 1_NodeUtil.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Determines whether the given value may be assigned to a define.
 *
 * @param val The value being assigned.
 * @param defines The list of names of existing defines.
 */
static boolean isValidDefineValue(Node val, Set<String> defines) {
  switch (val.getType()) {
    case Token.STRING:
    case Token.NUMBER:
    case Token.TRUE:
    case Token.FALSE:
      return true;

    // Binary operators are only valid if both children are valid.
    case Token.ADD:
    case Token.BITAND:
    case Token.BITNOT:
    case Token.BITOR:
    case Token.BITXOR:
    case Token.DIV:
    case Token.EQ:
    case Token.GE:
    case Token.GT:
    case Token.LE:
    case Token.LSH:
    case Token.LT:
    case Token.MOD:
    case Token.MUL:
    case Token.NE:
    case Token.RSH:
    case Token.SHEQ:
    case Token.SHNE:
    case Token.SUB:
    case Token.URSH:
      return isValidDefineValue(val.getFirstChild(), defines)
          && isValidDefineValue(val.getLastChild(), defines);

    // Uniary operators are valid if the child is valid.
    case Token.NOT:
    case Token.NEG:
    case Token.POS:
      return isValidDefineValue(val.getFirstChild(), defines);

    // Names are valid if and only if they are defines themselves.
    case Token.NAME:
    case Token.GETPROP:
      if (val.isQualifiedName()) {
        return defines.contains(val.getQualifiedName());
      }
  }
  return false;
}
 
Example 17
Source File: Closure_75_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Gets the boolean value of a node that represents a literal. This method
 * effectively emulates the <code>Boolean()</code> JavaScript cast function
 * except it return UNKNOWN for known values with side-effects, use
 * getExpressionBooleanValue if you don't care about side-effects.
 */
static TernaryValue getPureBooleanValue(Node n) {
  switch (n.getType()) {
    case Token.STRING:
      return TernaryValue.forBoolean(n.getString().length() > 0);

    case Token.NUMBER:
      return TernaryValue.forBoolean(n.getDouble() != 0);

    case Token.NOT:
      return getPureBooleanValue(n.getLastChild()).not();

    case Token.NULL:
    case Token.FALSE:
    case Token.VOID:
      return TernaryValue.FALSE;

    case Token.NAME:
      String name = n.getString();
      if ("undefined".equals(name)
          || "NaN".equals(name)) {
        // We assume here that programs don't change the value of the keyword
        // undefined to something other than the value undefined.
        return TernaryValue.FALSE;
      } else if ("Infinity".equals(name)) {
        return TernaryValue.TRUE;
      }
      break;

    case Token.TRUE:
    case Token.REGEXP:
      return TernaryValue.TRUE;

    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      if (!mayHaveSideEffects(n)) {
        return TernaryValue.TRUE;
      }
  }

  return TernaryValue.UNKNOWN;
}
 
Example 18
Source File: Closure_74_PeepholeFoldConstants_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Try to eveluate known String methods
 *    .indexOf(), .substr(), .substring()
 */
private Node tryFoldKnownStringMethods(Node subtree) {
  Preconditions.checkArgument(subtree.getType() == Token.CALL);

  // check if this is a call on a string method
  // then dispatch to specific folding method.
  Node callTarget = subtree.getFirstChild();
  if (callTarget == null) {
    return subtree;
  }

  if (!NodeUtil.isGet(callTarget)) {
    return subtree;
  }

  Node stringNode = callTarget.getFirstChild();
  Node functionName = stringNode.getNext();

  if ((stringNode.getType() != Token.STRING) || (
      (functionName.getType() != Token.STRING))) {
    return subtree;
  }

  String functionNameString = functionName.getString();
  Node firstArg = callTarget.getNext();
  if (firstArg == null) {
    if (functionNameString.equals("toLowerCase")) {
      subtree = tryFoldStringToLowerCase(subtree, stringNode);
    } else if (functionNameString.equals("toUpperCase")) {
      subtree = tryFoldStringToUpperCase(subtree, stringNode);
    }
    return subtree;
  } else if (NodeUtil.isImmutableValue(firstArg)) {
    if (functionNameString.equals("indexOf") ||
        functionNameString.equals("lastIndexOf")) {
      subtree = tryFoldStringIndexOf(subtree, functionNameString,
          stringNode, firstArg);
    } else if (functionNameString.equals("substr")) {
      subtree = tryFoldStringSubstr(subtree, stringNode, firstArg);
    } else if (functionNameString.equals("substring")) {
      subtree = tryFoldStringSubstring(subtree, stringNode, firstArg);
    }
  }

  return subtree;
}
 
Example 19
Source File: 1_NodeUtil.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Gets the value of a node as a String, or null if it cannot be converted.
 * When it returns a non-null String, this method effectively emulates the
 * <code>String()</code> JavaScript cast function.
 */
static String getStringValue(Node n) {
  // TODO(user): regex literals as well.
  switch (n.getType()) {
    case Token.STRING:
      return n.getString();

    case Token.NAME:
      String name = n.getString();
      if ("undefined".equals(name)
          || "Infinity".equals(name)
          || "NaN".equals(name)) {
        return name;
      }
      break;

    case Token.NUMBER:
      double value = n.getDouble();
      long longValue = (long) value;

      // Return "1" instead of "1.0"
      if (longValue == value) {
        return Long.toString(longValue);
      } else {
        return Double.toString(n.getDouble());
      }

    case Token.FALSE:
    case Token.TRUE:
    case Token.NULL:
      return Node.tokenToName(n.getType());

    case Token.VOID:
      return "undefined";

    case Token.NOT:
      TernaryValue child = getBooleanValue(n.getFirstChild());
      if (child != TernaryValue.UNKNOWN) {
        return child.toBoolean(true) ? "false" : "true"; // reversed.
      }
      break;

    case Token.ARRAYLIT:
      return arrayToString(n);

    case Token.OBJECTLIT:
      return "[object Object]";
  }
  return null;
}
 
Example 20
Source File: Closure_86_NodeUtil_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether the given value may be assigned to a define.
 *
 * @param val The value being assigned.
 * @param defines The list of names of existing defines.
 */
static boolean isValidDefineValue(Node val, Set<String> defines) {
  switch (val.getType()) {
    case Token.STRING:
    case Token.NUMBER:
    case Token.TRUE:
    case Token.FALSE:
      return true;

    // Binary operators are only valid if both children are valid.
    case Token.ADD:
    case Token.BITAND:
    case Token.BITNOT:
    case Token.BITOR:
    case Token.BITXOR:
    case Token.DIV:
    case Token.EQ:
    case Token.GE:
    case Token.GT:
    case Token.LE:
    case Token.LSH:
    case Token.LT:
    case Token.MOD:
    case Token.MUL:
    case Token.NE:
    case Token.RSH:
    case Token.SHEQ:
    case Token.SHNE:
    case Token.SUB:
    case Token.URSH:
      return isValidDefineValue(val.getFirstChild(), defines)
          && isValidDefineValue(val.getLastChild(), defines);

    // Uniary operators are valid if the child is valid.
    case Token.NOT:
    case Token.NEG:
    case Token.POS:
      return isValidDefineValue(val.getFirstChild(), defines);

    // Names are valid if and only if they are defines themselves.
    case Token.NAME:
    case Token.GETPROP:
      if (val.isQualifiedName()) {
        return defines.contains(val.getQualifiedName());
      }
  }
  return false;
}