/// <summary> /// Resolves a method /// </summary> /// <param name="method">The method to resolve</param> /// <returns>The TypeScript function definition</returns> protected virtual TsFunction Resolve(MethodInfo method) { var returnType = Resolve(method.ReturnType); var parameters = this.Reader.GetParameters(method); var tsFunction = new TsFunction(GetName(method)); tsFunction.ReturnType = returnType; if (method.IsGenericMethod) { foreach (var genericArgument in method.GetGenericArguments()) { var tsTypeParameter = new TsTypeParameter(new TsName(genericArgument.Name)); tsFunction.TypeParameters.Add(tsTypeParameter); } } foreach (var param in parameters.Select(x => new TsParameter(GetName(x), Resolve(x.ParameterType)))) { tsFunction.Parameters.Add(param); } return(tsFunction); }
/// <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> /// Formats a type parameter /// </summary> /// <param name="typeParameter">The type parameter</param> /// <returns>The string representation of the type parameter</returns> public virtual string Format(TsTypeParameter typeParameter) { return($"{ResolveTypeName(typeParameter.Name, typeParameter.IsExternallyDefined)}{(typeParameter.Extends == null ? string.Empty : $" extends {ResolveTypeName(typeParameter.Extends, typeParameter.IsExternallyDefined)}")}"); }