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

The following examples show how to use com.google.javascript.rhino.Node#isExprResult() . 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: Nopol2017_0010_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Handles a candidate definition for a goog.provided name.
 */
private void handleCandidateProvideDefinition(
    NodeTraversal t, Node n, Node parent) {
  if (t.inGlobalScope()) {
    String name = null;
    if (n.isName() && parent.isVar()) {
      name = n.getString();
    } else if (n.isAssign() &&
        parent.isExprResult()) {
      name = n.getFirstChild().getQualifiedName();
    }

    if (name != null) {
      if (parent.getBooleanProp(Node.IS_NAMESPACE)) {
        processProvideFromPreviousPass(t, name, parent);
      } else {
        ProvidedName pn = providedNames.get(name);
        if (pn != null) {
          pn.addDefinition(parent, t.getModule());
        }
      }
    }
  }
}
 
Example 2
Source File: Cardumen_00151_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Merge a list of nodes into a single expression.  The value of the
 * new expression is determined by the last expression in the list.
 */
private Node collapseReplacements(List<Node> replacements) {
  Node expr = null;
  for (Node rep : replacements) {
    if (rep.isExprResult()) {
      rep = rep.getFirstChild();
      rep.detachFromParent();
    }

    if (expr == null) {
      expr = rep;
    } else {
      expr = IR.comma(expr, rep);
    }
  }

  return expr;
}
 
Example 3
Source File: ImportBasedMapBuilder.java    From clutz with MIT License 6 votes vote down vote up
/** Matches `exports = {foo, bar};` */
protected boolean isObjectLiteralExport(Node statement) {
  if (!statement.isExprResult()) {
    return false;
  }

  if (!statement.getFirstChild().isAssign()) {
    return false;
  }

  if (!statement.getFirstFirstChild().isName()) {
    return false;
  }

  if (!statement.getFirstChild().getSecondChild().isObjectLit()) {
    return false;
  }

  return statement.getFirstFirstChild().getString().equals("exports");
}
 
Example 4
Source File: Cardumen_0092_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Merge a list of nodes into a single expression.  The value of the
 * new expression is determined by the last expression in the list.
 */
private Node collapseReplacements(List<Node> replacements) {
  Node expr = null;
  for (Node rep : replacements) {
    if (rep.isExprResult()) {
      rep = rep.getFirstChild();
      rep.detachFromParent();
    }

    if (expr == null) {
      expr = rep;
    } else {
      expr = IR.comma(expr, rep);
    }
  }

  return expr;
}
 
Example 5
Source File: CompilerInput.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
void visitSubtree(Node n, Node parent) {
  if (n.isCall()) {
    String require =
        codingConvention.extractClassNameIfRequire(n, parent);
    if (require != null) {
      requires.add(require);
    }

    String provide =
        codingConvention.extractClassNameIfProvide(n, parent);
    if (provide != null) {
      provides.add(provide);
    }
    return;
  } else if (parent != null &&
      !parent.isExprResult() &&
      !parent.isScript()) {
    return;
  }

  for (Node child = n.getFirstChild();
       child != null; child = child.getNext()) {
    visitSubtree(child, n);
  }
}
 
Example 6
Source File: Closure_113_ProcessClosurePrimitives_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Handles a candidate definition for a goog.provided name.
 */
private void handleCandidateProvideDefinition(
    NodeTraversal t, Node n, Node parent) {
  if (t.inGlobalScope()) {
    String name = null;
    if (n.isName() && parent.isVar()) {
      name = n.getString();
    } else if (n.isAssign() &&
        parent.isExprResult()) {
      name = n.getFirstChild().getQualifiedName();
    }

    if (name != null) {
      if (parent.getBooleanProp(Node.IS_NAMESPACE)) {
        processProvideFromPreviousPass(t, name, parent);
      } else {
        ProvidedName pn = providedNames.get(name);
        if (pn != null) {
          pn.addDefinition(parent, t.getModule());
        }
      }
    }
  }
}
 
Example 7
Source File: Cardumen_00203_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replace the current assign with its right hand side.
 */
void remove() {
  Node parent = assignNode.getParent();
  if (mayHaveSecondarySideEffects) {
    Node replacement = assignNode.getLastChild().detachFromParent();

    // Aggregate any expressions in GETELEMs.
    for (Node current = assignNode.getFirstChild();
         !current.isName();
         current = current.getFirstChild()) {
      if (current.isGetElem()) {
        replacement = IR.comma(
            current.getLastChild().detachFromParent(), replacement);
        replacement.copyInformationFrom(current);
      }
    }

    parent.replaceChild(assignNode, replacement);
  } else {
    Node gramps = parent.getParent();
    if (parent.isExprResult()) {
      gramps.removeChild(parent);
    } else {
      parent.replaceChild(assignNode,
          assignNode.getLastChild().detachFromParent());
    }
  }
}
 
Example 8
Source File: Cardumen_00151_t.java    From coming with MIT License 5 votes vote down vote up
@Override public void remove() {
  Node gramps = parent.getParent();
  if (gramps.isExprResult()) {
    // name.prototype.foo = function() { ... };
    changeProxy.removeChild(gramps.getParent(), gramps);
  } else {
    // ... name.prototype.foo = function() { ... } ...
    changeProxy.replaceWith(gramps, parent,
                            parent.getLastChild().cloneTree());
  }
}
 
Example 9
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
private Node trySplitComma(Node n) {
  if (late) {
    return n;
  }
  Node parent = n.getParent();
  Node left = n.getFirstChild();
  Node right = n.getLastChild();

  if (parent.isExprResult()
      && !parent.getParent().isLabel()) {
    // split comma
    n.detachChildren();
    // Replace the original expression with the left operand.
    parent.replaceChild(n, left);
    // Add the right expression afterward.
    Node newStatement = IR.exprResult(right);
    newStatement.copyInformationFrom(n);

    //This modifies outside the subtree, which is not
    //desirable in a peephole optimization.
    parent.getParent().addChildAfter(newStatement, parent);
    reportCodeChange();
    return left;
  } else {
    return n;
  }
}
 
Example 10
Source File: Cardumen_0021_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replace the current assign with its right hand side.
 */
void remove() {
  Node parent = assignNode.getParent();
  if (mayHaveSecondarySideEffects) {
    Node replacement = assignNode.getLastChild().detachFromParent();

    // Aggregate any expressions in GETELEMs.
    for (Node current = assignNode.getFirstChild();
         !current.isName();
         current = current.getFirstChild()) {
      if (current.isGetElem()) {
        replacement = IR.comma(
            current.getLastChild().detachFromParent(), replacement);
        replacement.copyInformationFrom(current);
      }
    }

    parent.replaceChild(assignNode, replacement);
  } else {
    Node gramps = parent.getParent();
    if (parent.isExprResult()) {
      gramps.removeChild(parent);
    } else {
      parent.replaceChild(assignNode,
          assignNode.getLastChild().detachFromParent());
    }
  }
}
 
Example 11
Source File: Cardumen_00151_t.java    From coming with MIT License 5 votes vote down vote up
@Override
public void remove() {
  // Setters have VAR, FUNCTION, or ASSIGN parent nodes. CALL parent
  // nodes are global refs, and are handled later in this function.
  Node containingNode = parent.getParent();
  switch (parent.getType()) {
    case Token.VAR:
      Preconditions.checkState(parent.hasOneChild());
      replaceWithRhs(containingNode, parent);
      break;
    case Token.FUNCTION:
      replaceWithRhs(containingNode, parent);
      break;
    case Token.ASSIGN:
      if (containingNode.isExprResult()) {
        replaceWithRhs(containingNode.getParent(), containingNode);
      } else {
        replaceWithRhs(containingNode, parent);
      }
      break;
    case Token.OBJECTLIT:
      // TODO(nicksantos): Come up with a way to remove this.
      // If we remove object lit keys, then we will need to also
      // create dependency scopes for them.
      break;
  }
}
 
Example 12
Source File: StripCode.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Use a while loop to get up out of any nested calls. For example,
 * if we have just detected that we need to remove the a.b() call
 * in a.b().c().d(), we'll have to remove all of the calls, and it
 * will take a few iterations through this loop to get up to d().
 */
void replaceHighestNestedCallWithNull(Node node, Node parent) {
  Node ancestor = parent;
  Node ancestorChild = node;
  while (true) {
    if (ancestor.getFirstChild() != ancestorChild) {
      replaceWithNull(ancestorChild, ancestor);
      break;
    }
    if (ancestor.isExprResult()) {
      // Remove the entire expression statement.
      Node ancParent = ancestor.getParent();
      replaceWithEmpty(ancestor, ancParent);
      break;
    }
    int type = ancestor.getType();
    if (type != Token.GETPROP &&
        type != Token.GETELEM &&
        type != Token.CALL) {
      replaceWithNull(ancestorChild, ancestor);
      break;
    }
    ancestorChild = ancestor;
    ancestor = ancestor.getParent();
  }
  compiler.reportCodeChange();
}
 
Example 13
Source File: Closure_40_NameAnalyzer_s.java    From coming with MIT License 5 votes vote down vote up
@Override public void remove() {
  Node gramps = parent.getParent();
  if (gramps.isExprResult()) {
    // name.prototype.foo = function() { ... };
    changeProxy.removeChild(gramps.getParent(), gramps);
  } else {
    // ... name.prototype.foo = function() { ... } ...
    changeProxy.replaceWith(gramps, parent,
                            parent.getLastChild().cloneTree());
  }
}
 
Example 14
Source File: Closure_45_RemoveUnusedVars_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replace the current assign with its right hand side.
 */
void remove() {
  Node parent = assignNode.getParent();
  if (mayHaveSecondarySideEffects) {
    Node replacement = assignNode.getLastChild().detachFromParent();

    // Aggregate any expressions in GETELEMs.
    for (Node current = assignNode.getFirstChild();
         !current.isName();
         current = current.getFirstChild()) {
      if (current.isGetElem()) {
        replacement = IR.comma(
            current.getLastChild().detachFromParent(), replacement);
        replacement.copyInformationFrom(current);
      }
    }

    parent.replaceChild(assignNode, replacement);
  } else {
    Node gramps = parent.getParent();
    if (parent.isExprResult()) {
      gramps.removeChild(parent);
    } else {
      parent.replaceChild(assignNode,
          assignNode.getLastChild().detachFromParent());
    }
  }
}
 
Example 15
Source File: Nopol2017_0027_t.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  inputId = t.getInputId();
  attachLiteralTypes(t, n);

  switch (n.getType()) {
    case Token.CALL:
      checkForClassDefiningCalls(t, n, parent);
      checkForCallingConventionDefiningCalls(n, delegateCallingConventions);
      break;

    case Token.FUNCTION:
      if (t.getInput() == null || !t.getInput().isExtern()) {
        nonExternFunctions.add(n);
      }

      // Hoisted functions are handled during pre-traversal.
      if (!NodeUtil.isHoistedFunctionDeclaration(n)) {
        defineFunctionLiteral(n, parent);
      }
      break;

    case Token.ASSIGN:
      // Handle initialization of properties.
      Node firstChild = n.getFirstChild();
      if (firstChild.isGetProp() &&
          firstChild.isQualifiedName()) {
        maybeDeclareQualifiedName(t, n.getJSDocInfo(),
            firstChild, n, firstChild.getNext());
      }
      break;

    case Token.CATCH:
      defineCatch(n, parent);
      break;

    case Token.VAR:
      defineVar(n, parent);
      break;

    case Token.GETPROP:
      // Handle stubbed properties.
      if (parent.isExprResult() &&
          n.isQualifiedName()) {
        maybeDeclareQualifiedName(t, n.getJSDocInfo(), n, parent, null);
      }
      break;
  }

  // Analyze any @lends object literals in this statement.
  if (n.getParent() != null && NodeUtil.isStatement(n) &&
      lentObjectLiterals != null) {
    for (Node objLit : lentObjectLiterals) {
      defineObjectLiteral(objLit);
    }
    lentObjectLiterals.clear();
  }
}
 
Example 16
Source File: Cardumen_00248_t.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  // VOID nodes appear when there are extra semicolons at the BLOCK level.
  // I've been unable to think of any cases where this indicates a bug,
  // and apparently some people like keeping these semicolons around,
  // so we'll allow it.
  if (n.isEmpty() ||
      n.isComma()) {
    return;
  }

  if (parent == null) {
    return;
  }

  // Do not try to remove a block or an expr result. We already handle
  // these cases when we visit the child, and the peephole passes will
  // fix up the tree in more clever ways when these are removed.
  if (n.isExprResult()) {
    return;
  }

  // This no-op statement was there so that JSDoc information could
  // be attached to the name. This check should not complain about it.
  if (n.isQualifiedName() && n.getJSDocInfo() != null) {
    return;
  }

  boolean isResultUsed = NodeUtil.isExpressionResultUsed(n);
  boolean isSimpleOp = NodeUtil.isSimpleOperatorType(n.getType());
  if (parent.getType() == Token.COMMA) {
    if (isResultUsed) {
      return;
    }
    if ((((!isSimpleOp) && (n.isGetProp())) && (parent.isAssign())) && ("prototype".equals(n.getLastChild().getString()))) {
      for (Node an : parent.getAncestors()) {
        int ancestorType = an.getType();
        if (ancestorType == Token.COMMA) continue;
        if (ancestorType != Token.EXPR_RESULT && ancestorType != Token.BLOCK) return;
        else break;
      }
    }
  } else if (parent.getType() != Token.EXPR_RESULT && parent.getType() != Token.BLOCK) {
    if (! (parent.getType() == Token.FOR && parent.getChildCount() == 4 && (n == parent.getFirstChild() || n == parent.getFirstChild().getNext().getNext()))) {
      return;
    }
  }
  if (
      (isSimpleOp || !NodeUtil.mayHaveSideEffects(n, t.getCompiler()))) {
    String msg = "This code lacks side-effects. Is there a bug?";
    if (n.isString()) {
      msg = "Is there a missing '+' on the previous line?";
    } else if (isSimpleOp) {
      msg = "The result of the '" + Token.name(n.getType()).toLowerCase() +
          "' operator is not being used.";
    }

    t.getCompiler().report(
        t.makeError(n, level, USELESS_CODE_ERROR, msg));
    // TODO(johnlenz): determine if it is necessary to
    // try to protect side-effect free statements as well.
    if (!NodeUtil.isStatement(n)) {
      problemNodes.add(n);
    }
  }
}
 
Example 17
Source File: jMutRepair_0048_s.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  // VOID nodes appear when there are extra semicolons at the BLOCK level.
  // I've been unable to think of any cases where this indicates a bug,
  // and apparently some people like keeping these semicolons around,
  // so we'll allow it.
  if (n.isEmpty() ||
      n.isComma()) {
    return;
  }

  if (parent == null) {
    return;
  }

  // Do not try to remove a block or an expr result. We already handle
  // these cases when we visit the child, and the peephole passes will
  // fix up the tree in more clever ways when these are removed.
  if (parent.getType() == Token.COMMA) {
    Node gramps = parent.getParent();
    if (gramps.isCall() && parent == gramps.getFirstChild()) {
      if (n == parent.getFirstChild() && parent.getChildCount() == 2 && n.getNext().isName() && "eval".equals(n.getNext().getString())) {
    return;
      }
  }

  // This no-op statement was there so that JSDoc information could
  // be attached to the name. This check should not complain about it.
    if (n == parent.getLastChild()) {
      for (Node an : parent.getAncestors()) {
        int ancestorType = an.getType();
        if (ancestorType == Token.COMMA)
          continue;
        if (ancestorType != Token.EXPR_RESULT && ancestorType != Token.BLOCK)
          return;
        else
          break;
      }
    }
  } else if (parent.getType() != Token.EXPR_RESULT && parent.getType() != Token.BLOCK) {
    if (parent.getType() == Token.FOR && parent.getChildCount() == 4 && (n == parent.getFirstChild() ||
         n == parent.getFirstChild().getNext().getNext())) {
    } else {
    return;
    }
  }

  boolean isResultUsed = NodeUtil.isExpressionResultUsed(n);
  boolean isSimpleOp = NodeUtil.isSimpleOperatorType(n.getType());
  if (!isResultUsed &&
      (isSimpleOp || !NodeUtil.mayHaveSideEffects(n, t.getCompiler()))) {
    if (n.isQualifiedName() && n.getJSDocInfo() != null) {
      return;
    } else if (n.isExprResult()) {
      return;
    }
    String msg = "This code lacks side-effects. Is there a bug?";
    if (n.isString()) {
      msg = "Is there a missing '+' on the previous line?";
    } else if (isSimpleOp) {
      msg = "The result of the '" + Token.name(n.getType()).toLowerCase() +
          "' operator is not being used.";
    }

    t.getCompiler().report(
        t.makeError(n, level, USELESS_CODE_ERROR, msg));
    // TODO(johnlenz): determine if it is necessary to
    // try to protect side-effect free statements as well.
    if (!NodeUtil.isStatement(n)) {
      problemNodes.add(n);
    }
  }
}
 
Example 18
Source File: Cardumen_00149_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Is this node a call expression statement?
 *
 * @param n The node
 * @return True if {@code n} is EXPR_RESULT and {@code n}'s
 *     first child is CALL
 */
static boolean isExprCall(Node n) {
  return n.isExprResult()
      && n.getFirstChild().isCall();
}
 
Example 19
Source File: Cardumen_0014_s.java    From coming with MIT License 2 votes vote down vote up
/**
 * Is this node an assignment expression statement?
 *
 * @param n The node
 * @return True if {@code n} is EXPR_RESULT and {@code n}'s
 *     first child is ASSIGN
 */
static boolean isExprAssign(Node n) {
  return n.isExprResult()
      && n.getFirstChild().isAssign();
}
 
Example 20
Source File: Closure_10_NodeUtil_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Is this node an assignment expression statement?
 *
 * @param n The node
 * @return True if {@code n} is EXPR_RESULT and {@code n}'s
 *     first child is ASSIGN
 */
static boolean isExprAssign(Node n) {
  return n.isExprResult()
      && n.getFirstChild().isAssign();
}