public virtual string Format(TsInterface tsInterface) { using (var sbc = new StringBuilderContext(this)) { this.WriteIndent(); this.Write("interface {0}{1} {2} {{", Format(tsInterface.Name), Format(tsInterface.TypeParameters), tsInterface.BaseInterfaces.Count > 0 ? string.Format("extends {0}", string.Join(", ", tsInterface.BaseInterfaces.Select(Format))) : string.Empty); this.WriteNewline(); using (Indent()) { foreach (var property in tsInterface.Properties) this.Write(this.Format(property)); foreach (var function in tsInterface.Functions) this.Write(this.Format(function)); } this.WriteIndent(); this.Write("}"); this.WriteNewline(); this.WriteNewline(); this.WriteIndent(); this.Write("export class {0}{1} {2} {3} {{", Format(tsInterface.Name).Substring(1), Format(tsInterface.TypeParameters), string.Format("implements {0}",tsInterface.Name.Name), tsInterface.BaseInterfaces.Count > 0 ? string.Format("extends {0}", string.Join(", ", tsInterface.BaseInterfaces.Select(Format))) : string.Empty); this.WriteNewline(); using (Indent()) { foreach (var property in tsInterface.Properties) { this.Write(this.Format(property).Replace("?","")); } foreach (var function in tsInterface.Functions) { this.Write(this.Format(function)); } } this.WriteIndent(); this.Write("}"); this.WriteNewline(); this.WriteNewline(); return sbc.ToString(); } }
/// <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) { this.Write("{"); foreach (var property in tsInterface.Properties.OrderBy(x => x.Name)) { this.Write(this.Format(property)); } foreach (var property in tsInterface.IndexerProperties.OrderBy(x => x.Name)) { this.Write(this.Format(property)); } foreach (var function in tsInterface.Functions.OrderBy(x => x.Name)) { this.Write(this.Format(function)); } this.Write("}"); return(sbc.ToString()); } else { this.WriteIndent(); this.Write("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); this.WriteNewline(); using (Indent()) { foreach (var property in tsInterface.Properties.OrderBy(x => x.Name)) { this.WriteIndent(); this.Write(this.Format(property)); this.WriteNewline(); } foreach (var property in tsInterface.IndexerProperties.OrderBy(x => x.Name)) { this.WriteIndent(); this.Write(this.Format(property)); this.WriteNewline(); } foreach (var function in tsInterface.Functions.OrderBy(x => x.Name)) { this.WriteIndent(); this.Write(this.Format(function)); this.WriteNewline(); } } this.WriteIndent(); this.Write("}"); this.WriteNewline(); 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; }