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

The following examples show how to use com.google.javascript.rhino.Node#isNew() . 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: jKali_003_t.java    From coming with MIT License 6 votes vote down vote up
static boolean constructorCallHasSideEffects(
    Node callNode, AbstractCompiler compiler) {
  if (!callNode.isNew()) {
    throw new IllegalStateException(
        "Expected NEW node, got " + Token.name(callNode.getType()));
  }

  if (callNode.isNoSideEffectsCall()) {
    return false;
  }

  Node nameNode = callNode.getFirstChild();
  if (nameNode.isName() &&
      CONSTRUCTORS_WITHOUT_SIDE_EFFECTS.contains(nameNode.getString())) {
    return false;
  }

  return true;
}
 
Example 2
Source File: Cardumen_00200_t.java    From coming with MIT License 6 votes vote down vote up
static boolean constructorCallHasSideEffects(
    Node callNode, AbstractCompiler compiler) {
  if (!callNode.isNew()) {
    throw new IllegalStateException(
        "Expected NEW node, got " + Token.name(callNode.getType()));
  }

  if (callNode.isNoSideEffectsCall()) {
    return false;
  }

  Node nameNode = callNode.getFirstChild();
  if (nameNode.isName() &&
      CONSTRUCTORS_WITHOUT_SIDE_EFFECTS.contains(nameNode.getString())) {
    return false;
  }

  return true;
}
 
Example 3
Source File: Cardumen_00149_s.java    From coming with MIT License 6 votes vote down vote up
static boolean constructorCallHasSideEffects(
    Node callNode, AbstractCompiler compiler) {
  if (!callNode.isNew()) {
    throw new IllegalStateException(
        "Expected NEW node, got " + Token.name(callNode.getType()));
  }

  if (callNode.isNoSideEffectsCall()) {
    return false;
  }

  Node nameNode = callNode.getFirstChild();
  if (nameNode.isName() &&
      CONSTRUCTORS_WITHOUT_SIDE_EFFECTS.contains(nameNode.getString())) {
    return false;
  }

  return true;
}
 
Example 4
Source File: Closure_10_NodeUtil_s.java    From coming with MIT License 6 votes vote down vote up
static boolean constructorCallHasSideEffects(
    Node callNode, AbstractCompiler compiler) {
  if (!callNode.isNew()) {
    throw new IllegalStateException(
        "Expected NEW node, got " + Token.name(callNode.getType()));
  }

  if (callNode.isNoSideEffectsCall()) {
    return false;
  }

  Node nameNode = callNode.getFirstChild();
  if (nameNode.isName() &&
      CONSTRUCTORS_WITHOUT_SIDE_EFFECTS.contains(nameNode.getString())) {
    return false;
  }

  return true;
}
 
Example 5
Source File: Cardumen_0014_t.java    From coming with MIT License 6 votes vote down vote up
static boolean constructorCallHasSideEffects(
    Node callNode, AbstractCompiler compiler) {
  if (!callNode.isNew()) {
    throw new IllegalStateException(
        "Expected NEW node, got " + Token.name(callNode.getType()));
  }

  if (callNode.isNoSideEffectsCall()) {
    return false;
  }

  Node nameNode = callNode.getFirstChild();
  if (nameNode.isName() &&
      CONSTRUCTORS_WITHOUT_SIDE_EFFECTS.contains(nameNode.getString())) {
    return false;
  }

  return true;
}
 
Example 6
Source File: MarkNoSideEffectCalls.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void visit(NodeTraversal traversal, Node node, Node parent) {
  if (!node.isCall() && !node.isNew()) {
    return;
  }

  Collection<Definition> definitions =
      defFinder.getDefinitionsReferencedAt(node.getFirstChild());
  if (definitions == null) {
    return;
  }

  for (Definition def : definitions) {
    Node lValue = def.getLValue();
    Preconditions.checkNotNull(lValue);
    if (!noSideEffectFunctionNames.contains(lValue) &&
        definitionTypeContainsFunctionType(def)) {
      return;
    }
  }

  node.setSideEffectFlags(Node.NO_SIDE_EFFECTS);
}
 
Example 7
Source File: Closure_30_FlowSensitiveInlineVariables_s.java    From coming with MIT License 6 votes vote down vote up
@Override
public boolean apply(Node n) {
  // When the node is null it means, we reached the implicit return
  // where the function returns (possibly without an return statement)
  if (n == null) {
    return false;
  }

  // TODO(user): We only care about calls to functions that
  // passes one of the dependent variable to a non-sideeffect free
  // function.
  if (n.isCall() && NodeUtil.functionCallHasSideEffects(n)) {
    return true;
  }

  if (n.isNew() && NodeUtil.constructorCallHasSideEffects(n)) {
    return true;
  }

  for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
    if (!ControlFlowGraph.isEnteringNewCfgNode(c) && apply(c)) {
      return true;
    }
  }
  return false;
}
 
Example 8
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
static boolean constructorCallHasSideEffects(
    Node callNode, AbstractCompiler compiler) {
  if (!callNode.isNew()) {
    throw new IllegalStateException(
        "Expected NEW node, got " + Token.name(callNode.getType()));
  }

  if (callNode.isNoSideEffectsCall()) {
    return false;
  }

  Node nameNode = callNode.getFirstChild();
  if (nameNode.isName() &&
      CONSTRUCTORS_WITHOUT_SIDE_EFFECTS.contains(nameNode.getString())) {
    return false;
  }

  return true;
}
 
Example 9
Source File: Closure_3_FlowSensitiveInlineVariables_t.java    From coming with MIT License 5 votes vote down vote up
@Override
public boolean apply(Node n) {
  // When the node is null it means, we reached the implicit return
  // where the function returns (possibly without an return statement)
  if (n == null) {
    return false;
  }

  // TODO(user): We only care about calls to functions that
  // passes one of the dependent variable to a non-side-effect free
  // function.
  if (n.isCall() && NodeUtil.functionCallHasSideEffects(n)) {
    return true;
  }

  if (n.isNew() && NodeUtil.constructorCallHasSideEffects(n)) {
    return true;
  }

  if (n.isDelProp()) {
    return true;
  }

  for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
    if (!ControlFlowGraph.isEnteringNewCfgNode(c) && apply(c)) {
      return true;
    }
  }
  return false;
}
 
Example 10
Source File: Cardumen_0088_t.java    From coming with MIT License 5 votes vote down vote up
@Override
public boolean apply(Node n) {
  // When the node is null it means, we reached the implicit return
  // where the function returns (possibly without an return statement)
  if (n == null) {
    return false;
  }

  // TODO(user): We only care about calls to functions that
  // passes one of the dependent variable to a non-side-effect free
  // function.
  if (n.isCall() && NodeUtil.functionCallHasSideEffects(n)) {
    return true;
  }

  if (n.isNew() && NodeUtil.constructorCallHasSideEffects(n)) {
    return true;
  }

  if (n.isDelProp()) {
    return true;
  }

  for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
    if ((!(n.isString())) && (apply(c))) {
      return true;
    }
  }
  return false;
}
 
Example 11
Source File: Cardumen_0015_t.java    From coming with MIT License 5 votes vote down vote up
@Override
public boolean apply(Node n) {
  // When the node is null it means, we reached the implicit return
  // where the function returns (possibly without an return statement)
  if (n == null) {
    return false;
  }

  // TODO(user): We only care about calls to functions that
  // passes one of the dependent variable to a non-side-effect free
  // function.
  if (n.isCall() && NodeUtil.functionCallHasSideEffects(n)) {
    return true;
  }

  if (n.isNew() && NodeUtil.constructorCallHasSideEffects(n)) {
    return true;
  }

  if (n.isDelProp()) {
    return true;
  }

  for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
    if ((!(n.isName())) && (apply(c))) {
      return true;
    }
  }
  return false;
}
 
Example 12
Source File: Cardumen_0015_s.java    From coming with MIT License 5 votes vote down vote up
@Override
public boolean apply(Node n) {
  // When the node is null it means, we reached the implicit return
  // where the function returns (possibly without an return statement)
  if (n == null) {
    return false;
  }

  // TODO(user): We only care about calls to functions that
  // passes one of the dependent variable to a non-side-effect free
  // function.
  if (n.isCall() && NodeUtil.functionCallHasSideEffects(n)) {
    return true;
  }

  if (n.isNew() && NodeUtil.constructorCallHasSideEffects(n)) {
    return true;
  }

  if (n.isDelProp()) {
    return true;
  }

  for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
    if (!ControlFlowGraph.isEnteringNewCfgNode(c) && apply(c)) {
      return true;
    }
  }
  return false;
}
 
Example 13
Source File: ObjectPropertyStringPreprocess.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (OBJECT_PROPERTY_STRING.equals(n.getQualifiedName())) {
    Node newName = IR.name(EXTERN_OBJECT_PROPERTY_STRING);
    newName.copyInformationFrom(n);
    parent.replaceChild(n, newName);
    compiler.reportCodeChange();
    return;
  }

  // Rewrite "new goog.testing.ObjectPropertyString(foo, 'bar')" to
  // "new goog.testing.ObjectPropertyString(window, foo.bar)" and
  // issues errors if bad arguments are encountered.
  if (!n.isNew()) {
    return;
  }

  Node objectName = n.getFirstChild();

  if (!EXTERN_OBJECT_PROPERTY_STRING.equals(
          objectName.getQualifiedName())) {
    return;
  }

  if (n.getChildCount() != 3) {
    compiler.report(t.makeError(n, INVALID_NUM_ARGUMENTS_ERROR,
        "" + n.getChildCount()));
    return;
  }

  Node firstArgument = objectName.getNext();
  if (!firstArgument.isQualifiedName()) {
    compiler.report(t.makeError(firstArgument,
        QUALIFIED_NAME_EXPECTED_ERROR,
        Token.name(firstArgument.getType())));
    return;
  }

  Node secondArgument = firstArgument.getNext();
  if (!secondArgument.isString()) {
    compiler.report(t.makeError(secondArgument,
        STRING_LITERAL_EXPECTED_ERROR,
        Token.name(secondArgument.getType())));
    return;
  }

  Node newFirstArgument = NodeUtil.newQualifiedNameNode(
      compiler.getCodingConvention(),
      compiler.getCodingConvention().getGlobalObject())
          .srcrefTree(firstArgument);

  Node newSecondArgument = NodeUtil.newQualifiedNameNode(
      compiler.getCodingConvention(),
      firstArgument.getQualifiedName() + "." +
      firstArgument.getNext().getString())
          .srcrefTree(secondArgument);

  n.replaceChild(firstArgument, newFirstArgument);
  n.replaceChild(secondArgument, newSecondArgument);

  compiler.reportCodeChange();
}
 
Example 14
Source File: InlineFunctions.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Find functions that can be inlined.
 */
private void checkNameUsage(NodeTraversal t, Node n, Node parent) {
  Preconditions.checkState(n.isName());

  if (isCandidateUsage(n)) {
    return;
  }

  // Other refs to a function name remove its candidacy for inlining
  String name = n.getString();
  FunctionState fs = fns.get(name);
  if (fs == null) {
    return;
  }

  // Unlike normal call/new parameters, references passed to
  // JSCompiler_ObjectPropertyString are not aliases of a value, but
  // a reference to the name itself, as such the value of the name is
  // unknown and can not be inlined.
  if (parent.isNew()) {
    Node target = parent.getFirstChild();
    if (target.isName() && target.getString().equals(
        ObjectPropertyStringPreprocess.EXTERN_OBJECT_PROPERTY_STRING)) {
      // This method is going to be replaced so don't inline it anywhere.
      fs.setInline(false);
    }
  }

  // If the name is being assigned to it can not be inlined.
  if (parent.isAssign() && parent.getFirstChild() == n) {
    // e.g. bar = something; <== we can't inline "bar"
    // so mark the function as uninlinable.
    // TODO(johnlenz): Should we just remove it from fns here?
    fs.setInline(false);
  } else {
    // e.g. var fn = bar; <== we can't inline "bar"
    // As this reference can't be inlined mark the function as
    // unremovable.
    fs.setRemove(false);
  }
}
 
Example 15
Source File: PureFunctionIdentifier.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(NodeTraversal traversal, Node node, Node parent) {

  if (inExterns) {
    return;
  }

  if (!NodeUtil.nodeTypeMayHaveSideEffects(node)
      && !node.isReturn()) {
    return;
  }

  if (node.isCall() || node.isNew()) {
    allFunctionCalls.add(node);
  }

  Node enclosingFunction = traversal.getEnclosingFunction();
  if (enclosingFunction != null) {
    FunctionInformation sideEffectInfo =
        functionSideEffectMap.get(enclosingFunction);
    Preconditions.checkNotNull(sideEffectInfo);

    if (NodeUtil.isAssignmentOp(node)) {
      visitAssignmentOrUnaryOperator(
          sideEffectInfo, traversal.getScope(),
          node, node.getFirstChild(), node.getLastChild());
    } else {
      switch(node.getType()) {
        case Token.CALL:
        case Token.NEW:
          visitCall(sideEffectInfo, node);
          break;
        case Token.DELPROP:
        case Token.DEC:
        case Token.INC:
          visitAssignmentOrUnaryOperator(
              sideEffectInfo, traversal.getScope(),
              node, node.getFirstChild(), null);
          break;
        case Token.NAME:
          // Variable definition are not side effects.
          // Just check that the name appears in the context of a
          // variable declaration.
          Preconditions.checkArgument(
              NodeUtil.isVarDeclaration(node));
          Node value = node.getFirstChild();
          // Assignment to local, if the value isn't a safe local value,
          // new object creation or literal or known primitive result
          // value, add it to the local blacklist.
          if (value != null && !NodeUtil.evaluatesToLocalValue(value)) {
            Scope scope = traversal.getScope();
            Var var = scope.getVar(node.getString());
            sideEffectInfo.blacklistLocal(var);
          }
          break;
        case Token.THROW:
          visitThrow(sideEffectInfo);
          break;
        case Token.RETURN:
          if (node.hasChildren()
              && !NodeUtil.evaluatesToLocalValue(node.getFirstChild())) {
            sideEffectInfo.setTaintsReturn();
          }
          break;
        default:
          throw new IllegalArgumentException(
              "Unhandled side effect node type " +
              Token.name(node.getType()));
      }
    }
  }
}
 
Example 16
Source File: Cardumen_0087_s.java    From coming with MIT License 2 votes vote down vote up
/**
 * @param node A node
 * @return Whether the call is a NEW or CALL node.
 */
static boolean isCallOrNew(Node node) {
  return node.isCall() || node.isNew();
}
 
Example 17
Source File: CheckAccessControls.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Whether the given access of a private constructor is legal.
 *
 * For example,
 * new PrivateCtor_(); // not legal
 * PrivateCtor_.newInstance(); // legal
 * x instanceof PrivateCtor_ // legal
 *
 * This is a weird special case, because our visibility system is inherited
 * from Java, and JavaScript has no distinction between classes and
 * constructors like Java does.
 *
 * We may want to revisit this if we decide to make the restrictions tighter.
 */
private static boolean isValidPrivateConstructorAccess(Node parent) {
  return !parent.isNew();
}
 
Example 18
Source File: Cardumen_0087_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * @param node A node
 * @return Whether the call is a NEW or CALL node.
 */
static boolean isCallOrNew(Node node) {
  return node.isCall() || node.isNew();
}
 
Example 19
Source File: Cardumen_0014_s.java    From coming with MIT License 2 votes vote down vote up
/**
 * @param node A node
 * @return Whether the call is a NEW or CALL node.
 */
static boolean isCallOrNew(Node node) {
  return node.isCall() || node.isNew();
}
 
Example 20
Source File: Cardumen_00200_s.java    From coming with MIT License 2 votes vote down vote up
/**
 * @param node A node
 * @return Whether the call is a NEW or CALL node.
 */
static boolean isCallOrNew(Node node) {
  return node.isCall() || node.isNew();
}