/// <summary> /// Formats a function /// </summary> /// <param name="function">The function</param> /// <returns>The string representation of the function</returns> public virtual string Format(TsFunction function) { using (var sbc = new StringBuilderContext(this)) { this.Write("{0}{1}({2}){3};", Format(function.Name), Format(function.TypeParameters), Format(function.Parameters), function.ReturnType == TsPrimitive.Any ? string.Empty : string.Format(": {0}", FormatReturnType(function.ReturnType)) ); return(sbc.ToString()); } }
public virtual string Format(TsFunction function) { using (var sbc = new StringBuilderContext(this)) { this.WriteIndent(); this.Write("{0}{1}({2}){3};", Format(function.Name), Format(function.TypeParameters), Format(function.Parameters), function.ReturnType == TsPrimitive.Any ? string.Empty : string.Format(": {0}", FormatReturnType(function.ReturnType)) ); this.WriteNewline(); return sbc.ToString(); } }
private TsInterface GenerateInterface(Type type) { var tsInterface = new TsInterface(GetName(type)); if (!this.TypeLookup.ContainsKey(type)) this.TypeLookup.Add(type, tsInterface); // resolve interfaces implemented by the type foreach (var interfaceType in type.GetInterfaces()) this.AddType(interfaceType); if (type.IsGenericType) { if (type.IsGenericTypeDefinition) { foreach (var genericArgument in type.GetGenericArguments()) { var tsTypeParameter = new TsTypeParameter(new TsName(genericArgument.Name)); var genericArgumentType = genericArgument.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 (type.BaseType != null) { var baseType = this.Resolve(type.BaseType); if (baseType != null && baseType != TsPrimitive.Any) tsInterface.BaseInterfaces.Add(baseType); } // process properties foreach (var property in this.Reader.GetProperties(type)) { tsInterface.Properties.Add(new TsProperty(GetName(property), Resolve(property.PropertyType))); } if (!IgnoreFunctions) { // process methods foreach (var method in this.Reader.GetMethods(type)) { 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), IsOptionalParameter(x)))) { tsFunction.Parameters.Add(param); } tsInterface.Functions.Add(tsFunction); } } return tsInterface; }