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

The following examples show how to use com.google.javascript.rhino.Node#isCall() . 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: Cardumen_0092_s.java    From coming with MIT License 6 votes vote down vote up
private void addSimplifiedExpression(Node n, Node parent) {
  if (parent.isVar()) {
    Node value = n.getFirstChild();
    if (value != null) {
      addSimplifiedChildren(value);
    }
  } else if (n.isAssign() &&
      (parent.isExprResult() ||
       parent.isFor() ||
       parent.isReturn())) {
    for (Node child : n.children()) {
      addSimplifiedChildren(child);
    }
  } else if (n.isCall() &&
             parent.isExprResult()) {
    addSimplifiedChildren(n);
  } else {
    addAllChildren(n);
  }
}
 
Example 2
Source File: Cardumen_00151_t.java    From coming with MIT License 6 votes vote down vote up
private void addSimplifiedExpression(Node n, Node parent) {
  if (parent.isVar()) {
    Node value = n.getFirstChild();
    if (value != null) {
      addSimplifiedChildren(value);
    }
  } else if (n.isAssign() &&
      (parent.isExprResult() ||
       parent.isFor() ||
       parent.isReturn())) {
    for (Node child : n.children()) {
      addSimplifiedChildren(child);
    }
  } else if (n.isCall() &&
             parent.isExprResult()) {
    addSimplifiedChildren(n);
  } else {
    addAllChildren(n);
  }
}
 
Example 3
Source File: patch1-Closure-22-Nopol2017_patch1-Closure-22-Nopol2017_s.java    From coming with MIT License 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isCall()) {
    Node target = n.getFirstChild();
    // TODO(johnlenz): add this to the coding convention
    // so we can remove goog.reflect.sinkValue as well.
    if (target.isName() && target.getString().equals(PROTECTOR_FN)) {
      Node expr = n.getLastChild();
      n.detachChildren();
      parent.replaceChild(n, expr);
    }
  }
}
 
Example 4
Source File: JGenProg2017_0053_t.java    From coming with MIT License 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isCall()) {
    Node target = n.getFirstChild();
    // TODO(johnlenz): add this to the coding convention
    // so we can remove goog.reflect.sinkValue as well.
    if (target.isName() && target.getString().equals(PROTECTOR_FN)) {
      Node expr = n.getLastChild();
      n.detachChildren();
      parent.replaceChild(n, expr);
    }
  }
}
 
Example 5
Source File: Nopol2017_0031_t.java    From coming with MIT License 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isCall()) {
    Node target = n.getFirstChild();
    // TODO(johnlenz): add this to the coding convention
    // so we can remove goog.reflect.sinkValue as well.
    if (target.isName() && target.getString().equals(PROTECTOR_FN)) {
      Node expr = n.getLastChild();
      n.detachChildren();
      parent.replaceChild(n, expr);
    }
  }
}
 
Example 6
Source File: PureFunctionIdentifierTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isNew()) {
    if (!NodeUtil.constructorCallHasSideEffects(n)) {
      noSideEffectCalls.add(generateNameString(n.getFirstChild()));
    }
  } else if (n.isCall()) {
    if (!NodeUtil.functionCallHasSideEffects(n)) {
      noSideEffectCalls.add(generateNameString(n.getFirstChild()));
    }
    if (NodeUtil.callHasLocalResult(n)) {
      localResultCalls.add(generateNameString(n.getFirstChild()));
    }
  }
}
 
Example 7
Source File: MinerrPass.java    From ng-closure-runner with MIT License 5 votes vote down vote up
private boolean isMinerrCall(Node ast) {
  if (ast.isCall()) {
    Node nameNode = ast.getFirstChild();
    if (nameNode.isName()) {
      String name = nameNode.getString();
      return name.equals("minErr");
    }
  }
  return false;
}
 
Example 8
Source File: AliasExternals.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Logic for when a getprop can be replaced.
 * Can't alias a call to eval per ECMA-262 spec section 15.1.2.1
 * Can't be an assign -> no a.b = c;
 * Can't be inc or dec -> no a.b++; or a.b--;
 * Must be a GETPROP (NODE, A) where A is a reserved name
 * @param propNameNode Property name node
 * @param getPropNode GETPROP node
 * @param parent parent node
 * @return True if can be replaced
 */
private boolean canReplaceWithGetProp(Node propNameNode, Node getPropNode,
      Node parent) {
  boolean isCallTarget = (parent.isCall())
      && (parent.getFirstChild() == getPropNode);
  boolean isAssignTarget = NodeUtil.isAssignmentOp(parent)
      && (parent.getFirstChild() == getPropNode);
  boolean isIncOrDec = (parent.isInc()) ||
      (parent.isDec());
  return (propNameNode.isString()) && !isAssignTarget
      && (!isCallTarget || !"eval".equals(propNameNode.getString()))
      && !isIncOrDec
      && props.containsKey(propNameNode.getString());
}
 
Example 9
Source File: Cardumen_0087_t.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.isCall()) {
    Node functionIndentifyingExpression = callNode.getFirstChild();
    if (isGet(functionIndentifyingExpression)) {
      Node last = functionIndentifyingExpression.getLastChild();
      if (last != null && last.isString()) {
        String propName = last.getString();
        return (propName.equals(methodName));
      }
    }
  }
  return false;
}
 
Example 10
Source File: jKali_0022_s.java    From coming with MIT License 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isCall()) {
    Node target = n.getFirstChild();
    // TODO(johnlenz): add this to the coding convention
    // so we can remove goog.reflect.sinkValue as well.
    if (target.isName() && target.getString().equals(PROTECTOR_FN)) {
      Node expr = n.getLastChild();
      n.detachChildren();
      parent.replaceChild(n, expr);
    }
  }
}
 
Example 11
Source File: Nopol2017_0032_s.java    From coming with MIT License 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isCall()) {
    Node target = n.getFirstChild();
    // TODO(johnlenz): add this to the coding convention
    // so we can remove goog.reflect.sinkValue as well.
    if (target.isName() && target.getString().equals(PROTECTOR_FN)) {
      Node expr = n.getLastChild();
      n.detachChildren();
      parent.replaceChild(n, expr);
    }
  }
}
 
Example 12
Source File: ImportBasedMapBuilder.java    From clutz with MIT License 5 votes vote down vote up
protected static boolean isGoogModuleCall(Node statement) {
  if (!statement.isExprResult()) {
    return false;
  }

  Node expression = statement.getFirstChild();
  return expression.isCall() && expression.getFirstChild().matchesQualifiedName("goog.module");
}
 
Example 13
Source File: ExternExportsPass.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  switch (n.getType()) {

    case Token.NAME:
    case Token.GETPROP:
      String name = n.getQualifiedName();
      if (name == null) {
        return;
      }

      if (parent.isAssign() || parent.isVar()) {
        definitionMap.put(name, parent);
      }

      // Only handle function calls. This avoids assignments
      // that do not export items directly.
      if (!parent.isCall()) {
        return;
      }

      if (exportPropertyFunctionNames.contains(name)) {
        handlePropertyExport(parent);
      }

      if (exportSymbolFunctionNames.contains(name)) {
        handleSymbolExport(parent);
      }
  }
}
 
Example 14
Source File: JGenProg2017_0011_t.java    From coming with MIT License 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isCall()) {
    Node target = n.getFirstChild();
    // TODO(johnlenz): add this to the coding convention
    // so we can remove goog.reflect.sinkValue as well.
    if (target.isName() && target.getString().equals(PROTECTOR_FN)) {
      Node expr = n.getLastChild();
      n.detachChildren();
      parent.replaceChild(n, expr);
    }
  }
}
 
Example 15
Source File: jMutRepair_005_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 16
Source File: JGenProg2017_0084_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 (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;
      }
  }

  } 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 17
Source File: Closure_1_RemoveUnusedVars_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * @return Whether the definitionSite represents a function whose call
 *      signature can be modified.
 */
private boolean canChangeSignature(Node function) {
  Definition definition = getFunctionDefinition(function);
  CodingConvention convention = compiler.getCodingConvention();

  Preconditions.checkState(!definition.isExtern());

  Collection<UseSite> useSites = defFinder.getUseSites(definition);
  for (UseSite site : useSites) {
    Node parent = site.node.getParent();

    // This was a use site removed by something else before we run.
    // 1. By another pass before us which means the definition graph is
    //    no updated properly.
    // 2. By the continuations algorithm above.
    if (parent == null) {
      continue; // Ignore it.
    }

    // Ignore references within goog.inherits calls.
    if (parent.isCall() &&
        convention.getClassesDefinedByCall(parent) != null) {
      continue;
    }

    // Accessing the property directly prevents rewrite.
    if (!SimpleDefinitionFinder.isCallOrNewSite(site)) {
      if (!(parent.isGetProp() &&
          NodeUtil.isFunctionObjectCall(parent.getParent()))) {
        return false;
      }
    }

    if (NodeUtil.isFunctionObjectApply(parent)) {
      return false;
    }

    // TODO(johnlenz): support specialization

    // Multiple definitions prevent rewrite.
    // Attempt to validate the state of the simple definition finder.
    Node nameNode = site.node;
    Collection<Definition> singleSiteDefinitions =
        defFinder.getDefinitionsReferencedAt(nameNode);
    Preconditions.checkState(singleSiteDefinitions.size() == 1);
    Preconditions.checkState(singleSiteDefinitions.contains(definition));
  }

  return true;
}
 
Example 18
Source File: Closure_45_RemoveUnusedVars_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * @return Whether the definitionSite represents a function whose call
 *      signature can be modified.
 */
private boolean canChangeSignature(Node function) {
  Definition definition = getFunctionDefinition(function);
  CodingConvention convention = compiler.getCodingConvention();

  Preconditions.checkState(!definition.isExtern());

  Collection<UseSite> useSites = defFinder.getUseSites(definition);
  for (UseSite site : useSites) {
    Node parent = site.node.getParent();

    // This was a use site removed by something else before we run.
    // 1. By another pass before us which means the definition graph is
    //    no updated properly.
    // 2. By the continuations algorithm above.
    if (parent == null) {
      continue; // Ignore it.
    }

    // Ignore references within goog.inherits calls.
    if (parent.isCall() &&
        convention.getClassesDefinedByCall(parent) != null) {
      continue;
    }

    // Accessing the property directly prevents rewrite.
    if (!SimpleDefinitionFinder.isCallOrNewSite(site)) {
      if (!(parent.isGetProp() &&
          NodeUtil.isFunctionObjectCall(parent.getParent()))) {
        return false;
      }
    }

    if (NodeUtil.isFunctionObjectApply(parent)) {
      return false;
    }

    // TODO(johnlenz): support specialization

    // Multiple definitions prevent rewrite.
    // Attempt to validate the state of the simple definition finder.
    Node nameNode = site.node;
    Collection<Definition> singleSiteDefinitions =
        defFinder.getDefinitionsReferencedAt(nameNode);
    Preconditions.checkState(singleSiteDefinitions.size() == 1);
    Preconditions.checkState(singleSiteDefinitions.contains(definition));
  }

  return true;
}
 
Example 19
Source File: Closure_24_ScopedAliases_t.java    From coming with MIT License 4 votes vote down vote up
private boolean isCallToScopeMethod(Node n) {
  return n.isCall() &&
      SCOPING_METHOD_NAME.equals(n.getFirstChild().getQualifiedName());
}
 
Example 20
Source File: Cardumen_00149_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();
}