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

The following examples show how to use com.google.javascript.rhino.jstype.JSType#equals() . 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_96_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example 2
Source File: Closure_35_TypeInference_t.java    From coming with MIT License 6 votes vote down vote up
private FlowScope traverseCall(Node n, FlowScope scope) {
  scope = traverseChildren(n, scope);

  Node left = n.getFirstChild();
  JSType functionType = getJSType(left).restrictByNotNullOrUndefined();
  if (functionType != null) {
    if (functionType.isFunctionType()) {
      FunctionType fnType = functionType.toMaybeFunctionType();
      n.setJSType(fnType.getReturnType());
      backwardsInferenceFromCallSite(n, fnType);
    } else if (functionType.equals(getNativeType(CHECKED_UNKNOWN_TYPE))) {
      n.setJSType(getNativeType(CHECKED_UNKNOWN_TYPE));
    }
  }

  scope = tightenTypesAfterAssertions(scope, n);
  return scope;
}
 
Example 3
Source File: Closure_35_TypeInference_s.java    From coming with MIT License 6 votes vote down vote up
private FlowScope traverseCall(Node n, FlowScope scope) {
  scope = traverseChildren(n, scope);

  Node left = n.getFirstChild();
  JSType functionType = getJSType(left).restrictByNotNullOrUndefined();
  if (functionType != null) {
    if (functionType.isFunctionType()) {
      FunctionType fnType = functionType.toMaybeFunctionType();
      n.setJSType(fnType.getReturnType());
      backwardsInferenceFromCallSite(n, fnType);
    } else if (functionType.equals(getNativeType(CHECKED_UNKNOWN_TYPE))) {
      n.setJSType(getNativeType(CHECKED_UNKNOWN_TYPE));
    }
  }

  scope = tightenTypesAfterAssertions(scope, n);
  return scope;
}
 
Example 4
Source File: Closure_25_TypeInference_t.java    From coming with MIT License 6 votes vote down vote up
private FlowScope traverseCall(Node n, FlowScope scope) {
  scope = traverseChildren(n, scope);

  Node left = n.getFirstChild();
  JSType functionType = getJSType(left).restrictByNotNullOrUndefined();
  if (functionType != null) {
    if (functionType.isFunctionType()) {
      FunctionType fnType = functionType.toMaybeFunctionType();
      n.setJSType(fnType.getReturnType());
      backwardsInferenceFromCallSite(n, fnType);
    } else if (functionType.equals(getNativeType(CHECKED_UNKNOWN_TYPE))) {
      n.setJSType(getNativeType(CHECKED_UNKNOWN_TYPE));
    }
  }

  scope = tightenTypesAfterAssertions(scope, n);
  return scope;
}
 
Example 5
Source File: Closure_25_TypeInference_s.java    From coming with MIT License 6 votes vote down vote up
private FlowScope traverseCall(Node n, FlowScope scope) {
  scope = traverseChildren(n, scope);

  Node left = n.getFirstChild();
  JSType functionType = getJSType(left).restrictByNotNullOrUndefined();
  if (functionType != null) {
    if (functionType.isFunctionType()) {
      FunctionType fnType = functionType.toMaybeFunctionType();
      n.setJSType(fnType.getReturnType());
      backwardsInferenceFromCallSite(n, fnType);
    } else if (functionType.equals(getNativeType(CHECKED_UNKNOWN_TYPE))) {
      n.setJSType(getNativeType(CHECKED_UNKNOWN_TYPE));
    }
  }

  scope = tightenTypesAfterAssertions(scope, n);
  return scope;
}
 
Example 6
Source File: Closure_66_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example 7
Source File: Closure_66_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example 8
Source File: Closure_69_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example 9
Source File: Closure_69_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example 10
Source File: Nopol2017_0051_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example 11
Source File: Nopol2017_0051_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example 12
Source File: DeclarationGenerator.java    From clutz with MIT License 6 votes vote down vote up
/**
 * Special handling for simple typing returning polymorphic this type in TypeScript. Prefer
 * `func(): this` instead of `func<T>(this: T): T` when any params are not templatized.
 */
private boolean shouldEmitThisParam(FunctionType ftype) {
  final JSType typeOfThis = ftype.getTypeOfThis();
  if (typeOfThis == null
      || !typeOfThis.isTemplateType()
      || !typeOfThis.equals(ftype.getReturnType())) {
    return true;
  }

  for (Parameter parameter : ftype.getParameters()) {
    JSType paramType = parameter.getJSType();
    if (!paramType.isTemplatizedType()) {
      continue;
    }

    final TemplateTypeMap templateTypeMap = paramType.getTemplateTypeMap();
    for (TemplateType key : templateTypeMap.getTemplateKeys()) {
      if (templateTypeMap.getResolvedTemplateType(key).equals(typeOfThis)) {
        return true;
      }
    }
  }

  return false;
}
 
Example 13
Source File: Closure_25_TypeInference_s.java    From coming with MIT License 5 votes vote down vote up
private JSType getPropertyType(JSType objType, String propName,
    Node n, FlowScope scope) {
  // Scopes sometimes contain inferred type info about qualified names.
  String qualifiedName = n.getQualifiedName();
  StaticSlot<JSType> var = scope.getSlot(qualifiedName);
  if (var != null) {
    JSType varType = var.getType();
    if (varType != null) {
      if (varType.equals(getNativeType(UNKNOWN_TYPE)) &&
          var != syntacticScope.getSlot(qualifiedName)) {
        // If the type of this qualified name has been checked in this scope,
        // then use CHECKED_UNKNOWN_TYPE instead to indicate that.
        return getNativeType(CHECKED_UNKNOWN_TYPE);
      } else {
        return varType;
      }
    }
  }

  JSType propertyType = null;
  if (objType != null) {
    propertyType = objType.findPropertyType(propName);
  }

  if ((propertyType == null || propertyType.isUnknownType()) &&
      qualifiedName != null) {
    // If we find this node in the registry, then we can infer its type.
    ObjectType regType = ObjectType.cast(registry.getType(qualifiedName));
    if (regType != null) {
      propertyType = regType.getConstructor();
    }
  }

  return propertyType;
}
 
Example 14
Source File: Closure_25_TypeInference_t.java    From coming with MIT License 5 votes vote down vote up
private JSType getPropertyType(JSType objType, String propName,
    Node n, FlowScope scope) {
  // Scopes sometimes contain inferred type info about qualified names.
  String qualifiedName = n.getQualifiedName();
  StaticSlot<JSType> var = scope.getSlot(qualifiedName);
  if (var != null) {
    JSType varType = var.getType();
    if (varType != null) {
      if (varType.equals(getNativeType(UNKNOWN_TYPE)) &&
          var != syntacticScope.getSlot(qualifiedName)) {
        // If the type of this qualified name has been checked in this scope,
        // then use CHECKED_UNKNOWN_TYPE instead to indicate that.
        return getNativeType(CHECKED_UNKNOWN_TYPE);
      } else {
        return varType;
      }
    }
  }

  JSType propertyType = null;
  if (objType != null) {
    propertyType = objType.findPropertyType(propName);
  }

  if ((propertyType == null || propertyType.isUnknownType()) &&
      qualifiedName != null) {
    // If we find this node in the registry, then we can infer its type.
    ObjectType regType = ObjectType.cast(registry.getType(qualifiedName));
    if (regType != null) {
      propertyType = regType.getConstructor();
    }
  }

  return propertyType;
}
 
Example 15
Source File: Closure_35_TypeInference_s.java    From coming with MIT License 5 votes vote down vote up
private JSType getPropertyType(JSType objType, String propName,
    Node n, FlowScope scope) {
  // Scopes sometimes contain inferred type info about qualified names.
  String qualifiedName = n.getQualifiedName();
  StaticSlot<JSType> var = scope.getSlot(qualifiedName);
  if (var != null) {
    JSType varType = var.getType();
    if (varType != null) {
      if (varType.equals(getNativeType(UNKNOWN_TYPE)) &&
          var != syntacticScope.getSlot(qualifiedName)) {
        // If the type of this qualified name has been checked in this scope,
        // then use CHECKED_UNKNOWN_TYPE instead to indicate that.
        return getNativeType(CHECKED_UNKNOWN_TYPE);
      } else {
        return varType;
      }
    }
  }

  JSType propertyType = null;
  if (objType != null) {
    propertyType = objType.findPropertyType(propName);
  }

  if ((propertyType == null || propertyType.isUnknownType()) &&
      qualifiedName != null) {
    // If we find this node in the registry, then we can infer its type.
    ObjectType regType = ObjectType.cast(registry.getType(qualifiedName));
    if (regType != null) {
      propertyType = regType.getConstructor();
    }
  }

  return propertyType;
}
 
Example 16
Source File: Closure_35_TypeInference_t.java    From coming with MIT License 5 votes vote down vote up
private JSType getPropertyType(JSType objType, String propName,
    Node n, FlowScope scope) {
  // Scopes sometimes contain inferred type info about qualified names.
  String qualifiedName = n.getQualifiedName();
  StaticSlot<JSType> var = scope.getSlot(qualifiedName);
  if (var != null) {
    JSType varType = var.getType();
    if (varType != null) {
      if (varType.equals(getNativeType(UNKNOWN_TYPE)) &&
          var != syntacticScope.getSlot(qualifiedName)) {
        // If the type of this qualified name has been checked in this scope,
        // then use CHECKED_UNKNOWN_TYPE instead to indicate that.
        return getNativeType(CHECKED_UNKNOWN_TYPE);
      } else {
        return varType;
      }
    }
  }

  JSType propertyType = null;
  if (objType != null) {
    propertyType = objType.findPropertyType(propName);
  }

  if ((propertyType == null || propertyType.isUnknownType()) &&
      qualifiedName != null) {
    // If we find this node in the registry, then we can infer its type.
    ObjectType regType = ObjectType.cast(registry.getType(qualifiedName));
    if (regType != null) {
      propertyType = regType.getConstructor();
    }
  }

  return propertyType;
}
 
Example 17
Source File: TightenTypes.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private boolean equalsJSType(JSType jsType) {
  if (jsType == null || jstype == null) {
    return jstype == jsType;
  } else {
    return jsType.equals(this.jstype);
  }
}
 
Example 18
Source File: TypeCollectionPass.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
private void processSymbol(Symbol symbol) {
  final JSType globalThis =
      compiler.getTypeRegistry().getNativeObjectType(JSTypeNative.GLOBAL_THIS);

  if (typeRegistry.isType(symbol.getName())) {
    return;
  }

  JSType type = null;
  boolean isFromCompilerRegistry = false;

  if (symbol.getReferencedSymbol() != null
      && typeRegistry.isType(symbol.getReferencedSymbol())) {
    type = typeRegistry.getType(symbol.getReferencedSymbol()).getType();
  }

  if (type == null) {
    type = compiler.getTypeRegistry().getGlobalType(symbol.getName());
    isFromCompilerRegistry = type != null;
  }

  if (type == null) {
    type = globalThis;
    for (String part : Splitter.on('.').split(symbol.getName())) {
      type = type.findPropertyType(part);
      if (type == null) {
        return;
      }
    }
  }

  if (type != null) {
    if (type.isInstanceType()
        && (isFromCompilerRegistry
            || shouldConvertToConstructor(type)
            || shouldConvertToConstructor(symbol))) {
      type = type.toObjectType().getConstructor();
    } else if (type.isEnumElementType()) {
      type = type.toMaybeEnumElementType().getEnumType();
    }
  }

  if (type == null) {
    log.warning("skipping " + symbol.getName() + "; no type");
    return;
  } else if (globalThis.equals(type)) {
    log.warning("skipping " + symbol.getName() + "; references global this");
    return;
  } else if (type.isOrdinaryFunction() && !typeRegistry.isProvided(symbol.getName())) {
    log.info("skipping " + symbol.getName() + "; is ordinary function");
    return;
  }
  collectTypes(symbol.getName(), type, symbol.getNode(), symbol.getJSDocInfo());
}
 
Example 19
Source File: DeclarationGenerator.java    From clutz with MIT License 4 votes vote down vote up
/** Whether the type was written as the literal 'Function' type */
private boolean isLiteralFunction(JSType type) {
  return type.equals(typeRegistry.getNativeType(JSTypeNative.FUNCTION_TYPE));
}
 
Example 20
Source File: DeclarationGenerator.java    From clutz with MIT License 4 votes vote down vote up
private void visitEnumType(String symbolName, String qualifiedName, EnumType type, Node node) {
  // Enums are top level vars, but also declare a corresponding type:
  // <pre>
  // /** @enum {ValueType} */ var MyEnum = {A: ..., B: ...};
  // type MyEnum = EnumValueType;
  // var MyEnum: {A: MyEnum, B: MyEnum, ...};
  // </pre>
  // We special case "number" enums and "string" enums.

  // TS `type` declarations accept only unqualified names.
  String unqualifiedName = getUnqualifiedName(symbolName);

  // @enums can be aliased by assignment. Emit a type alias + value alias for the situation.
  String elementsTypeName = type.getElementsType().getReferenceName();
  if (qualifiedName.equals(elementsTypeName)) {
    // @enums can also be aliased to external values even if they have the same type.
    if (node != null && node.getNext() != null && node.getNext().isGetProp()) {
      elementsTypeName = node.getNext().getQualifiedName();
    }
  }

  if (!qualifiedName.equals(elementsTypeName)) {
    emitComment(symbolName + " aliases enum " + elementsTypeName);
    emit("export import");
    emit(unqualifiedName);
    emit("=");
    emit(elementsTypeName);
    emit(";");
    emitBreak();
    return;
  }

  // The current node points to either:
  // 1) The GETPROP node for a goog.provide style export - a.b.MyEnum = {...};
  // 2) The STRINGLIT node for a goog.module style export - exports = { MyEnum: {...}, ...}
  // For case 1) we need to get the next node, while for 2) we need to get the first child.
  Node objectOfAllMembers = node.getParent().isAssign() ? node.getNext() : node.getFirstChild();

  Map<String, Node> elements = new LinkedHashMap<>();
  for (Node element : objectOfAllMembers.children()) {
    elements.put(element.getString(), element.getFirstChild());
  }

  JSType primitiveType = type.getEnumeratedTypeOfEnumObject();
  if (maybeStringOrNumericEnum(type, unqualifiedName, objectOfAllMembers, elements)) {
    return;
  }

  // We checked the types of all members in the enums above. Since it's not all string literals
  // or numbers, we cannot emit a TypeScript enum. Instead, we emit it as a var/type alias pair.
  // Specially for string enums, we still try to emit as many literal types as possible to make
  // it close to the original intent from Closure. To do that we also inline all literal types
  // in the type alias so this type alias can be compared and assigned by the literal types
  // (because the literal types don't have clutzEnumBrand).
  if (primitiveType.equals(stringType)) {
    Set<String> literalTypes = collectAllLiterals(elements);
    visitTypeAliasForMixedStringEnums(primitiveType, unqualifiedName, literalTypes);
  } else {
    visitTypeAlias(primitiveType, unqualifiedName, true);
  }

  emit("let");
  emit(unqualifiedName);
  emit(": {");
  emitBreak();
  indent();
  for (String elem : sorted(type.getElements())) {
    emit(elem);
    emit(":");
    // For string enums that have some literal values and some calculated values, we  try to use
    // the literal values as types as much as possible. For calculated values there's nothing
    // much we can do. Just back off and use the type alias.
    Node n = elements.get(elem);
    if (primitiveType.equals(stringType) && n.isString()) {
      emit("'" + escapeEcmaScript(n.getString()) + "'");
    } else {
      // No need to use type.getMembersType(), this must match the type alias we just declared.
      emit(unqualifiedName);
    }
    emit(",");
    emitBreak();
  }
  unindent();
  emit("}");
  emitNoSpace(";");
  emitBreak();
}