Java Code Examples for com.google.javascript.rhino.JSDocInfo#hasEnumParameterType()

The following examples show how to use com.google.javascript.rhino.JSDocInfo#hasEnumParameterType() . 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_106_GlobalNamespace_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determines whether a set operation is a constructor or enumeration
 * declaration. The set operation may either be an assignment to a name,
 * a variable declaration, or an object literal key mapping.
 *
 * @param n The node that represents the name being set
 * @param parent Parent node of {@code n} (an ASSIGN, VAR, or OBJLIT node)
 * @return Whether the set operation is either a constructor or enum
 *     declaration
 */
private boolean isConstructorOrEnumDeclaration(Node n, Node parent) {
  JSDocInfo info;
  int valueNodeType;
  switch (parent.getType()) {
    case Token.ASSIGN:
      info = parent.getJSDocInfo();
      valueNodeType = n.getNext().getType();
      break;
    case Token.VAR:
      info = n.getJSDocInfo();
      if (info == null) {
        info = parent.getJSDocInfo();
      }
      Node valueNode = n.getFirstChild();
      valueNodeType = valueNode != null ? valueNode.getType() : Token.VOID;
      break;
    default:
      return false;
  }
  // Heed the annotations only if they're sensibly used.
  return info != null &&
         (info.isConstructor() && valueNodeType == Token.FUNCTION ||
          info.hasEnumParameterType() && valueNodeType == Token.OBJECTLIT);
}
 
Example 2
Source File: Closure_89_GlobalNamespace_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether a set operation is a constructor or enumeration
 * declaration. The set operation may either be an assignment to a name,
 * a variable declaration, or an object literal key mapping.
 *
 * @param n The node that represents the name being set
 * @param parent Parent node of {@code n} (an ASSIGN, VAR, or OBJLIT node)
 * @return Whether the set operation is either a constructor or enum
 *     declaration
 */
private boolean isConstructorOrEnumDeclaration(Node n, Node parent) {
  // NOTE(nicksantos): This does not handle named constructors
  // function a() {}
  // For legacy reasons, we should not fix this, because we do not
  // know who's depending on the current behavior.

  JSDocInfo info;
  int valueNodeType;
  switch (parent.getType()) {
    case Token.ASSIGN:
      info = parent.getJSDocInfo();
      valueNodeType = n.getNext().getType();
      break;
    case Token.VAR:
      info = n.getJSDocInfo();
      if (info == null) {
        info = parent.getJSDocInfo();
      }
      Node valueNode = n.getFirstChild();
      valueNodeType = valueNode != null ? valueNode.getType() : Token.VOID;
      break;
    default:
      return false;
  }
  // Heed the annotations only if they're sensibly used.
  return info != null &&
         (info.isConstructor() && valueNodeType == Token.FUNCTION ||
          info.hasEnumParameterType() && valueNodeType == Token.OBJECTLIT);
}
 
Example 3
Source File: Closure_54_TypedScopeCreator_t.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 4
Source File: Closure_95_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Defines a variable based on the {@link Token#NAME} node passed.
 * @param name The {@link Token#NAME} node.
 * @param var The parent of the {@code name} node, which must be a
 *     {@link Token#VAR} node.
 * @param parent {@code var}'s parent.
 * @param info the {@link JSDocInfo} information relating to this
 *     {@code name} node.
 */
private void defineName(Node name, Node var, Node parent, JSDocInfo info) {
  Node value = name.getFirstChild();

  if (value != null && value.getType() == Token.FUNCTION) {
    // function
    String functionName = name.getString();
    FunctionType functionType =
        getFunctionType(functionName, value, info, null);
    if (functionType.isReturnTypeInferred() &&
        scope.isLocal()) {
      defineSlot(name, var, null);
    } else {
      defineSlot(name, var, functionType);
    }
  } else {
    // variable's type
    JSType type = null;
    if (info == null) {
      // the variable's type will be inferred
      CompilerInput input = compiler.getInput(sourceName);
      Preconditions.checkNotNull(input, sourceName);
      type = input.isExtern() ?
          getNativeType(UNKNOWN_TYPE) : null;
    } else if (info.hasEnumParameterType()) {
      type = getEnumType(name.getString(), var, value,
          info.getEnumParameterType().evaluate(scope, typeRegistry));
    } else if (info.isConstructor()) {
      type = getFunctionType(name.getString(), value, info, name);
    } else {
      type = getDeclaredTypeInAnnotation(sourceName, name, info);
    }

    defineSlot(name, var, type);
  }
}
 
Example 5
Source File: Nopol2017_0027_t.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 6
Source File: Closure_95_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Look for a type declaration on a GETPROP node.
 *
 * @param info The doc info for this property.
 * @param n A top-level GETPROP node (it should not be contained inside
 *     another GETPROP).
 * @param rhsValue The node that {@code n} is being initialized to,
 *     or {@code null} if this is a stub declaration.
 */
private JSType getDeclaredGetPropType(NodeTraversal t, JSDocInfo info,
    Node n, Node rhsValue) {
  if (info != null && info.hasType()) {
    return getDeclaredTypeInAnnotation(t, n, info);
  } else if (info != null && info.hasEnumParameterType()) {
    return n.getJSType();
  } else if (rhsValue != null &&
      rhsValue.getType() == Token.FUNCTION) {
    return rhsValue.getJSType();
  } else {
    return getDeclaredTypeInAnnotation(t, n, info);
  }
}
 
Example 7
Source File: Closure_48_TypedScopeCreator_t.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: Closure_95_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Look for a type declaration on a GETPROP node.
 *
 * @param info The doc info for this property.
 * @param n A top-level GETPROP node (it should not be contained inside
 *     another GETPROP).
 * @param rhsValue The node that {@code n} is being initialized to,
 *     or {@code null} if this is a stub declaration.
 */
private JSType getDeclaredGetPropType(NodeTraversal t, JSDocInfo info,
    Node n, Node rhsValue) {
  if (info != null && info.hasType()) {
    return getDeclaredTypeInAnnotation(t, n, info);
  } else if (info != null && info.hasEnumParameterType()) {
    return n.getJSType();
  } else if (rhsValue != null &&
      rhsValue.getType() == Token.FUNCTION) {
    return rhsValue.getJSType();
  } else {
    return getDeclaredTypeInAnnotation(t, n, info);
  }
}
 
Example 9
Source File: Nopol2017_0027_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Look for a type declaration on a property assignment
 * (in an ASSIGN or an object literal key).
 *
 * @param info The doc info for this property.
 * @param lValue The l-value node.
 * @param rValue The node that {@code n} is being initialized to,
 *     or {@code null} if this is a stub declaration.
 */
private JSType getDeclaredType(String sourceName, JSDocInfo info,
    Node lValue, @Nullable Node rValue) {
  if (info != null && info.hasType()) {
    return getDeclaredTypeInAnnotation(sourceName, lValue, info);
  } else if (rValue != null && rValue.isFunction() &&
      shouldUseFunctionLiteralType(
          JSType.toMaybeFunctionType(rValue.getJSType()), info, lValue)) {
    return rValue.getJSType();
  } else if (info != null) {
    if (info.hasEnumParameterType()) {
      if (rValue != null && rValue.isObjectLit()) {
        return rValue.getJSType();
      } else {
        return createEnumTypeFromNodes(
            rValue, lValue.getQualifiedName(), info, lValue);
      }
    } else if (info.isConstructor() || info.isInterface()) {
      return createFunctionTypeFromNodes(
          rValue, lValue.getQualifiedName(), info, lValue);
    } else {
      // Check if this is constant, and if it has a known type.
      if (info.isConstant()) {
        JSType knownType = null;
        if (rValue != null) {
          if (rValue.getJSType() != null && !rValue.getJSType().isUnknownType()) {
            // If rValue has a type-cast, we use the type in the type-cast.
            // If rValue's type was already computed during scope creation,
            // then we can safely use that.
            if (!(com.google.javascript.jscomp.TypedScopeCreator.DELEGATE_PROXY_SUFFIX.length() == -1 + com.google.javascript.jscomp.TypedScopeCreator.this.functionAnalysisResults.size())) {
              return rValue.getJSType();
            }
          } else if (rValue.isOr()) {
            // Check for a very specific JS idiom:
            // var x = x || TYPE;
            // This is used by Closure's base namespace for esoteric
            // reasons.
            Node firstClause = rValue.getFirstChild();
            Node secondClause = firstClause.getNext();
            boolean namesMatch = firstClause.isName()
                && lValue.isName()
                && firstClause.getString().equals(lValue.getString());
            if (namesMatch && secondClause.getJSType() != null
                && !secondClause.getJSType().isUnknownType()) {
              return secondClause.getJSType();
            }
          }
        }
      }
    }
  }

  return getDeclaredTypeInAnnotation(sourceName, lValue, info);
}
 
Example 10
Source File: Closure_17_TypedScopeCreator_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether a qualified name is inferred.
 * NOTE(nicksantos): Determining whether a property is declared or not
 * is really really obnoxious.
 *
 * The problem is that there are two (equally valid) coding styles:
 *
 * (function() {
 *   /* The authoritative definition of goog.bar. /
 *   goog.bar = function() {};
 * })();
 *
 * function f() {
 *   goog.bar();
 *   /* Reset goog.bar to a no-op. /
 *   goog.bar = function() {};
 * }
 *
 * In a dynamic language with first-class functions, it's very difficult
 * to know which one the user intended without looking at lots of
 * contextual information (the second example demonstrates a small case
 * of this, but there are some really pathological cases as well).
 *
 * The current algorithm checks if either the declaration has
 * JsDoc type information, or @const with a known type,
 * or a function literal with a name we haven't seen before.
 */
private boolean isQualifiedNameInferred(
    String qName, Node n, JSDocInfo info,
    Node rhsValue, JSType valueType) {
  if (valueType == null) {
    return true;
  }

  boolean inferred = true;
  if (info != null) {
    inferred = !(info.hasType()
        || info.hasEnumParameterType()
        || (info.isConstant() && valueType != null
            && !valueType.isUnknownType())
        || FunctionTypeBuilder.isFunctionTypeDeclaration(info));
  }

  if (inferred && rhsValue != null && rhsValue.isFunction()) {
    if (info != null) {
      return false;
    } else if (!scope.isDeclared(qName, false) &&
        n.isUnscopedQualifiedName()) {

      // Check if this is in a conditional block.
      // Functions assigned in conditional blocks are inferred.
      for (Node current = n.getParent();
           !(current.isScript() || current.isFunction());
           current = current.getParent()) {
        if (NodeUtil.isControlStructure(current)) {
          return true;
        }
      }

      // Check if this is assigned in an inner scope.
      // Functions assigned in inner scopes are inferred.
      AstFunctionContents contents =
          getFunctionAnalysisResults(scope.getRootNode());
      if (contents == null ||
          !contents.getEscapedQualifiedNames().contains(qName)) {
        return false;
      }
    }
  }
  return inferred;
}
 
Example 11
Source File: Closure_17_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether a qualified name is inferred.
 * NOTE(nicksantos): Determining whether a property is declared or not
 * is really really obnoxious.
 *
 * The problem is that there are two (equally valid) coding styles:
 *
 * (function() {
 *   /* The authoritative definition of goog.bar. /
 *   goog.bar = function() {};
 * })();
 *
 * function f() {
 *   goog.bar();
 *   /* Reset goog.bar to a no-op. /
 *   goog.bar = function() {};
 * }
 *
 * In a dynamic language with first-class functions, it's very difficult
 * to know which one the user intended without looking at lots of
 * contextual information (the second example demonstrates a small case
 * of this, but there are some really pathological cases as well).
 *
 * The current algorithm checks if either the declaration has
 * JsDoc type information, or @const with a known type,
 * or a function literal with a name we haven't seen before.
 */
private boolean isQualifiedNameInferred(
    String qName, Node n, JSDocInfo info,
    Node rhsValue, JSType valueType) {
  if (valueType == null) {
    return true;
  }

  boolean inferred = true;
  if (info != null) {
    inferred = !(info.hasType()
        || info.hasEnumParameterType()
        || (info.isConstant() && valueType != null
            && !valueType.isUnknownType())
        || FunctionTypeBuilder.isFunctionTypeDeclaration(info));
  }

  if (inferred && rhsValue != null && rhsValue.isFunction()) {
    if (info != null) {
      return false;
    } else if (!scope.isDeclared(qName, false) &&
        n.isUnscopedQualifiedName()) {

      // Check if this is in a conditional block.
      // Functions assigned in conditional blocks are inferred.
      for (Node current = n.getParent();
           !(current.isScript() || current.isFunction());
           current = current.getParent()) {
        if (NodeUtil.isControlStructure(current)) {
          return true;
        }
      }

      // Check if this is assigned in an inner scope.
      // Functions assigned in inner scopes are inferred.
      AstFunctionContents contents =
          getFunctionAnalysisResults(scope.getRootNode());
      if (contents == null ||
          !contents.getEscapedQualifiedNames().contains(qName)) {
        return false;
      }
    }
  }
  return inferred;
}
 
Example 12
Source File: Closure_95_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
private void identifyEnumInNameNode(Node nameNode, JSDocInfo info) {
  if (info != null && info.hasEnumParameterType()) {
    registry.identifyEnumName(nameNode.getQualifiedName());
  }
}
 
Example 13
Source File: Closure_70_TypedScopeCreator_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Look for a type declaration on a property assignment
 * (in an ASSIGN or an object literal key).
 *
 * @param info The doc info for this property.
 * @param lValue The l-value node.
 * @param rValue The node that {@code n} is being initialized to,
 *     or {@code null} if this is a stub declaration.
 */
private JSType getDeclaredType(String sourceName, JSDocInfo info,
    Node lValue, @Nullable Node rValue) {
  if (info != null && info.hasType()) {
    return getDeclaredTypeInAnnotation(sourceName, lValue, info);
  } else if (rValue != null && rValue.getType() == Token.FUNCTION &&
      shouldUseFunctionLiteralType(
          (FunctionType) rValue.getJSType(), info, lValue)) {
    return rValue.getJSType();
  } else if (info != null) {
    if (info.hasEnumParameterType()) {
      if (rValue != null && rValue.getType() == Token.OBJECTLIT) {
        return rValue.getJSType();
      } else {
        return createEnumTypeFromNodes(
            rValue, lValue.getQualifiedName(), info, lValue);
      }
    } else if (info.isConstructor() || info.isInterface()) {
      return createFunctionTypeFromNodes(
          rValue, lValue.getQualifiedName(), info, lValue);
    } else {
      // Check if this is constant, and if it has a known type.
      if (info.isConstant()) {
        JSType knownType = null;
        if (rValue != null) {
          if (rValue.getJSType() != null
              && !rValue.getJSType().isUnknownType()) {
            return rValue.getJSType();
          } else if (rValue.getType() == Token.OR) {
            // Check for a very specific JS idiom:
            // var x = x || TYPE;
            // This is used by Closure's base namespace for esoteric
            // reasons.
            Node firstClause = rValue.getFirstChild();
            Node secondClause = firstClause.getNext();
            boolean namesMatch = firstClause.getType() == Token.NAME
                && lValue.getType() == Token.NAME
                && firstClause.getString().equals(lValue.getString());
            if (namesMatch && secondClause.getJSType() != null
                && !secondClause.getJSType().isUnknownType()) {
              return secondClause.getJSType();
            }
          }
        }
      }
    }
  }

  return getDeclaredTypeInAnnotation(sourceName, lValue, info);
}
 
Example 14
Source File: Closure_54_TypedScopeCreator_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Look for a type declaration on a property assignment
 * (in an ASSIGN or an object literal key).
 *
 * @param info The doc info for this property.
 * @param lValue The l-value node.
 * @param rValue The node that {@code n} is being initialized to,
 *     or {@code null} if this is a stub declaration.
 */
private JSType getDeclaredType(String sourceName, JSDocInfo info,
    Node lValue, @Nullable Node rValue) {
  if (info != null && info.hasType()) {
    return getDeclaredTypeInAnnotation(sourceName, lValue, info);
  } else if (rValue != null && rValue.getType() == Token.FUNCTION &&
      shouldUseFunctionLiteralType(
          JSType.toMaybeFunctionType(rValue.getJSType()), info, lValue)) {
    return rValue.getJSType();
  } else if (info != null) {
    if (info.hasEnumParameterType()) {
      if (rValue != null && rValue.getType() == Token.OBJECTLIT) {
        return rValue.getJSType();
      } else {
        return createEnumTypeFromNodes(
            rValue, lValue.getQualifiedName(), info, lValue);
      }
    } else if (info.isConstructor() || info.isInterface()) {
      return createFunctionTypeFromNodes(
          rValue, lValue.getQualifiedName(), info, lValue);
    } else {
      // Check if this is constant, and if it has a known type.
      if (info.isConstant()) {
        JSType knownType = null;
        if (rValue != null) {
          if (rValue.getJSType() != null
              && !rValue.getJSType().isUnknownType()) {
            return rValue.getJSType();
          } else if (rValue.getType() == Token.OR) {
            // Check for a very specific JS idiom:
            // var x = x || TYPE;
            // This is used by Closure's base namespace for esoteric
            // reasons.
            Node firstClause = rValue.getFirstChild();
            Node secondClause = firstClause.getNext();
            boolean namesMatch = firstClause.getType() == Token.NAME
                && lValue.getType() == Token.NAME
                && firstClause.getString().equals(lValue.getString());
            if (namesMatch && secondClause.getJSType() != null
                && !secondClause.getJSType().isUnknownType()) {
              return secondClause.getJSType();
            }
          }
        }
      }
    }
  }

  return getDeclaredTypeInAnnotation(sourceName, lValue, info);
}
 
Example 15
Source File: Nopol2017_0027_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Look for a type declaration on a property assignment
 * (in an ASSIGN or an object literal key).
 *
 * @param info The doc info for this property.
 * @param lValue The l-value node.
 * @param rValue The node that {@code n} is being initialized to,
 *     or {@code null} if this is a stub declaration.
 */
private JSType getDeclaredType(String sourceName, JSDocInfo info,
    Node lValue, @Nullable Node rValue) {
  if (info != null && info.hasType()) {
    return getDeclaredTypeInAnnotation(sourceName, lValue, info);
  } else if (rValue != null && rValue.isFunction() &&
      shouldUseFunctionLiteralType(
          JSType.toMaybeFunctionType(rValue.getJSType()), info, lValue)) {
    return rValue.getJSType();
  } else if (info != null) {
    if (info.hasEnumParameterType()) {
      if (rValue != null && rValue.isObjectLit()) {
        return rValue.getJSType();
      } else {
        return createEnumTypeFromNodes(
            rValue, lValue.getQualifiedName(), info, lValue);
      }
    } else if (info.isConstructor() || info.isInterface()) {
      return createFunctionTypeFromNodes(
          rValue, lValue.getQualifiedName(), info, lValue);
    } else {
      // Check if this is constant, and if it has a known type.
      if (info.isConstant()) {
        JSType knownType = null;
        if (rValue != null) {
          if (rValue.getJSType() != null && !rValue.getJSType().isUnknownType()) {
            // If rValue has a type-cast, we use the type in the type-cast.
            // If rValue's type was already computed during scope creation,
            // then we can safely use that.
            return rValue.getJSType();
          } else if (rValue.isOr()) {
            // Check for a very specific JS idiom:
            // var x = x || TYPE;
            // This is used by Closure's base namespace for esoteric
            // reasons.
            Node firstClause = rValue.getFirstChild();
            Node secondClause = firstClause.getNext();
            boolean namesMatch = firstClause.isName()
                && lValue.isName()
                && firstClause.getString().equals(lValue.getString());
            if (namesMatch && secondClause.getJSType() != null
                && !secondClause.getJSType().isUnknownType()) {
              return secondClause.getJSType();
            }
          }
        }
      }
    }
  }

  return getDeclaredTypeInAnnotation(sourceName, lValue, info);
}
 
Example 16
Source File: Nopol2017_0027_s.java    From coming with MIT License 4 votes vote down vote up
private void defineObjectLiteral(Node objectLit) {
  // Handle the @lends annotation.
  JSType type = null;
  JSDocInfo info = objectLit.getJSDocInfo();
  if (info != null &&
      info.getLendsName() != null) {
    String lendsName = info.getLendsName();
    Var lendsVar = scope.getVar(lendsName);
    if (lendsVar == null) {
      compiler.report(
          JSError.make(sourceName, objectLit, UNKNOWN_LENDS, lendsName));
    } else {
      type = lendsVar.getType();
      if (type == null) {
        type = typeRegistry.getNativeType(UNKNOWN_TYPE);
      }
      if (!type.isSubtype(typeRegistry.getNativeType(OBJECT_TYPE))) {
        compiler.report(
            JSError.make(sourceName, objectLit, LENDS_ON_NON_OBJECT,
                lendsName, type.toString()));
        type = null;
      } else {
        objectLit.setJSType(type);
      }
    }
  }

  info = NodeUtil.getBestJSDocInfo(objectLit);
  Node lValue = NodeUtil.getBestLValue(objectLit);
  String lValueName = NodeUtil.getBestLValueName(lValue);
  boolean createdEnumType = false;
  if (info != null && info.hasEnumParameterType()) {
    type = createEnumTypeFromNodes(objectLit, lValueName, info, lValue);
    createdEnumType = true;
  }

  if (type == null) {
    type = typeRegistry.createAnonymousObjectType();
  }

  setDeferredType(objectLit, type);

  // If this is an enum, the properties were already taken care of above.
  processObjectLitProperties(
      objectLit, ObjectType.cast(objectLit.getJSType()), !createdEnumType);
}
 
Example 17
Source File: Closure_54_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Look for a type declaration on a property assignment
 * (in an ASSIGN or an object literal key).
 *
 * @param info The doc info for this property.
 * @param lValue The l-value node.
 * @param rValue The node that {@code n} is being initialized to,
 *     or {@code null} if this is a stub declaration.
 */
private JSType getDeclaredType(String sourceName, JSDocInfo info,
    Node lValue, @Nullable Node rValue) {
  if (info != null && info.hasType()) {
    return getDeclaredTypeInAnnotation(sourceName, lValue, info);
  } else if (rValue != null && rValue.getType() == Token.FUNCTION &&
      shouldUseFunctionLiteralType(
          JSType.toMaybeFunctionType(rValue.getJSType()), info, lValue)) {
    return rValue.getJSType();
  } else if (info != null) {
    if (info.hasEnumParameterType()) {
      if (rValue != null && rValue.getType() == Token.OBJECTLIT) {
        return rValue.getJSType();
      } else {
        return createEnumTypeFromNodes(
            rValue, lValue.getQualifiedName(), info, lValue);
      }
    } else if (info.isConstructor() || info.isInterface()) {
      return createFunctionTypeFromNodes(
          rValue, lValue.getQualifiedName(), info, lValue);
    } else {
      // Check if this is constant, and if it has a known type.
      if (info.isConstant()) {
        JSType knownType = null;
        if (rValue != null) {
          if (rValue.getJSType() != null
              && !rValue.getJSType().isUnknownType()) {
            return rValue.getJSType();
          } else if (rValue.getType() == Token.OR) {
            // Check for a very specific JS idiom:
            // var x = x || TYPE;
            // This is used by Closure's base namespace for esoteric
            // reasons.
            Node firstClause = rValue.getFirstChild();
            Node secondClause = firstClause.getNext();
            boolean namesMatch = firstClause.getType() == Token.NAME
                && lValue.getType() == Token.NAME
                && firstClause.getString().equals(lValue.getString());
            if (namesMatch && secondClause.getJSType() != null
                && !secondClause.getJSType().isUnknownType()) {
              return secondClause.getJSType();
            }
          }
        }
      }
    }
  }

  return getDeclaredTypeInAnnotation(sourceName, lValue, info);
}
 
Example 18
Source File: Closure_17_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Look for a type declaration on a property assignment
 * (in an ASSIGN or an object literal key).
 *
 * @param info The doc info for this property.
 * @param lValue The l-value node.
 * @param rValue The node that {@code n} is being initialized to,
 *     or {@code null} if this is a stub declaration.
 */
private JSType getDeclaredType(String sourceName, JSDocInfo info,
    Node lValue, @Nullable Node rValue) {
  if (info != null && info.hasType()) {
    return getDeclaredTypeInAnnotation(sourceName, lValue, info);
  } else if (rValue != null && rValue.isFunction() &&
      shouldUseFunctionLiteralType(
          JSType.toMaybeFunctionType(rValue.getJSType()), info, lValue)) {
    return rValue.getJSType();
  } else if (info != null) {
    if (info.hasEnumParameterType()) {
      if (rValue != null && rValue.isObjectLit()) {
        return rValue.getJSType();
      } else {
        return createEnumTypeFromNodes(
            rValue, lValue.getQualifiedName(), info, lValue);
      }
    } else if (info.isConstructor() || info.isInterface()) {
      return createFunctionTypeFromNodes(
          rValue, lValue.getQualifiedName(), info, lValue);
    } else {
      // Check if this is constant, and if it has a known type.
      if (info.isConstant()) {
        JSType knownType = null;
        if (rValue != null) {
          JSDocInfo rValueInfo = rValue.getJSDocInfo();
          if (rValueInfo != null && rValueInfo.hasType()) {
            // If rValue has a type-cast, we use the type in the type-cast.
            return rValueInfo.getType().evaluate(scope, typeRegistry);
          } else if (rValue.getJSType() != null
              && !rValue.getJSType().isUnknownType()) {
            // If rValue's type was already computed during scope creation,
            // then we can safely use that.
            return rValue.getJSType();
          } else if (rValue.isOr()) {
            // Check for a very specific JS idiom:
            // var x = x || TYPE;
            // This is used by Closure's base namespace for esoteric
            // reasons.
            Node firstClause = rValue.getFirstChild();
            Node secondClause = firstClause.getNext();
            boolean namesMatch = firstClause.isName()
                && lValue.isName()
                && firstClause.getString().equals(lValue.getString());
            if (namesMatch && secondClause.getJSType() != null
                && !secondClause.getJSType().isUnknownType()) {
              return secondClause.getJSType();
            }
          }
        }
      }
    }
  }

  return getDeclaredTypeInAnnotation(sourceName, lValue, info);
}
 
Example 19
Source File: Closure_95_TypedScopeCreator_s.java    From coming with MIT License 4 votes vote down vote up
private void identifyEnumInNameNode(Node nameNode, JSDocInfo info) {
  if (info != null && info.hasEnumParameterType()) {
    registry.identifyEnumName(nameNode.getQualifiedName());
  }
}
 
Example 20
Source File: Closure_43_TypedScopeCreator_s.java    From coming with MIT License 3 votes vote down vote up
/**
 * Determines whether a qualified name is inferred.
 * NOTE(nicksantos): Determining whether a property is declared or not
 * is really really obnoxious.
 *
 * The problem is that there are two (equally valid) coding styles:
 *
 * (function() {
 *   /* The authoritative definition of goog.bar. /
 *   goog.bar = function() {};
 * })();
 *
 * function f() {
 *   goog.bar();
 *   /* Reset goog.bar to a no-op. /
 *   goog.bar = function() {};
 * }
 *
 * In a dynamic language with first-class functions, it's very difficult
 * to know which one the user intended without looking at lots of
 * contextual information (the second example demonstrates a small case
 * of this, but there are some really pathological cases as well).
 *
 * The current algorithm checks if either the declaration has
 * jsdoc type information, or @const with a known type,
 * or a function literal with a name we haven't seen before.
 */
private boolean isQualifiedNameInferred(
    String qName, Node n, JSDocInfo info,
    Node rhsValue, JSType valueType) {
  if (valueType == null) {
    return true;
  }

  boolean inferred = true;
  if (info != null) {
    inferred = !(info.hasType()
        || info.hasEnumParameterType()
        || (info.isConstant() && valueType != null
            && !valueType.isUnknownType())
        || FunctionTypeBuilder.isFunctionTypeDeclaration(info));
  }

  if (inferred && rhsValue != null && rhsValue.isFunction()) {
    if (info != null) {
      inferred = false;
    } else if (!scope.isDeclared(qName, false) &&
               n.isUnscopedQualifiedName()) {
      inferred = false;
    }
  }
  return inferred;
}