Java Code Examples for com.google.javascript.rhino.jstype.FunctionType#isConstructor()

The following examples show how to use com.google.javascript.rhino.jstype.FunctionType#isConstructor() . 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_69_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Visits a NEW node.
 */
private void visitNew(NodeTraversal t, Node n) {
  Node constructor = n.getFirstChild();
  FunctionType type = getFunctionType(constructor);
  if (type != null && type.isConstructor()) {
    visitParameterList(t, n, type);
    ensureTyped(t, n, type.getInstanceType());
  } else {
    // TODO(user): add support for namespaced objects.
    if (constructor.getType() != Token.GETPROP) {
      // TODO(user): make the constructor node have lineno/charno
      // and use constructor for a more precise error indication.
      // It seems that GETPROP nodes are missing this information.
      Node line;
      if (constructor.getLineno() < 0 || constructor.getCharno() < 0) {
        line = n;
      } else {
        line = constructor;
      }
      report(t, line, NOT_A_CONSTRUCTOR);
    }
    ensureTyped(t, n);
  }
}
 
Example 2
Source File: Closure_66_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Visits a NEW node.
 */
private void visitNew(NodeTraversal t, Node n) {
  Node constructor = n.getFirstChild();
  FunctionType type = getFunctionType(constructor);
  if (type != null && type.isConstructor()) {
    visitParameterList(t, n, type);
    ensureTyped(t, n, type.getInstanceType());
  } else {
    // TODO(user): add support for namespaced objects.
    if (constructor.getType() != Token.GETPROP) {
      // TODO(user): make the constructor node have lineno/charno
      // and use constructor for a more precise error indication.
      // It seems that GETPROP nodes are missing this information.
      Node line;
      if (constructor.getLineno() < 0 || constructor.getCharno() < 0) {
        line = n;
      } else {
        line = constructor;
      }
      report(t, line, NOT_A_CONSTRUCTOR);
    }
    ensureTyped(t, n);
  }
}
 
Example 3
Source File: RuntimeTypeCheck.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void visitFunction(NodeTraversal t, Node n) {
  FunctionType funType = n.getJSType().toMaybeFunctionType();
  if (funType != null && !funType.isConstructor()) {
    return;
  }

  Node nodeToInsertAfter = findNodeToInsertAfter(n);

  nodeToInsertAfter = addMarker(funType, nodeToInsertAfter, null);

  TreeSet<ObjectType> stuff = Sets.newTreeSet(ALPHA);
  Iterables.addAll(stuff, funType.getAllImplementedInterfaces());
  for (ObjectType interfaceType : stuff) {
    nodeToInsertAfter =
        addMarker(funType, nodeToInsertAfter, interfaceType);
  }
}
 
Example 4
Source File: Closure_96_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a {@link Token#FUNCTION} 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 visitFunction(NodeTraversal t, Node n) {
  JSDocInfo info = n.getJSDocInfo();

  FunctionType functionType = (FunctionType) n.getJSType();
  String functionPrivateName = n.getFirstChild().getString();
  if (functionType.isInterface() || functionType.isConstructor()) {
    FunctionType baseConstructor = functionType.
        getPrototype().getImplicitPrototype().getConstructor();
    if (baseConstructor != null &&
        baseConstructor != getNativeType(OBJECT_FUNCTION_TYPE) &&
        (baseConstructor.isConstructor() && functionType.isInterface() ||
         baseConstructor.isInterface() && functionType.isConstructor())) {
      compiler.report(
          t.makeError(n, CONFLICTING_EXTENDED_TYPE, functionPrivateName));
    }

    for (JSType baseInterface : functionType.getImplementedInterfaces()) {
      boolean badImplementedType = false;
      ObjectType baseInterfaceObj = ObjectType.cast(baseInterface);
      if (baseInterfaceObj != null) {
        FunctionType interfaceConstructor =
            baseInterfaceObj.getConstructor();
        if (interfaceConstructor != null &&
            !interfaceConstructor.isInterface()) {
          badImplementedType = true;
        }
      } else {
        badImplementedType = true;
      }
      if (badImplementedType) {
        report(t, n, BAD_IMPLEMENTED_TYPE, functionPrivateName);
      }
    }
    if (functionType.isConstructor()) {
      validator.expectAllInterfacePropertiesImplemented(functionType);
    }
  }
}
 
Example 5
Source File: Closure_118_DisambiguateProperties_t.java    From coming with MIT License 5 votes vote down vote up
@Override
public void recordInterfaces(JSType type, JSType relatedType,
                             DisambiguateProperties<JSType>.Property p) {
  ObjectType objType = ObjectType.cast(type);
  if (objType != null) {
    FunctionType constructor;
    if (objType.isFunctionType()) {
      constructor = objType.toMaybeFunctionType();
    } else if (objType.isFunctionPrototypeType()) {
      constructor = objType.getOwnerFunction();
    } else {
      constructor = objType.getConstructor();
    }
    while (constructor != null) {
      for (ObjectType itype : constructor.getImplementedInterfaces()) {
        JSType top = getTypeWithProperty(p.name, itype);
        if (top != null) {
          p.addType(itype, top, relatedType);
        } else {
          recordInterfaces(itype, relatedType, p);
        }

        // If this interface invalidated this property, return now.
        if (p.skipRenaming) {
          return;
        }
      }
      if (constructor.isInterface() || constructor.isConstructor()) {
        constructor = constructor.getSuperClassConstructor();
      } else {
        constructor = null;
      }
    }
  }
}
 
Example 6
Source File: Closure_11_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
private void checkPropertyInheritanceOnGetpropAssign(
    NodeTraversal t, Node assign, Node object, String property,
    JSDocInfo info, JSType propertyType) {
  // Inheritance checks for prototype properties.
  //
  // TODO(nicksantos): This isn't the right place to do this check. We
  // really want to do this when we're looking at the constructor.
  // We'd find all its properties and make sure they followed inheritance
  // rules, like we currently do for @implements to make sure
  // all the methods are implemented.
  //
  // As-is, this misses many other ways to override a property.
  //
  // object.prototype.property = ...;
  if (object.isGetProp()) {
    Node object2 = object.getFirstChild();
    String property2 = NodeUtil.getStringValue(object.getLastChild());

    if ("prototype".equals(property2)) {
      JSType jsType = getJSType(object2);
      if (jsType.isFunctionType()) {
        FunctionType functionType = jsType.toMaybeFunctionType();
        if (functionType.isConstructor() || functionType.isInterface()) {
          checkDeclaredPropertyInheritance(
              t, assign, functionType, property, info, propertyType);
        }
      }
    }
  }
}
 
Example 7
Source File: Closure_103_DisambiguateProperties_s.java    From coming with MIT License 5 votes vote down vote up
@Override public JSType getInstanceFromPrototype(JSType type) {
  if (type.isFunctionPrototypeType()) {
    FunctionPrototypeType prototype = (FunctionPrototypeType) type;
    FunctionType owner = prototype.getOwnerFunction();
    if (owner.isConstructor() || owner.isInterface()) {
      return ((FunctionPrototypeType) type).getOwnerFunction()
          .getInstanceType();
    }
  }
  return null;
}
 
Example 8
Source File: Closure_2_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
private void checkPropertyInheritanceOnGetpropAssign(
    NodeTraversal t, Node assign, Node object, String property,
    JSDocInfo info, JSType propertyType) {
  // Inheritance checks for prototype properties.
  //
  // TODO(nicksantos): This isn't the right place to do this check. We
  // really want to do this when we're looking at the constructor.
  // We'd find all its properties and make sure they followed inheritance
  // rules, like we currently do for @implements to make sure
  // all the methods are implemented.
  //
  // As-is, this misses many other ways to override a property.
  //
  // object.prototype.property = ...;
  if (object.isGetProp()) {
    Node object2 = object.getFirstChild();
    String property2 = NodeUtil.getStringValue(object.getLastChild());

    if ("prototype".equals(property2)) {
      JSType jsType = getJSType(object2);
      if (jsType.isFunctionType()) {
        FunctionType functionType = jsType.toMaybeFunctionType();
        if (functionType.isConstructor() || functionType.isInterface()) {
          checkDeclaredPropertyInheritance(
              t, assign, functionType, property, info, propertyType);
        }
      }
    }
  }
}
 
Example 9
Source File: Closure_11_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
private void checkPropertyInheritanceOnGetpropAssign(
    NodeTraversal t, Node assign, Node object, String property,
    JSDocInfo info, JSType propertyType) {
  // Inheritance checks for prototype properties.
  //
  // TODO(nicksantos): This isn't the right place to do this check. We
  // really want to do this when we're looking at the constructor.
  // We'd find all its properties and make sure they followed inheritance
  // rules, like we currently do for @implements to make sure
  // all the methods are implemented.
  //
  // As-is, this misses many other ways to override a property.
  //
  // object.prototype.property = ...;
  if (object.isGetProp()) {
    Node object2 = object.getFirstChild();
    String property2 = NodeUtil.getStringValue(object.getLastChild());

    if ("prototype".equals(property2)) {
      JSType jsType = getJSType(object2);
      if (jsType.isFunctionType()) {
        FunctionType functionType = jsType.toMaybeFunctionType();
        if (functionType.isConstructor() || functionType.isInterface()) {
          checkDeclaredPropertyInheritance(
              t, assign, functionType, property, info, propertyType);
        }
      }
    }
  }
}
 
Example 10
Source File: Closure_118_DisambiguateProperties_s.java    From coming with MIT License 5 votes vote down vote up
@Override
public void recordInterfaces(JSType type, JSType relatedType,
                             DisambiguateProperties<JSType>.Property p) {
  ObjectType objType = ObjectType.cast(type);
  if (objType != null) {
    FunctionType constructor;
    if (objType.isFunctionType()) {
      constructor = objType.toMaybeFunctionType();
    } else if (objType.isFunctionPrototypeType()) {
      constructor = objType.getOwnerFunction();
    } else {
      constructor = objType.getConstructor();
    }
    while (constructor != null) {
      for (ObjectType itype : constructor.getImplementedInterfaces()) {
        JSType top = getTypeWithProperty(p.name, itype);
        if (top != null) {
          p.addType(itype, top, relatedType);
        } else {
          recordInterfaces(itype, relatedType, p);
        }

        // If this interface invalidated this property, return now.
        if (p.skipRenaming) {
          return;
        }
      }
      if (constructor.isInterface() || constructor.isConstructor()) {
        constructor = constructor.getSuperClassConstructor();
      } else {
        constructor = null;
      }
    }
  }
}
 
Example 11
Source File: Closure_96_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a CALL 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 visitCall(NodeTraversal t, Node n) {
  Node child = n.getFirstChild();
  JSType childType = getJSType(child).restrictByNotNullOrUndefined();

  if (!childType.canBeCalled()) {
    report(t, n, NOT_CALLABLE, childType.toString());
    ensureTyped(t, n);
    return;
  }

  // A couple of types can be called as if they were functions.
  // If it is a function type, then validate parameters.
  if (childType instanceof FunctionType) {
    FunctionType functionType = (FunctionType) childType;

    // Non-native constructors should never be called directly.
    if (functionType.isConstructor() &&
        !functionType.isNativeObjectType()) {
      report(t, n, CONSTRUCTOR_NOT_CALLABLE, childType.toString());
    }

    visitParameterList(t, n, functionType);
    ensureTyped(t, n, functionType.getReturnType());
  } else {
    ensureTyped(t, n);
  }

  // TODO: Add something to check for calls of RegExp objects, which is not
  // supported by IE.  Either say something about the return type or warn
  // about the non-portability of the call or both.
}
 
Example 12
Source File: Closure_71_CheckAccessControls_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Normalize the type of a constructor, its instance, and its prototype
 * all down to the same type (the instance type).
 */
private JSType normalizeClassType(JSType type) {
  if (type == null || type.isUnknownType()) {
    return type;
  } else if (type.isConstructor()) {
    return ((FunctionType) type).getInstanceType();
  } else if (type.isFunctionPrototypeType()) {
    FunctionType owner = ((FunctionPrototypeType) type).getOwnerFunction();
    if (owner.isConstructor()) {
      return owner.getInstanceType();
    }
  }
  return type;
}
 
Example 13
Source File: Closure_96_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a CALL 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 visitCall(NodeTraversal t, Node n) {
  Node child = n.getFirstChild();
  JSType childType = getJSType(child).restrictByNotNullOrUndefined();

  if (!childType.canBeCalled()) {
    report(t, n, NOT_CALLABLE, childType.toString());
    ensureTyped(t, n);
    return;
  }

  // A couple of types can be called as if they were functions.
  // If it is a function type, then validate parameters.
  if (childType instanceof FunctionType) {
    FunctionType functionType = (FunctionType) childType;

    // Non-native constructors should never be called directly.
    if (functionType.isConstructor() &&
        !functionType.isNativeObjectType()) {
      report(t, n, CONSTRUCTOR_NOT_CALLABLE, childType.toString());
    }

    visitParameterList(t, n, functionType);
    ensureTyped(t, n, functionType.getReturnType());
  } else {
    ensureTyped(t, n);
  }

  // TODO: Add something to check for calls of RegExp objects, which is not
  // supported by IE.  Either say something about the return type or warn
  // about the non-portability of the call or both.
}
 
Example 14
Source File: Nopol2017_0051_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits a {@link Token#FUNCTION} 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 visitFunction(NodeTraversal t, Node n) {
  FunctionType functionType = (FunctionType) n.getJSType();
  String functionPrivateName = n.getFirstChild().getString();
  if (functionType.isConstructor()) {
    FunctionType baseConstructor = functionType.
        getPrototype().getImplicitPrototype().getConstructor();
    if (baseConstructor != null &&
        baseConstructor != getNativeType(OBJECT_FUNCTION_TYPE) &&
        (baseConstructor.isInterface() && functionType.isConstructor())) {
      compiler.report(
          t.makeError(n, CONFLICTING_EXTENDED_TYPE, functionPrivateName));
    } else {
      // All interfaces are properly implemented by a class
      for (JSType baseInterface : functionType.getImplementedInterfaces()) {
        boolean badImplementedType = false;
        ObjectType baseInterfaceObj = ObjectType.cast(baseInterface);
        if (baseInterfaceObj != null) {
          FunctionType interfaceConstructor =
            baseInterfaceObj.getConstructor();
          if (interfaceConstructor != null &&
              !interfaceConstructor.isInterface()) {
            badImplementedType = true;
          }
        } else {
          badImplementedType = true;
        }
        if (badImplementedType) {
          report(t, n, BAD_IMPLEMENTED_TYPE, functionPrivateName);
        }
      }
      // check properties
      validator.expectAllInterfaceProperties(t, n, functionType);
    }
  } else if (functionType.isInterface()) {
    // Interface must extend only interfaces
    for (ObjectType extInterface : functionType.getExtendedInterfaces()) {
      if (extInterface.getConstructor() != null
          && !extInterface.getConstructor().isInterface()) {
        compiler.report(
            t.makeError(n, CONFLICTING_EXTENDED_TYPE, functionPrivateName));
      }
    }
    // Interface cannot implement any interfaces
    if (functionType.hasImplementedInterfaces()) {
      compiler.report(t.makeError(n,
          CONFLICTING_IMPLEMENTED_TYPE, functionPrivateName));
    }
    // Check whether the extended interfaces have any conflicts
    if (functionType.getExtendedInterfacesCount() > 1) {
      // Only check when extending more than one interfaces
      HashMap<String, ObjectType> properties
          = new HashMap<String, ObjectType>();
      HashMap<String, ObjectType> currentProperties
          = new HashMap<String, ObjectType>();
      for (ObjectType interfaceType : functionType.getExtendedInterfaces()) {
        currentProperties.clear();
        checkInterfaceConflictProperties(t, n, functionPrivateName,
            properties, currentProperties, interfaceType);
        properties.putAll(currentProperties);
      }
    }
  }
}
 
Example 15
Source File: Nopol2017_0051_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits a CALL 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 visitCall(NodeTraversal t, Node n) {
  Node child = n.getFirstChild();
  JSType childType = getJSType(child).restrictByNotNullOrUndefined();

  if (!childType.canBeCalled()) {
    report(t, n, NOT_CALLABLE, childType.toString());
    ensureTyped(t, n);
    return;
  }

  // A couple of types can be called as if they were functions.
  // If it is a function type, then validate parameters.
  if (childType instanceof FunctionType) {
    FunctionType functionType = (FunctionType) childType;

    boolean isExtern = false;
    JSDocInfo functionJSDocInfo = functionType.getJSDocInfo();
    if(functionJSDocInfo != null) {
      String sourceName = functionJSDocInfo.getSourceName();
      CompilerInput functionSource = compiler.getInput(sourceName);
      isExtern = functionSource.isExtern();
    }

    // Non-native constructors should not be called directly
    // unless they specify a return type and are defined
    // in an extern.
    if (functionType.isConstructor() &&
        !functionType.isNativeObjectType() &&
        (functionType.getReturnType().isUnknownType() ||
         functionType.getReturnType().isVoidType() ||
         !isExtern)) {
      report(t, n, CONSTRUCTOR_NOT_CALLABLE, childType.toString());
    }

    // Functions with explcit 'this' types must be called in a GETPROP
    // or GETELEM.
    if (functionType.isOrdinaryFunction() &&
        !functionType.getTypeOfThis().isUnknownType() &&
        !functionType.getTypeOfThis().isNativeObjectType() &&
        !(child.getType() == Token.GETELEM ||
          child.getType() == Token.GETPROP)) {
      report(t, n, EXPECTED_THIS_TYPE, functionType.toString());
    }

    visitParameterList(t, n, functionType);
    ensureTyped(t, n, functionType.getReturnType());
  } else {
    ensureTyped(t, n);
  }

  // TODO: Add something to check for calls of RegExp objects, which is not
  // supported by IE.  Either say something about the return type or warn
  // about the non-portability of the call or both.
}
 
Example 16
Source File: Closure_96_TypeCheck_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Given a constructor type and a property name, check that the property has
 * the JSDoc annotation @override iff the property is declared on a
 * superclass. Several checks regarding inheritance correctness are also
 * performed.
 */
private void checkDeclaredPropertyInheritance(
    NodeTraversal t, Node n, FunctionType ctorType, String propertyName,
    JSDocInfo info, JSType propertyType) {
  // TODO(user): We're not 100% confident that type-checking works,
  // so we return quietly if the unknown type is a superclass of this type.
  // Remove this check as we become more confident. We should flag a warning
  // when the unknown type is on the inheritance chain, as it is likely
  // because of a programmer error.
  if (ctorType.hasUnknownSupertype()) {
    return;
  }

  FunctionType superClass = ctorType.getSuperClassConstructor();
  boolean superClassHasProperty = superClass != null &&
      superClass.getPrototype().hasProperty(propertyName);
  boolean declaredOverride = info != null && info.isOverride();

  boolean foundInterfaceProperty = false;
  if (ctorType.isConstructor()) {
    for (JSType implementedInterface : ctorType.getImplementedInterfaces()) {
      if (implementedInterface.isUnknownType()) {
        continue;
      }
      FunctionType interfaceType =
          implementedInterface.toObjectType().getConstructor();
      boolean interfaceHasProperty =
          interfaceType.getPrototype().hasProperty(propertyName);
      foundInterfaceProperty = foundInterfaceProperty || interfaceHasProperty;
      if (reportMissingOverride.isOn() && !declaredOverride &&
          interfaceHasProperty) {
        // @override not present, but the property does override an interface
        // property
        compiler.report(t.makeError(n, reportMissingOverride,
            HIDDEN_INTERFACE_PROPERTY, propertyName,
            interfaceType.getTopMostDefiningType(propertyName).toString()));
      }
      if (!declaredOverride) {
        continue;
      }
      // @override is present and we have to check that it is ok
      if (interfaceHasProperty) {
        JSType interfacePropType =
            interfaceType.getPrototype().getPropertyType(propertyName);
        if (!propertyType.canAssignTo(interfacePropType)) {
          compiler.report(t.makeError(n,
              HIDDEN_INTERFACE_PROPERTY_MISMATCH, propertyName,
              interfaceType.getTopMostDefiningType(propertyName).toString(),
              interfacePropType.toString(), propertyType.toString()));
        }
      }
    }
  }

  if (!declaredOverride && !superClassHasProperty) {
    // nothing to do here, it's just a plain new property
    return;
  }

  JSType topInstanceType = superClassHasProperty ?
      superClass.getTopMostDefiningType(propertyName) : null;
  if (reportMissingOverride.isOn() && ctorType.isConstructor() &&
      !declaredOverride && superClassHasProperty) {
    // @override not present, but the property does override a superclass
    // property
    compiler.report(t.makeError(n, reportMissingOverride,
        HIDDEN_SUPERCLASS_PROPERTY, propertyName,
        topInstanceType.toString()));
  }
  if (!declaredOverride) {
    // there's no @override to check
    return;
  }
  // @override is present and we have to check that it is ok
  if (superClassHasProperty) {
    // there is a superclass implementation
    JSType superClassPropType =
        superClass.getPrototype().getPropertyType(propertyName);
    if (!propertyType.canAssignTo(superClassPropType)) {
      compiler.report(
          t.makeError(n, HIDDEN_SUPERCLASS_PROPERTY_MISMATCH,
              propertyName, topInstanceType.toString(),
              superClassPropType.toString(), propertyType.toString()));
    }
  } else if (!foundInterfaceProperty) {
    // there is no superclass nor interface implementation
    compiler.report(
        t.makeError(n, UNKNOWN_OVERRIDE,
            propertyName, ctorType.getInstanceType().toString()));
  }
}
 
Example 17
Source File: Closure_11_TypeCheck_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits a CALL 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 visitCall(NodeTraversal t, Node n) {
  Node child = n.getFirstChild();
  JSType childType = getJSType(child).restrictByNotNullOrUndefined();

  if (!childType.canBeCalled()) {
    report(t, n, NOT_CALLABLE, childType.toString());
    ensureTyped(t, n);
    return;
  }

  // A couple of types can be called as if they were functions.
  // If it is a function type, then validate parameters.
  if (childType.isFunctionType()) {
    FunctionType functionType = childType.toMaybeFunctionType();

    boolean isExtern = false;
    JSDocInfo functionJSDocInfo = functionType.getJSDocInfo();
    if( functionJSDocInfo != null  &&
        functionJSDocInfo.getAssociatedNode() != null) {
      isExtern = functionJSDocInfo.getAssociatedNode().isFromExterns();
    }

    // Non-native constructors should not be called directly
    // unless they specify a return type and are defined
    // in an extern.
    if (functionType.isConstructor() &&
        !functionType.isNativeObjectType() &&
        (functionType.getReturnType().isUnknownType() ||
         functionType.getReturnType().isVoidType() ||
         !isExtern)) {
      report(t, n, CONSTRUCTOR_NOT_CALLABLE, childType.toString());
    }

    // Functions with explicit 'this' types must be called in a GETPROP
    // or GETELEM.
    if (functionType.isOrdinaryFunction() &&
        !functionType.getTypeOfThis().isUnknownType() &&
        !functionType.getTypeOfThis().isNativeObjectType() &&
        !(child.isGetElem() ||
          child.isGetProp())) {
      report(t, n, EXPECTED_THIS_TYPE, functionType.toString());
    }

    visitParameterList(t, n, functionType);
    ensureTyped(t, n, functionType.getReturnType());
  } else {
    ensureTyped(t, n);
  }

  // TODO: Add something to check for calls of RegExp objects, which is not
  // supported by IE.  Either say something about the return type or warn
  // about the non-portability of the call or both.
}
 
Example 18
Source File: Closure_95_TypedScopeCreator_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Defines a typed variable. The defining node will be annotated with the
 * variable's type of {@link JSTypeNative#UNKNOWN_TYPE} if its type is
 * inferred.
 *
 * Slots may be any variable or any qualified name in the global scope.
 *
 * @param n the defining NAME or GETPROP node.
 * @param parent the {@code n}'s parent.
 * @param type the variable's type. It may be {@code null} if
 *     {@code inferred} is {@code true}.
 */
void defineSlot(Node n, Node parent, JSType type, boolean inferred) {
  Preconditions.checkArgument(inferred || type != null);

  // Only allow declarations of NAMEs and qualfied names.
  boolean shouldDeclareOnGlobalThis = false;
  if (n.getType() == Token.NAME) {
    Preconditions.checkArgument(
        parent.getType() == Token.FUNCTION ||
        parent.getType() == Token.VAR ||
        parent.getType() == Token.LP ||
        parent.getType() == Token.CATCH);
    shouldDeclareOnGlobalThis = scope.isGlobal() &&
        (parent.getType() == Token.VAR ||
         parent.getType() == Token.FUNCTION);
  } else {
    Preconditions.checkArgument(
        n.getType() == Token.GETPROP &&
        (parent.getType() == Token.ASSIGN ||
         parent.getType() == Token.EXPR_RESULT));
  }
  String variableName = n.getQualifiedName();
  Preconditions.checkArgument(!variableName.isEmpty());

  // If n is a property, then we should really declare it in the
  // scope where the root object appears. This helps out people
  // who declare "global" names in an anonymous namespace.
  Scope scopeToDeclareIn = scope;

    // don't try to declare in the global scope if there's
    // already a symbol there with this name.

  // declared in closest scope?
  if (scopeToDeclareIn.isDeclared(variableName, false)) {
    Var oldVar = scopeToDeclareIn.getVar(variableName);
    validator.expectUndeclaredVariable(
        sourceName, n, parent, oldVar, variableName, type);
  } else {
    if (!inferred) {
      setDeferredType(n, type);
    }
    CompilerInput input = compiler.getInput(sourceName);
    scopeToDeclareIn.declare(variableName, n, type, input, inferred);

    if (shouldDeclareOnGlobalThis) {
      ObjectType globalThis =
          typeRegistry.getNativeObjectType(JSTypeNative.GLOBAL_THIS);
      boolean isExtern = input.isExtern();
      if (inferred) {
        globalThis.defineInferredProperty(variableName,
            type == null ?
                getNativeType(JSTypeNative.NO_TYPE) :
                type,
            isExtern);
      } else {
        globalThis.defineDeclaredProperty(variableName, type, isExtern);
      }
    }

    // If we're in the global scope, also declare var.prototype
    // in the scope chain.
    if (scopeToDeclareIn.isGlobal() && type instanceof FunctionType) {
      FunctionType fnType = (FunctionType) type;
      if (fnType.isConstructor() || fnType.isInterface()) {
        FunctionType superClassCtor = fnType.getSuperClassConstructor();
        scopeToDeclareIn.declare(variableName + ".prototype", n,
            fnType.getPrototype(), compiler.getInput(sourceName),
            /* declared iff there's an explicit supertype */
            superClassCtor == null ||
            superClassCtor.getInstanceType().equals(
                getNativeType(OBJECT_TYPE)));
      }
    }
  }
}
 
Example 19
Source File: Closure_125_TypeCheck_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits a {@link Token#FUNCTION} 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 visitFunction(NodeTraversal t, Node n) {
  FunctionType functionType = JSType.toMaybeFunctionType(n.getJSType());
  String functionPrivateName = n.getFirstChild().getString();
  if (functionType.isConstructor()) {
    FunctionType baseConstructor = functionType.getSuperClassConstructor();
    if (baseConstructor != getNativeType(OBJECT_FUNCTION_TYPE) &&
        baseConstructor != null &&
        baseConstructor.isInterface()) {
      compiler.report(
          t.makeError(n, CONFLICTING_EXTENDED_TYPE,
                      "constructor", functionPrivateName));
    } else {
      if (baseConstructor != getNativeType(OBJECT_FUNCTION_TYPE)) {
        ObjectType proto = functionType.getPrototype();
        if (functionType.makesStructs() && !proto.isStruct()) {
          compiler.report(t.makeError(n, CONFLICTING_SHAPE_TYPE,
                                      "struct", functionPrivateName));
        } else if (functionType.makesDicts() && !proto.isDict()) {
          compiler.report(t.makeError(n, CONFLICTING_SHAPE_TYPE,
                                      "dict", functionPrivateName));
        }
      }
      // All interfaces are properly implemented by a class
      for (JSType baseInterface : functionType.getImplementedInterfaces()) {
        boolean badImplementedType = false;
        ObjectType baseInterfaceObj = ObjectType.cast(baseInterface);
        if (baseInterfaceObj != null) {
          FunctionType interfaceConstructor =
            baseInterfaceObj.getConstructor();
          if (interfaceConstructor != null &&
              !interfaceConstructor.isInterface()) {
            badImplementedType = true;
          }
        } else {
          badImplementedType = true;
        }
        if (badImplementedType) {
          report(t, n, BAD_IMPLEMENTED_TYPE, functionPrivateName);
        }
      }
      // check properties
      validator.expectAllInterfaceProperties(t, n, functionType);
    }
  } else if (functionType.isInterface()) {
    // Interface must extend only interfaces
    for (ObjectType extInterface : functionType.getExtendedInterfaces()) {
      if (extInterface.getConstructor() != null
          && !extInterface.getConstructor().isInterface()) {
        compiler.report(
            t.makeError(n, CONFLICTING_EXTENDED_TYPE,
                        "interface", functionPrivateName));
      }
    }

    // Check whether the extended interfaces have any conflicts
    if (functionType.getExtendedInterfacesCount() > 1) {
      // Only check when extending more than one interfaces
      HashMap<String, ObjectType> properties
          = new HashMap<String, ObjectType>();
      HashMap<String, ObjectType> currentProperties
          = new HashMap<String, ObjectType>();
      for (ObjectType interfaceType : functionType.getExtendedInterfaces()) {
        currentProperties.clear();
        checkInterfaceConflictProperties(t, n, functionPrivateName,
            properties, currentProperties, interfaceType);
        properties.putAll(currentProperties);
      }
    }
  }
}
 
Example 20
Source File: Closure_69_TypeCheck_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits a {@link Token#FUNCTION} 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 visitFunction(NodeTraversal t, Node n) {
  FunctionType functionType = (FunctionType) n.getJSType();
  String functionPrivateName = n.getFirstChild().getString();
  if (functionType.isConstructor()) {
    FunctionType baseConstructor = functionType.
        getPrototype().getImplicitPrototype().getConstructor();
    if (baseConstructor != null &&
        baseConstructor != getNativeType(OBJECT_FUNCTION_TYPE) &&
        (baseConstructor.isInterface() && functionType.isConstructor())) {
      compiler.report(
          t.makeError(n, CONFLICTING_EXTENDED_TYPE, functionPrivateName));
    } else {
      // All interfaces are properly implemented by a class
      for (JSType baseInterface : functionType.getImplementedInterfaces()) {
        boolean badImplementedType = false;
        ObjectType baseInterfaceObj = ObjectType.cast(baseInterface);
        if (baseInterfaceObj != null) {
          FunctionType interfaceConstructor =
            baseInterfaceObj.getConstructor();
          if (interfaceConstructor != null &&
              !interfaceConstructor.isInterface()) {
            badImplementedType = true;
          }
        } else {
          badImplementedType = true;
        }
        if (badImplementedType) {
          report(t, n, BAD_IMPLEMENTED_TYPE, functionPrivateName);
        }
      }
      // check properties
      validator.expectAllInterfaceProperties(t, n, functionType);
    }
  } else if (functionType.isInterface()) {
    // Interface must extend only interfaces
    for (ObjectType extInterface : functionType.getExtendedInterfaces()) {
      if (extInterface.getConstructor() != null
          && !extInterface.getConstructor().isInterface()) {
        compiler.report(
            t.makeError(n, CONFLICTING_EXTENDED_TYPE, functionPrivateName));
      }
    }
    // Interface cannot implement any interfaces
    if (functionType.hasImplementedInterfaces()) {
      compiler.report(t.makeError(n,
          CONFLICTING_IMPLEMENTED_TYPE, functionPrivateName));
    }
    // Check whether the extended interfaces have any conflicts
    if (functionType.getExtendedInterfacesCount() > 1) {
      // Only check when extending more than one interfaces
      HashMap<String, ObjectType> properties
          = new HashMap<String, ObjectType>();
      HashMap<String, ObjectType> currentProperties
          = new HashMap<String, ObjectType>();
      for (ObjectType interfaceType : functionType.getExtendedInterfaces()) {
        currentProperties.clear();
        checkInterfaceConflictProperties(t, n, functionPrivateName,
            properties, currentProperties, interfaceType);
        properties.putAll(currentProperties);
      }
    }
  }
}