/// <summary> /// Resolves a property /// </summary> /// <param name="property">The property to resolve</param> /// <returns></returns> protected virtual TsProperty Resolve(PropertyInfo property) { TsType propertyType; bool optional = false; var propertyTypeInfo = property.PropertyType.GetTypeInfo(); if (propertyTypeInfo.IsGenericType && propertyTypeInfo.GetGenericTypeDefinition() == typeof(Nullable <>)) { var genericArguments = propertyTypeInfo.GetGenericArguments(); propertyType = this.Resolve(genericArguments[0]); optional = true; } else if (propertyTypeInfo.IsGenericType && !propertyTypeInfo.IsGenericTypeDefinition && propertyTypeInfo.GetGenericTypeDefinition() == typeof(Dictionary <,>)) { // find type used for dictionary values var genericArguments = propertyTypeInfo.GetGenericArguments(); var keyType = genericArguments[0]; var valueType = genericArguments[1]; var tsKeyType = this.Resolve(keyType); var tsArgType = this.Resolve(valueType); var inlineInterfaceType = new TsInterface(); inlineInterfaceType.IndexerProperties.Add(new TsIndexerProperty(new TsName("key"), tsKeyType, tsArgType)); propertyType = inlineInterfaceType; } else { propertyType = Resolve(property.PropertyType); } return(new TsProperty(GetName(property), propertyType, optional)); }
private TsType CreateGenericDictionaryType() { var tsInterface = new TsInterface(new TsName("Dictionary", "System.Collections.Generic")); var tsKeyType = new TsInterface(new TsName("TKey")); var tsValueType = new TsInterface(new TsName("TValue")); tsInterface.TypeParameters.Add(new TsTypeParameter(new TsName("Tkey"))); tsInterface.TypeParameters.Add(new TsTypeParameter(new TsName("TValue"))); tsInterface.IndexerProperties.Add(new TsIndexerProperty(new TsName("key"), TsPrimitive.String, tsValueType)); return(tsInterface); }
/// <summary> /// Formats an interface /// </summary> /// <param name="tsInterface">The interface</param> /// <returns>The string representation of the interface</returns> public virtual string Format(TsInterface tsInterface) { using (var sbc = new StringBuilderContext(this)) { if (tsInterface.IsLiteral) { Write("{"); foreach (var property in tsInterface.Properties.OrderBy(x => x.Name)) { Write(Format(property)); } foreach (var property in tsInterface.IndexerProperties.OrderBy(x => x.Name)) { Write(Format(property)); } foreach (var function in tsInterface.Functions.OrderBy(x => x.Name)) { Write(Format(function)); } Write("}"); return(sbc.ToString()); } else { WriteIndent(); Write("export interface {0}{1} {2} {{", Format(tsInterface.Name), Format(tsInterface.TypeParameters), tsInterface.BaseInterfaces.Count > 0 ? string.Format("extends {0}", string.Join(", ", tsInterface.BaseInterfaces.OrderBy(x => x.Name).Select(Format))) : string.Empty); WriteNewline(); using (Indent()) { foreach (var property in tsInterface.Properties.OrderBy(x => x.Name)) { WriteIndent(); Write(Format(property)); WriteNewline(); } foreach (var property in tsInterface.IndexerProperties.OrderBy(x => x.Name)) { WriteIndent(); Write(Format(property)); WriteNewline(); } foreach (var function in tsInterface.Functions.OrderBy(x => x.Name)) { WriteIndent(); Write(Format(function)); WriteNewline(); } } WriteIndent(); Write("}"); WriteNewline(); WriteNewline(); return(sbc.ToString()); } } }
/// <summary> /// Generates a TypeScript interface for a particular CLR type /// </summary> /// <param name="type">The type</param> /// <returns>The resulting TypeScript interface</returns> private TsInterface GenerateInterface(Type type) { var typeInfo = type.GetTypeInfo(); var tsInterface = new TsInterface(GetName(type)); this.AddType(tsInterface, type); // resolve non-inherited interfaces implemented by the type var interfaces = typeInfo.BaseType == null?typeInfo.GetInterfaces() : typeInfo.GetInterfaces().Except(typeInfo.BaseType.GetTypeInfo().GetInterfaces()); foreach (var interfaceType in interfaces) { this.AddType(interfaceType); } if (typeInfo.IsGenericType) { if (typeInfo.IsGenericTypeDefinition) { foreach (var genericArgument in typeInfo.GetGenericArguments()) { var tsTypeParameter = new TsTypeParameter(new TsName(genericArgument.Name)); var genericArgumentType = genericArgument.GetTypeInfo().GetGenericParameterConstraints().FirstOrDefault(); if (genericArgumentType != null) { var tsTypeParameterType = Resolve(genericArgumentType); tsTypeParameter.Extends = tsTypeParameterType.Name; } tsInterface.TypeParameters.Add(tsTypeParameter); } } else { var genericType = type.GetGenericTypeDefinition(); var tsGenericType = this.Resolve(genericType); } } // resolve the base class if present if (typeInfo.BaseType != null) { var baseType = this.Resolve(typeInfo.BaseType); if (baseType != null && baseType != TsPrimitive.Any) { tsInterface.BaseInterfaces.Add(baseType); } } // process fields foreach (var field in this.Reader.GetFields(typeInfo)) { var tsProperty = this.Resolve(field); if (tsProperty != null) { tsInterface.Properties.Add(tsProperty); } } // process properties foreach (var property in this.Reader.GetProperties(typeInfo)) { var tsProperty = this.Resolve(property); if (tsProperty != null) { tsInterface.Properties.Add(tsProperty); } } // process methods foreach (var method in this.Reader.GetMethods(typeInfo)) { var tsFunction = this.Resolve(method); if (tsFunction != null) { tsInterface.Functions.Add(tsFunction); } } return(tsInterface); }
/// <summary> /// Generates a TypeScript interface for a particular CLR type /// </summary> /// <param name="type">The type</param> /// <returns>The resulting TypeScript interface</returns> private TsInterface GenerateInterface(ITypeSymbol type) { if (type.DeclaringSyntaxReferences.Length == 0) { //We do not generate interface from other assemblies. return(null); } var tsInterface = new TsInterface(GetName(type)); AddType(tsInterface, type); // resolve non-inherited interfaces implemented by the type foreach (var interfaceType in type.AllInterfaces) { AddType(interfaceType); } if (type is INamedTypeSymbol symbol && symbol.IsGenericType) { //Skipping generic types for now return(null); } // resolve the base class if present if (type.BaseType != null && type.BaseType.SpecialType != SpecialType.System_Object) { var baseType = Resolve(type.BaseType); if (baseType != null && baseType != TsPrimitive.Any) { tsInterface.BaseInterfaces.Add(baseType); } } // process properties foreach (var property in type.GetMembers().OfType <IPropertySymbol>()) { var tsProperty = Resolve(property); if (tsProperty != null) { tsInterface.Properties.Add(tsProperty); } } // process fields foreach (var field in type.GetMembers().OfType <IFieldSymbol>()) { var tsProperty = Resolve(field); if (tsProperty != null) { tsInterface.Properties.Add(tsProperty); } } // process methods foreach (var method in type.GetMembers().OfType <IMethodSymbol>()) { var tsFunction = Resolve(method); if (tsFunction != null) { tsInterface.Functions.Add(tsFunction); } } return(tsInterface); }