/// <summary> /// Main code generator method. This method should write corresponding TypeScript code for element (1st argument) to /// WriterWrapper (3rd argument) using TypeResolver if necessary /// </summary> /// <param name="element">Element code to be generated to output</param> /// <param name="resolver">Type resolver</param> /// <param name="sw">Output writer</param> public virtual void Generate(MemberInfo element, TypeResolver resolver, WriterWrapper sw) { if (element.IsIgnored()) { return; } var t = GetType(element); string typeName = null; var propName = element.Name; var tp = ConfigurationRepository.Instance.ForMember <TsPropertyAttribute>(element); if (tp != null) { if (tp.StrongType != null) { typeName = resolver.ResolveTypeName(tp.StrongType); } else if (!string.IsNullOrEmpty(tp.Type)) { typeName = tp.Type; } if (!string.IsNullOrEmpty(tp.Name)) { propName = tp.Name; } if (tp.ForceNullable && element.DeclaringType.IsExportingAsInterface() && !Settings.SpecialCase) { propName = propName + "?"; } } if (string.IsNullOrEmpty(typeName)) { typeName = resolver.ResolveTypeName(t); } if (!propName.EndsWith("?") && t.IsNullable() && element.DeclaringType.IsExportingAsInterface() && !Settings.SpecialCase) { propName = propName + "?"; } if (element is PropertyInfo) { propName = Settings.ConditionallyConvertPropertyNameToCamelCase(propName); } propName = element.CamelCaseFromAttribute(propName); sw.Tab(); Settings.Documentation.WriteDocumentation(element, sw); sw.Indent(); if (!element.DeclaringType.IsExportingAsInterface() || Settings.SpecialCase) { var modifier = Settings.SpecialCase ? AccessModifier.Public : element.GetModifier(); sw.Write("{0} ", modifier.ToModifierText()); } sw.Write("{0}: {1};", propName, typeName); sw.Br(); sw.UnTab(); }
/// <summary> /// Writes to ouput file namespace closing /// </summary> /// <param name="namespaceName">Namespace name</param> /// <param name="sw">Output writer</param> public virtual void WriteNamespaceEnd(string namespaceName, WriterWrapper sw) { if (string.IsNullOrEmpty(namespaceName)) { return; } sw.UnTab(); sw.Indent(); sw.WriteLine("}"); sw.Br(); }
/// <summary> /// Writes output comment with automatic multiline division /// </summary> /// <param name="sw">Output writer</param> /// <param name="comment">Comment (multiline allowed)</param> public void WriteComment(WriterWrapper sw, string comment) { sw.Br(); sw.Indent(); var lines = comment.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (lines.Length == 1) { sw.WriteLine("// {0} ", lines[0]); } else { Begin(sw); foreach (var line in lines) { Line(sw, line); } End(sw); sw.Br(); } }
/// <summary> /// Main code generator method. This method should write corresponding TypeScript code for element (1st argument) to /// WriterWrapper (3rd argument) using TypeResolver if necessary /// </summary> /// <param name="element">Element code to be generated to output</param> /// <param name="resolver">Type resolver</param> /// <param name="sw">Output writer</param> public virtual void Generate(Type element, TypeResolver resolver, WriterWrapper sw) { var values = Enum.GetValues(element); var name = element.GetName(); var fmt = Settings.GetDeclarationFormat(element); var fields = element.GetFields().Where(c => !c.IsSpecialName).ToDictionary(c => c.Name, c => c); Settings.Documentation.WriteDocumentation(element, sw); sw.Indent(); sw.Write(string.Format(fmt, "enum {0} {{ "), name); sw.Br(); sw.Tab(); for (var index = 0; index < values.Length; index++) { var v = values.GetValue(index); var n = Enum.GetName(element, v); if (fields.ContainsKey(n)) { var fieldItself = fields[n]; Settings.Documentation.WriteDocumentation(fieldItself, sw); var attr = ConfigurationRepository.Instance.ForEnumValue(fieldItself); if (attr != null) { n = attr.Name; } sw.Indent(); sw.Write("{0} = {1}", n, Convert.ToInt64(v)); if (index < values.Length - 1) { sw.Write(","); } sw.Br(); } } sw.UnTab(); sw.WriteLine("}"); }
/// <summary> /// Writes method body to output writer /// </summary> /// <param name="returnType">Method return type</param> /// <param name="resolver">Type resolver</param> /// <param name="sw">Output writer</param> /// <param name="content">Content for non-void body</param> protected virtual void GenerateBody(string returnType, TypeResolver resolver, WriterWrapper sw, string content = "return null;") { if (Settings.ExportPureTypings) //Ambient class declarations cannot have a body { sw.Write(";"); sw.Br(); } else { if (returnType != "void") { sw.WriteLine(); sw.WriteIndented(@"{{ {0} }}", content); } else { sw.Write(" {{ }}"); sw.Br(); } } }
/// <summary> /// Main code generator method. This method should write corresponding TypeScript code for element (1st argument) to /// WriterWrapper (3rd argument) using TypeResolver if necessary /// </summary> /// <param name="element">Element code to be generated to output</param> /// <param name="resolver">Type resolver</param> /// <param name="sw">Output writer</param> public virtual void Generate(MethodInfo element, TypeResolver resolver, WriterWrapper sw) { if (element.IsIgnored()) { return; } var isInterfaceMethod = element.DeclaringType.IsExportingAsInterface() && !Settings.SpecialCase; string name, type; GetFunctionNameAndReturnType(element, resolver, out name, out type); sw.Tab(); Settings.Documentation.WriteDocumentation(element, sw); sw.Indent(); var modifier = element.GetModifier(); if (Settings.SpecialCase) { modifier = AccessModifier.Public; } WriteFunctionName(element.IsStatic, modifier, name, sw, isInterfaceMethod); WriteMethodParameters(element, resolver, sw); WriteRestOfDeclaration(type, sw); if (isInterfaceMethod) { sw.Write(";"); sw.Br(); } else { GenerateBody(type, resolver, sw); } sw.UnTab(); }
/// <summary> /// Writes output comment with automatic multiline division /// </summary> /// <param name="sw">Output writer</param> /// <param name="comment">Comment (multiline allowed)</param> public void WriteComment(WriterWrapper sw, string comment) { sw.Br(); sw.Indent(); var lines = comment.Split(new[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries); if (lines.Length == 1) { sw.WriteLine("// {0} ", lines[0]); } else { Begin(sw); foreach (var line in lines) { Line(sw, line); } End(sw); sw.Br(); } }