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

The following examples show how to use com.google.javascript.rhino.jstype.ObjectType#isInstanceType() . 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_25_TypeInference_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Declares a property on its owner, if necessary.
 * @return True if a property was declared.
 */
private boolean ensurePropertyDeclaredHelper(
    Node getprop, ObjectType objectType) {
  String propName = getprop.getLastChild().getString();
  String qName = getprop.getQualifiedName();
  if (qName != null) {
    Var var = syntacticScope.getVar(qName);
    if (var != null && !var.isTypeInferred()) {
      // Handle normal declarations that could not be addressed earlier.
      if (propName.equals("prototype") ||
      // Handle prototype declarations that could not be addressed earlier.
          (!objectType.hasOwnProperty(propName) &&
           (!objectType.isInstanceType() ||
               (var.isExtern() && !objectType.isNativeObjectType())))) {
        return objectType.defineDeclaredProperty(
            propName, var.getType(), getprop);
      }
    }
  }
  return false;
}
 
Example 2
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 3
Source File: TypeInference.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Declares a property on its owner, if necessary.
 * @return True if a property was declared.
 */
private boolean ensurePropertyDeclaredHelper(
    Node getprop, ObjectType objectType) {
  String propName = getprop.getLastChild().getString();
  String qName = getprop.getQualifiedName();
  if (qName != null) {
    Var var = syntacticScope.getVar(qName);
    if (var != null && !var.isTypeInferred()) {
      // Handle normal declarations that could not be addressed earlier.
      if (propName.equals("prototype") ||
      // Handle prototype declarations that could not be addressed earlier.
          (!objectType.hasOwnProperty(propName) &&
           (!objectType.isInstanceType() ||
               (var.isExtern() && !objectType.isNativeObjectType())))) {
        return objectType.defineDeclaredProperty(
            propName, var.getType(), getprop);
      }
    }
  }
  return false;
}
 
Example 4
Source File: Closure_112_TypeInference_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Declares a property on its owner, if necessary.
 * @return True if a property was declared.
 */
private boolean ensurePropertyDeclaredHelper(
    Node getprop, ObjectType objectType) {
  String propName = getprop.getLastChild().getString();
  String qName = getprop.getQualifiedName();
  if (qName != null) {
    Var var = syntacticScope.getVar(qName);
    if (var != null && !var.isTypeInferred()) {
      // Handle normal declarations that could not be addressed earlier.
      if (propName.equals("prototype") ||
      // Handle prototype declarations that could not be addressed earlier.
          (!objectType.hasOwnProperty(propName) &&
           (!objectType.isInstanceType() ||
               (var.isExtern() && !objectType.isNativeObjectType())))) {
        return objectType.defineDeclaredProperty(
            propName, var.getType(), getprop);
      }
    }
  }
  return false;
}
 
Example 5
Source File: Closure_35_TypeInference_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Declares a property on its owner, if necessary.
 * @return True if a property was declared.
 */
private boolean ensurePropertyDeclaredHelper(
    Node getprop, ObjectType objectType) {
  String propName = getprop.getLastChild().getString();
  String qName = getprop.getQualifiedName();
  if (qName != null) {
    Var var = syntacticScope.getVar(qName);
    if (var != null && !var.isTypeInferred()) {
      // Handle normal declarations that could not be addressed earlier.
      if (propName.equals("prototype") ||
      // Handle prototype declarations that could not be addressed earlier.
          (!objectType.hasOwnProperty(propName) &&
           (!objectType.isInstanceType() ||
               (var.isExtern() && !objectType.isNativeObjectType())))) {
        return objectType.defineDeclaredProperty(
            propName, var.getType(), getprop);
      }
    }
  }
  return false;
}
 
Example 6
Source File: Closure_35_TypeInference_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Declares a property on its owner, if necessary.
 * @return True if a property was declared.
 */
private boolean ensurePropertyDeclaredHelper(
    Node getprop, ObjectType objectType) {
  String propName = getprop.getLastChild().getString();
  String qName = getprop.getQualifiedName();
  if (qName != null) {
    Var var = syntacticScope.getVar(qName);
    if (var != null && !var.isTypeInferred()) {
      // Handle normal declarations that could not be addressed earlier.
      if (propName.equals("prototype") ||
      // Handle prototype declarations that could not be addressed earlier.
          (!objectType.hasOwnProperty(propName) &&
           (!objectType.isInstanceType() ||
               (var.isExtern() && !objectType.isNativeObjectType())))) {
        return objectType.defineDeclaredProperty(
            propName, var.getType(), getprop);
      }
    }
  }
  return false;
}
 
Example 7
Source File: Closure_25_TypeInference_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Declares a property on its owner, if necessary.
 * @return True if a property was declared.
 */
private boolean ensurePropertyDeclaredHelper(
    Node getprop, ObjectType objectType) {
  String propName = getprop.getLastChild().getString();
  String qName = getprop.getQualifiedName();
  if (qName != null) {
    Var var = syntacticScope.getVar(qName);
    if (var != null && !var.isTypeInferred()) {
      // Handle normal declarations that could not be addressed earlier.
      if (propName.equals("prototype") ||
      // Handle prototype declarations that could not be addressed earlier.
          (!objectType.hasOwnProperty(propName) &&
           (!objectType.isInstanceType() ||
               (var.isExtern() && !objectType.isNativeObjectType())))) {
        return objectType.defineDeclaredProperty(
            propName, var.getType(), getprop);
      }
    }
  }
  return false;
}
 
Example 8
Source File: TypeExpressionParser.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
@Override
public Void caseObjectType(ObjectType type) {
  if (type.isRecordType()) {
    caseRecordType(type);
  } else if (type.isInstanceType()) {
    caseInstanceType(type);
  } else if (type instanceof PrototypeObjectType) {
    casePrototypeObjectType((PrototypeObjectType) type);
  } else {
    throw new UnsupportedOperationException();
  }
  return null;
}
 
Example 9
Source File: InlineProperties.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Invalidates the given type, so that no properties on it will be renamed.
 */
private void addInvalidatingType(JSType type) {
  type = type.restrictByNotNullOrUndefined();
  if (type.isUnionType()) {
    for (JSType alt : type.toMaybeUnionType().getAlternates()) {
      addInvalidatingType(alt);
    }
  }

  invalidatingTypes.add(type);
  ObjectType objType = ObjectType.cast(type);
  if (objType != null && objType.isInstanceType()) {
    invalidatingTypes.add(objType.getImplicitPrototype());
  }
}
 
Example 10
Source File: AmbiguateProperties.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Invalidates the given type, so that no properties on it will be renamed.
 */
private void addInvalidatingType(JSType type) {
  type = type.restrictByNotNullOrUndefined();
  if (type.isUnionType()) {
    for (JSType alt : type.toMaybeUnionType().getAlternates()) {
      addInvalidatingType(alt);
    }
  }

  invalidatingTypes.add(type);
  ObjectType objType = ObjectType.cast(type);
  if (objType != null && objType.isInstanceType()) {
    invalidatingTypes.add(objType.getImplicitPrototype());
  }
}
 
Example 11
Source File: Closure_35_TypeInference_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Defines a property if the property has not been defined yet.
 */
private void ensurePropertyDefined(Node getprop, JSType rightType) {
  String propName = getprop.getLastChild().getString();
  JSType nodeType = getJSType(getprop.getFirstChild());
  ObjectType objectType = ObjectType.cast(
      nodeType.restrictByNotNullOrUndefined());
  if (objectType == null) {
    registry.registerPropertyOnType(propName, nodeType);
  } else {
    if (ensurePropertyDeclaredHelper(getprop, objectType)) {
      return;
    }

    if (!objectType.isPropertyTypeDeclared(propName)) {
      // We do not want a "stray" assign to define an inferred property
      // for every object of this type in the program. So we use a heuristic
      // approach to determine whether to infer the propery.
      //
      // 1) If the property is already defined, join it with the previously
      //    inferred type.
      // 2) If this isn't an instance object, define it.
      // 3) If the property of an object is being assigned in the constructor,
      //    define it.
      // 4) If this is a stub, define it.
      // 5) Otherwise, do not define the type, but declare it in the registry
      //    so that we can use it for missing property checks.
      if (objectType.hasProperty(propName) ||
          !objectType.isInstanceType()) {
        if ("prototype".equals(propName)) {
          objectType.defineDeclaredProperty(
              propName, rightType, getprop);
        } else {
          objectType.defineInferredProperty(
              propName, rightType, getprop);
        }
      } else {
        if (getprop.getFirstChild().isThis() &&
            getJSType(syntacticScope.getRootNode()).isConstructor()) {
          objectType.defineInferredProperty(
              propName, rightType, getprop);
        } else {
          registry.registerPropertyOnType(propName, objectType);
        }
      }
    }
  }
}
 
Example 12
Source File: Closure_35_TypeInference_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Defines a property if the property has not been defined yet.
 */
private void ensurePropertyDefined(Node getprop, JSType rightType) {
  String propName = getprop.getLastChild().getString();
  JSType nodeType = getJSType(getprop.getFirstChild());
  ObjectType objectType = ObjectType.cast(
      nodeType.restrictByNotNullOrUndefined());
  if (objectType == null) {
    registry.registerPropertyOnType(propName, nodeType);
  } else {
    if (ensurePropertyDeclaredHelper(getprop, objectType)) {
      return;
    }

    if (!objectType.isPropertyTypeDeclared(propName)) {
      // We do not want a "stray" assign to define an inferred property
      // for every object of this type in the program. So we use a heuristic
      // approach to determine whether to infer the propery.
      //
      // 1) If the property is already defined, join it with the previously
      //    inferred type.
      // 2) If this isn't an instance object, define it.
      // 3) If the property of an object is being assigned in the constructor,
      //    define it.
      // 4) If this is a stub, define it.
      // 5) Otherwise, do not define the type, but declare it in the registry
      //    so that we can use it for missing property checks.
      if (objectType.hasProperty(propName) ||
          !objectType.isInstanceType()) {
        if ("prototype".equals(propName)) {
          objectType.defineDeclaredProperty(
              propName, rightType, getprop);
        } else {
          objectType.defineInferredProperty(
              propName, rightType, getprop);
        }
      } else {
        if (getprop.getFirstChild().isThis() &&
            getJSType(syntacticScope.getRootNode()).isConstructor()) {
          objectType.defineInferredProperty(
              propName, rightType, getprop);
        } else {
          registry.registerPropertyOnType(propName, objectType);
        }
      }
    }
  }
}
 
Example 13
Source File: Closure_112_TypeInference_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Defines a property if the property has not been defined yet.
 */
private void ensurePropertyDefined(Node getprop, JSType rightType) {
  String propName = getprop.getLastChild().getString();
  Node obj = getprop.getFirstChild();
  JSType nodeType = getJSType(obj);
  ObjectType objectType = ObjectType.cast(
      nodeType.restrictByNotNullOrUndefined());
  boolean propCreationInConstructor = obj.isThis() &&
      getJSType(syntacticScope.getRootNode()).isConstructor();

  if (objectType == null) {
    registry.registerPropertyOnType(propName, nodeType);
  } else {
    if (nodeType.isStruct() && !objectType.hasProperty(propName)) {
      // In general, we don't want to define a property on a struct object,
      // b/c TypeCheck will later check for improper property creation on
      // structs. There are two exceptions.
      // 1) If it's a property created inside the constructor, on the newly
      //    created instance, allow it.
      // 2) If it's a prototype property, allow it. For example:
      //    Foo.prototype.bar = baz;
      //    where Foo.prototype is a struct and the assignment happens at the
      //    top level and the constructor Foo is defined in the same file.
      boolean staticPropCreation = false;
      Node maybeAssignStm = getprop.getParent().getParent();
      if (syntacticScope.isGlobal() &&
          NodeUtil.isPrototypePropertyDeclaration(maybeAssignStm)) {
        String propCreationFilename = maybeAssignStm.getSourceFileName();
        Node ctor = objectType.getOwnerFunction().getSource();
        if (ctor != null &&
            ctor.getSourceFileName().equals(propCreationFilename)) {
          staticPropCreation = true;
        }
      }
      if (!propCreationInConstructor && !staticPropCreation) {
        return; // Early return to avoid creating the property below.
      }
    }

    if (ensurePropertyDeclaredHelper(getprop, objectType)) {
      return;
    }

    if (!objectType.isPropertyTypeDeclared(propName)) {
      // We do not want a "stray" assign to define an inferred property
      // for every object of this type in the program. So we use a heuristic
      // approach to determine whether to infer the property.
      //
      // 1) If the property is already defined, join it with the previously
      //    inferred type.
      // 2) If this isn't an instance object, define it.
      // 3) If the property of an object is being assigned in the constructor,
      //    define it.
      // 4) If this is a stub, define it.
      // 5) Otherwise, do not define the type, but declare it in the registry
      //    so that we can use it for missing property checks.
      if (objectType.hasProperty(propName) || !objectType.isInstanceType()) {
        if ("prototype".equals(propName)) {
          objectType.defineDeclaredProperty(propName, rightType, getprop);
        } else {
          objectType.defineInferredProperty(propName, rightType, getprop);
        }
      } else if (propCreationInConstructor) {
        objectType.defineInferredProperty(propName, rightType, getprop);
      } else {
        registry.registerPropertyOnType(propName, objectType);
      }
    }
  }
}
 
Example 14
Source File: Closure_112_TypeInference_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Defines a property if the property has not been defined yet.
 */
private void ensurePropertyDefined(Node getprop, JSType rightType) {
  String propName = getprop.getLastChild().getString();
  Node obj = getprop.getFirstChild();
  JSType nodeType = getJSType(obj);
  ObjectType objectType = ObjectType.cast(
      nodeType.restrictByNotNullOrUndefined());
  boolean propCreationInConstructor = obj.isThis() &&
      getJSType(syntacticScope.getRootNode()).isConstructor();

  if (objectType == null) {
    registry.registerPropertyOnType(propName, nodeType);
  } else {
    if (nodeType.isStruct() && !objectType.hasProperty(propName)) {
      // In general, we don't want to define a property on a struct object,
      // b/c TypeCheck will later check for improper property creation on
      // structs. There are two exceptions.
      // 1) If it's a property created inside the constructor, on the newly
      //    created instance, allow it.
      // 2) If it's a prototype property, allow it. For example:
      //    Foo.prototype.bar = baz;
      //    where Foo.prototype is a struct and the assignment happens at the
      //    top level and the constructor Foo is defined in the same file.
      boolean staticPropCreation = false;
      Node maybeAssignStm = getprop.getParent().getParent();
      if (syntacticScope.isGlobal() &&
          NodeUtil.isPrototypePropertyDeclaration(maybeAssignStm)) {
        String propCreationFilename = maybeAssignStm.getSourceFileName();
        Node ctor = objectType.getOwnerFunction().getSource();
        if (ctor != null &&
            ctor.getSourceFileName().equals(propCreationFilename)) {
          staticPropCreation = true;
        }
      }
      if (!propCreationInConstructor && !staticPropCreation) {
        return; // Early return to avoid creating the property below.
      }
    }

    if (ensurePropertyDeclaredHelper(getprop, objectType)) {
      return;
    }

    if (!objectType.isPropertyTypeDeclared(propName)) {
      // We do not want a "stray" assign to define an inferred property
      // for every object of this type in the program. So we use a heuristic
      // approach to determine whether to infer the property.
      //
      // 1) If the property is already defined, join it with the previously
      //    inferred type.
      // 2) If this isn't an instance object, define it.
      // 3) If the property of an object is being assigned in the constructor,
      //    define it.
      // 4) If this is a stub, define it.
      // 5) Otherwise, do not define the type, but declare it in the registry
      //    so that we can use it for missing property checks.
      if (objectType.hasProperty(propName) || !objectType.isInstanceType()) {
        if ("prototype".equals(propName)) {
          objectType.defineDeclaredProperty(propName, rightType, getprop);
        } else {
          objectType.defineInferredProperty(propName, rightType, getprop);
        }
      } else if (propCreationInConstructor) {
        objectType.defineInferredProperty(propName, rightType, getprop);
      } else {
        registry.registerPropertyOnType(propName, objectType);
      }
    }
  }
}
 
Example 15
Source File: Closure_71_CheckAccessControls_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether the given property with @const tag got reassigned
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkConstantProperty(NodeTraversal t,
    Node getprop) {
  // Check whether the property is modified
  Node parent = getprop.getParent();
  if (!(NodeUtil.isAssignmentOp(parent) && parent.getFirstChild() == getprop)
      && (parent.getType() != Token.INC) && (parent.getType() != Token.DEC)) {
    return;
  }

  ObjectType objectType =
    ObjectType.cast(dereference(getprop.getFirstChild().getJSType()));
  String propertyName = getprop.getLastChild().getString();

  // Check whether constant properties are reassigned
  if (objectType != null) {
    ObjectType oType = objectType;
    while (oType != null) {
      if (oType.hasReferenceName()) {
        if (initializedConstantProperties.containsEntry(
                oType.getReferenceName(), propertyName)) {
          compiler.report(
              t.makeError(getprop, CONST_PROPERTY_REASSIGNED_VALUE,
                  propertyName));
          break;
        }
      }
      oType = oType.getImplicitPrototype();
    }

    JSDocInfo info = objectType.getOwnPropertyJSDocInfo(propertyName);
    if (info != null && info.isConstant()
        && objectType.hasReferenceName()) {
      initializedConstantProperties.put(objectType.getReferenceName(),
          propertyName);
    }

    // Add the prototype when we're looking at an instance object
    if (objectType.isInstanceType()) {
      ObjectType prototype = objectType.getImplicitPrototype();
      if (prototype != null) {
        JSDocInfo prototypeInfo
          = prototype.getOwnPropertyJSDocInfo(propertyName);
        if (prototypeInfo != null && prototypeInfo.isConstant()
            && prototype.hasReferenceName()) {
          initializedConstantProperties.put(prototype.getReferenceName(),
              propertyName);
        }
      }
    }
  }
}
 
Example 16
Source File: Closure_71_CheckAccessControls_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether the given property with @const tag got reassigned
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkConstantProperty(NodeTraversal t,
    Node getprop) {
  // Check whether the property is modified
  Node parent = getprop.getParent();
  if (!(NodeUtil.isAssignmentOp(parent) && parent.getFirstChild() == getprop)
      && (parent.getType() != Token.INC) && (parent.getType() != Token.DEC)) {
    return;
  }

  ObjectType objectType =
    ObjectType.cast(dereference(getprop.getFirstChild().getJSType()));
  String propertyName = getprop.getLastChild().getString();

  // Check whether constant properties are reassigned
  if (objectType != null) {
    ObjectType oType = objectType;
    while (oType != null) {
      if (oType.hasReferenceName()) {
        if (initializedConstantProperties.containsEntry(
                oType.getReferenceName(), propertyName)) {
          compiler.report(
              t.makeError(getprop, CONST_PROPERTY_REASSIGNED_VALUE,
                  propertyName));
          break;
        }
      }
      oType = oType.getImplicitPrototype();
    }

    JSDocInfo info = objectType.getOwnPropertyJSDocInfo(propertyName);
    if (info != null && info.isConstant()
        && objectType.hasReferenceName()) {
      initializedConstantProperties.put(objectType.getReferenceName(),
          propertyName);
    }

    // Add the prototype when we're looking at an instance object
    if (objectType.isInstanceType()) {
      ObjectType prototype = objectType.getImplicitPrototype();
      if (prototype != null) {
        JSDocInfo prototypeInfo
          = prototype.getOwnPropertyJSDocInfo(propertyName);
        if (prototypeInfo != null && prototypeInfo.isConstant()
            && prototype.hasReferenceName()) {
          initializedConstantProperties.put(prototype.getReferenceName(),
              propertyName);
        }
      }
    }
  }
}
 
Example 17
Source File: CheckAccessControls.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Determines whether the given property with @const tag got reassigned
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkConstantProperty(NodeTraversal t,
    Node getprop) {
  // Check whether the property is modified
  Node parent = getprop.getParent();
  boolean isDelete = parent.isDelProp();
  if (!(NodeUtil.isAssignmentOp(parent) && parent.getFirstChild() == getprop)
      && !parent.isInc() && !parent.isDec()
      && !isDelete) {
    return;
  }

  ObjectType objectType =
    ObjectType.cast(dereference(getprop.getFirstChild().getJSType()));
  String propertyName = getprop.getLastChild().getString();

  boolean isConstant = isPropertyDeclaredConstant(objectType, propertyName);

  // Check whether constant properties are reassigned
  if (isConstant) {
    if (isDelete) {
      compiler.report(
          t.makeError(getprop, CONST_PROPERTY_DELETED, propertyName));
      return;
    }

    ObjectType oType = objectType;
    while (oType != null) {
      if (oType.hasReferenceName()) {
        if (initializedConstantProperties.containsEntry(
                oType.getReferenceName(), propertyName)) {
          compiler.report(
              t.makeError(getprop, CONST_PROPERTY_REASSIGNED_VALUE,
                  propertyName));
          break;
        }
      }
      oType = oType.getImplicitPrototype();
    }

    Preconditions.checkState(objectType.hasReferenceName());
    initializedConstantProperties.put(objectType.getReferenceName(),
        propertyName);

    // Add the prototype when we're looking at an instance object
    if (objectType.isInstanceType()) {
      ObjectType prototype = objectType.getImplicitPrototype();
      if (prototype != null) {
        if (prototype.hasProperty(propertyName)
            && prototype.hasReferenceName()) {
          initializedConstantProperties.put(prototype.getReferenceName(),
              propertyName);
        }
      }
    }
  }
}
 
Example 18
Source File: TypeInference.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Defines a property if the property has not been defined yet.
 */
private void ensurePropertyDefined(Node getprop, JSType rightType) {
  String propName = getprop.getLastChild().getString();
  Node obj = getprop.getFirstChild();
  JSType nodeType = getJSType(obj);
  ObjectType objectType = ObjectType.cast(
      nodeType.restrictByNotNullOrUndefined());
  if (objectType == null) {
    registry.registerPropertyOnType(propName, nodeType);
  } else {
    // Don't add the property to @struct objects outside a constructor
    if (nodeType.isStruct() && !objectType.hasProperty(propName)) {
      if (!(obj.isThis() &&
            getJSType(syntacticScope.getRootNode()).isConstructor())) {
        return;
      }
    }

    if (ensurePropertyDeclaredHelper(getprop, objectType)) {
      return;
    }

    if (!objectType.isPropertyTypeDeclared(propName)) {
      // We do not want a "stray" assign to define an inferred property
      // for every object of this type in the program. So we use a heuristic
      // approach to determine whether to infer the property.
      //
      // 1) If the property is already defined, join it with the previously
      //    inferred type.
      // 2) If this isn't an instance object, define it.
      // 3) If the property of an object is being assigned in the constructor,
      //    define it.
      // 4) If this is a stub, define it.
      // 5) Otherwise, do not define the type, but declare it in the registry
      //    so that we can use it for missing property checks.
      if (objectType.hasProperty(propName) || !objectType.isInstanceType()) {
        if ("prototype".equals(propName)) {
          objectType.defineDeclaredProperty(propName, rightType, getprop);
        } else {
          objectType.defineInferredProperty(propName, rightType, getprop);
        }
      } else if (obj.isThis() &&
                 getJSType(syntacticScope.getRootNode()).isConstructor()) {
        objectType.defineInferredProperty(propName, rightType, getprop);
      } else {
        registry.registerPropertyOnType(propName, objectType);
      }
    }
  }
}
 
Example 19
Source File: Closure_25_TypeInference_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Defines a property if the property has not been defined yet.
 */
private void ensurePropertyDefined(Node getprop, JSType rightType) {
  String propName = getprop.getLastChild().getString();
  JSType nodeType = getJSType(getprop.getFirstChild());
  ObjectType objectType = ObjectType.cast(
      nodeType.restrictByNotNullOrUndefined());
  if (objectType == null) {
    registry.registerPropertyOnType(propName, nodeType);
  } else {
    if (ensurePropertyDeclaredHelper(getprop, objectType)) {
      return;
    }

    if (!objectType.isPropertyTypeDeclared(propName)) {
      // We do not want a "stray" assign to define an inferred property
      // for every object of this type in the program. So we use a heuristic
      // approach to determine whether to infer the property.
      //
      // 1) If the property is already defined, join it with the previously
      //    inferred type.
      // 2) If this isn't an instance object, define it.
      // 3) If the property of an object is being assigned in the constructor,
      //    define it.
      // 4) If this is a stub, define it.
      // 5) Otherwise, do not define the type, but declare it in the registry
      //    so that we can use it for missing property checks.
      if (objectType.hasProperty(propName) ||
          !objectType.isInstanceType()) {
        if ("prototype".equals(propName)) {
          objectType.defineDeclaredProperty(
              propName, rightType, getprop);
        } else {
          objectType.defineInferredProperty(
              propName, rightType, getprop);
        }
      } else {
        if (getprop.getFirstChild().isThis() &&
            getJSType(syntacticScope.getRootNode()).isConstructor()) {
          objectType.defineInferredProperty(
              propName, rightType, getprop);
        } else {
          registry.registerPropertyOnType(propName, objectType);
        }
      }
    }
  }
}
 
Example 20
Source File: Closure_25_TypeInference_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Defines a property if the property has not been defined yet.
 */
private void ensurePropertyDefined(Node getprop, JSType rightType) {
  String propName = getprop.getLastChild().getString();
  JSType nodeType = getJSType(getprop.getFirstChild());
  ObjectType objectType = ObjectType.cast(
      nodeType.restrictByNotNullOrUndefined());
  if (objectType == null) {
    registry.registerPropertyOnType(propName, nodeType);
  } else {
    if (ensurePropertyDeclaredHelper(getprop, objectType)) {
      return;
    }

    if (!objectType.isPropertyTypeDeclared(propName)) {
      // We do not want a "stray" assign to define an inferred property
      // for every object of this type in the program. So we use a heuristic
      // approach to determine whether to infer the property.
      //
      // 1) If the property is already defined, join it with the previously
      //    inferred type.
      // 2) If this isn't an instance object, define it.
      // 3) If the property of an object is being assigned in the constructor,
      //    define it.
      // 4) If this is a stub, define it.
      // 5) Otherwise, do not define the type, but declare it in the registry
      //    so that we can use it for missing property checks.
      if (objectType.hasProperty(propName) ||
          !objectType.isInstanceType()) {
        if ("prototype".equals(propName)) {
          objectType.defineDeclaredProperty(
              propName, rightType, getprop);
        } else {
          objectType.defineInferredProperty(
              propName, rightType, getprop);
        }
      } else {
        if (getprop.getFirstChild().isThis() &&
            getJSType(syntacticScope.getRootNode()).isConstructor()) {
          objectType.defineInferredProperty(
              propName, rightType, getprop);
        } else {
          registry.registerPropertyOnType(propName, objectType);
        }
      }
    }
  }
}