Java Code Examples for com.google.javascript.rhino.jstype.StaticSlot#getDeclaration()

The following examples show how to use com.google.javascript.rhino.jstype.StaticSlot#getDeclaration() . 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_6_TypeValidator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that the property in an interface that this type implements is
 * implemented and correctly typed.
 */
private void expectInterfaceProperty(NodeTraversal t, Node n,
    ObjectType instance, ObjectType implementedInterface, String prop) {
  StaticSlot<JSType> propSlot = instance.getSlot(prop);
  if (propSlot == null) {
    // Not implemented
    String sourceName = n.getSourceFileName();
    sourceName = sourceName == null ? "" : sourceName;
    registerMismatch(instance, implementedInterface,
        report(JSError.make(sourceName, n,
        INTERFACE_METHOD_NOT_IMPLEMENTED,
        prop, implementedInterface.toString(), instance.toString())));
  } else {
    Node propNode = propSlot.getDeclaration() == null ?
        null : propSlot.getDeclaration().getNode();

    // Fall back on the constructor node if we can't find a node for the
    // property.
    propNode = propNode == null ? n : propNode;

    JSType found = propSlot.getType();
    JSType required
        = implementedInterface.getImplicitPrototype().getPropertyType(prop);
    found = found.restrictByNotNullOrUndefined();
    required = required.restrictByNotNullOrUndefined();
    if (!found.canAssignTo(required)) {
      // Implemented, but not correctly typed
      FunctionType constructor =
          implementedInterface.toObjectType().getConstructor();
      registerMismatch(found, required, report(t.makeError(propNode,
          HIDDEN_INTERFACE_PROPERTY_MISMATCH, prop,
          constructor.getTopMostDefiningType(prop).toString(),
          required.toString(), found.toString())));
    }
  }
}
 
Example 2
Source File: Closure_6_TypeValidator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that the property in an interface that this type implements is
 * implemented and correctly typed.
 */
private void expectInterfaceProperty(NodeTraversal t, Node n,
    ObjectType instance, ObjectType implementedInterface, String prop) {
  StaticSlot<JSType> propSlot = instance.getSlot(prop);
  if (propSlot == null) {
    // Not implemented
    String sourceName = n.getSourceFileName();
    sourceName = sourceName == null ? "" : sourceName;
    registerMismatch(instance, implementedInterface,
        report(JSError.make(sourceName, n,
        INTERFACE_METHOD_NOT_IMPLEMENTED,
        prop, implementedInterface.toString(), instance.toString())));
  } else {
    Node propNode = propSlot.getDeclaration() == null ?
        null : propSlot.getDeclaration().getNode();

    // Fall back on the constructor node if we can't find a node for the
    // property.
    propNode = propNode == null ? n : propNode;

    JSType found = propSlot.getType();
    JSType required
        = implementedInterface.getImplicitPrototype().getPropertyType(prop);
    found = found.restrictByNotNullOrUndefined();
    required = required.restrictByNotNullOrUndefined();
    if (!found.canAssignTo(required)) {
      // Implemented, but not correctly typed
      FunctionType constructor =
          implementedInterface.toObjectType().getConstructor();
      registerMismatch(found, required, report(t.makeError(propNode,
          HIDDEN_INTERFACE_PROPERTY_MISMATCH, prop,
          constructor.getTopMostDefiningType(prop).toString(),
          required.toString(), found.toString())));
    }
  }
}
 
Example 3
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Expect that the property in an interface that this type implements is
 * implemented and correctly typed.
 */
private void expectInterfaceProperty(NodeTraversal t, Node n,
    ObjectType instance, ObjectType implementedInterface, String prop) {
  StaticSlot<JSType> propSlot = instance.getSlot(prop);
  if (propSlot == null) {
    // Not implemented
    String sourceName = n.getSourceFileName();
    sourceName = sourceName == null ? "" : sourceName;
    registerMismatch(instance, implementedInterface,
        report(JSError.make(sourceName, n,
        INTERFACE_METHOD_NOT_IMPLEMENTED,
        prop, implementedInterface.toString(), instance.toString())));
  } else {
    Node propNode = propSlot.getDeclaration() == null ?
        null : propSlot.getDeclaration().getNode();

    // Fall back on the constructor node if we can't find a node for the
    // property.
    propNode = propNode == null ? n : propNode;

    JSType found = propSlot.getType();
    JSType required
        = implementedInterface.getImplicitPrototype().getPropertyType(prop);
    found = found.restrictByNotNullOrUndefined();
    required = required.restrictByNotNullOrUndefined();
    if (!found.isSubtype(required)) {
      // Implemented, but not correctly typed
      FunctionType constructor =
          implementedInterface.toObjectType().getConstructor();
      registerMismatch(found, required, report(t.makeError(propNode,
          HIDDEN_INTERFACE_PROPERTY_MISMATCH, prop,
          constructor.getTopMostDefiningType(prop).toString(),
          required.toString(), found.toString())));
    }
  }
}
 
Example 4
Source File: Closure_117_TypeValidator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Expect that the property in an interface that this type implements is
 * implemented and correctly typed.
 */
private void expectInterfaceProperty(NodeTraversal t, Node n,
    ObjectType instance, ObjectType implementedInterface, String prop) {
  StaticSlot<JSType> propSlot = instance.getSlot(prop);
  if (propSlot == null) {
    // Not implemented
    String sourceName = n.getSourceFileName();
    sourceName = sourceName == null ? "" : sourceName;
    registerMismatch(instance, implementedInterface,
        report(JSError.make(sourceName, n,
        INTERFACE_METHOD_NOT_IMPLEMENTED,
        prop, implementedInterface.toString(), instance.toString())));
  } else {
    Node propNode = propSlot.getDeclaration() == null ?
        null : propSlot.getDeclaration().getNode();

    // Fall back on the constructor node if we can't find a node for the
    // property.
    propNode = propNode == null ? n : propNode;

    JSType found = propSlot.getType();
    found = found.restrictByNotNullOrUndefined();

    JSType required
        = implementedInterface.getImplicitPrototype().getPropertyType(prop);
    TemplateTypeMap typeMap = implementedInterface.getTemplateTypeMap();
    if (!typeMap.isEmpty()) {
      TemplateTypeMapReplacer replacer = new TemplateTypeMapReplacer(
          typeRegistry, typeMap);
      required = required.visit(replacer);
    }
    required = required.restrictByNotNullOrUndefined();

    if (!found.isSubtype(required)) {
      // Implemented, but not correctly typed
      FunctionType constructor =
          implementedInterface.toObjectType().getConstructor();
      registerMismatch(found, required, report(t.makeError(propNode,
          HIDDEN_INTERFACE_PROPERTY_MISMATCH, prop,
          constructor.getTopMostDefiningType(prop).toString(),
          required.toString(), found.toString())));
    }
  }
}
 
Example 5
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Expect that the property in an interface that this type implements is
 * implemented and correctly typed.
 */
private void expectInterfaceProperty(NodeTraversal t, Node n,
    ObjectType instance, ObjectType implementedInterface, String prop) {
  StaticSlot<JSType> propSlot = instance.getSlot(prop);
  if (propSlot == null) {
    // Not implemented
    String sourceName = n.getSourceFileName();
    sourceName = sourceName == null ? "" : sourceName;
    registerMismatch(instance, implementedInterface,
        report(JSError.make(sourceName, n,
        INTERFACE_METHOD_NOT_IMPLEMENTED,
        prop, implementedInterface.toString(), instance.toString())));
  } else {
    Node propNode = propSlot.getDeclaration() == null ?
        null : propSlot.getDeclaration().getNode();

    // Fall back on the constructor node if we can't find a node for the
    // property.
    propNode = propNode == null ? n : propNode;

    JSType found = propSlot.getType();
    found = found.restrictByNotNullOrUndefined();

    JSType required
        = implementedInterface.getImplicitPrototype().getPropertyType(prop);
    TemplateTypeMap typeMap = implementedInterface.getTemplateTypeMap();
    if (!typeMap.isEmpty()) {
      TemplateTypeMapReplacer replacer = new TemplateTypeMapReplacer(
          typeRegistry, typeMap);
      required = required.visit(replacer);
    }
    required = required.restrictByNotNullOrUndefined();

    if (!found.isSubtype(required)) {
      // Implemented, but not correctly typed
      FunctionType constructor =
          implementedInterface.toObjectType().getConstructor();
      registerMismatch(found, required, report(t.makeError(propNode,
          HIDDEN_INTERFACE_PROPERTY_MISMATCH, prop,
          constructor.getTopMostDefiningType(prop).toString(),
          required.toString(), found.toString())));
    }
  }
}
 
Example 6
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());
      }
    }
  }
}