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

The following examples show how to use com.google.javascript.rhino.jstype.ObjectType#getOwnPropertyNames() . 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_35_TypeInference_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Suppose X is an object with inferred properties.
 * Suppose also that X is used in a way where it would only type-check
 * correctly if some of those properties are widened.
 * Then we should be polite and automatically widen X's properties for him.
 *
 * For a concrete example, consider:
 * param x {{prop: (number|undefined)}}
 * function f(x) {}
 * f({});
 *
 * If we give the anonymous object an inferred property of (number|undefined),
 * then this code will type-check appropriately.
 */
private void inferPropertyTypesToMatchConstraint(
    JSType type, JSType constraint) {
  if (type == null || constraint == null) {
    return;
  }

  ObjectType constraintObj =
      ObjectType.cast(constraint.restrictByNotNullOrUndefined());
  if (constraintObj != null && constraintObj.isRecordType()) {
    ObjectType objType = ObjectType.cast(type.restrictByNotNullOrUndefined());
    if (objType != null) {
      for (String prop : constraintObj.getOwnPropertyNames()) {
        JSType propType = constraintObj.getPropertyType(prop);
        if (!objType.isPropertyTypeDeclared(prop)) {
          JSType typeToInfer = propType;
          if (!objType.hasProperty(prop)) {
            typeToInfer =
                getNativeType(VOID_TYPE).getLeastSupertype(propType);
          }
          objType.defineInferredProperty(prop, typeToInfer, null);
        }
      }
    }
  }
}
 
Example 2
Source File: NameReferenceGraphConstruction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates the name in the graph if it does not already exist. Also puts all
 * the properties and prototype properties of this name in the graph.
 */
private Name recordClassConstructorOrInterface(
    String name, FunctionType type, @Nullable Node n, @Nullable Node parent,
    @Nullable Node gParent, @Nullable Node rhs) {
  Preconditions.checkArgument(type.isConstructor() || type.isInterface());
  Name symbol = graph.defineNameIfNotExists(name, isExtern);
  if (rhs != null) {
    // TODO(user): record the definition.
    symbol.setType(getType(rhs));
    if (n.isAssign()) {
      symbol.addAssignmentDeclaration(n);
    } else {
      symbol.addFunctionDeclaration(n);
    }
  }
  ObjectType prototype = type.getPrototype();
  for (String prop : prototype.getOwnPropertyNames()) {
    graph.defineNameIfNotExists(
        name + ".prototype." + prop, isExtern);
  }
  return symbol;
}
 
Example 3
Source File: ConcreteTypeTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public StaticScope<ConcreteType> createInstanceScope(
    ObjectType instanceType) {
  FakeScope parentScope = null;
  if (instanceType.getImplicitPrototype() != null) {
    ConcreteInstanceType prototype =
        createConcreteInstance(instanceType.getImplicitPrototype());
    parentScope = (FakeScope) prototype.getScope();
  }

  FakeScope scope = new FakeScope(parentScope);
  for (String propName : instanceType.getOwnPropertyNames()) {
    scope.addSlot(propName);
  }
  return scope;
}
 
Example 4
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
private Map<String, InstanceProperty> getOwnProperties(ObjectType object) {
  ObjectType definingType = object;
  if (definingType.isFunctionPrototypeType()) {
    definingType = definingType.getOwnerFunction();
  } else if (definingType.isInstanceType()) {
    definingType = definingType.getConstructor();
  }

  Map<String, InstanceProperty> properties = new HashMap<>();
  for (String name : object.getOwnPropertyNames()) {
    if (!"constructor".equals(name)) {
      Property property = object.getOwnSlot(name);
      properties.put(
          property.getName(),
          InstanceProperty.builder()
              .setOwnerType(getFirst(registry.getTypes(definingType), null))
              .setDefinedByType(definingType)
              .setName(property.getName())
              .setType(getType(object, property))
              .setNode(property.getNode())
              .setJsDoc(JsDoc.from(property.getJSDocInfo()))
              .build());
    }
  }
  return properties;
}
 
Example 5
Source File: Nopol2017_0029_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Check whether there's any property conflict for for a particular super
 * interface
 * @param t The node traversal object that supplies context
 * @param n The node being visited
 * @param functionName The function name being checked
 * @param properties The property names in the super interfaces that have
 * been visited
 * @param currentProperties The property names in the super interface
 * that have been visited
 * @param interfaceType The super interface that is being visited
 */
private void checkInterfaceConflictProperties(NodeTraversal t, Node n,
    String functionName, HashMap<String, ObjectType> properties,
    HashMap<String, ObjectType> currentProperties,
    ObjectType interfaceType) {
  ObjectType implicitProto = interfaceType.getImplicitPrototype();
  Set<String> currentPropertyNames;
    // This can be the case if interfaceType is proxy to a non-existent
    // object (which is a bad type annotation, but shouldn't crash).
    currentPropertyNames = implicitProto.getOwnPropertyNames();
  for (String name : currentPropertyNames) {
    ObjectType oType = properties.get(name);
    if (oType != null) {
      if (!interfaceType.getPropertyType(name).isEquivalentTo(
          oType.getPropertyType(name))) {
        compiler.report(
            t.makeError(n, INCOMPATIBLE_EXTENDED_PROPERTY_TYPE,
                functionName, name, oType.toString(),
                interfaceType.toString()));
      }
    }
    currentProperties.put(name, interfaceType);
  }
  for (ObjectType iType : interfaceType.getCtorExtendedInterfaces()) {
    checkInterfaceConflictProperties(t, n, functionName, properties,
        currentProperties, iType);
  }
}
 
Example 6
Source File: Nopol2017_0029_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Check whether there's any property conflict for for a particular super
 * interface
 * @param t The node traversal object that supplies context
 * @param n The node being visited
 * @param functionName The function name being checked
 * @param properties The property names in the super interfaces that have
 * been visited
 * @param currentProperties The property names in the super interface
 * that have been visited
 * @param interfaceType The super interface that is being visited
 */
private void checkInterfaceConflictProperties(NodeTraversal t, Node n,
    String functionName, HashMap<String, ObjectType> properties,
    HashMap<String, ObjectType> currentProperties,
    ObjectType interfaceType) {
  ObjectType implicitProto = interfaceType.getImplicitPrototype();
  Set<String> currentPropertyNames;
    // This can be the case if interfaceType is proxy to a non-existent
    // object (which is a bad type annotation, but shouldn't crash).
    currentPropertyNames = implicitProto.getOwnPropertyNames();
  for (String name : currentPropertyNames) {
    ObjectType oType = properties.get(name);
    if (oType != null) {
      if (!interfaceType.getPropertyType(name).isEquivalentTo(
          oType.getPropertyType(name))) {
        compiler.report(
            t.makeError(n, INCOMPATIBLE_EXTENDED_PROPERTY_TYPE,
                functionName, name, oType.toString(),
                interfaceType.toString()));
      }
    }
    currentProperties.put(name, interfaceType);
  }
  for (ObjectType iType : interfaceType.getCtorExtendedInterfaces()) {
    checkInterfaceConflictProperties(t, n, functionName, properties,
        currentProperties, iType);
  }
}
 
Example 7
Source File: Closure_125_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Check whether there's any property conflict for for a particular super
 * interface
 * @param t The node traversal object that supplies context
 * @param n The node being visited
 * @param functionName The function name being checked
 * @param properties The property names in the super interfaces that have
 * been visited
 * @param currentProperties The property names in the super interface
 * that have been visited
 * @param interfaceType The super interface that is being visited
 */
private void checkInterfaceConflictProperties(NodeTraversal t, Node n,
    String functionName, HashMap<String, ObjectType> properties,
    HashMap<String, ObjectType> currentProperties,
    ObjectType interfaceType) {
  ObjectType implicitProto = interfaceType.getImplicitPrototype();
  Set<String> currentPropertyNames;
  if (implicitProto == null) {
    // This can be the case if interfaceType is proxy to a non-existent
    // object (which is a bad type annotation, but shouldn't crash).
    currentPropertyNames = ImmutableSet.of();
  } else {
    currentPropertyNames = implicitProto.getOwnPropertyNames();
  }
  for (String name : currentPropertyNames) {
    ObjectType oType = properties.get(name);
    if (oType != null) {
      if (!interfaceType.getPropertyType(name).isEquivalentTo(
          oType.getPropertyType(name))) {
        compiler.report(
            t.makeError(n, INCOMPATIBLE_EXTENDED_PROPERTY_TYPE,
                functionName, name, oType.toString(),
                interfaceType.toString()));
      }
    }
    currentProperties.put(name, interfaceType);
  }
  for (ObjectType iType : interfaceType.getCtorExtendedInterfaces()) {
    checkInterfaceConflictProperties(t, n, functionName, properties,
        currentProperties, iType);
  }
}
 
Example 8
Source File: Closure_125_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Check whether there's any property conflict for for a particular super
 * interface
 * @param t The node traversal object that supplies context
 * @param n The node being visited
 * @param functionName The function name being checked
 * @param properties The property names in the super interfaces that have
 * been visited
 * @param currentProperties The property names in the super interface
 * that have been visited
 * @param interfaceType The super interface that is being visited
 */
private void checkInterfaceConflictProperties(NodeTraversal t, Node n,
    String functionName, HashMap<String, ObjectType> properties,
    HashMap<String, ObjectType> currentProperties,
    ObjectType interfaceType) {
  ObjectType implicitProto = interfaceType.getImplicitPrototype();
  Set<String> currentPropertyNames;
  if (implicitProto == null) {
    // This can be the case if interfaceType is proxy to a non-existent
    // object (which is a bad type annotation, but shouldn't crash).
    currentPropertyNames = ImmutableSet.of();
  } else {
    currentPropertyNames = implicitProto.getOwnPropertyNames();
  }
  for (String name : currentPropertyNames) {
    ObjectType oType = properties.get(name);
    if (oType != null) {
      if (!interfaceType.getPropertyType(name).isEquivalentTo(
          oType.getPropertyType(name))) {
        compiler.report(
            t.makeError(n, INCOMPATIBLE_EXTENDED_PROPERTY_TYPE,
                functionName, name, oType.toString(),
                interfaceType.toString()));
      }
    }
    currentProperties.put(name, interfaceType);
  }
  for (ObjectType iType : interfaceType.getCtorExtendedInterfaces()) {
    checkInterfaceConflictProperties(t, n, functionName, properties,
        currentProperties, iType);
  }
}
 
Example 9
Source File: Closure_2_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Check whether there's any property conflict for for a particular super
 * interface
 * @param t The node traversal object that supplies context
 * @param n The node being visited
 * @param functionName The function name being checked
 * @param properties The property names in the super interfaces that have
 * been visited
 * @param currentProperties The property names in the super interface
 * that have been visited
 * @param interfaceType The super interface that is being visited
 */
private void checkInterfaceConflictProperties(NodeTraversal t, Node n,
    String functionName, HashMap<String, ObjectType> properties,
    HashMap<String, ObjectType> currentProperties,
    ObjectType interfaceType) {
  ObjectType implicitProto = interfaceType.getImplicitPrototype();
  Set<String> currentPropertyNames;
    // This can be the case if interfaceType is proxy to a non-existent
    // object (which is a bad type annotation, but shouldn't crash).
    currentPropertyNames = implicitProto.getOwnPropertyNames();
  for (String name : currentPropertyNames) {
    ObjectType oType = properties.get(name);
    if (oType != null) {
      if (!interfaceType.getPropertyType(name).isEquivalentTo(
          oType.getPropertyType(name))) {
        compiler.report(
            t.makeError(n, INCOMPATIBLE_EXTENDED_PROPERTY_TYPE,
                functionName, name, oType.toString(),
                interfaceType.toString()));
      }
    }
    currentProperties.put(name, interfaceType);
  }
  for (ObjectType iType : interfaceType.getCtorExtendedInterfaces()) {
    checkInterfaceConflictProperties(t, n, functionName, properties,
        currentProperties, iType);
  }
}
 
Example 10
Source File: Closure_2_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Check whether there's any property conflict for for a particular super
 * interface
 * @param t The node traversal object that supplies context
 * @param n The node being visited
 * @param functionName The function name being checked
 * @param properties The property names in the super interfaces that have
 * been visited
 * @param currentProperties The property names in the super interface
 * that have been visited
 * @param interfaceType The super interface that is being visited
 */
private void checkInterfaceConflictProperties(NodeTraversal t, Node n,
    String functionName, HashMap<String, ObjectType> properties,
    HashMap<String, ObjectType> currentProperties,
    ObjectType interfaceType) {
  ObjectType implicitProto = interfaceType.getImplicitPrototype();
  Set<String> currentPropertyNames;
  if (implicitProto == null) {
    // This can be the case if interfaceType is proxy to a non-existent
    // object (which is a bad type annotation, but shouldn't crash).
    currentPropertyNames = ImmutableSet.of();
  } else {
    currentPropertyNames = implicitProto.getOwnPropertyNames();
  }
  for (String name : currentPropertyNames) {
    ObjectType oType = properties.get(name);
    if (oType != null) {
      if (!interfaceType.getPropertyType(name).isEquivalentTo(
          oType.getPropertyType(name))) {
        compiler.report(
            t.makeError(n, INCOMPATIBLE_EXTENDED_PROPERTY_TYPE,
                functionName, name, oType.toString(),
                interfaceType.toString()));
      }
    }
    currentProperties.put(name, interfaceType);
  }
  for (ObjectType iType : interfaceType.getCtorExtendedInterfaces()) {
    checkInterfaceConflictProperties(t, n, functionName, properties,
        currentProperties, iType);
  }
}
 
Example 11
Source File: TypeCheck.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check whether there's any property conflict for for a particular super
 * interface
 * @param t The node traversal object that supplies context
 * @param n The node being visited
 * @param functionName The function name being checked
 * @param properties The property names in the super interfaces that have
 * been visited
 * @param currentProperties The property names in the super interface
 * that have been visited
 * @param interfaceType The super interface that is being visited
 */
private void checkInterfaceConflictProperties(NodeTraversal t, Node n,
    String functionName, HashMap<String, ObjectType> properties,
    HashMap<String, ObjectType> currentProperties,
    ObjectType interfaceType) {
  ObjectType implicitProto = interfaceType.getImplicitPrototype();
  Set<String> currentPropertyNames;
  if (implicitProto == null) {
    // This can be the case if interfaceType is proxy to a non-existent
    // object (which is a bad type annotation, but shouldn't crash).
    currentPropertyNames = ImmutableSet.of();
  } else {
    currentPropertyNames = implicitProto.getOwnPropertyNames();
  }
  for (String name : currentPropertyNames) {
    ObjectType oType = properties.get(name);
    if (oType != null) {
      if (!interfaceType.getPropertyType(name).isEquivalentTo(
          oType.getPropertyType(name))) {
        compiler.report(
            t.makeError(n, INCOMPATIBLE_EXTENDED_PROPERTY_TYPE,
                functionName, name, oType.toString(),
                interfaceType.toString()));
      }
    }
    currentProperties.put(name, interfaceType);
  }
  for (ObjectType iType : interfaceType.getCtorExtendedInterfaces()) {
    checkInterfaceConflictProperties(t, n, functionName, properties,
        currentProperties, iType);
  }
}
 
Example 12
Source File: TightenTypes.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public StaticScope<ConcreteType> createInstanceScope(
    ObjectType instanceType) {
  ConcreteScope parentScope = null;
  ObjectType implicitProto = instanceType.getImplicitPrototype();
  if (implicitProto != null && !implicitProto.isUnknownType()) {
    ConcreteInstanceType prototype = createConcreteInstance(implicitProto);
    parentScope = (ConcreteScope) prototype.getScope();
  }
  ConcreteScope scope = new ConcreteScope(parentScope);
  for (String propName : instanceType.getOwnPropertyNames()) {
    scope.declareSlot(propName, null);
  }
  return scope;
}
 
Example 13
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
/** Returns the raw properties for the given type. */
public List<Property> getProperties(NominalType nominalType) {
  JSType type = nominalType.getType();
  ObjectType object = ObjectType.cast(type);
  if (object == null) {
    return ImmutableList.of();
  }

  List<Property> properties = new ArrayList<>();
  for (String name : object.getOwnPropertyNames()) {
    Property property = null;
    if (type.isFunctionType()) {
      if (!isBuiltInFunctionProperty(type, name)) {
        property = object.getOwnSlot(name);
      }
    } else if (!"prototype".equals(name)) {
      property = object.getOwnSlot(name);
    }

    if (property != null) {
      if (property.getType().isConstructor()
          && isConstructorTypeDefinition(
              property.getType(), JsDoc.from(property.getJSDocInfo()))) {
        continue;
      }

      // If the property is another module and the inspected type is also a module, then count
      // the property as a static property. Otherwise, if the property i registered as a nominal
      // type, it does not count as a static property. It should also be ignored if it is not
      // registered as a nominal type, but its qualified name has been filtered out by the user.
      String qualifiedName = nominalType.getName() + "." + property.getName();
      if (((inspectedType.isModuleExports() && registry.isModule(property.getType()))
              || registry.getTypes(property.getType()).isEmpty())
          && !typeFilter.test(qualifiedName)) {
        properties.add(property);
      }
    }
  }
  return properties;
}
 
Example 14
Source File: TypeCollectionPass.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
@Override
public Object caseObjectType(ObjectType type) {
  if (type.isGlobalThisType()) {
    return null;
  }

  for (String name : type.getOwnPropertyNames()) {
    if (type.getPropertyType(name).isEnumElementType()) {
      continue;
    }
    crawl(type.getPropertyType(name));
  }
  return null;
}
 
Example 15
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());
      }
    }
  }
}