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

The following examples show how to use com.google.javascript.rhino.Node#isQualifiedName() . 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: 1_ClosureCodingConvention.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
@Override
public ObjectLiteralCast getObjectLiteralCast(NodeTraversal t,
    Node callNode) {
  Preconditions.checkArgument(callNode.getType() == Token.CALL);
  Node callName = callNode.getFirstChild();
  if (!"goog.reflect.object".equals(callName.getQualifiedName()) ||
      callNode.getChildCount() != 3) {
    return null;
  }

  Node typeNode = callName.getNext();
  if (!typeNode.isQualifiedName()) {
    return null;
  }

  Node objectNode = typeNode.getNext();
  if (objectNode.getType() != Token.OBJECTLIT) {
    // TODO(johnlenz): The coding convention should not be performing checks.
    t.getCompiler().report(JSError.make(t.getSourceName(), callNode,
                                        OBJECTLIT_EXPECTED));
    return null;
  }

  return new ObjectLiteralCast(typeNode.getQualifiedName(),
                               typeNode.getNext());
}
 
Example 2
Source File: Closure_89_GlobalNamespace_t.java    From coming with MIT License 6 votes vote down vote up
public boolean apply(Node n) {
  if (!n.isQualifiedName()) {
    return false;
  }

  Node current;
  for (current = n;
       current.getType() == Token.GETPROP;
       current = current.getFirstChild()) {
    if (newNodes.contains(current)) {
      return true;
    }
  }

  return current.getType() == Token.NAME && newNodes.contains(current);
}
 
Example 3
Source File: Closure_89_GlobalNamespace_s.java    From coming with MIT License 6 votes vote down vote up
public boolean apply(Node n) {
  if (!n.isQualifiedName()) {
    return false;
  }

  Node current;
  for (current = n;
       current.getType() == Token.GETPROP;
       current = current.getFirstChild()) {
    if (newNodes.contains(current)) {
      return true;
    }
  }

  return current.getType() == Token.NAME && newNodes.contains(current);
}
 
Example 4
Source File: Closure_70_TypedScopeCreator_t.java    From coming with MIT License 6 votes vote down vote up
private void identifyNameNode(
    Node nameNode, Node valueNode, JSDocInfo info) {
  if (nameNode.isQualifiedName()) {
    if (info != null) {
      if (info.hasEnumParameterType()) {
        registry.identifyNonNullableName(nameNode.getQualifiedName());
      } else if (info.hasTypedefType()) {
        registry.identifyNonNullableName(nameNode.getQualifiedName());
      }
    }

    if (valueNode != null &&
        LEGACY_TYPEDEF.equals(valueNode.getQualifiedName())) {
      registry.identifyNonNullableName(nameNode.getQualifiedName());
    }
  }
}
 
Example 5
Source File: Closure_70_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Given a node, determines whether that node names a prototype
 * property, and if so, returns the qualified name node representing
 * the owner of that property. Otherwise, returns null.
 */
private static Node getPrototypePropertyOwner(Node n) {
  if (n.getType() == Token.GETPROP) {
    Node firstChild = n.getFirstChild();
    if (firstChild.getType() == Token.GETPROP &&
        firstChild.getLastChild().getString().equals("prototype")) {
      Node maybeOwner = firstChild.getFirstChild();
      if (maybeOwner.isQualifiedName()) {
        return maybeOwner;
      }
    }
  }
  return null;
}
 
Example 6
Source File: Closure_48_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
private void identifyNameNode(
    Node nameNode, Node valueNode, JSDocInfo info) {
  if (nameNode.isQualifiedName()) {
    if (info != null) {
      if (info.hasEnumParameterType()) {
        registry.identifyNonNullableName(nameNode.getQualifiedName());
      } else if (info.hasTypedefType()) {
        registry.identifyNonNullableName(nameNode.getQualifiedName());
      }
    }
  }
}
 
Example 7
Source File: Closure_17_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
private void identifyNameNode(
    Node nameNode, Node valueNode, JSDocInfo info) {
  if (nameNode.isQualifiedName()) {
    if (info != null) {
      if (info.hasEnumParameterType()) {
        registry.identifyNonNullableName(nameNode.getQualifiedName());
      } else if (info.hasTypedefType()) {
        registry.identifyNonNullableName(nameNode.getQualifiedName());
      }
    }
  }
}
 
Example 8
Source File: ClosureCodingConvention.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ObjectLiteralCast getObjectLiteralCast(Node callNode) {
  Preconditions.checkArgument(callNode.isCall());
  ObjectLiteralCast proxyCast = super.getObjectLiteralCast(callNode);
  if (proxyCast != null) {
    return proxyCast;
  }

  Node callName = callNode.getFirstChild();
  if (!"goog.reflect.object".equals(callName.getQualifiedName()) ||
      callNode.getChildCount() != 3) {
    return null;
  }

  Node typeNode = callName.getNext();
  if (!typeNode.isQualifiedName()) {
    return null;
  }

  Node objectNode = typeNode.getNext();
  if (!objectNode.isObjectLit()) {
    return new ObjectLiteralCast(null, null, OBJECTLIT_EXPECTED);
  }

  return new ObjectLiteralCast(
      typeNode.getQualifiedName(), typeNode.getNext(), null);
}
 
Example 9
Source File: Nopol2017_0027_s.java    From coming with MIT License 5 votes vote down vote up
private void identifyNameNode(
    Node nameNode, Node valueNode, JSDocInfo info) {
  if (nameNode.isQualifiedName()) {
    if (info != null) {
      if (info.hasEnumParameterType()) {
        registry.identifyNonNullableName(nameNode.getQualifiedName());
      } else if (info.hasTypedefType()) {
        registry.identifyNonNullableName(nameNode.getQualifiedName());
      }
    }
  }
}
 
Example 10
Source File: Closure_25_TypeInference_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * If we access a property of a symbol, then that symbol is not
 * null or undefined.
 */
private FlowScope dereferencePointer(Node n, FlowScope scope) {
  if (n.isQualifiedName()) {
    JSType type = getJSType(n);
    JSType narrowed = type.restrictByNotNullOrUndefined();
    if (type != narrowed) {
      scope = narrowScope(scope, n, narrowed);
    }
  }
  return scope;
}
 
Example 11
Source File: Closure_96_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits an ASSIGN node for cases such as
 * <pre>
 * interface.property2.property = ...;
 * </pre>
 */
private void visitInterfaceGetprop(NodeTraversal t, Node assign, Node object,
    String property, Node lvalue, Node rvalue) {

  JSType rvalueType = getJSType(rvalue);

  // Only 2 values are allowed for methods:
  //    goog.abstractMethod
  //    function () {};
  // or for properties, no assignment such as:
  //    InterfaceFoo.prototype.foobar;

  String abstractMethodName =
      compiler.getCodingConvention().getAbstractMethodName();
  if (!rvalueType.isOrdinaryFunction() &&
      !(rvalue.isQualifiedName() &&
        rvalue.getQualifiedName().equals(abstractMethodName))) {
    // This is bad i18n style but we don't localize our compiler errors.
    String abstractMethodMessage = (abstractMethodName != null)
       ? ", or " + abstractMethodName
       : "";
    compiler.report(
        t.makeError(object, INVALID_INTERFACE_MEMBER_DECLARATION,
            abstractMethodMessage));
  }

  if (assign.getLastChild().getType() == Token.FUNCTION
      && !NodeUtil.isEmptyBlock(assign.getLastChild().getLastChild())) {
    compiler.report(
        t.makeError(object, INTERFACE_FUNCTION_NOT_EMPTY,
            abstractMethodName));
  }
}
 
Example 12
Source File: jKali_0034_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: ClosureRewriteClass.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Validates the class definition and if valid, destructively extracts
 * the class definition from the AST.
 */
private ClassDefinition extractClassDefinition(
    Node targetName, Node callNode) {

  // name = goog.defineClass(superClass, {...}, [modifier, ...])
  Node superClass = NodeUtil.getArgumentForCallOrNew(callNode, 0);
  if (superClass == null ||
      (!superClass.isNull() && !superClass.isQualifiedName())) {
    compiler.report(JSError.make(callNode, GOOG_CLASS_SUPER_CLASS_NOT_VALID));
    return null;
  }
  if (NodeUtil.isNullOrUndefined(superClass)) {
    superClass = null;
  }

  Node description = NodeUtil.getArgumentForCallOrNew(callNode, 1);
  if (description == null
      || !description.isObjectLit()
      || !validateObjLit(description)) {
    // report bad class definition
    compiler.report(JSError.make(callNode, GOOG_CLASS_DESCRIPTOR_NOT_VALID));
    return null;
  }

  int paramCount = callNode.getChildCount() -1;
  if (paramCount > 2) {
    compiler.report(JSError.make(callNode, GOOG_CLASS_UNEXPECTED_PARAMS));
    return null;
  }

  Node constructor = extractProperty(description, "constructor");
  if (constructor == null) {
    // report missing constructor
    compiler.report(JSError.make(description, GOOG_CLASS_CONSTRUCTOR_MISING));
    return null;
  }
  JSDocInfo info = NodeUtil.getBestJSDocInfo(constructor);

  Node classModifier = null;
  Node statics = null;
  Node staticsProp = extractProperty(description, "statics");
  if (staticsProp != null) {
    if (staticsProp.isObjectLit() && validateObjLit(staticsProp)) {
      statics = staticsProp;
    } else if (staticsProp.isFunction()) {
      classModifier = staticsProp;
    } else {
      compiler.report(
          JSError.make(staticsProp, GOOG_CLASS_STATICS_NOT_VALID));
      return null;
    }
  }

  if (statics == null) {
    statics = IR.objectlit();
  }

  // Ok, now rip apart the definition into its component pieces.
  // Remove the "special" property key nodes.
  maybeDetach(constructor.getParent());
  maybeDetach(statics.getParent());
  if (classModifier != null) {
    maybeDetach(classModifier.getParent());
  }
  ClassDefinition def = new ClassDefinition(
      targetName,
      maybeDetach(superClass),
      new MemberDefinition(info, null, maybeDetach(constructor)),
      objectLitToList(maybeDetach(statics)),
      objectLitToList(description),
      maybeDetach(classModifier));
  return def;
}
 
Example 14
Source File: Cardumen_00150_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 (n == parent.getLastChild()) {
      for (Node an : parent.getAncestors()) {
        int ancestorType = an.getType();
        if (ancestorType == Token.COMMA) continue;
        if ((compiler) == null) 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 15
Source File: Cardumen_0018_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 (n == parent.getLastChild()) {
      for (Node an : parent.getAncestors()) {
        int ancestorType = an.getType();
        if (ancestorType == Token.COMMA) continue;
        if ((((((((com.google.javascript.jscomp.NodeUtil.isAssignmentOp(n)) && ((n.getFirstChild()) == n)) || ((com.google.javascript.jscomp.NodeUtil.isForIn(n)) && ((n.getFirstChild()) == n))) || (n.isVar())) || ((n.isFunction()) && ((n.getFirstChild()) == n))) || (n.isDec())) || (n.isInc())) || (n.isParamList())) 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 16
Source File: jKali_0042_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 (n == parent.getLastChild()) {
      for (Node an : parent.getAncestors()) {
        int ancestorType = an.getType();
        if (ancestorType == Token.COMMA) continue;
        if (false) 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: Closure_70_TypedScopeCreator_s.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  attachLiteralTypes(t, n);

  switch (n.getType()) {
    case Token.CALL:
      checkForClassDefiningCalls(t, n, parent);
      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.getType() == Token.GETPROP &&
          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.getType() == Token.EXPR_RESULT &&
          n.isQualifiedName()) {
        maybeDeclareQualifiedName(t, n.getJSDocInfo(), n, parent, null);
      }
      break;
  }
}
 
Example 18
Source File: Closure_70_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  attachLiteralTypes(t, n);

  switch (n.getType()) {
    case Token.CALL:
      checkForClassDefiningCalls(t, n, parent);
      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.getType() == Token.GETPROP &&
          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.getType() == Token.EXPR_RESULT &&
          n.isQualifiedName()) {
        maybeDeclareQualifiedName(t, n.getJSDocInfo(), n, parent, null);
      }
      break;
  }
}
 
Example 19
Source File: Closure_10_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether the given value may be assigned to a define.
 *
 * @param val The value being assigned.
 * @param defines The list of names of existing defines.
 */
static boolean isValidDefineValue(Node val, Set<String> defines) {
  switch (val.getType()) {
    case Token.STRING:
    case Token.NUMBER:
    case Token.TRUE:
    case Token.FALSE:
      return true;

    // Binary operators are only valid if both children are valid.
    case Token.ADD:
    case Token.BITAND:
    case Token.BITNOT:
    case Token.BITOR:
    case Token.BITXOR:
    case Token.DIV:
    case Token.EQ:
    case Token.GE:
    case Token.GT:
    case Token.LE:
    case Token.LSH:
    case Token.LT:
    case Token.MOD:
    case Token.MUL:
    case Token.NE:
    case Token.RSH:
    case Token.SHEQ:
    case Token.SHNE:
    case Token.SUB:
    case Token.URSH:
      return isValidDefineValue(val.getFirstChild(), defines)
          && isValidDefineValue(val.getLastChild(), defines);

    // Unary operators are valid if the child is valid.
    case Token.NOT:
    case Token.NEG:
    case Token.POS:
      return isValidDefineValue(val.getFirstChild(), defines);

    // Names are valid if and only if they are defines themselves.
    case Token.NAME:
    case Token.GETPROP:
      if (val.isQualifiedName()) {
        return defines.contains(val.getQualifiedName());
      }
  }
  return false;
}
 
Example 20
Source File: jMutRepair_0023_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);
    }
  }
}