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

The following examples show how to use com.google.javascript.rhino.jstype.JSType#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: AbstractClosureVisitor.java    From jsinterop-generator with Apache License 2.0 6 votes vote down vote up
private void accept(StaticTypedSlot var, boolean isStatic) {
  JSType type = var.getType();

  if ((type.isInterface() || type.isConstructor())
      && !type.isInstanceType()
      && toFunctionType(type).getSource() != null
      && !isTypeAlias(var)) {
    acceptClassOrInterface(toFunctionType(type));
  } else if (isModule(type)) {
    acceptModule(var);
  } else if (isTypedef(var)) {
    acceptTypedef(var);
  } else if (type.isEnumType()) {
    acceptEnumType(toEnumType(type));
  } else if (isTypeAlias(var)) {
    // We don't process type alias.
    //   /** @constructor */ function Foo() {};
    //   /** @const */ var Bar = Foo;
  } else {
    acceptMember(var, isStatic);
  }
}
 
Example 2
Source File: DeclarationGenerator.java    From clutz with MIT License 6 votes vote down vote up
private boolean needsAlias(Set<String> shadowedSymbols, String provide, TypedVar symbol) {
  if (!shadowedSymbols.contains(provide)) {
    return false;
  }
  // Emit var foo : any for provided but not declared symbols.
  if (symbol == null) {
    return true;
  }
  JSType type = symbol.getType();
  if (type == null) {
    return false;
  }

  // Emit var foo : PrivateType for private symbols.
  if (isPrivate(type.getJSDocInfo()) && !isConstructor(type.getJSDocInfo())) {
    return true;
  }
  // Only var declarations have collisions, while class, interface, function, and typedef can
  // coexist with namespaces.
  if (type.isInterface() || type.isConstructor() || type.isFunctionType() || isTypedef(type)) {
    return false;
  }
  return isDefaultExport(symbol);
}
 
Example 3
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
Map<String, InstanceProperty> getInstanceProperties(JSType type) {
  Map<String, InstanceProperty> properties = new HashMap<>();

  if (type.isConstructor() || type.isInterface()) {
    type = ((FunctionType) type).getInstanceType();
  }

  ObjectType object = type.toObjectType();
  FunctionType ctor = object.getConstructor();
  if (ctor != null) {
    ObjectType prototype = ctor.getPrototype();
    verify(prototype != null);
    properties = getOwnProperties(prototype);
  }
  properties.putAll(getOwnProperties(object));
  return properties;
}
 
Example 4
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
/** Finds the properties defined on a class. */
@Nullable
@CheckReturnValue
private InstanceProperty findFirstClassOverride(Iterable<InstanceProperty> properties) {
  for (InstanceProperty property : properties) {
    JSType definedOn = property.getDefinedByType();
    if (definedOn.isInterface()) {
      continue;
    } else if (definedOn.isInstanceType()) {
      FunctionType ctor = definedOn.toObjectType().getConstructor();
      if (ctor != null && ctor.isInterface()) {
        continue;
      }
    }
    return property;
  }
  return null;
}
 
Example 5
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
private NamedType getPropertyLink(NominalType context, InstanceProperty property) {
  JSType type = property.getDefinedByType();
  TypeExpressionParser parser =
      expressionParserFactory.create(linkFactory.withTypeContext(context));

  if (property.getOwnerType().isPresent()) {
    type = property.getOwnerType().get().getType();
  }

  if (type.isConstructor() || type.isInterface()) {
    type = ((FunctionType) type).getInstanceType();
  }
  type = stripTemplateTypeInformation(type);

  return buildPropertyLink(parser, type, property.getName());
}
 
Example 6
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
private void resolveNames(Node node) {
  if (node.isName() || node.isString()) {
    String name = node.getString();
    if (registry.isType(name)) {
      NominalType nominalType = registry.getType(name);
      JSType jsType = nominalType.getType();
      if (jsType.isConstructor() || jsType.isInterface()) {
        String referenceName = jsType.toMaybeObjectType().getReferenceName();
        if (!name.equals(referenceName)
            && !isNullOrEmpty(referenceName)
            && !jsRegistry.getGlobalType(referenceName).isNamedType()) {
          node.setString(referenceName);
        }
      }
    }
  }

  for (Node child = node.getFirstChild(); child != null; child = child.getNext()) {
    resolveNames(child);
  }
}
 
Example 7
Source File: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
/**
 * A type representing an object literal that is inferred through goog.provide(...) or @const {}
 * and assigning properties to it.
 *
 * <p>We start with object types and narrow away known non-namespace types.
 *
 * <p>The terminology "namespace" type is non-standard.
 */
private static boolean isNamespaceType(JSType type) {
  if (!type.isObject()) return false;
  return !type.isInterface()
      && !type.isInstanceType()
      && !type.isEnumType()
      && !type.isFunctionType()
      && !isTypedef(type);
}
 
Example 8
Source File: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
/**
 * Returns true if {@code propType} is creating a new type in the TypeScript sense - i.e. it's a
 * constructor function (class or interface), enum, or typedef.
 */
private boolean isDefiningType(JSType propType) {
  return isClassLike(propType)
      || propType.isEnumType()
      || propType.isInterface()
      || isTypedef(propType);
}
 
Example 9
Source File: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
/**
 * Returns true for types that are class like, i.e. that define a constructor or interface, but
 * excludes typedefs and the built-in constructor functions such as {@code Function}.
 */
private boolean isClassLike(JSType propType) {
  // Confusingly, the typedef type returns true on isConstructor checks, so we need to filter
  // the NoType through this utility method.
  return !isTypedef(propType)
      && (propType.isConstructor() || propType.isInterface())
      // "Function" is a constructor, but does not define a new type for our purposes.
      && !propType.toMaybeObjectType().isNativeObjectType()
      && !propType.isFunctionPrototypeType();
}
 
Example 10
Source File: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
private boolean isAliasedClassOrInterface(TypedVar symbol, JSType type) {
  // Confusingly typedefs are constructors. However, they cannot be aliased AFAICT.
  if (type.isNoType()) return false;
  if (!type.isConstructor() && !type.isInterface()) return false;
  String symbolName = symbol.getName();
  String typeName = type.getDisplayName();
  // Turns out that for aliases the symbol and type name differ.
  return !symbolName.equals(typeName) || KNOWN_CLASS_ALIASES.containsKey(symbolName);
}
 
Example 11
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
TypeInspector(
    @Provided DossierFileSystem dfs,
    @Provided CommentParser parser,
    @Provided TypeRegistry registry,
    @Provided StaticTypedScope globalScope,
    @Provided JSTypeRegistry jsRegistry,
    @Provided @TypeFilter Predicate<String> typeFilter,
    @Provided TypeExpressionParserFactory expressionParserFactory,
    @Provided LinkFactoryBuilder linkFactoryBuilder,
    NominalType inspectedType) {
  this.dfs = dfs;
  this.parser = parser;
  this.registry = registry;
  this.globalScope = globalScope;
  this.jsRegistry = jsRegistry;
  this.expressionParserFactory = expressionParserFactory;
  this.typeFilter = typeFilter;
  this.linkFactory = linkFactoryBuilder.create(inspectedType);
  this.inspectedType = inspectedType;

  JSType type = inspectedType.getType();
  if (type.isConstructor() || type.isInterface()) {
    type = type.toMaybeFunctionType().getInstanceType();
  } else {
    type = jsRegistry.getNativeType(JSTypeNative.UNKNOWN_TYPE);
  }
  typeMapReplacer =
      TemplateTypeReplacer.forPartialReplacement(jsRegistry, type.getTemplateTypeMap());
}
 
Example 12
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
@Nullable
private DefinedByType getDefinedByComment(
    final LinkFactory linkFactory,
    final NominalType context,
    JSType currentType,
    InstanceProperty property) {
  JSType propertyDefinedOn = property.getDefinedByType();

  if (propertyDefinedOn.isConstructor() || propertyDefinedOn.isInterface()) {
    propertyDefinedOn = propertyDefinedOn.toMaybeFunctionType().getInstanceType();
  }
  if (currentType.equals(propertyDefinedOn)) {
    return null;
  }

  JSType definedByType = stripTemplateTypeInformation(propertyDefinedOn);

  List<NominalType> types = registry.getTypes(definedByType);
  if (types.isEmpty() && definedByType.isInstanceType()) {
    types = registry.getTypes(definedByType.toObjectType().getConstructor());
  }

  TypeExpressionParser parser =
      expressionParserFactory.create(linkFactory.withTypeContext(context));

  if (!types.isEmpty()) {
    definedByType = types.get(0).getType();
    if (definedByType.isConstructor() || definedByType.isInterface()) {
      definedByType =
          stripTemplateTypeInformation(definedByType.toMaybeFunctionType().getInstanceType());
    }
  }

  NamedType type = buildPropertyLink(parser, definedByType, property.getName());
  return DefinedByType.create(
      type, ObjectType.cast(definedByType).getConstructor().isInterface());
}
 
Example 13
Source File: TypeRegistry.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
/**
 * Recomputes the type hierarchy relationships for all nominal types in this registry using the
 * given global scope and JS registry.
 */
public void computeTypeRelationships(StaticTypedScope globalScope, JSTypeRegistry jsRegistry) {
  checkArgument(globalScope.getParentScope() == null, "not a global scope");

  knownImplementations.clear();
  subInterfaces.clear();
  directSubtypes.clear();
  implementedInterfaces.clear();

  Set<FunctionType> processed = new HashSet<>();
  for (NominalType nominalType : typesByName.values()) {
    JSType jsType = nominalType.getType();
    if (!jsType.isConstructor() && !jsType.isInterface()) {
      continue;
    }

    FunctionType ctor = jsType.toMaybeFunctionType();
    if (ctor == null || !processed.add(ctor)) {
      continue;
    }

    if (ctor.isInterface()) {
      scanExtendedInterfaces(new HashSet<>(), ctor);

    } else {
      scanImplementedInterfaces(ctor);
      computeTypeHiearchy(ctor, globalScope, jsRegistry);
    }
  }
}
 
Example 14
Source File: TypeRegistry.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private JSType getSuperInstance(
    ObjectType instance,
    FunctionType ctor,
    StaticTypedScope globalScope,
    JSTypeRegistry jsRegistry) {
  JSType superInstance;
  if (ctor.getJSDocInfo() != null && ctor.getJSDocInfo().getBaseType() != null) {
    List<TemplateType> templateTypes = instance.getTemplateTypeMap().getTemplateKeys();
    StaticTypedScope scope =
        templateTypes.isEmpty()
            ? globalScope
            : jsRegistry.createScopeWithTemplates(globalScope, templateTypes);

    JSTypeExpression baseTypeExpression = ctor.getJSDocInfo().getBaseType();
    superInstance = Types.evaluate(baseTypeExpression, scope, jsRegistry);

    // The type expression will resolve to a named type if it is an aliased reference to
    // a module's exported type. Compensate by checking dossier's type registry, which
    // tracks exported types by their exported name (whereas the compiler tracks them by
    // their initially declared name from within the module).
    if (superInstance.isNamedType()
        && isType(superInstance.toMaybeNamedType().getReferenceName())) {
      superInstance = getType(superInstance.toMaybeNamedType().getReferenceName()).getType();
      if (superInstance.isConstructor() || superInstance.isInterface()) {
        superInstance = superInstance.toMaybeFunctionType().getTypeOfThis();
      }
    }

  } else {
    FunctionType superCtor = ctor.getSuperClassConstructor();
    if (superCtor == null) {
      return null;
    }
    superInstance = superCtor.getTypeOfThis();
  }
  return superInstance;
}
 
Example 15
Source File: AbstractTypeInspectorTest.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
public void isInstanceMethod(JSType typeOfThis) {
  JSType type = actual().getType();
  assertWithMessage("not a function").that(type.isFunctionType()).isTrue();

  if (typeOfThis.isConstructor() || typeOfThis.isInterface()) {
    typeOfThis = ((FunctionType) typeOfThis).getInstanceType();
  }
  FunctionType function = (FunctionType) type;
  assertWithMessage("wrong type of this").that(function.getTypeOfThis()).isEqualTo(typeOfThis);
}
 
Example 16
Source File: TypeCollectionPass.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
private void recordType(NominalType type) {
  if (externTypes.contains(type.getType()) && !type.getJsDoc().isTypedef()) {
    logfmt("Skipping extern alias: %s", type.getName());
    return;
  }

  if (type.getName().contains("$$")) {
    int index = type.getName().indexOf("$$");
    String id = type.getName().substring(index + 2);
    if (typeRegistry.isModule(id)) {
      Module module = typeRegistry.getModule(id);
      if (module.isEs6()) {
        for (AliasRegion region : typeRegistry.getAliasRegions(type.getSourceFile())) {
          if (region.getRange().contains(type.getSourcePosition())) {
            String alias = type.getName().substring(0, index);
            region.addAlias(alias, type.getName());
          }
        }
      }
      logfmt("Skipping module alias: %s", type.getName());
      return;
    }
  }

  JSType jsType = type.getType();
  if (!type.getModule().isPresent()
      && !jsType.isConstructor()
      && !jsType.isInterface()
      && !jsType.isEnumType()
      && !type.getJsDoc().isTypedef()
      && !type.getJsDoc().isDefine()
      && !typeRegistry.isProvided(type.getName())
      && !typeRegistry.isImplicitNamespace(type.getName())
      && symbolTable.getSlot(type.getName()) == null) {
    logfmt("Ignoring undeclared namespace %s", type.getName());
    return;
  }

  if (!typeRegistry.getTypes(type.getType()).isEmpty()) {
    logfmt(
        "Found type alias: %s = %s",
        type.getName(), typeRegistry.getTypes(type.getType()).iterator().next().getName());
  }
  typeRegistry.addType(type);
}