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

The following examples show how to use com.google.javascript.rhino.Node#getJSDocInfo() . 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_75_NodeUtil_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * @param nameNode A name node
 * @return The JSDocInfo for the name node
 */
static JSDocInfo getInfoForNameNode(Node nameNode) {
  JSDocInfo info = null;
  Node parent = null;
  if (nameNode != null) {
    info = nameNode.getJSDocInfo();
    parent = nameNode.getParent();
  }

  if (info == null && parent != null &&
      ((parent.getType() == Token.VAR && parent.hasOneChild()) ||
        parent.getType() == Token.FUNCTION)) {
    info = parent.getJSDocInfo();
  }
  return info;
}
 
Example 2
Source File: Closure_86_NodeUtil_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * @param nameNode A name node
 * @return The JSDocInfo for the name node
 */
static JSDocInfo getInfoForNameNode(Node nameNode) {
  JSDocInfo info = null;
  Node parent = null;
  if (nameNode != null) {
    info = nameNode.getJSDocInfo();
    parent = nameNode.getParent();
  }

  if (info == null && parent != null &&
      ((parent.getType() == Token.VAR && parent.hasOneChild()) ||
        parent.getType() == Token.FUNCTION)) {
    info = parent.getJSDocInfo();
  }
  return info;
}
 
Example 3
Source File: FieldCleanupPass.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node p) {
  // We are a root GetProp
  if (n.isGetProp() && !p.isGetProp()) {
    String propName = getFieldName(n);
    JSType type = n.getFirstChild().getJSType();
    if (type == null || type.toObjectType() == null) {
      // Note cases like <primitive>.field
      return;
    }
    removeProperty(type.toObjectType(), propName);
  }
  if (n.getJSDocInfo() != null) {
    n.getJSDocInfo().setAssociatedNode(null);
  }
}
 
Example 4
Source File: Closure_94_NodeUtil_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Get the JSDocInfo for a function.
 */
static JSDocInfo getFunctionInfo(Node n) {
  Preconditions.checkState(n.getType() == Token.FUNCTION);
  JSDocInfo fnInfo = n.getJSDocInfo();
  if (fnInfo == null && NodeUtil.isFunctionExpression(n)) {
    // Look for the info on other nodes.
    Node parent = n.getParent();
    if (parent.getType() == Token.ASSIGN) {
      // on ASSIGNs
      fnInfo = parent.getJSDocInfo();
    } else if (parent.getType() == Token.NAME) {
      // on var NAME = function() { ... };
      fnInfo = parent.getParent().getJSDocInfo();
    }
  }
  return fnInfo;
}
 
Example 5
Source File: Nopol2017_0051_s.java    From coming with MIT License 6 votes vote down vote up
private void checkNoTypeCheckSection(Node n, boolean enterSection) {
  switch (n.getType()) {
    case Token.SCRIPT:
    case Token.BLOCK:
    case Token.VAR:
    case Token.FUNCTION:
    case Token.ASSIGN:
      JSDocInfo info = n.getJSDocInfo();
      if (info != null && info.isNoTypeCheck()) {
        if (enterSection) {
          noTypeCheckSection++;
        } else {
          noTypeCheckSection--;
        }
      }
      validator.setShouldReport(noTypeCheckSection == 0);
      break;
  }
}
 
Example 6
Source File: Closure_43_TypedScopeCreator_t.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.isGetProp() ||
      !member.getFirstChild().isThis()) {
    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 7
Source File: jMutRepair_003_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Get the JSDocInfo for a function.
 */
public static JSDocInfo getFunctionJSDocInfo(Node n) {
  Preconditions.checkState(n.isFunction());
  JSDocInfo fnInfo = n.getJSDocInfo();
  if (fnInfo == null && NodeUtil.isFunctionExpression(n)) {
    // Look for the info on other nodes.
    Node parent = n.getParent();
    if (parent.isAssign()) {
      // on ASSIGNs
      fnInfo = parent.getJSDocInfo();
    } else if (parent.isName()) {
      // on var NAME = function() { ... };
      fnInfo = parent.getParent().getJSDocInfo();
    }
  }
  return fnInfo;
}
 
Example 8
Source File: Closure_61_NodeUtil_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Get the JSDocInfo for a function.
 */
public static JSDocInfo getFunctionJSDocInfo(Node n) {
  Preconditions.checkState(n.getType() == Token.FUNCTION);
  JSDocInfo fnInfo = n.getJSDocInfo();
  if (fnInfo == null && NodeUtil.isFunctionExpression(n)) {
    // Look for the info on other nodes.
    Node parent = n.getParent();
    if (parent.getType() == Token.ASSIGN) {
      // on ASSIGNs
      fnInfo = parent.getJSDocInfo();
    } else if (parent.getType() == Token.NAME) {
      // on var NAME = function() { ... };
      fnInfo = parent.getParent().getJSDocInfo();
    }
  }
  return fnInfo;
}
 
Example 9
Source File: CheckGlobalThis.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Gets a function's JSDoc information, if it has any. Checks for a few
 * patterns (ellipses show where JSDoc would be):
 * <pre>
 * ... function() {}
 * ... x = function() {};
 * var ... x = function() {};
 * ... var x = function() {};
 * </pre>
 */
private JSDocInfo getFunctionJsDocInfo(Node n) {
  JSDocInfo jsDoc = n.getJSDocInfo();
  Node parent = n.getParent();
  if (jsDoc == null) {
    int parentType = parent.getType();
    if (parentType == Token.NAME || parentType == Token.ASSIGN) {
      jsDoc = parent.getJSDocInfo();
      if (jsDoc == null && parentType == Token.NAME) {
        Node gramps = parent.getParent();
        if (gramps.isVar()) {
          jsDoc = gramps.getJSDocInfo();
        }
      }
    }
  }
  return jsDoc;
}
 
Example 10
Source File: Closure_81_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
private void setFileOverviewJsDoc(Node irNode) {
  // Only after we've seen all @fileoverview entries, attach the
  // last one to the root node, and copy the found license strings
  // to that node.
  irNode.setJSDocInfo(rootNodeJsDocHolder.getJSDocInfo());
  if (fileOverviewInfo != null) {
    if ((irNode.getJSDocInfo() != null) &&
        (irNode.getJSDocInfo().getLicense() != null)) {
      fileOverviewInfo.setLicense(irNode.getJSDocInfo().getLicense());
    }
    irNode.setJSDocInfo(fileOverviewInfo);
  }
}
 
Example 11
Source File: Closure_95_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Handle typedefs.
 * @param t The current traversal.
 * @param candidate An ASSIGN or VAR node.
 */
// TODO(nicksantos): Kill this.
private void checkForOldStyleTypedef(NodeTraversal t, Node candidate) {
  // old-style typedefs
  String typedef = codingConvention.identifyTypeDefAssign(candidate);
  if (typedef != null) {
    // TODO(nicksantos|user): This is a terrible, terrible hack
    // to bail out on recusive typedefs. We'll eventually need
    // to handle these properly.
    typeRegistry.forwardDeclareType(typedef);

    JSDocInfo info = candidate.getJSDocInfo();
    JSType realType = null;
    if (info != null && info.getType() != null) {
      realType = info.getType().evaluate(scope, typeRegistry);
    }

    if (realType == null) {
      compiler.report(
          JSError.make(
              t.getSourceName(), candidate, MALFORMED_TYPEDEF, typedef));
    }

    typeRegistry.declareType(typedef, realType);

    // Duplicate typedefs get handled when we try to register
    // this typedef in the scope.
  }
}
 
Example 12
Source File: Closure_129_PrepareAst_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Translate dispatcher info into the property expected node.
 */
private void annotateDispatchers(Node n, Node parent) {
  Preconditions.checkState(n.isFunction());
  if (parent.getJSDocInfo() != null
      && parent.getJSDocInfo().isJavaDispatch()) {
    if (parent.isAssign()) {
      Preconditions.checkState(parent.getLastChild() == n);
      n.putBooleanProp(Node.IS_DISPATCHER, true);
    }
  }
}
 
Example 13
Source File: Closure_2_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a VAR node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitVar(NodeTraversal t, Node n) {
  // TODO(nicksantos): Fix this so that the doc info always shows up
  // on the NAME node. We probably want to wait for the parser
  // merge to fix this.
  JSDocInfo varInfo = n.hasOneChild() ? n.getJSDocInfo() : null;
  for (Node name : n.children()) {
    Node value = name.getFirstChild();
    // A null var would indicate a bug in the scope creation logic.
    Var var = t.getScope().getVar(name.getString());

    if (value != null) {
      JSType valueType = getJSType(value);
      JSType nameType = var.getType();
      nameType = (nameType == null) ? getNativeType(UNKNOWN_TYPE) : nameType;

      JSDocInfo info = name.getJSDocInfo();
      if (info == null) {
        info = varInfo;
      }

      checkEnumAlias(t, info, value);
      if (var.isTypeInferred()) {
        ensureTyped(t, name, valueType);
      } else {
        validator.expectCanAssignTo(
            t, value, valueType, nameType, "initializing variable");
      }
    }
  }
}
 
Example 14
Source File: ReplaceIdGenerators.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  JSDocInfo doc = n.getJSDocInfo();
  if (doc == null) {
    return;
  }

  int numGeneratorAnnotations =
      (doc.isConsistentIdGenerator() ? 1 : 0) +
      (doc.isIdGenerator() ? 1 : 0) +
      (doc.isStableIdGenerator() ? 1 : 0);
  if (numGeneratorAnnotations == 0) {
    return;
  } else if (numGeneratorAnnotations > 1) {
    compiler.report(t.makeError(n, CONFLICTING_GENERATOR_TYPE));
  }

  String name = null;
  if (n.isAssign()) {
    name = n.getFirstChild().getQualifiedName();
  } else if (n.isVar()) {
    name = n.getFirstChild().getString();
  } else if (n.isFunction()){
    name = n.getFirstChild().getString();
    if (name.isEmpty()) {
      return;
    }
  }

  if (doc.isConsistentIdGenerator()) {
    consistNameMap.put(name, Maps.<String, String>newLinkedHashMap());
    nameGenerators.put(
        name, createNameSupplier(RenameStrategy.CONSISTENT, previousMap.get(name)));
  } else if (doc.isStableIdGenerator()) {
    nameGenerators.put(
        name, createNameSupplier(RenameStrategy.STABLE, previousMap.get(name)));
  } else {
    nameGenerators.put(
        name, createNameSupplier(RenameStrategy.INCONSISTENT, previousMap.get(name)));
  }
  idGeneratorMaps.put(name, Maps.<String, String>newLinkedHashMap());
}
 
Example 15
Source File: Closure_35_TypeInference_t.java    From coming with MIT License 4 votes vote down vote up
private FlowScope traverseObjectLiteral(Node n, FlowScope scope) {
  JSType type = n.getJSType();
  Preconditions.checkNotNull(type);

  for (Node name = n.getFirstChild(); name != null; name = name.getNext()) {
    scope = traverse(name.getFirstChild(), scope);
  }

  // Object literals can be reflected on other types, or changed with
  // type casts.
  // See CodingConvention#getObjectLiteralCase and goog.object.reflect.
  // Ignore these types of literals.
  // TODO(nicksantos): There should be an "anonymous object" type that
  // we can check for here.
  ObjectType objectType = ObjectType.cast(type);
  if (objectType == null) {
    return scope;
  }

  boolean hasLendsName = n.getJSDocInfo() != null &&
      n.getJSDocInfo().getLendsName() != null;
  if (objectType.hasReferenceName() && !hasLendsName) {
    return scope;
  }

  String qObjName = NodeUtil.getBestLValueName(
      NodeUtil.getBestLValue(n));
  for (Node name = n.getFirstChild(); name != null;
       name = name.getNext()) {
    Node value = name.getFirstChild();
    String memberName = NodeUtil.getObjectLitKeyName(name);
    if (memberName != null) {
      JSType rawValueType =  name.getFirstChild().getJSType();
      JSType valueType = NodeUtil.getObjectLitKeyTypeFromValueType(
          name, rawValueType);
      if (valueType == null) {
        valueType = getNativeType(UNKNOWN_TYPE);
      }
      objectType.defineInferredProperty(memberName, valueType, name);

      // Do normal flow inference if this is a direct property assignment.
      if (qObjName != null && name.isString()) {
        String qKeyName = qObjName + "." + memberName;
        Var var = syntacticScope.getVar(qKeyName);
        JSType oldType = var == null ? null : var.getType();
        if (var != null && var.isTypeInferred()) {
          var.setType(oldType == null ?
              valueType : oldType.getLeastSupertype(oldType));
        }

        scope.inferQualifiedSlot(name, qKeyName,
            oldType == null ? getNativeType(UNKNOWN_TYPE) : oldType,
            valueType);
      }
    } else {
      n.setJSType(getNativeType(UNKNOWN_TYPE));
    }
  }
  return scope;
}
 
Example 16
Source File: Closure_71_CheckAccessControls_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether the given property is visible in the current context.
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkPropertyVisibility(NodeTraversal t,
    Node getprop, Node parent) {
  ObjectType objectType =
      ObjectType.cast(dereference(getprop.getFirstChild().getJSType()));
  String propertyName = getprop.getLastChild().getString();

  if (objectType != null) {
    // Is this a normal property access, or are we trying to override
    // an existing property?
    boolean isOverride = parent.getJSDocInfo() != null &&
        parent.getType() == Token.ASSIGN &&
        parent.getFirstChild() == getprop;

    // Find the lowest property defined on a class with visibility
    // information.
    if (isOverride) {
      objectType = objectType.getImplicitPrototype();
    }
    JSDocInfo docInfo = null;
    for (; objectType != null;
         objectType = objectType.getImplicitPrototype()) {
      docInfo = objectType.getOwnPropertyJSDocInfo(propertyName);
      if (docInfo != null &&
          docInfo.getVisibility() != Visibility.INHERITED) {
        break;
      }
    }

    if (objectType == null) {
      // We couldn't find a visibility modifier; assume it's public.
      return;
    }

    boolean sameInput =
        t.getInput().getName().equals(docInfo.getSourceName());
    Visibility visibility = docInfo.getVisibility();
    JSType ownerType = normalizeClassType(objectType);
    if (isOverride) {
      // Check an ASSIGN statement that's trying to override a property
      // on a superclass.
      JSDocInfo overridingInfo = parent.getJSDocInfo();
      Visibility overridingVisibility = overridingInfo == null ?
          Visibility.INHERITED : overridingInfo.getVisibility();

      // Check that (a) the property *can* be overridden, and
      // (b) that the visibility of the override is the same as the
      // visibility of the original property.
      if (visibility == Visibility.PRIVATE && !sameInput) {
        compiler.report(
            t.makeError(getprop, PRIVATE_OVERRIDE,
                objectType.toString()));
      } else if (overridingVisibility != Visibility.INHERITED &&
          overridingVisibility != visibility) {
        compiler.report(
            t.makeError(getprop, VISIBILITY_MISMATCH,
                visibility.name(), objectType.toString(),
                overridingVisibility.name()));
      }
    } else {
      if (sameInput) {
        // private access is always allowed in the same file.
        return;
      } else if (visibility == Visibility.PRIVATE &&
          (currentClass == null || ownerType.differsFrom(currentClass))) {
        if (docInfo.isConstructor() &&
            isValidPrivateConstructorAccess(parent)) {
          return;
        }

        // private access is not allowed outside the file from a different
        // enclosing class.
        compiler.report(
            t.makeError(getprop,
                BAD_PRIVATE_PROPERTY_ACCESS,
                propertyName,
                validator.getReadableJSTypeName(
                    getprop.getFirstChild(), true)));
      } else if (visibility == Visibility.PROTECTED) {
        // There are 3 types of legal accesses of a protected property:
        // 1) Accesses in the same file
        // 2) Overriding the property in a subclass
        // 3) Accessing the property from inside a subclass
        // The first two have already been checked for.
        if (currentClass == null || !currentClass.isSubtype(ownerType)) {
          compiler.report(
              t.makeError(getprop,  BAD_PROTECTED_PROPERTY_ACCESS,
                  propertyName,
                  validator.getReadableJSTypeName(
                      getprop.getFirstChild(), true)));
        }
      }
    }
  }
}
 
Example 17
Source File: CheckGlobalThis.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Since this pass reports errors only when a global {@code this} keyword
 * is encountered, there is no reason to traverse non global contexts.
 */
@Override
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {

  if (n.isFunction()) {
    // Don't traverse functions that are constructors or have the @this
    // or @override annotation.
    JSDocInfo jsDoc = getFunctionJsDocInfo(n);
    if (jsDoc != null &&
        (jsDoc.isConstructor() ||
         jsDoc.isInterface() ||
         jsDoc.hasThisType() ||
         jsDoc.isOverride())) {
      return false;
    }

    // Don't traverse functions unless they would normally
    // be able to have a @this annotation associated with them. e.g.,
    // var a = function() { }; // or
    // function a() {} // or
    // a.x = function() {}; // or
    // var a = {x: function() {}};
    int pType = parent.getType();
    if (!(pType == Token.BLOCK ||
          pType == Token.SCRIPT ||
          pType == Token.NAME ||
          pType == Token.ASSIGN ||

          // object literal keys
          pType == Token.STRING_KEY)) {
      return false;
    }

    // Don't traverse functions that are getting lent to a prototype.
    Node gramps = parent.getParent();
    if (NodeUtil.isObjectLitKey(parent, gramps)) {
      JSDocInfo maybeLends = gramps.getJSDocInfo();
      if (maybeLends != null &&
          maybeLends.getLendsName() != null &&
          maybeLends.getLendsName().endsWith(".prototype")) {
        return false;
      }
    }
  }

  if (parent != null && parent.isAssign()) {
    Node lhs = parent.getFirstChild();
    Node rhs = lhs.getNext();

    if (n == lhs) {
      // Always traverse the left side of the assignment. To handle
      // nested assignments properly (e.g., (a = this).property = c;),
      // assignLhsChild should not be overridden.
      if (assignLhsChild == null) {
        assignLhsChild = lhs;
      }
    } else {
      // Only traverse the right side if it's not an assignment to a prototype
      // property or subproperty.
      if (NodeUtil.isGet(lhs)) {
        if (lhs.isGetProp() &&
            lhs.getLastChild().getString().equals("prototype")) {
          return false;
        }
        Node llhs = lhs.getFirstChild();
        if (llhs.isGetProp() &&
            llhs.getLastChild().getString().equals("prototype")) {
          return false;
        }
      }
    }
  }

  return true;
}
 
Example 18
Source File: jKali_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;
      }
  }

  } 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: jMutRepair_0048_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 20
Source File: Closure_24_ScopedAliases_s.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (isCallToScopeMethod(n)) {
    validateScopeCall(t, n, n.getParent());
  }

  if (t.getScopeDepth() < 2) {
    return;
  }

  int type = n.getType();
  Var aliasVar = null;
  if (type == Token.NAME) {
    String name = n.getString();
    Var lexicalVar = t.getScope().getVar(n.getString());
    if (lexicalVar != null && lexicalVar == aliases.get(name)) {
      aliasVar = lexicalVar;
    }
  }

  // Validate the top-level of the goog.scope block.
  if (t.getScopeDepth() == 2) {
    if (aliasVar != null && NodeUtil.isLValue(n)) {
      if (aliasVar.getNode() == n) {
        aliasDefinitionsInOrder.add(n);

        // Return early, to ensure that we don't record a definition
        // twice.
        return;
      } else {
        report(t, n, GOOG_SCOPE_ALIAS_REDEFINED, n.getString());
      }
    }

    if (type == Token.RETURN) {
      report(t, n, GOOG_SCOPE_USES_RETURN);
    } else if (type == Token.THIS) {
      report(t, n, GOOG_SCOPE_REFERENCES_THIS);
    } else if (type == Token.THROW) {
      report(t, n, GOOG_SCOPE_USES_THROW);
    }
  }

  // Validate all descendent scopes of the goog.scope block.
  if (t.getScopeDepth() >= 2) {
    // Check if this name points to an alias.
    if (aliasVar != null) {
      // Note, to support the transitive case, it's important we don't
      // clone aliasedNode here.  For example,
      // var g = goog; var d = g.dom; d.createElement('DIV');
      // The node in aliasedNode (which is "g") will be replaced in the
      // changes pass above with "goog".  If we cloned here, we'd end up
      // with <code>g.dom.createElement('DIV')</code>.
      Node aliasedNode = aliasVar.getInitialValue();
      aliasUsages.add(new AliasedNode(n, aliasedNode));
    }

    JSDocInfo info = n.getJSDocInfo();
    if (info != null) {
      for (Node node : info.getTypeNodes()) {
        fixTypeNode(node);
      }
    }

    // TODO(robbyw): Error for goog.scope not at root.
  }
}