Java Code Examples for com.google.javascript.rhino.Node#getBooleanProp()

The following examples show how to use com.google.javascript.rhino.Node#getBooleanProp() . 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_93_ProcessClosurePrimitives_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return Whether the node is namespace placeholder.
 */
private static boolean isNamespacePlaceholder(Node n) {
  if (!n.getBooleanProp(Node.IS_NAMESPACE)) {
    return false;
  }

  Node value = null;
  if (n.getType() == Token.EXPR_RESULT) {
    Node assign = n.getFirstChild();
    value = assign.getLastChild();
  } else if (n.getType() == Token.VAR) {
    Node name = n.getFirstChild();
    value = name.getFirstChild();
  }

  return value != null
    && value.getType() == Token.OBJECTLIT
    && !value.hasChildren();
}
 
Example 2
Source File: Closure_102_Normalize_s.java    From coming with MIT License 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  // Note: Constant properties annotations are not propagated.
  if (n.getType() == Token.NAME) {
    if (n.getString().isEmpty()) {
      return;
    }

    JSDocInfo info = null;
    // Find the JSDocInfo for a top level variable.
    Var var = t.getScope().getVar(n.getString());
    if (var != null) {
      info = var.getJSDocInfo();
    }

    if ((info != null && info.isConstant()) &&
        !n.getBooleanProp(Node.IS_CONSTANT_NAME)) {
      n.putBooleanProp(Node.IS_CONSTANT_NAME, true);
      if (assertOnChange) {
        String name = n.getString();
        throw new IllegalStateException(
            "Unexpected const change.\n" +
            "  name: "+ name + "\n" +
            "  gramps:" + n.getParent().getParent().toStringTree());
      }
      // Even though the AST has changed (an annotation was added),
      // the annotations are not compared so don't report the change.
      // reportCodeChange("constant annotation");
    }
  }
}
 
Example 3
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) {
  // Note: Constant properties annotations are not propagated.
  if (n.getType() == Token.NAME) {
    if (n.getString().isEmpty()) {
      return;
    }

    JSDocInfo info = null;
    // Find the JSDocInfo for a top level variable.
    Var var = t.getScope().getVar(n.getString());
    if (var != null) {
      info = var.getJSDocInfo();
    }

    boolean shouldBeConstant =
        (info != null && info.isConstant()) ||
        NodeUtil.isConstantByConvention(
            compiler.getCodingConvention(), n, parent);
    boolean isMarkedConstant = n.getBooleanProp(Node.IS_CONSTANT_NAME);
    if (shouldBeConstant && !isMarkedConstant) {
      if (assertOnChange) {
        String name = n.getString();
        throw new IllegalStateException(
            "Unexpected const change.\n" +
            "  name: "+ name + "\n" +
            "  parent:" + n.getParent().toStringTree());
      }
      n.putBooleanProp(Node.IS_CONSTANT_NAME, true);
    }
  }
}
 
Example 4
Source File: Closure_34_CodeGenerator_t.java    From coming with MIT License 5 votes vote down vote up
/** Outputs a js string, using the optimal (single/double) quote character */
private void addJsString(Node n) {
  String s = n.getString();
  boolean useSlashV = n.getBooleanProp(Node.SLASH_V);
  if (useSlashV) {
    add(jsString(n.getString(), useSlashV));
  } else {
    String cached = ESCAPED_JS_STRINGS.get(s);
    if (cached == null) {
      cached = jsString(n.getString(), useSlashV);
      ESCAPED_JS_STRINGS.put(s, cached);
    }
    add(cached);
  }
}
 
Example 5
Source File: 1_NodeUtil.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Copy any annotations that follow a named value.
 * @param source
 * @param destination
 */
static void copyNameAnnotations(Node source, Node destination) {
  if (source.getBooleanProp(Node.IS_CONSTANT_NAME)) {
    destination.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  }
}
 
Example 6
Source File: Closure_102_Normalize_s.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.getType() == Token.NAME) {
    String name = n.getString();
    if (n.getString().isEmpty()) {
      return;
    }

    boolean isConst = n.getBooleanProp(Node.IS_CONSTANT_NAME);
    if (checkUserDeclarations) {
      boolean expectedConst = false;
      if (NodeUtil.isConstantName(n)
          || compiler.getCodingConvention().isConstant(n.getString())) {
        expectedConst = true;
      } else {
        expectedConst = false;

        JSDocInfo info = null;
        Var var = t.getScope().getVar(n.getString());
        if (var != null) {
          info = var.getJSDocInfo();
        }

        if (info != null && info.isConstant()) {
          expectedConst = true;
        } else {
          expectedConst = false;
        }
      }

      if (expectedConst) {
        Preconditions.checkState(expectedConst == isConst,
            "The name " + name + " is not annotated as constant.");
      } else {
        Preconditions.checkState(expectedConst == isConst,
            "The name " + name + " should not be annotated as constant.");
      }
    }

    Boolean value = constantMap.get(name);
    if (value == null) {
      constantMap.put(name, isConst);
    } else {
      Preconditions.checkState(value.booleanValue() == isConst,
          "The name " + name + " is not consistently annotated as " +
          "constant.");
    }
  }
}
 
Example 7
Source File: Closure_80_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Copy any annotations that follow a named value.
 * @param source
 * @param destination
 */
static void copyNameAnnotations(Node source, Node destination) {
  if (source.getBooleanProp(Node.IS_CONSTANT_NAME)) {
    destination.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  }
}
 
Example 8
Source File: Closure_94_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Copy any annotations that follow a named value.
 * @param source
 * @param destination
 */
static void copyNameAnnotations(Node source, Node destination) {
  if (source.getBooleanProp(Node.IS_CONSTANT_NAME)) {
    destination.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  }
}
 
Example 9
Source File: Closure_80_NodeUtil_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * @param locals A predicate to apply to unknown local values.
 * @return Whether the node is known to be a value that is not a reference
 *     outside the expression scope.
 */
static boolean evaluatesToLocalValue(Node value, Predicate<Node> locals) {
  switch (value.getType()) {
    case Token.ASSIGN:
      // A result that is aliased by a non-local name, is the effectively the
      // same as returning a non-local name, but this doesn't matter if the
      // value is immutable.
      return NodeUtil.isImmutableValue(value.getLastChild())
          || (locals.apply(value)
              && evaluatesToLocalValue(value.getLastChild(), locals));
    case Token.COMMA:
      return evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.AND:
    case Token.OR:
      return evaluatesToLocalValue(value.getFirstChild(), locals)
         && evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.HOOK:
      return evaluatesToLocalValue(value.getFirstChild().getNext(), locals)
         && evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.INC:
    case Token.DEC:
      if (value.getBooleanProp(Node.INCRDECR_PROP)) {
        return evaluatesToLocalValue(value.getFirstChild(), locals);
      } else {
        return true;
      }
    case Token.THIS:
      return locals.apply(value);
    case Token.NAME:
      return isImmutableValue(value) || locals.apply(value);
    case Token.GETELEM:
    case Token.GETPROP:
      // There is no information about the locality of object properties.
      return locals.apply(value);
    case Token.CALL:
      return callHasLocalResult(value)
          || isToStringMethodCall(value)
          || locals.apply(value);
    case Token.NEW:
      return newHasLocalResult(value)
             || locals.apply(value);
    case Token.FUNCTION:
    case Token.REGEXP:
    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      // Literals objects with non-literal children are allowed.
      return true;
    case Token.DELPROP:
    case Token.IN:
      // TODO(johnlenz): should IN operator be included in #isSimpleOperator?
      return true;
    default:
      // Other op force a local value:
      //  x = '' + g (x is now an local string)
      //  x -= g (x is now an local number)
      if (isAssignmentOp(value)
          || isSimpleOperator(value)
          || isImmutableValue(value)) {
        return true;
      }

      throw new IllegalStateException(
          "Unexpected expression node" + value +
          "\n parent:" + value.getParent());
  }
}
 
Example 10
Source File: jMutRepair_003_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * @param locals A predicate to apply to unknown local values.
 * @return Whether the node is known to be a value that is not a reference
 *     outside the expression scope.
 */
static boolean evaluatesToLocalValue(Node value, Predicate<Node> locals) {
  switch (value.getType()) {
    case Token.ASSIGN:
      // A result that is aliased by a non-local name, is the effectively the
      // same as returning a non-local name, but this doesn't matter if the
      // value is immutable.
      return NodeUtil.isImmutableValue(value.getLastChild())
          || (locals.apply(value)
              && evaluatesToLocalValue(value.getLastChild(), locals));
    case Token.COMMA:
      return evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.AND:
    case Token.OR:
      return evaluatesToLocalValue(value.getFirstChild(), locals)
         && evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.HOOK:
      return evaluatesToLocalValue(value.getFirstChild().getNext(), locals)
         && evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.INC:
    case Token.DEC:
      if (value.getBooleanProp(Node.INCRDECR_PROP)) {
        return evaluatesToLocalValue(value.getFirstChild(), locals);
      } else {
        return true;
      }
    case Token.THIS:
      return locals.apply(value);
    case Token.NAME:
      return isImmutableValue(value) || locals.apply(value);
    case Token.GETELEM:
    case Token.GETPROP:
      // There is no information about the locality of object properties.
      return locals.apply(value);
    case Token.CALL:
      return callHasLocalResult(value)
          || isToStringMethodCall(value)
          || locals.apply(value);
    case Token.NEW:
      return newHasLocalResult(value)
             || locals.apply(value);
    case Token.FUNCTION:
    case Token.REGEXP:
    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      // Literals objects with non-literal children are allowed.
      return true;
    case Token.DELPROP:
    case Token.IN:
      // TODO(johnlenz): should IN operator be included in #isSimpleOperator?
      return true;
    default:
      // Other op force a local value:
      //  x = '' + g (x is now an local string)
      //  x -= g (x is now an local number)
      if (isAssignmentOp(value)
          || isSimpleOperator(value)
          || isImmutableValue(value)) {
        return true;
      }

      throw new IllegalStateException(
          "Unexpected expression node" + value +
          "\n parent:" + value.getParent());
  }
}
 
Example 11
Source File: Cardumen_0087_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Copy any annotations that follow a named value.
 * @param source
 * @param destination
 */
static void copyNameAnnotations(Node source, Node destination) {
  if (source.getBooleanProp(Node.IS_CONSTANT_NAME)) {
    destination.putBooleanProp(Node.IS_CONSTANT_NAME, true);
  }
}
 
Example 12
Source File: 1_CodeGenerator.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return Whether the name is an indirect eval.
 */
private boolean isIndirectEval(Node n) {
  return n.getType() == Token.NAME && "eval".equals(n.getString()) &&
      !n.getBooleanProp(Node.DIRECT_EVAL);
}
 
Example 13
Source File: Cardumen_0087_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * @param locals A predicate to apply to unknown local values.
 * @return Whether the node is known to be a value that is not a reference
 *     outside the expression scope.
 */
static boolean evaluatesToLocalValue(Node value, Predicate<Node> locals) {
  switch (value.getType()) {
    case Token.ASSIGN:
      // A result that is aliased by a non-local name, is the effectively the
      // same as returning a non-local name, but this doesn't matter if the
      // value is immutable.
      return NodeUtil.isImmutableValue(value.getLastChild())
          || (locals.apply(value)
              && evaluatesToLocalValue(value.getLastChild(), locals));
    case Token.COMMA:
      return evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.AND:
    case Token.OR:
      return evaluatesToLocalValue(value.getFirstChild(), locals)
         && evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.HOOK:
      return evaluatesToLocalValue(value.getFirstChild().getNext(), locals)
         && evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.INC:
    case Token.DEC:
      if (value.getBooleanProp(Node.INCRDECR_PROP)) {
        return evaluatesToLocalValue(value.getFirstChild(), locals);
      } else {
        return true;
      }
    case Token.THIS:
      return locals.apply(value);
    case Token.NAME:
      return isImmutableValue(value) || locals.apply(value);
    case Token.GETELEM:
    case Token.GETPROP:
      // There is no information about the locality of object properties.
      return locals.apply(value);
    case Token.CALL:
      return callHasLocalResult(value)
          || isToStringMethodCall(value)
          || locals.apply(value);
    case Token.NEW:
      return newHasLocalResult(value)
             || locals.apply(value);
    case Token.FUNCTION:
    case Token.REGEXP:
    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      // Literals objects with non-literal children are allowed.
      return true;
    case Token.DELPROP:
    case Token.IN:
      // TODO(johnlenz): should IN operator be included in #isSimpleOperator?
      return true;
    default:
      // Other op force a local value:
      //  x = '' + g (x is now an local string)
      //  x -= g (x is now an local number)
      if (isAssignmentOp(value)
          || isSimpleOperator(value)
          || isImmutableValue(value)) {
        return true;
      }

      throw new IllegalStateException(
          "Unexpected expression node" + value +
          "\n parent:" + value.getParent());
  }
}
 
Example 14
Source File: Closure_94_NodeUtil_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Returns true if a name node represents a constant variable.
 *
 * <p>Determining whether a variable is constant has three steps:
 * <ol>
 * <li>In CodingConventionAnnotator, any name that matches the
 *     {@link CodingConvention#isConstant(String)} is annotated with an
 *     IS_CONSTANT_NAME property.
 * <li>The normalize pass renames any variable with the IS_CONSTANT_NAME
 *     annotation and that is initialized to a constant value with
 *     a variable name inlucding $$constant.
 * <li>Return true here if the variable includes $$constant in its name.
 * </ol>
 *
 * @param node A NAME or STRING node
 * @return True if the variable is constant
 */
static boolean isConstantName(Node node) {
  return node.getBooleanProp(Node.IS_CONSTANT_NAME);
}
 
Example 15
Source File: Closure_86_NodeUtil_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Returns true if a name node represents a constant variable.
 *
 * <p>Determining whether a variable is constant has three steps:
 * <ol>
 * <li>In CodingConventionAnnotator, any name that matches the
 *     {@link CodingConvention#isConstant(String)} is annotated with an
 *     IS_CONSTANT_NAME property.
 * <li>The normalize pass renames any variable with the IS_CONSTANT_NAME
 *     annotation and that is initialized to a constant value with
 *     a variable name inlucding $$constant.
 * <li>Return true here if the variable includes $$constant in its name.
 * </ol>
 *
 * @param node A NAME or STRING node
 * @return True if the variable is constant
 */
static boolean isConstantName(Node node) {
  return node.getBooleanProp(Node.IS_CONSTANT_NAME);
}
 
Example 16
Source File: Closure_10_NodeUtil_s.java    From coming with MIT License 2 votes vote down vote up
/**
 * Returns true if a name node represents a constant variable.
 *
 * <p>Determining whether a variable is constant has three steps:
 * <ol>
 * <li>In CodingConventionAnnotator, any name that matches the
 *     {@link CodingConvention#isConstant(String)} is annotated with an
 *     IS_CONSTANT_NAME property.
 * <li>The normalize pass renames any variable with the IS_CONSTANT_NAME
 *     annotation and that is initialized to a constant value with
 *     a variable name including $$constant.
 * <li>Return true here if the variable includes $$constant in its name.
 * </ol>
 *
 * @param node A NAME or STRING node
 * @return True if the variable is constant
 */
static boolean isConstantName(Node node) {
  return node.getBooleanProp(Node.IS_CONSTANT_NAME);
}
 
Example 17
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns true if a name node represents a constant variable.
 *
 * <p>Determining whether a variable is constant has three steps:
 * <ol>
 * <li>In CodingConventionAnnotator, any name that matches the
 *     {@link CodingConvention#isConstant(String)} is annotated with an
 *     IS_CONSTANT_NAME property.
 * <li>The normalize pass renames any variable with the IS_CONSTANT_NAME
 *     annotation and that is initialized to a constant value with
 *     a variable name including $$constant.
 * <li>Return true here if the variable includes $$constant in its name.
 * </ol>
 *
 * @param node A NAME or STRING node
 * @return True if the variable is constant
 */
static boolean isConstantName(Node node) {
  return node.getBooleanProp(Node.IS_CONSTANT_NAME);
}
 
Example 18
Source File: jMutRepair_003_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Returns true if a name node represents a constant variable.
 *
 * <p>Determining whether a variable is constant has three steps:
 * <ol>
 * <li>In CodingConventionAnnotator, any name that matches the
 *     {@link CodingConvention#isConstant(String)} is annotated with an
 *     IS_CONSTANT_NAME property.
 * <li>The normalize pass renames any variable with the IS_CONSTANT_NAME
 *     annotation and that is initialized to a constant value with
 *     a variable name including $$constant.
 * <li>Return true here if the variable includes $$constant in its name.
 * </ol>
 *
 * @param node A NAME or STRING node
 * @return True if the variable is constant
 */
static boolean isConstantName(Node node) {
  return node.getBooleanProp(Node.IS_CONSTANT_NAME);
}
 
Example 19
Source File: Cardumen_00149_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Returns true if a name node represents a constant variable.
 *
 * <p>Determining whether a variable is constant has three steps:
 * <ol>
 * <li>In CodingConventionAnnotator, any name that matches the
 *     {@link CodingConvention#isConstant(String)} is annotated with an
 *     IS_CONSTANT_NAME property.
 * <li>The normalize pass renames any variable with the IS_CONSTANT_NAME
 *     annotation and that is initialized to a constant value with
 *     a variable name including $$constant.
 * <li>Return true here if the variable includes $$constant in its name.
 * </ol>
 *
 * @param node A NAME or STRING node
 * @return True if the variable is constant
 */
static boolean isConstantName(Node node) {
  return node.getBooleanProp(Node.IS_CONSTANT_NAME);
}
 
Example 20
Source File: jKali_003_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Returns true if a name node represents a constant variable.
 *
 * <p>Determining whether a variable is constant has three steps:
 * <ol>
 * <li>In CodingConventionAnnotator, any name that matches the
 *     {@link CodingConvention#isConstant(String)} is annotated with an
 *     IS_CONSTANT_NAME property.
 * <li>The normalize pass renames any variable with the IS_CONSTANT_NAME
 *     annotation and that is initialized to a constant value with
 *     a variable name including $$constant.
 * <li>Return true here if the variable includes $$constant in its name.
 * </ol>
 *
 * @param node A NAME or STRING node
 * @return True if the variable is constant
 */
static boolean isConstantName(Node node) {
  return node.getBooleanProp(Node.IS_CONSTANT_NAME);
}