Java Code Examples for com.google.javascript.rhino.jstype.ObjectType#isFunctionPrototypeType()

The following examples show how to use com.google.javascript.rhino.jstype.ObjectType#isFunctionPrototypeType() . 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: TightenTypes.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private Collection<Action> getImplicitActionsFromPropNonUnion(
    ObjectType jsType, String prop, Node fnNode) {
  JSType propType = jsType.getPropertyType(prop)
      .restrictByNotNullOrUndefined();
  if (jsType.isPropertyInExterns(prop) && propType.isFunctionType()) {
    ObjectType thisType = jsType;
    if (jsType.isFunctionPrototypeType()) {
      thisType = thisType.getOwnerFunction().getInstanceType();
    }
    FunctionType callType = propType.toMaybeFunctionType();
    Action action = createExternFunctionCall(
        fnNode, thisType, callType);
    return Lists.<Action>newArrayList(action);
  }
  return Lists.<Action>newArrayList();
}
 
Example 2
Source File: Closure_43_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Resolve any stub delcarations to unknown types if we could not
 * find types for them during traversal.
 */
void resolveStubDeclarations() {
  for (StubDeclaration stub : stubDeclarations) {
    Node n = stub.node;
    Node parent = n.getParent();
    String qName = n.getQualifiedName();
    String propName = n.getLastChild().getString();
    String ownerName = stub.ownerName;
    boolean isExtern = stub.isExtern;

    if (scope.isDeclared(qName, false)) {
      continue;
    }

    // If we see a stub property, make sure to register this property
    // in the type registry.
    ObjectType ownerType = getObjectSlot(ownerName);
    ObjectType unknownType = typeRegistry.getNativeObjectType(UNKNOWN_TYPE);
    defineSlot(n, parent, unknownType, true);

    if (ownerType != null &&
        (isExtern || ownerType.isFunctionPrototypeType())) {
      // If this is a stub for a prototype, just declare it
      // as an unknown type. These are seen often in externs.
      ownerType.defineInferredProperty(
          propName, unknownType, n);
    } else {
      typeRegistry.registerPropertyOnType(
          propName, ownerType == null ? unknownType : ownerType);
    }
  }
}
 
Example 3
Source File: Closure_95_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Resolve any stub delcarations to unknown types if we could not
 * find types for them during traversal.
 */
void resolveStubDeclarations() {
  for (StubDeclaration stub : stubDeclarations) {
    Node n = stub.node;
    Node parent = n.getParent();
    String qName = n.getQualifiedName();
    String propName = n.getLastChild().getString();
    String ownerName = stub.ownerName;
    boolean isExtern = stub.isExtern;

    if (scope.isDeclared(qName, false)) {
      continue;
    }

    // If we see a stub property, make sure to register this property
    // in the type registry.
    ObjectType ownerType = getObjectSlot(ownerName);
    ObjectType unknownType = typeRegistry.getNativeObjectType(UNKNOWN_TYPE);
    defineSlot(n, parent, unknownType, true);

    if (ownerType != null &&
        (isExtern || ownerType.isFunctionPrototypeType())) {
      // If this is a stub for a prototype, just declare it
      // as an unknown type. These are seen often in externs.
      ownerType.defineInferredProperty(
          propName, unknownType, isExtern);
    } else {
      typeRegistry.registerPropertyOnType(
          propName, ownerType == null ? unknownType : ownerType);
    }
  }
}
 
Example 4
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 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_54_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Resolve any stub delcarations to unknown types if we could not
 * find types for them during traversal.
 */
void resolveStubDeclarations() {
  for (StubDeclaration stub : stubDeclarations) {
    Node n = stub.node;
    Node parent = n.getParent();
    String qName = n.getQualifiedName();
    String propName = n.getLastChild().getString();
    String ownerName = stub.ownerName;
    boolean isExtern = stub.isExtern;

    if (scope.isDeclared(qName, false)) {
      continue;
    }

    // If we see a stub property, make sure to register this property
    // in the type registry.
    ObjectType ownerType = getObjectSlot(ownerName);
    ObjectType unknownType = typeRegistry.getNativeObjectType(UNKNOWN_TYPE);
    defineSlot(n, parent, unknownType, true);

    if (ownerType != null &&
        (isExtern || ownerType.isFunctionPrototypeType())) {
      // If this is a stub for a prototype, just declare it
      // as an unknown type. These are seen often in externs.
      ownerType.defineInferredProperty(
          propName, unknownType, n);
    } else {
      typeRegistry.registerPropertyOnType(
          propName, ownerType == null ? unknownType : ownerType);
    }
  }
}
 
Example 7
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 8
Source File: TypedScopeCreator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Resolve any stub declarations to unknown types if we could not
 * find types for them during traversal.
 */
void resolveStubDeclarations() {
  for (StubDeclaration stub : stubDeclarations) {
    Node n = stub.node;
    Node parent = n.getParent();
    String qName = n.getQualifiedName();
    String propName = n.getLastChild().getString();
    String ownerName = stub.ownerName;
    boolean isExtern = stub.isExtern;

    if (scope.isDeclared(qName, false)) {
      continue;
    }

    // If we see a stub property, make sure to register this property
    // in the type registry.
    ObjectType ownerType = getObjectSlot(ownerName);
    defineSlot(n, parent, unknownType, true);

    if (ownerType != null &&
        (isExtern || ownerType.isFunctionPrototypeType())) {
      // If this is a stub for a prototype, just declare it
      // as an unknown type. These are seen often in externs.
      ownerType.defineInferredProperty(
          propName, unknownType, n);
    } else {
      typeRegistry.registerPropertyOnType(
          propName, ownerType == null ? unknownType : ownerType);
    }
  }
}
 
Example 9
Source File: Closure_43_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Resolve any stub delcarations to unknown types if we could not
 * find types for them during traversal.
 */
void resolveStubDeclarations() {
  for (StubDeclaration stub : stubDeclarations) {
    Node n = stub.node;
    Node parent = n.getParent();
    String qName = n.getQualifiedName();
    String propName = n.getLastChild().getString();
    String ownerName = stub.ownerName;
    boolean isExtern = stub.isExtern;

    if (scope.isDeclared(qName, false)) {
      continue;
    }

    // If we see a stub property, make sure to register this property
    // in the type registry.
    ObjectType ownerType = getObjectSlot(ownerName);
    ObjectType unknownType = typeRegistry.getNativeObjectType(UNKNOWN_TYPE);
    defineSlot(n, parent, unknownType, true);

    if (ownerType != null &&
        (isExtern || ownerType.isFunctionPrototypeType())) {
      // If this is a stub for a prototype, just declare it
      // as an unknown type. These are seen often in externs.
      ownerType.defineInferredProperty(
          propName, unknownType, n);
    } else {
      typeRegistry.registerPropertyOnType(
          propName, ownerType == null ? unknownType : ownerType);
    }
  }
}
 
Example 10
Source File: Closure_48_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Resolve any stub delcarations to unknown types if we could not
 * find types for them during traversal.
 */
void resolveStubDeclarations() {
  for (StubDeclaration stub : stubDeclarations) {
    Node n = stub.node;
    Node parent = n.getParent();
    String qName = n.getQualifiedName();
    String propName = n.getLastChild().getString();
    String ownerName = stub.ownerName;
    boolean isExtern = stub.isExtern;

    if (scope.isDeclared(qName, false)) {
      continue;
    }

    // If we see a stub property, make sure to register this property
    // in the type registry.
    ObjectType ownerType = getObjectSlot(ownerName);
    ObjectType unknownType = typeRegistry.getNativeObjectType(UNKNOWN_TYPE);
    defineSlot(n, parent, unknownType, true);

    if (ownerType != null &&
        (isExtern || ownerType.isFunctionPrototypeType())) {
      // If this is a stub for a prototype, just declare it
      // as an unknown type. These are seen often in externs.
      ownerType.defineInferredProperty(
          propName, unknownType, n);
    } else {
      typeRegistry.registerPropertyOnType(
          propName, ownerType == null ? unknownType : ownerType);
    }
  }
}
 
Example 11
Source File: Closure_48_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Resolve any stub delcarations to unknown types if we could not
 * find types for them during traversal.
 */
void resolveStubDeclarations() {
  for (StubDeclaration stub : stubDeclarations) {
    Node n = stub.node;
    Node parent = n.getParent();
    String qName = n.getQualifiedName();
    String propName = n.getLastChild().getString();
    String ownerName = stub.ownerName;
    boolean isExtern = stub.isExtern;

    if (scope.isDeclared(qName, false)) {
      continue;
    }

    // If we see a stub property, make sure to register this property
    // in the type registry.
    ObjectType ownerType = getObjectSlot(ownerName);
    ObjectType unknownType = typeRegistry.getNativeObjectType(UNKNOWN_TYPE);
    defineSlot(n, parent, unknownType, true);

    if (ownerType != null &&
        (isExtern || ownerType.isFunctionPrototypeType())) {
      // If this is a stub for a prototype, just declare it
      // as an unknown type. These are seen often in externs.
      ownerType.defineInferredProperty(
          propName, unknownType, n);
    } else {
      typeRegistry.registerPropertyOnType(
          propName, ownerType == null ? unknownType : ownerType);
    }
  }
}
 
Example 12
Source File: Closure_70_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Resolve any stub delcarations to unknown types if we could not
 * find types for them during traversal.
 */
void resolveStubDeclarations() {
  for (StubDeclaration stub : stubDeclarations) {
    Node n = stub.node;
    Node parent = n.getParent();
    String qName = n.getQualifiedName();
    String propName = n.getLastChild().getString();
    String ownerName = stub.ownerName;
    boolean isExtern = stub.isExtern;

    if (scope.isDeclared(qName, false)) {
      continue;
    }

    // If we see a stub property, make sure to register this property
    // in the type registry.
    ObjectType ownerType = getObjectSlot(ownerName);
    ObjectType unknownType = typeRegistry.getNativeObjectType(UNKNOWN_TYPE);
    defineSlot(n, parent, unknownType, true);

    if (ownerType != null &&
        (isExtern || ownerType.isFunctionPrototypeType())) {
      // If this is a stub for a prototype, just declare it
      // as an unknown type. These are seen often in externs.
      ownerType.defineInferredProperty(
          propName, unknownType, isExtern, n);
    } else {
      typeRegistry.registerPropertyOnType(
          propName, ownerType == null ? unknownType : ownerType);
    }
  }
}
 
Example 13
Source File: Closure_70_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Resolve any stub delcarations to unknown types if we could not
 * find types for them during traversal.
 */
void resolveStubDeclarations() {
  for (StubDeclaration stub : stubDeclarations) {
    Node n = stub.node;
    Node parent = n.getParent();
    String qName = n.getQualifiedName();
    String propName = n.getLastChild().getString();
    String ownerName = stub.ownerName;
    boolean isExtern = stub.isExtern;

    if (scope.isDeclared(qName, false)) {
      continue;
    }

    // If we see a stub property, make sure to register this property
    // in the type registry.
    ObjectType ownerType = getObjectSlot(ownerName);
    ObjectType unknownType = typeRegistry.getNativeObjectType(UNKNOWN_TYPE);
    defineSlot(n, parent, unknownType, true);

    if (ownerType != null &&
        (isExtern || ownerType.isFunctionPrototypeType())) {
      // If this is a stub for a prototype, just declare it
      // as an unknown type. These are seen often in externs.
      ownerType.defineInferredProperty(
          propName, unknownType, isExtern, n);
    } else {
      typeRegistry.registerPropertyOnType(
          propName, ownerType == null ? unknownType : ownerType);
    }
  }
}
 
Example 14
Source File: Closure_17_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Resolve any stub declarations to unknown types if we could not
 * find types for them during traversal.
 */
void resolveStubDeclarations() {
  for (StubDeclaration stub : stubDeclarations) {
    Node n = stub.node;
    Node parent = n.getParent();
    String qName = n.getQualifiedName();
    String propName = n.getLastChild().getString();
    String ownerName = stub.ownerName;
    boolean isExtern = stub.isExtern;

    if (scope.isDeclared(qName, false)) {
      continue;
    }

    // If we see a stub property, make sure to register this property
    // in the type registry.
    ObjectType ownerType = getObjectSlot(ownerName);
    ObjectType unknownType = typeRegistry.getNativeObjectType(UNKNOWN_TYPE);
    defineSlot(n, parent, unknownType, true);

    if (ownerType != null &&
        (isExtern || ownerType.isFunctionPrototypeType())) {
      // If this is a stub for a prototype, just declare it
      // as an unknown type. These are seen often in externs.
      ownerType.defineInferredProperty(
          propName, unknownType, n);
    } else {
      typeRegistry.registerPropertyOnType(
          propName, ownerType == null ? unknownType : ownerType);
    }
  }
}
 
Example 15
Source File: Closure_17_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Resolve any stub declarations to unknown types if we could not
 * find types for them during traversal.
 */
void resolveStubDeclarations() {
  for (StubDeclaration stub : stubDeclarations) {
    Node n = stub.node;
    Node parent = n.getParent();
    String qName = n.getQualifiedName();
    String propName = n.getLastChild().getString();
    String ownerName = stub.ownerName;
    boolean isExtern = stub.isExtern;

    if (scope.isDeclared(qName, false)) {
      continue;
    }

    // If we see a stub property, make sure to register this property
    // in the type registry.
    ObjectType ownerType = getObjectSlot(ownerName);
    ObjectType unknownType = typeRegistry.getNativeObjectType(UNKNOWN_TYPE);
    defineSlot(n, parent, unknownType, true);

    if (ownerType != null &&
        (isExtern || ownerType.isFunctionPrototypeType())) {
      // If this is a stub for a prototype, just declare it
      // as an unknown type. These are seen often in externs.
      ownerType.defineInferredProperty(
          propName, unknownType, n);
    } else {
      typeRegistry.registerPropertyOnType(
          propName, ownerType == null ? unknownType : ownerType);
    }
  }
}
 
Example 16
Source File: Nopol2017_0027_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Resolve any stub declarations to unknown types if we could not
 * find types for them during traversal.
 */
void resolveStubDeclarations() {
  for (StubDeclaration stub : stubDeclarations) {
    Node n = stub.node;
    Node parent = n.getParent();
    String qName = n.getQualifiedName();
    String propName = n.getLastChild().getString();
    String ownerName = stub.ownerName;
    boolean isExtern = stub.isExtern;

    if (scope.isDeclared(qName, false)) {
      continue;
    }

    // If we see a stub property, make sure to register this property
    // in the type registry.
    ObjectType ownerType = getObjectSlot(ownerName);
    ObjectType unknownType = typeRegistry.getNativeObjectType(UNKNOWN_TYPE);
    defineSlot(n, parent, unknownType, true);

    if (ownerType != null &&
        (isExtern || ownerType.isFunctionPrototypeType())) {
      // If this is a stub for a prototype, just declare it
      // as an unknown type. These are seen often in externs.
      ownerType.defineInferredProperty(
          propName, unknownType, n);
    } else {
      typeRegistry.registerPropertyOnType(
          propName, ownerType == null ? unknownType : ownerType);
    }
  }
}
 
Example 17
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Given a node, get a human-readable name for the type of that node so
 * that will be easy for the programmer to find the original declaration.
 *
 * For example, if SubFoo's property "bar" might have the human-readable
 * name "Foo.prototype.bar".
 *
 * @param n The node.
 * @param dereference If true, the type of the node will be dereferenced
 *     to an Object type, if possible.
 */
String getReadableJSTypeName(Node n, boolean dereference) {
  // If we're analyzing a GETPROP, the property may be inherited by the
  // prototype chain. So climb the prototype chain and find out where
  // the property was originally defined.
  if (n.isGetProp()) {
    ObjectType objectType = getJSType(n.getFirstChild()).dereference();
    if (objectType != null) {
      String propName = n.getLastChild().getString();
      if (objectType.getConstructor() != null &&
          objectType.getConstructor().isInterface()) {
        objectType = FunctionType.getTopDefiningInterface(
            objectType, propName);
      } else {
        // classes
        while (objectType != null && !objectType.hasOwnProperty(propName)) {
          objectType = objectType.getImplicitPrototype();
        }
      }

      // Don't show complex function names or anonymous types.
      // Instead, try to get a human-readable type name.
      if (objectType != null &&
          (objectType.getConstructor() != null ||
           objectType.isFunctionPrototypeType())) {
        return objectType.toString() + "." + propName;
      }
    }
  }

  JSType type = getJSType(n);
  if (dereference) {
    ObjectType dereferenced = type.dereference();
    if (dereferenced != null) {
      type = dereferenced;
    }
  }

  String qualifiedName = n.getQualifiedName();
  if (type.isFunctionPrototypeType() ||
      (type.toObjectType() != null &&
       type.toObjectType().getConstructor() != null)) {
    return type.toString();
  } else if (qualifiedName != null) {
    return qualifiedName;
  } else if (type.isFunctionType()) {
    // Don't show complex function names.
    return "function";
  } else {
    return type.toString();
  }
}
 
Example 18
Source File: Closure_6_TypeValidator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Given a node, get a human-readable name for the type of that node so
 * that will be easy for the programmer to find the original declaration.
 *
 * For example, if SubFoo's property "bar" might have the human-readable
 * name "Foo.prototype.bar".
 *
 * @param n The node.
 * @param dereference If true, the type of the node will be dereferenced
 *     to an Object type, if possible.
 */
String getReadableJSTypeName(Node n, boolean dereference) {
  // If we're analyzing a GETPROP, the property may be inherited by the
  // prototype chain. So climb the prototype chain and find out where
  // the property was originally defined.
  if (n.isGetProp()) {
    ObjectType objectType = getJSType(n.getFirstChild()).dereference();
    if (objectType != null) {
      String propName = n.getLastChild().getString();
      if (objectType.getConstructor() != null &&
          objectType.getConstructor().isInterface()) {
        objectType = FunctionType.getTopDefiningInterface(
            objectType, propName);
      } else {
        // classes
        while (objectType != null && !objectType.hasOwnProperty(propName)) {
          objectType = objectType.getImplicitPrototype();
        }
      }

      // Don't show complex function names or anonymous types.
      // Instead, try to get a human-readable type name.
      if (objectType != null &&
          (objectType.getConstructor() != null ||
           objectType.isFunctionPrototypeType())) {
        return objectType.toString() + "." + propName;
      }
    }
  }

  JSType type = getJSType(n);
  if (dereference) {
    ObjectType dereferenced = type.dereference();
    if (dereferenced != null) {
      type = dereferenced;
    }
  }

  String qualifiedName = n.getQualifiedName();
  if (type.isFunctionPrototypeType() ||
      (type.toObjectType() != null &&
       type.toObjectType().getConstructor() != null)) {
    return type.toString();
  } else if (qualifiedName != null) {
    return qualifiedName;
  } else if (type.isFunctionType()) {
    // Don't show complex function names.
    return "function";
  } else {
    return type.toString();
  }
}
 
Example 19
Source File: SymbolTable.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Build a property scope for the given symbol. Any properties of the symbol
 * will be added to the property scope.
 *
 * It is important that property scopes are created in order from the leaves
 * up to the root, so this should only be called from #fillPropertyScopes.
 * If you try to create a property scope for a parent before its leaf,
 * then the leaf will get cut and re-added to the parent property scope,
 * and weird things will happen.
 */
private void createPropertyScopeFor(Symbol s) {
  // In order to build a property scope for s, we will need to build
  // a property scope for all its implicit prototypes first. This means
  // that sometimes we will already have built its property scope
  // for a previous symbol.
  if (s.propertyScope != null) {
    return;
  }

  SymbolScope parentPropertyScope = null;
  ObjectType type = s.getType() == null ? null : s.getType().toObjectType();
  if (type == null) {
    return;
  }

  ObjectType proto = type.getParentScope();
  if (proto != null && proto != type && proto.getConstructor() != null) {
    Symbol parentSymbol = getSymbolForInstancesOf(proto.getConstructor());
    if (parentSymbol != null) {
      createPropertyScopeFor(parentSymbol);
      parentPropertyScope = parentSymbol.getPropertyScope();
    }
  }

  ObjectType instanceType = type;
  Iterable<String> propNames = type.getOwnPropertyNames();
  if (instanceType.isFunctionPrototypeType()) {
    // Merge the properties of "Foo.prototype" and "new Foo()" together.
    instanceType = instanceType.getOwnerFunction().getInstanceType();
    Set<String> set = Sets.newHashSet(propNames);
    Iterables.addAll(set, instanceType.getOwnPropertyNames());
    propNames = set;
  }

  s.setPropertyScope(new SymbolScope(null, parentPropertyScope, type, s));
  for (String propName : propNames) {
    StaticSlot<JSType> newProp = instanceType.getSlot(propName);
    if (newProp.getDeclaration() == null) {
      // Skip properties without declarations. We won't know how to index
      // them, because we index things by node.
      continue;
    }

    // We have symbol tables that do not do type analysis. They just try
    // to build a complete index of all objects in the program. So we might
    // already have symbols for things like "Foo.bar". If this happens,
    // throw out the old symbol and use the type-based symbol.
    Symbol oldProp = symbols.get(newProp.getDeclaration().getNode(),
        s.getName() + "." + propName);
    if (oldProp != null) {
      removeSymbol(oldProp);
    }

    // If we've already have an entry in the table for this symbol,
    // then skip it. This should only happen if we screwed up,
    // and declared multiple distinct properties with the same name
    // at the same node. We bail out here to be safe.
    if (symbols.get(newProp.getDeclaration().getNode(),
            newProp.getName()) != null) {
      logger.warning("Found duplicate symbol " + newProp);
      continue;
    }

    Symbol newSym = copySymbolTo(newProp, s.propertyScope);
    if (oldProp != null) {
      if (newSym.getJSDocInfo() == null) {
        newSym.setJSDocInfo(oldProp.getJSDocInfo());
      }
      newSym.setPropertyScope(oldProp.propertyScope);
      for (Reference ref : oldProp.references.values()) {
        newSym.defineReferenceAt(ref.getNode());
      }
    }
  }
}
 
Example 20
Source File: Closure_117_TypeValidator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Given a node, get a human-readable name for the type of that node so
 * that will be easy for the programmer to find the original declaration.
 *
 * For example, if SubFoo's property "bar" might have the human-readable
 * name "Foo.prototype.bar".
 *
 * @param n The node.
 * @param dereference If true, the type of the node will be dereferenced
 *     to an Object type, if possible.
 */
String getReadableJSTypeName(Node n, boolean dereference) {
  JSType type = getJSType(n);
  if (dereference) {
    ObjectType dereferenced = type.dereference();
    if (dereferenced != null) {
      type = dereferenced;
    }
  }

  // The best type name is the actual type name.
  if (type.isFunctionPrototypeType() ||
      (type.toObjectType() != null &&
       type.toObjectType().getConstructor() != null)) {
    return type.toString();
  }

  // If we're analyzing a GETPROP, the property may be inherited by the
  // prototype chain. So climb the prototype chain and find out where
  // the property was originally defined.
  if (n.isGetProp()) {
    ObjectType objectType = getJSType(n.getFirstChild()).dereference();
    if (objectType != null) {
      String propName = n.getLastChild().getString();
      if (objectType.getConstructor() != null &&
          objectType.getConstructor().isInterface()) {
        objectType = FunctionType.getTopDefiningInterface(
            objectType, propName);
      } else {
        // classes
        while (objectType != null && !objectType.hasOwnProperty(propName)) {
          objectType = objectType.getImplicitPrototype();
        }
      }

      // Don't show complex function names or anonymous types.
      // Instead, try to get a human-readable type name.
      if (objectType != null &&
          (objectType.getConstructor() != null ||
           objectType.isFunctionPrototypeType())) {
        return objectType.toString() + "." + propName;
      }
    }
  }

  String qualifiedName = n.getQualifiedName();
  if (qualifiedName != null) {
    return qualifiedName;
  } else if (type.isFunctionType()) {
    // Don't show complex function names.
    return "function";
  } else {
    return type.toString();
  }
}