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

The following examples show how to use com.google.javascript.rhino.jstype.FunctionType#getInstanceType() . 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: ClosureCodingConvention.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the type for a type assertion, or null if the function asserts
 * that the node must not be null or undefined.
 */
@Override
public JSType getAssertedType(Node call, JSTypeRegistry registry) {
  if (call.getChildCount() > 2) {
    Node constructor = call.getFirstChild().getNext().getNext();
    if (constructor != null) {
      JSType ownerType = constructor.getJSType();
      if (ownerType != null
          && ownerType.isFunctionType()
          && ownerType.isConstructor()) {
        FunctionType functionType = ((FunctionType) ownerType);
        return functionType.getInstanceType();
      }
    }
  }
  return super.getAssertedType(call, registry);
}
 
Example 2
Source File: TypedScopeCreatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testAbstractMethod() {
  testSame(
      "/** @type {!Function} */ var abstractMethod;" +
      "/** @constructor */ function Foo() {}" +
      "/** @param {number} x */ Foo.prototype.bar = abstractMethod;");
  assertEquals(
      "Function", findNameType("abstractMethod", globalScope).toString());

  FunctionType ctor = (FunctionType) findNameType("Foo", globalScope);
  ObjectType instance = ctor.getInstanceType();
  assertEquals("Foo", instance.toString());

  ObjectType proto = instance.getImplicitPrototype();
  assertEquals("Foo.prototype", proto.toString());

  assertEquals(
      "function (this:Foo, number): ?",
      proto.getPropertyType("bar").toString());
}
 
Example 3
Source File: TypedScopeCreatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testAbstractMethod4() {
  testSame(
      "/** @type {!Function} */ var abstractMethod;" +
      "/** @constructor */ function Foo() {}" +
      "Foo.prototype = {/** @param {number} x */ bar: abstractMethod};");
  assertEquals(
      "Function", findNameType("abstractMethod", globalScope).toString());

  FunctionType ctor = (FunctionType) findNameType("Foo", globalScope);
  ObjectType instance = ctor.getInstanceType();
  assertEquals("Foo", instance.toString());

  ObjectType proto = instance.getImplicitPrototype();
  assertEquals("Foo.prototype", proto.toString());

  assertEquals(
      // should be: "function (this:Foo, number): ?"
      "function (this:Foo, number): ?",
      proto.getPropertyType("bar").toString());
}
 
Example 4
Source File: Closure_117_TypeValidator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that all properties on interfaces that this type implements are
 * implemented and correctly typed.
 */
void expectAllInterfaceProperties(NodeTraversal t, Node n,
    FunctionType type) {
  ObjectType instance = type.getInstanceType();
  for (ObjectType implemented : type.getAllImplementedInterfaces()) {
    if (implemented.getImplicitPrototype() != null) {
      for (String prop :
           implemented.getImplicitPrototype().getOwnPropertyNames()) {
        expectInterfaceProperty(t, n, instance, implemented, prop);
      }
    }
  }
}
 
Example 5
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that all properties on interfaces that this type implements are
 * implemented and correctly typed.
 */
void expectAllInterfaceProperties(NodeTraversal t, Node n,
    FunctionType type) {
  ObjectType instance = type.getInstanceType();
  for (ObjectType implemented : type.getAllImplementedInterfaces()) {
    if (implemented.getImplicitPrototype() != null) {
      for (String prop :
           implemented.getImplicitPrototype().getOwnPropertyNames()) {
        expectInterfaceProperty(t, n, instance, implemented, prop);
      }
    }
  }
}
 
Example 6
Source File: Closure_6_TypeValidator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that all properties on interfaces that this type implements are
 * implemented and correctly typed.
 */
void expectAllInterfaceProperties(NodeTraversal t, Node n,
    FunctionType type) {
  ObjectType instance = type.getInstanceType();
  for (ObjectType implemented : type.getAllImplementedInterfaces()) {
    if (implemented.getImplicitPrototype() != null) {
      for (String prop :
           implemented.getImplicitPrototype().getOwnPropertyNames()) {
        expectInterfaceProperty(t, n, instance, implemented, prop);
      }
    }
  }
}
 
Example 7
Source File: Closure_6_TypeValidator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that all properties on interfaces that this type implements are
 * implemented and correctly typed.
 */
void expectAllInterfaceProperties(NodeTraversal t, Node n,
    FunctionType type) {
  ObjectType instance = type.getInstanceType();
  for (ObjectType implemented : type.getAllImplementedInterfaces()) {
    if (implemented.getImplicitPrototype() != null) {
      for (String prop :
           implemented.getImplicitPrototype().getOwnPropertyNames()) {
        expectInterfaceProperty(t, n, instance, implemented, prop);
      }
    }
  }
}
 
Example 8
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 9
Source File: Closure_71_CheckAccessControls_t.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 10
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Expect that all properties on interfaces that this type implements are
 * implemented and correctly typed.
 */
void expectAllInterfaceProperties(NodeTraversal t, Node n,
    FunctionType type) {
  ObjectType instance = type.getInstanceType();
  for (ObjectType implemented : type.getAllImplementedInterfaces()) {
    if (implemented.getImplicitPrototype() != null) {
      for (String prop :
           implemented.getImplicitPrototype().getOwnPropertyNames()) {
        expectInterfaceProperty(t, n, instance, implemented, prop);
      }
    }
  }
}
 
Example 11
Source File: InlineProperties.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private JSType maybeGetInstanceTypeFromPrototypeRef(Node src) {
  JSType ownerType = getJSType(src.getFirstChild());
  if (ownerType.isFunctionType() && ownerType.isConstructor()) {
    FunctionType functionType = ((FunctionType) ownerType);
    return functionType.getInstanceType();
  }
  return null;
}
 
Example 12
Source File: CheckAccessControls.java    From astor with GNU General Public License v2.0 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.isNominalConstructor()) {
    return (type.toMaybeFunctionType()).getInstanceType();
  } else if (type.isFunctionPrototypeType()) {
    FunctionType owner = ((ObjectType) type).getOwnerFunction();
    if (owner.isConstructor()) {
      return owner.getInstanceType();
    }
  }
  return type;
}
 
Example 13
Source File: SemanticReverseAbstractInterpreter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public JSType caseUnknownType() {
  FunctionType funcTarget = JSType.toMaybeFunctionType(target);
  if (funcTarget != null && funcTarget.hasInstanceType()) {
    return funcTarget.getInstanceType();
  }
  return getNativeType(UNKNOWN_TYPE);
}
 
Example 14
Source File: AmbiguateProperties.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds the instance of the given constructor, its implicit prototype and all
 * its related types to the given bit set.
 */
private void addRelatedInstance(
    FunctionType constructor, JSTypeBitSet related) {
  // TODO(user): A constructor which doesn't have an instance type
  // (e.g. it's missing the @constructor annotation) should be an invalidating
  // type which doesn't reach this code path.
  if (constructor.hasInstanceType()) {
    ObjectType instanceType = constructor.getInstanceType();
    related.set(getIntForType(instanceType.getImplicitPrototype()));
    computeRelatedTypes(instanceType);
    related.or(relatedBitsets.get(instanceType));
  }
}
 
Example 15
Source File: TypeRegistry.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private JSType getInstanceType(final JSTypeRegistry jsRegistry, FunctionType ctor) {
  ObjectType instance = ctor.getInstanceType();
  if (ctor.getJSDocInfo() != null && !ctor.getJSDocInfo().getTemplateTypeNames().isEmpty()) {
    ImmutableList<JSType> templateTypes =
        ctor.getJSDocInfo()
            .getTemplateTypeNames()
            .stream()
            .map(jsRegistry::createTemplateType)
            .collect(toImmutableList());
    instance = jsRegistry.createTemplatizedType(instance, templateTypes);
  }
  return instance;
}
 
Example 16
Source File: SymbolTable.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Gets the symbol for the given constructor or interface.
 */
public Symbol getSymbolDeclaredBy(FunctionType fn) {
  Preconditions.checkState(fn.isConstructor() || fn.isInterface());
  ObjectType instanceType = fn.getInstanceType();
  return getSymbolForName(fn.getSource(), instanceType.getReferenceName());
}