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

The following examples show how to use com.google.javascript.rhino.jstype.FunctionType#isInterface() . 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_117_TypeValidator_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Expect that the first type can be assigned to a symbol of the second
 * type.
 *
 * @param t The node traversal.
 * @param n The node to issue warnings on.
 * @param rightType The type on the RHS of the assign.
 * @param leftType The type of the symbol on the LHS of the assign.
 * @param owner The owner of the property being assigned to.
 * @param propName The name of the property being assigned to.
 * @return True if the types matched, false otherwise.
 */
boolean expectCanAssignToPropertyOf(NodeTraversal t, Node n, JSType rightType,
    JSType leftType, Node owner, String propName) {
  // The NoType check is a hack to make typedefs work OK.
  if (!leftType.isNoType() && !rightType.isSubtype(leftType)) {
    // Do not type-check interface methods, because we expect that
    // they will have dummy implementations that do not match the type
    // annotations.
    JSType ownerType = getJSType(owner);
    if (ownerType.isFunctionPrototypeType()) {
      FunctionType ownerFn = ownerType.toObjectType().getOwnerFunction();
      if (ownerFn.isInterface() &&
          rightType.isFunctionType() && leftType.isFunctionType()) {
        return true;
      }
    }

    mismatch(t, n,
        "assignment to property " + propName + " of " +
        getReadableJSTypeName(owner, true),
        rightType, leftType);
    return false;
  }
  return true;
}
 
Example 2
Source File: Closure_6_TypeValidator_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Expect that the first type can be assigned to a symbol of the second
 * type.
 *
 * @param t The node traversal.
 * @param n The node to issue warnings on.
 * @param rightType The type on the RHS of the assign.
 * @param leftType The type of the symbol on the LHS of the assign.
 * @param owner The owner of the property being assigned to.
 * @param propName The name of the property being assigned to.
 * @return True if the types matched, false otherwise.
 */
boolean expectCanAssignToPropertyOf(NodeTraversal t, Node n, JSType rightType,
    JSType leftType, Node owner, String propName) {
  // The NoType check is a hack to make typedefs work OK.
  if (!leftType.isNoType() && !rightType.canAssignTo(leftType)) {
    // Do not type-check interface methods, because we expect that
    // they will have dummy implementations that do not match the type
    // annotations.
    JSType ownerType = getJSType(owner);
    if (ownerType.isFunctionPrototypeType()) {
      FunctionType ownerFn = ownerType.toObjectType().getOwnerFunction();
      if (ownerFn.isInterface() &&
          rightType.isFunctionType() && leftType.isFunctionType()) {
        return true;
      }
    }

    mismatch(t, n,
        "assignment to property " + propName + " of " +
        getReadableJSTypeName(owner, true),
        rightType, leftType);
    return false;
  }
  return true;
}
 
Example 3
Source File: DisambiguateProperties.java    From astor with GNU General Public License v2.0 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 4
Source File: Closure_103_DisambiguateProperties_t.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 5
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 6
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 7
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 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: Nopol2017_0029_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 10
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 11
Source File: InheritanceVisitor.java    From jsinterop-generator with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean visitClassOrInterface(FunctionType type) {
  Type javaType = getCurrentJavaType();

  if (type.isInterface() && type.getExtendedInterfacesCount() > 0) {
    javaType.getExtendedTypes().addAll(createTypeReferences(type.getExtendedInterfaces()));
  } else if (type.isConstructor()) {
    getSuperType(type).ifPresent(t -> javaType.getExtendedTypes().add(createTypeReference(t)));
    javaType
        .getImplementedTypes()
        .addAll(createTypeReferences(type.getOwnImplementedInterfaces()));
  }

  return false;
}
 
Example 12
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 13
Source File: Closure_69_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 = (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 14
Source File: Closure_2_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_EXTENDED_TYPE,
                                      "struct", functionPrivateName));
        } else if (functionType.makesDicts() && !proto.isDict()) {
          compiler.report(t.makeError(n, CONFLICTING_EXTENDED_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 15
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
private Function getFunctionData(
    String name,
    FunctionType function,
    Node node,
    PropertyDocs docs,
    @Nullable DefinedByType definedBy,
    Iterable<InstanceProperty> overrides) {
  boolean isConstructor = function.isConstructor() && !isFunctionTypeConstructor(function);
  boolean isInterface = !isConstructor && function.isInterface();

  Function.Builder builder =
      Function.newBuilder()
          .setBase(getBasePropertyDetails(name, function, node, docs, definedBy, overrides));

  if (isConstructor) {
    builder.setIsConstructor(true);
  }

  if (!isConstructor && !isInterface) {
    PropertyDocs returnDocs =
        findPropertyDocs(docs, overrides, input -> input.hasAnnotation(Annotation.RETURN));

    if (returnDocs != null) {
      Comment returnComment =
          parser.parseComment(
              returnDocs.getJsDoc().getReturnClause().getDescription(),
              linkFactory.withTypeContext(returnDocs.getContextType()));
      if (returnComment.getTokenCount() > 0) {
        builder.getReturnBuilder().setDescription(returnComment);
      }
    }

    TypeExpression returnType = getReturnType(docs, overrides, function);
    if (returnType != null) {
      builder.getReturnBuilder().setType(returnType);
    }
  }

  builder
      .addAllTemplateName(docs.getJsDoc().getTemplateTypeNames())
      .addAllThrown(buildThrowsData(docs.getContextType(), node, docs.getJsDoc()))
      .addAllParameter(getParameters(function, node, docs, overrides));

  return builder.build();
}
 
Example 16
Source File: TypeCheck.java    From astor with GNU General Public License v2.0 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_EXTENDED_TYPE,
                                      "struct", functionPrivateName));
        } else if (functionType.makesDicts() && !proto.isDict()) {
          compiler.report(t.makeError(n, CONFLICTING_EXTENDED_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 17
Source File: Nopol2017_0029_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_EXTENDED_TYPE,
                                      "struct", functionPrivateName));
        } else if (functionType.makesDicts() && !proto.isDict()) {
          compiler.report(t.makeError(n, CONFLICTING_EXTENDED_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();
        if (com.google.javascript.jscomp.TypeCheck.this.unknownCount < com.google.javascript.jscomp.TypeCheck.this.typedCount) {
          checkInterfaceConflictProperties(t, n, functionPrivateName,
          properties, currentProperties, interfaceType);
        }
        properties.putAll(currentProperties);
      }
    }
  }
}
 
Example 18
Source File: Closure_95_TypedScopeCreator_t.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;
  if (n.getType() == Token.GETPROP && !scope.isGlobal() &&
      isQnameRootedInGlobalScope(n)) {
    Scope globalScope = scope.getGlobalScope();

    // don't try to declare in the global scope if there's
    // already a symbol there with this name.
    if (!globalScope.isDeclared(variableName, false)) {
      scopeToDeclareIn = scope.getGlobalScope();
    }
  }

  // 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_41_FunctionTypeBuilder_s.java    From coming with MIT License 4 votes vote down vote up
private void maybeSetBaseType(FunctionType fnType) {
  if (!fnType.isInterface() && baseType != null) {
    fnType.setPrototypeBasedOn(baseType);
  }
}
 
Example 20
Source File: FunctionTypeBuilder.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private void maybeSetBaseType(FunctionType fnType) {
  if (!fnType.isInterface() && baseType != null) {
    fnType.setPrototypeBasedOn(baseType);
  }
}