@Override public com.google.javascript.rhino.jstype.JSType getAssertedOldType(Node call, JSTypeRegistry registry) { return registry.getGlobalType(typeName); } }
public BanThrowOfNonErrorTypes(AbstractCompiler compiler, Requirement requirement) throws InvalidRequirementSpec { super(compiler, requirement); errorObjType = compiler.getTypeRegistry().getGlobalType("Error"); }
private AstFactory(JSTypeRegistry registry) { this.registry = registry; this.unknownType = getNativeType(JSTypeNative.UNKNOWN_TYPE); this.argumentsTypeSupplier = Suppliers.memoize( () -> { JSType globalType = registry.getGlobalType("Arguments"); if (globalType != null) { return globalType; } else { return unknownType; } }); ; }
protected JSType union(List<String> typeNames) { JSTypeRegistry registry = compiler.getTypeRegistry(); List<JSType> types = new ArrayList<>(); for (String typeName : typeNames) { JSType type = registry.getGlobalType(typeName); if (type != null) { types.add(type); } } if (types.isEmpty()) { return null; } else { return registry.createUnionType(types); } }
public BanCreateElement(AbstractCompiler compiler, Requirement requirement) throws InvalidRequirementSpec { super(compiler, requirement); bannedTags = new HashSet<>(); for (String value : requirement.getValueList()) { bannedTags.add(Ascii.toLowerCase(value)); } if (bannedTags.isEmpty()) { throw new InvalidRequirementSpec("Specify one or more values."); } domHelperType = compiler.getTypeRegistry().getGlobalType("goog.dom.DomHelper"); documentType = compiler.getTypeRegistry().getGlobalType("Document"); }
public Es6RewriteClass(AbstractCompiler compiler) { this.compiler = compiler; this.registry = compiler.getTypeRegistry(); this.astFactory = compiler.createAstFactory(); // Finds the type for `ObjectPropertyDescriptor`. Fallback to the unknown type if it's not // present, which may happen if typechecking hasn't run or this is a unit test w/o externs. JSType actualObjectPropertyDescriptorType = registry.getGlobalType("ObjectPropertyDescriptor"); this.objectPropertyDescriptorType = actualObjectPropertyDescriptorType != null ? actualObjectPropertyDescriptorType : registry.getNativeType(JSTypeNative.UNKNOWN_TYPE); }
/** * @return The Config object for the class match the specified type or null if no match was found. */ private Config findMatchingClass(JSType callClassType, Collection<String> declarationNames) { if (!callClassType.isEmptyType() && !callClassType.isUnknownType()) { for (String declarationName : declarationNames) { String className = getClassFromDeclarationName(declarationName); JSType methodClassType = registry.getGlobalType(className); if (methodClassType != null && callClassType.isSubtypeOf(methodClassType)) { return functions.get(declarationName); } } } return null; }
boolean isTypeCompatible(JSType receiverType, String typeName) { // Unknown/general types are compatible with everything. if (receiverType == null) { return true; } // Look up the typename in the registry. All the polyfilled method // receiver types are built-in JS types, so they had better not be // missing from the registry. JSType type = compiler.getTypeRegistry().getGlobalType(typeName); if (type == null) { throw new RuntimeException("Missing built-in type: " + typeName); } // If there is any non-bottom type in common, then the types are compatible. if (!receiverType.meetWith(type).isEmptyType()) { return true; } // One last check - if this is a wrapped primitive type, then check the unwrapped version too. String primitiveType = unwrapPrimitiveWrapperTypename(typeName); return primitiveType != null && isTypeCompatible(receiverType, primitiveType); } }
RestrictedPropertyWrite(AbstractCompiler compiler, Requirement requirement) throws InvalidRequirementSpec { super(compiler, requirement); if (requirement.getValueCount() == 0) { throw new InvalidRequirementSpec("missing value"); } JSTypeRegistry registry = compiler.getTypeRegistry(); ImmutableList.Builder<Restriction> builder = ImmutableList.builder(); for (String value : requirement.getValueList()) { String type = ConformanceUtil.getClassFromDeclarationName(ConformanceUtil.removeTypeDecl(value)); String property = ConformanceUtil.getPropertyFromDeclarationName(ConformanceUtil.removeTypeDecl(value)); String restrictedDecl = ConformanceUtil.getTypeFromValue(value); if (type == null || property == null || restrictedDecl == null) { throw new InvalidRequirementSpec("bad prop value"); } JSType restrictedType = ConformanceUtil.evaluateTypeString(compiler, restrictedDecl); builder.add(new Restriction(registry.getGlobalType(type), property, restrictedType)); } restrictions = builder.build(); }
public BanCreateDom(AbstractCompiler compiler, Requirement requirement) throws InvalidRequirementSpec { super(compiler, requirement); bannedTagAttrs = new ArrayList<>(); for (String value : requirement.getValueList()) { String[] tagAttr = value.split("\\."); if (tagAttr.length != 2 || tagAttr[0].isEmpty() || tagAttr[1].isEmpty()) { throw new InvalidRequirementSpec("Values must be in the format tagname.attribute."); } tagAttr[0] = tagAttr[0].toLowerCase(); bannedTagAttrs.add(tagAttr); } if (bannedTagAttrs.isEmpty()) { throw new InvalidRequirementSpec("Specify one or more values."); } domHelperType = compiler.getTypeRegistry().getGlobalType("goog.dom.DomHelper"); classNameTypes = compiler.getTypeRegistry().createUnionType(ImmutableList.of( compiler.getTypeRegistry().getNativeType(JSTypeNative.STRING_TYPE), compiler.getTypeRegistry().getNativeType(JSTypeNative.ARRAY_TYPE), compiler.getTypeRegistry().getNativeType(JSTypeNative.NULL_TYPE), compiler.getTypeRegistry().getNativeType(JSTypeNative.VOID_TYPE))); }
JSType typedef = compiler.getTypeRegistry().getGlobalType(name); if (typedef != null) { declareTypedefNamespace(symbol.getName(), typedef, Collections.emptySet());
foundNamespaceMembers = true; JSType registryType = typeRegistry.getGlobalType(qualifiedName); if (registryType != null) { visitTypeAlias(registryType, propName, false);
RestrictedMethodCall(AbstractCompiler compiler, Requirement requirement) throws InvalidRequirementSpec { super(compiler, requirement); if (requirement.getValueCount() == 0) { throw new InvalidRequirementSpec("missing value"); } JSTypeRegistry registry = compiler.getTypeRegistry(); ImmutableList.Builder <Restriction> builder = ImmutableList.builder(); for (String value : requirement.getValueList()) { String type = ConformanceUtil.getClassFromDeclarationName(ConformanceUtil.removeTypeDecl(value)); String property = ConformanceUtil.getPropertyFromDeclarationName(ConformanceUtil.removeTypeDecl(value)); String restrictedDecl = ConformanceUtil.getTypeFromValue(value); if (type == null || property == null || restrictedDecl == null) { throw new InvalidRequirementSpec("bad prop value"); } FunctionType restrictedCallType = ConformanceUtil.evaluateTypeString( compiler, restrictedDecl).toMaybeFunctionType(); if (restrictedCallType == null) { throw new InvalidRequirementSpec("invalid conformance type"); } builder.add(new Restriction(registry.getGlobalType(type), property, restrictedCallType)); } restrictions = builder.build(); }
BannedProperty(AbstractCompiler compiler, Requirement requirement) throws InvalidRequirementSpec { super(compiler, requirement); if (requirement.getValueCount() == 0) { throw new InvalidRequirementSpec("missing value"); } checkArgument( requirement.getType() == Type.BANNED_PROPERTY || requirement.getType() == Type.BANNED_PROPERTY_READ || requirement.getType() == Type.BANNED_PROPERTY_WRITE || requirement.getType() == Type.BANNED_PROPERTY_NON_CONSTANT_WRITE || requirement.getType() == Type.BANNED_PROPERTY_CALL); requirementType = requirement.getType(); JSTypeRegistry registry = compiler.getTypeRegistry(); ImmutableList.Builder<Property> builder = ImmutableList.builder(); List<String> values = requirement.getValueList(); for (String value : values) { String type = ConformanceUtil.getClassFromDeclarationName(value); String property = ConformanceUtil.getPropertyFromDeclarationName(value); if (type == null || property == null) { throw new InvalidRequirementSpec("bad prop value"); } builder.add(new Property(registry.getGlobalType(type), property)); } props = builder.build(); }
/** * Finds all typedefs in the program and build a Type -> typedef name mapping. The mapping is * needed because when walking type definitions closure inlines the typedefs values. */ void collectTypedefs() { for (TypedVar var : compiler.getTopScope().getAllSymbols()) { if (shouldSkipVar(var)) { continue; } // In Closure, unlike TypeScript there is no pure type space. Thus even typedefs declare // symbols. The type of the symbol corresponding to the typedef is *not* the same as the type // declared by the typedef. JSType type = var.getType(); if (type == null || !isTypedef(type) || var.getName().startsWith("window.") || isPrivate(var.getJSDocInfo())) { continue; } JSType realType = compiler.getTypeRegistry().getGlobalType(var.getName()); if (realType != null && shouldEmitTypedefByName(realType) && !typedefs.containsKey(realType)) { typedefs.put(realType, var.getName()); } } }
JSType type = typeRegistry.getGlobalType(dottedName); JSType autobox = type == null ? null : type.autoboxesTo(); result =
stringType = compiler.getTypeRegistry().getNativeType(JSTypeNative.STRING_TYPE); iterableType = compiler.getTypeRegistry().getGlobalType("Iterable"); iteratorIterableType = compiler.getTypeRegistry().getGlobalType("IteratorIterable"); arrayType = compiler.getTypeRegistry().getGlobalType("Array");
/** * Given a method definition node, returns the {@link ObjectType} corresponding * to the class the method is defined on, or null if it is not a prototype method. */ private ObjectType findMethodOwner(Node n) { if (n == null) { return null; } Node parent = n.getParent(); FunctionType ctor = null; if (parent.isAssign()) { Node target = parent.getFirstChild(); if (NodeUtil.isPrototypeProperty(target)) { // TODO(johnlenz): handle non-global types JSType type = registry.getGlobalType(target.getFirstFirstChild().getQualifiedName()); ctor = type != null ? ((ObjectType) type).getConstructor() : null; } } else if (parent.isClass()) { // TODO(sdh): test this case once the type checker understands ES6 classes ctor = parent.getJSType().toMaybeFunctionType(); } return ctor != null ? ctor.getInstanceType() : null; }
JSType registryType = typeRegistry.getGlobalType(symbol.getName()); if (registryType != null) { visitTypeAlias(registryType, symbol); JSType registryType = typeRegistry.getGlobalType(symbol.getName()); if (type.isUnknownType() && registryType != null && registryType.isEnumElementType()) { visitTypeValueAlias(symbol.getName(), (EnumElementType) registryType);
SingleGeneratorFunctionTranspiler(Node genFunc, int genaratorNestingLevel) { this.generatorNestingLevel = genaratorNestingLevel; this.originalGeneratorBody = genFunc.getLastChild(); ObjectType contextType = null; if (shouldAddTypes) { // Find the yield type of the generator. // e.g. given @return {!Generator<number>}, we want this.yieldType to be number. yieldType = unknownType; if (genFunc.getJSType() != null && genFunc.getJSType().isFunctionType()) { FunctionType fnType = genFunc.getJSType().toMaybeFunctionType(); this.originalGenReturnType = fnType.getReturnType(); yieldType = JsIterables.getElementType(originalGenReturnType, registry); } JSType globalContextType = registry.getGlobalType("$jscomp.generator.Context"); if (globalContextType == null) { // We don't have the es6/generator polyfill, which can happen in tests using a // NonInjectingCompiler or if someone sets --inject_libraries=false. Don't crash, just // back off on giving some type information. contextType = registry.getNativeObjectType(JSTypeNative.OBJECT_TYPE); } else { contextType = registry.createTemplatizedType(globalContextType.toMaybeObjectType(), yieldType); } } this.context = new TranspilationContext(contextType); }