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

The following examples show how to use com.google.javascript.rhino.Node#isString() . 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_48_TypedScopeCreator_s.java    From coming with MIT License 6 votes vote down vote up
private void maybeCollectMember(NodeTraversal t,
    Node member, Node nodeWithJsDocInfo, @Nullable Node value) {
  JSDocInfo info = nodeWithJsDocInfo.getJSDocInfo();

  // Do nothing if there is no JSDoc type info, or
  // if the node is not a member expression, or
  // if the member expression is not of the form: this.someProperty.
  if (info == null ||
      member.getType() != Token.GETPROP ||
      member.getFirstChild().getType() != Token.THIS) {
    return;
  }

  member.getFirstChild().setJSType(thisType);
  JSType jsType = getDeclaredType(t.getSourceName(), info, member, value);
  Node name = member.getLastChild();
  if (jsType != null &&
      (name.isName() || name.isString())) {
    thisType.defineDeclaredProperty(
        name.getString(),
        jsType,
        member);
  }
}
 
Example 2
Source File: Closure_16_ScopedAliases_s.java    From coming with MIT License 6 votes vote down vote up
private void fixTypeNode(Node typeNode) {
  if (typeNode.isString()) {
    String name = typeNode.getString();
    int endIndex = name.indexOf('.');
    if (endIndex == -1) {
      endIndex = name.length();
    }
    String baseName = name.substring(0, endIndex);
    Var aliasVar = aliases.get(baseName);
    if (aliasVar != null) {
      Node aliasedNode = aliasVar.getInitialValue();
      aliasUsages.add(new AliasedTypeNode(typeNode, aliasedNode.getQualifiedName() + name.substring(endIndex)));
    }
  }

  for (Node child = typeNode.getFirstChild(); child != null;
       child = child.getNext()) {
    fixTypeNode(child);
  }
}
 
Example 3
Source File: Closure_111_ClosureReverseAbstractInterpreter_t.java    From coming with MIT License 6 votes vote down vote up
@Override
public FlowScope getPreciserScopeKnowingConditionOutcome(Node condition,
    FlowScope blindScope, boolean outcome) {
  if (condition.isCall() && condition.getChildCount() == 2) {
    Node callee = condition.getFirstChild();
    Node param = condition.getLastChild();
    if (callee.isGetProp() && param.isQualifiedName()) {
      JSType paramType =  getTypeIfRefinable(param, blindScope);
      Node left = callee.getFirstChild();
      Node right = callee.getLastChild();
      if (left.isName() && "goog".equals(left.getString()) &&
          right.isString()) {
        Function<TypeRestriction, JSType> restricter =
            restricters.get(right.getString());
        if (restricter != null) {
          return restrictParameter(param, paramType, blindScope, restricter,
              outcome);
        }
      }
    }
  }
  return nextPreciserScopeKnowingConditionOutcome(
      condition, blindScope, outcome);
}
 
Example 4
Source File: Closure_110_ScopedAliases_s.java    From coming with MIT License 6 votes vote down vote up
private void fixTypeNode(Node typeNode) {
  if (typeNode.isString()) {
    String name = typeNode.getString();
    int endIndex = name.indexOf('.');
    if (endIndex == -1) {
      endIndex = name.length();
    }
    String baseName = name.substring(0, endIndex);
    Var aliasVar = aliases.get(baseName);
    if (aliasVar != null) {
      aliasUsages.add(new AliasedTypeNode(aliasVar, typeNode));
    }
  }

  for (Node child = typeNode.getFirstChild(); child != null;
       child = child.getNext()) {
    fixTypeNode(child);
  }
}
 
Example 5
Source File: Closure_24_ScopedAliases_s.java    From coming with MIT License 6 votes vote down vote up
private void fixTypeNode(Node typeNode) {
  if (typeNode.isString()) {
    String name = typeNode.getString();
    int endIndex = name.indexOf('.');
    if (endIndex == -1) {
      endIndex = name.length();
    }
    String baseName = name.substring(0, endIndex);
    Var aliasVar = aliases.get(baseName);
    if (aliasVar != null) {
      Node aliasedNode = aliasVar.getInitialValue();
      aliasUsages.add(new AliasedTypeNode(typeNode,
          aliasedNode.getQualifiedName() + name.substring(endIndex)));
    }
  }

  for (Node child = typeNode.getFirstChild(); child != null;
       child = child.getNext()) {
    fixTypeNode(child);
  }
}
 
Example 6
Source File: ClosureReverseAbstractInterpreter.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public FlowScope getPreciserScopeKnowingConditionOutcome(Node condition,
    FlowScope blindScope, boolean outcome) {
  if (condition.isCall() && condition.getChildCount() == 2) {
    Node callee = condition.getFirstChild();
    Node param = condition.getLastChild();
    if (callee.isGetProp() && param.isQualifiedName()) {
      JSType paramType =  getTypeIfRefinable(param, blindScope);
      Node left = callee.getFirstChild();
      Node right = callee.getLastChild();
      if (left.isName() && "goog".equals(left.getString()) &&
          right.isString()) {
        Function<TypeRestriction, JSType> restricter =
            restricters.get(right.getString());
        if (restricter != null) {
          return restrictParameter(param, paramType, blindScope, restricter,
              outcome);
        }
      }
    }
  }
  return nextPreciserScopeKnowingConditionOutcome(
      condition, blindScope, outcome);
}
 
Example 7
Source File: Closure_113_ProcessClosurePrimitives_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Verifies that setCssNameMapping is called with the correct methods.
 *
 * @return Whether the arguments checked out okay
 */
private boolean verifySetCssNameMapping(NodeTraversal t, Node methodName,
    Node firstArg) {
  DiagnosticType diagnostic = null;
  if (firstArg == null) {
    diagnostic = NULL_ARGUMENT_ERROR;
  } else if (!firstArg.isObjectLit()) {
    diagnostic = EXPECTED_OBJECTLIT_ERROR;
  } else if (firstArg.getNext() != null) {
    Node secondArg = firstArg.getNext();
    if (!secondArg.isString()) {
      diagnostic = EXPECTED_STRING_ERROR;
    } else if (secondArg.getNext() != null) {
      diagnostic = TOO_MANY_ARGUMENTS_ERROR;
    }
  }
  if (diagnostic != null) {
    compiler.report(
        t.makeError(methodName,
            diagnostic, methodName.getQualifiedName()));
    return false;
  }
  return true;
}
 
Example 8
Source File: RenamePrototypes.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.GETPROP:
    case Token.GETELEM:
      Node dest = n.getFirstChild().getNext();
      if (dest.isString()) {
        String s = dest.getString();
        if (s.equals("prototype")) {
          processPrototypeParent(parent, t.getInput());
        } else {
          markPropertyAccessCandidate(dest, t.getInput());
        }
      }
      break;
    case Token.OBJECTLIT:
      if (!prototypeObjLits.contains(n)) {
        // Object literals have their property name/value pairs as a flat
        // list as their children. We want every other node in order to get
        // only the property names.
        for (Node child = n.getFirstChild();
             child != null;
             child = child.getNext()) {

          if (TokenStream.isJSIdentifier(child.getString())) {
            markObjLitPropertyCandidate(child, t.getInput());
          }
        }
      }
      break;
  }
}
 
Example 9
Source File: Cardumen_00200_s.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: Closure_47_SourceMap_s.java    From coming with MIT License 5 votes vote down vote up
@Override public boolean apply(Node node) {
  return node.isCall()
      || node.isNew()
      || node.isFunction()
      || node.isName()
      || NodeUtil.isGet(node)
      || NodeUtil.isObjectLitKey(node, node.getParent())
      || (node.isString() && NodeUtil.isGet(node.getParent()));
}
 
Example 11
Source File: Nodes.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
public static boolean isString(@Nullable Node n) {
  return n != null && n.isString();
}
 
Example 12
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 13
Source File: Closure_21_CheckSideEffects_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 (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 (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()))) {
      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 14
Source File: patch1-Closure-22-Nopol2017_patch1-Closure-22-Nopol2017_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 15
Source File: jMutRepair_0032_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_00108_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 17
Source File: PeepholeFoldConstants.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private Node tryFoldObjectPropAccess(Node n, Node left, Node right) {
  Preconditions.checkArgument(NodeUtil.isGet(n));

  if (!left.isObjectLit() || !right.isString()) {
    return n;
  }

  if (isAssignmentTarget(n)) {
    // If GETPROP/GETELEM is used as assignment target the object literal is
    // acting as a temporary we can't fold it here:
    //    "{a:x}.a += 1" is not "x += 1"
    return n;
  }

  // find the last definition in the object literal
  Node key = null;
  Node value = null;
  for (Node c = left.getFirstChild(); c != null; c = c.getNext()) {
    if (c.getString().equals(right.getString())) {
      switch (c.getType()) {
        case Token.SETTER_DEF:
          continue;
        case Token.GETTER_DEF:
        case Token.STRING_KEY:
          if (value != null && mayHaveSideEffects(value)) {
            // The previously found value had side-effects
            return n;
          }
          key = c;
          value = key.getFirstChild();
          break;
        default:
          throw new IllegalStateException();
      }
    } else if (mayHaveSideEffects(c.getFirstChild())) {
      // We don't handle the side-effects here as they might need a temporary
      // or need to be reordered.
      return n;
    }
  }

  // Didn't find a definition of the name in the object literal, it might
  // be coming from the Object prototype
  if (value == null) {
    return n;
  }

  if (value.isFunction() && NodeUtil.referencesThis(value)) {
    // 'this' may refer to the object we are trying to remove
    return n;
  }

  Node replacement = value.detachFromParent();
  if (key.isGetterDef()){
    replacement = IR.call(replacement);
    replacement.putBooleanProp(Node.FREE_CALL, true);
  }

  n.getParent().replaceChild(n, replacement);
  reportCodeChange();
  return n;
}
 
Example 18
Source File: jMutRepair_0040_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;
      }
  }

  // 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 19
Source File: jKali_0043_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;
      }
  }

  // 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 (true)
          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 20
Source File: PeepholeReplaceKnownMethods.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Try to evaluate known String methods
 *    .indexOf(), .substr(), .substring()
 */
private Node tryFoldKnownStringMethods(Node subtree) {
  Preconditions.checkArgument(subtree.isCall());

  // check if this is a call on a string method
  // then dispatch to specific folding method.
  Node callTarget = subtree.getFirstChild();
  if (callTarget == null) {
    return subtree;
  }

  if (!NodeUtil.isGet(callTarget)) {
    return subtree;
  }

  Node stringNode = callTarget.getFirstChild();
  Node functionName = stringNode.getNext();

  if ((!stringNode.isString()) ||
      (!functionName.isString())) {
    return subtree;
  }

  String functionNameString = functionName.getString();
  Node firstArg = callTarget.getNext();
  if (functionNameString.equals("split")) {
    subtree = tryFoldStringSplit(subtree, stringNode, firstArg);
  } else if (firstArg == null) {
    if (functionNameString.equals("toLowerCase")) {
      subtree = tryFoldStringToLowerCase(subtree, stringNode);
    } else if (functionNameString.equals("toUpperCase")) {
      subtree = tryFoldStringToUpperCase(subtree, stringNode);
    }
    return subtree;
  } else if (NodeUtil.isImmutableValue(firstArg)) {
    if (functionNameString.equals("indexOf") ||
        functionNameString.equals("lastIndexOf")) {
      subtree = tryFoldStringIndexOf(subtree, functionNameString,
          stringNode, firstArg);
    } else if (functionNameString.equals("substr")) {
      subtree = tryFoldStringSubstr(subtree, stringNode, firstArg);
    } else if (functionNameString.equals("substring")) {
      subtree = tryFoldStringSubstring(subtree, stringNode, firstArg);
    } else if (functionNameString.equals("charAt")) {
      subtree = tryFoldStringCharAt(subtree, stringNode, firstArg);
    } else if (functionNameString.equals("charCodeAt")) {
      subtree = tryFoldStringCharCodeAt(subtree, stringNode, firstArg);
    }
  }

  return subtree;
}