Java Code Examples for com.google.javascript.rhino.jstype.TernaryValue#toBoolean()

The following examples show how to use com.google.javascript.rhino.jstype.TernaryValue#toBoolean() . 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_78_PeepholeFoldConstants_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Try to fold a AND/OR node.
 */
private Node tryFoldAndOr(Node n, Node left, Node right) {
  Node parent = n.getParent();

  Node result = null;

  int type = n.getType();

  TernaryValue leftVal = NodeUtil.getBooleanValue(left);

  if (leftVal != TernaryValue.UNKNOWN) {
    boolean lval = leftVal.toBoolean(true);

    // (TRUE || x) => TRUE (also, (3 || x) => 3)
    // (FALSE && x) => FALSE
    if (lval && type == Token.OR ||
        !lval && type == Token.AND) {
      result = left;

    } else {
      // (FALSE || x) => x
      // (TRUE && x) => x
      result = right;
    }
  }

  // Note: Right hand side folding is handled by
  // PeepholeSubstituteAlternateSyntax#tryMinimizeCondition

  if (result != null) {
    // Fold it!
    n.removeChild(result);
    parent.replaceChild(n, result);
    reportCodeChange();

    return result;
  } else {
    return n;
  }
}
 
Example 2
Source File: Closure_78_PeepholeFoldConstants_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Try to fold a AND/OR node.
 */
private Node tryFoldAndOr(Node n, Node left, Node right) {
  Node parent = n.getParent();

  Node result = null;

  int type = n.getType();

  TernaryValue leftVal = NodeUtil.getBooleanValue(left);

  if (leftVal != TernaryValue.UNKNOWN) {
    boolean lval = leftVal.toBoolean(true);

    // (TRUE || x) => TRUE (also, (3 || x) => 3)
    // (FALSE && x) => FALSE
    if (lval && type == Token.OR ||
        !lval && type == Token.AND) {
      result = left;

    } else {
      // (FALSE || x) => x
      // (TRUE && x) => x
      result = right;
    }
  }

  // Note: Right hand side folding is handled by
  // PeepholeSubstituteAlternateSyntax#tryMinimizeCondition

  if (result != null) {
    // Fold it!
    n.removeChild(result);
    parent.replaceChild(n, result);
    reportCodeChange();

    return result;
  } else {
    return n;
  }
}
 
Example 3
Source File: Closure_74_PeepholeFoldConstants_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Try to fold a AND/OR node.
 */
private Node tryFoldAndOr(Node n, Node left, Node right) {
  Node parent = n.getParent();

  Node result = null;

  int type = n.getType();

  TernaryValue leftVal = NodeUtil.getImpureBooleanValue(left);

  if (leftVal != TernaryValue.UNKNOWN) {
    boolean lval = leftVal.toBoolean(true);

    // (TRUE || x) => TRUE (also, (3 || x) => 3)
    // (FALSE && x) => FALSE
    if (lval && type == Token.OR ||
        !lval && type == Token.AND) {
      result = left;

    } else if (!mayHaveSideEffects(left)) {
      // (FALSE || x) => x
      // (TRUE && x) => x
      result = right;
    }
  }

  // Note: Right hand side folding is handled by
  // PeepholeSubstituteAlternateSyntax#tryMinimizeCondition

  if (result != null) {
    // Fold it!
    n.removeChild(result);
    parent.replaceChild(n, result);
    reportCodeChange();

    return result;
  } else {
    return n;
  }
}
 
Example 4
Source File: Closure_23_PeepholeFoldConstants_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Try to fold a AND/OR node.
 */
private Node tryFoldAndOr(Node n, Node left, Node right) {
  Node parent = n.getParent();

  Node result = null;

  int type = n.getType();

  TernaryValue leftVal = NodeUtil.getImpureBooleanValue(left);

  if (leftVal != TernaryValue.UNKNOWN) {
    boolean lval = leftVal.toBoolean(true);

    // (TRUE || x) => TRUE (also, (3 || x) => 3)
    // (FALSE && x) => FALSE
    if (lval && type == Token.OR ||
        !lval && type == Token.AND) {
      result = left;

    } else if (!mayHaveSideEffects(left)) {
      // (FALSE || x) => x
      // (TRUE && x) => x
      result = right;
    }
  }

  // Note: Right hand side folding is handled by
  // PeepholeSubstituteAlternateSyntax#tryMinimizeCondition

  if (result != null) {
    // Fold it!
    n.removeChild(result);
    parent.replaceChild(n, result);
    reportCodeChange();

    return result;
  } else {
    return n;
  }
}
 
Example 5
Source File: Cardumen_00200_t.java    From coming with MIT License 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:
    case Token.STRING_KEY:
      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:
      return getStringValue(n.getDouble());

    case Token.FALSE:
      return "false";

    case Token.TRUE:
      return "true";

    case Token.NULL:
      return "null";

    case Token.VOID:
      return "undefined";

    case Token.NOT:
      TernaryValue child = getPureBooleanValue(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 6
Source File: Closure_75_NodeUtil_t.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().getType() == Token.NAME
          && 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 7
Source File: Closure_61_NodeUtil_s.java    From coming with MIT License 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:
      return getStringValue(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 = getPureBooleanValue(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 8
Source File: Cardumen_00149_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 9
Source File: Closure_80_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().getType() == Token.NAME
          && n.getFirstChild().getString().equals("Infinity")) {
        return Double.NEGATIVE_INFINITY;
      }
      return null;

    case Token.NOT:
      TernaryValue child = getBooleanValue(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 10
Source File: Closure_97_PeepholeFoldConstants_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Try to fold a AND/OR node.
 */
private Node tryFoldAndOr(Node n, Node left, Node right) {
  Node parent = n.getParent();

  Node result = null;

  int type = n.getType();

  TernaryValue leftVal = NodeUtil.getBooleanValue(left);

  if (leftVal != TernaryValue.UNKNOWN) {
    boolean lval = leftVal.toBoolean(true);

    // (TRUE || x) => TRUE (also, (3 || x) => 3)
    // (FALSE && x) => FALSE
    if (lval && type == Token.OR ||
        !lval && type == Token.AND) {
      result = left;

    } else {
      // (FALSE || x) => x
      // (TRUE && x) => x
      result = right;
    }
  } else {
    TernaryValue rightVal = NodeUtil.getBooleanValue(right);
    if (rightVal != TernaryValue.UNKNOWN) {

    // Note: We cannot always fold when the constant is on the
    // right, because the typed value of the expression will depend
    // on the type of the constant on the right, even if the boolean
    // equivalent of the value does not. Specifically, in "a = x ||
    // 0", a will be numeric 0 if x is undefined (and hence is
    // e.g. a valid array index). However, it is safe to fold
    // e.g. "if (x || true)" because 'if' doesn't care if the
    // expression is 'true' or '3'.
    int pt = parent.getType();
    if (pt == Token.IF || pt == Token.WHILE || pt == Token.DO ||
        (pt == Token.FOR && NodeUtil.getConditionExpression(parent) == n) ||
        (pt == Token.HOOK && parent.getFirstChild() == n)) {
      boolean rval = rightVal.toBoolean(true);

      // (x || FALSE) => x
      // (x && TRUE) => x
      if (type == Token.OR && !rval ||
          type == Token.AND && rval) {
        result = left;
      } else {
        // If x has no side-effects:
        //   (x || TRUE) => TRUE
        //   (x && FALSE) => FALSE
        if (!NodeUtil.mayHaveSideEffects(left)) {
          result = right;
        }
      }
      }
    }
  }

  // Note: The parser parses 'x && FALSE && y' as 'x && (FALSE && y)', so
  // there is not much need to worry about const values on left's
  // right child.

  if (result != null) {
    // Fold it!
    n.removeChild(result);
    parent.replaceChild(n, result);
    reportCodeChange();

    return result;
  } else {
    return n;
  }
}
 
Example 11
Source File: jMutRepair_003_t.java    From coming with MIT License 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:
    case Token.STRING_KEY:
      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:
      return getStringValue(n.getDouble());

    case Token.FALSE:
      return "false";

    case Token.TRUE:
      return "true";

    case Token.NULL:
      return "null";

    case Token.VOID:
      return "undefined";

    case Token.NOT:
      TernaryValue child = getPureBooleanValue(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 12
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 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: 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 14
Source File: Cardumen_0014_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 15
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 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:
    case Token.STRING_KEY:
      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:
      return getStringValue(n.getDouble());

    case Token.FALSE:
      return "false";

    case Token.TRUE:
      return "true";

    case Token.NULL:
      return "null";

    case Token.VOID:
      return "undefined";

    case Token.NOT:
      TernaryValue child = getPureBooleanValue(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 16
Source File: Cardumen_0087_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 17
Source File: Closure_97_PeepholeFoldConstants_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Try to fold a AND/OR node.
 */
private Node tryFoldAndOr(Node n, Node left, Node right) {
  Node parent = n.getParent();

  Node result = null;

  int type = n.getType();

  TernaryValue leftVal = NodeUtil.getBooleanValue(left);

  if (leftVal != TernaryValue.UNKNOWN) {
    boolean lval = leftVal.toBoolean(true);

    // (TRUE || x) => TRUE (also, (3 || x) => 3)
    // (FALSE && x) => FALSE
    if (lval && type == Token.OR ||
        !lval && type == Token.AND) {
      result = left;

    } else {
      // (FALSE || x) => x
      // (TRUE && x) => x
      result = right;
    }
  } else {
    TernaryValue rightVal = NodeUtil.getBooleanValue(right);
    if (rightVal != TernaryValue.UNKNOWN) {

    // Note: We cannot always fold when the constant is on the
    // right, because the typed value of the expression will depend
    // on the type of the constant on the right, even if the boolean
    // equivalent of the value does not. Specifically, in "a = x ||
    // 0", a will be numeric 0 if x is undefined (and hence is
    // e.g. a valid array index). However, it is safe to fold
    // e.g. "if (x || true)" because 'if' doesn't care if the
    // expression is 'true' or '3'.
    int pt = parent.getType();
    if (pt == Token.IF || pt == Token.WHILE || pt == Token.DO ||
        (pt == Token.FOR && NodeUtil.getConditionExpression(parent) == n) ||
        (pt == Token.HOOK && parent.getFirstChild() == n)) {
      boolean rval = rightVal.toBoolean(true);

      // (x || FALSE) => x
      // (x && TRUE) => x
      if (type == Token.OR && !rval ||
          type == Token.AND && rval) {
        result = left;
      } else {
        // If x has no side-effects:
        //   (x || TRUE) => TRUE
        //   (x && FALSE) => FALSE
        if (!NodeUtil.mayHaveSideEffects(left)) {
          result = right;
        }
      }
      }
    }
  }

  // Note: The parser parses 'x && FALSE && y' as 'x && (FALSE && y)', so
  // there is not much need to worry about const values on left's
  // right child.

  if (result != null) {
    // Fold it!
    n.removeChild(result);
    parent.replaceChild(n, result);
    reportCodeChange();

    return result;
  } else {
    return n;
  }
}
 
Example 18
Source File: Closure_60_NodeUtil_t.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().getType() == Token.NAME
          && 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 19
Source File: Closure_75_NodeUtil_t.java    From coming with MIT License 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 = getPureBooleanValue(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: jMutRepair_003_s.java    From coming with MIT License 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:
    case Token.STRING_KEY:
      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:
      return getStringValue(n.getDouble());

    case Token.FALSE:
      return "false";

    case Token.TRUE:
      return "true";

    case Token.NULL:
      return "null";

    case Token.VOID:
      return "undefined";

    case Token.NOT:
      TernaryValue child = getPureBooleanValue(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;
}