예제 #1
0
 /// <summary>
 /// Formats an enumaration as integers
 /// </summary>
 /// <param name="tsEnum">The enumeration</param>
 /// <returns>The string representation of the enumeration</returns>
 protected string FormatEnumAsIntegers(TsEnum tsEnum)
 {
     using (var sbc = new StringBuilderContext(this))
     {
         this.WriteIndent();
         this.Write("const enum {0} {{", Format(tsEnum.Name));
         this.WriteNewline();
         using (Indent())
         {
             var values = tsEnum.Values.OrderBy(x => x.Key).ToArray();
             for (int i = 0; i < values.Length; i++)
             {
                 var postFix = i < values.Length - 1 ? "," : string.Empty;
                 var entry   = values[i];
                 this.WriteIndent();
                 if (entry.Value.HasValue)
                 {
                     this.Write("{0} = {1}{2}", entry.Key, entry.Value, postFix);
                 }
                 else
                 {
                     this.Write("{0}{1}", entry.Key, postFix);
                 }
                 this.WriteNewline();
             }
         }
         this.WriteIndent();
         this.Write("}");
         this.WriteNewline();
         return(sbc.ToString());
     }
 }
예제 #2
0
 /// <summary>
 /// Formats a parameter
 /// </summary>
 /// <param name="parameter">The parameter</param>
 /// <returns>The string representation of a parameter</returns>
 public virtual string Format(TsParameter parameter)
 {
     using (var sbc = new StringBuilderContext(this))
     {
         this.Write("{0}{1}: {2}", Format(parameter.Name), parameter.Optional ? "?" : string.Empty, Format(parameter.Type));
         return(sbc.ToString());
     }
 }
예제 #3
0
 /// <summary>
 /// Formats an indexer property
 /// </summary>
 /// <param name="property">The property</param>
 /// <returns></returns>
 public virtual string Format(TsIndexerProperty property)
 {
     using (var sbc = new StringBuilderContext(this))
     {
         this.Write("[{0}: {1}]: {2};", Format(property.Name), Format(property.IndexerType), Format(property.ReturnType));
         return(sbc.ToString());
     }
 }
예제 #4
0
 /// <summary>
 /// Formats a property
 /// </summary>
 /// <param name="property">The property</param>
 /// <returns>The string representation of the property</returns>
 public virtual string Format(TsProperty property)
 {
     using (var sbc = new StringBuilderContext(this))
     {
         this.Write("{0}{1}: {2};", Format(property.Name), property.Optional?"?":"", Format(property.Type));
         return(sbc.ToString());
     }
 }
 /// <summary>
 /// Constructor
 /// </summary>
 public TsFormatter(bool generateTypeWithNamespace)
 {
     _generateTypeWithNamespace = generateTypeWithNamespace;
     Context = new StringBuilderContext(this);
     ReservedWordsMapping = new Dictionary <string, string>()
     {
         { "function", "_function" }
     };
 }
예제 #6
0
 /// <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());
     }
 }
예제 #7
0
        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();
            }
        }
예제 #8
0
 /// <summary>
 /// Formats an enumeration as string
 /// </summary>
 /// <param name="tsEnum">The enumeration</param>
 /// <returns>The string representation of the enumeration</returns>
 protected string FormatEnumAsStrings(TsEnum tsEnum)
 {
     using (var sbc = new StringBuilderContext(this))
     {
         this.WriteIndent();
         this.Write("type {0} = ", Format(tsEnum.Name));
         var values = tsEnum.Values.OrderBy(x => x.Key).ToArray();
         for (int i = 0; i < values.Length; i++)
         {
             var postFix = i < values.Length - 1 ? " | " : string.Empty;
             var entry   = values[i];
             this.Write("\'{0}\'{1}", entry.Key, postFix);
         }
         this.Write(";");
         this.WriteNewline();
         return(sbc.ToString());
     }
 }
예제 #9
0
 public virtual string Format(TsModule module)
 {
     using (var sbc = new StringBuilderContext(this))
     {
         this.WriteIndent();
         this.Write("module {0} {{", Format(module.Name));
         this.WriteNewline();
         using (Indent())
         {
             foreach (var type in module.Types.OfType<TsEnum>())
                 this.Write(this.Format(type));
             foreach (var type in module.Types.OfType<TsInterface>())
                 this.Write(this.Format(type));
         }
         this.WriteIndent();
         this.Write("}");
         this.WriteNewline();
         return sbc.ToString();
     }
 }
예제 #10
0
 /// <summary>
 /// Formats a module
 /// </summary>
 /// <param name="module">The module</param>
 /// <returns>The string representation of the module</returns>
 public virtual string Format(TsModule module, Compilation compilation)
 {
     using (var sbc = new StringBuilderContext(this))
     {
         WriteNewline();
         //using (Indent())
         {
             foreach (var type in module.Types.OfType <TsEnum>().OrderBy(x => x.Name))
             {
                 Write(Format(type));
             }
             foreach (var type in module.Types.OfType <TsInterface>().OrderBy(x => x.Name))
             {
                 Write(Format(type));
                 WriteNewline();
                 Write(Format(type.GetMetaDataInfo(compilation)));
                 WriteNewline();
             }
         }
         WriteNewline();
         return(sbc.ToString());
     }
 }
예제 #11
0
 /// <summary>
 /// Formats a module
 /// </summary>
 /// <param name="module">The module</param>
 /// <returns>The string representation of the module</returns>
 public virtual string Format(TsModule module)
 {
     using (var sbc = new StringBuilderContext(this))
     {
         this.WriteIndent();
         this.Write("declare module {0} {{", Format(module.Name));
         this.WriteNewline();
         using (Indent())
         {
             foreach (var type in module.Types.OfType <TsEnum>().OrderBy(x => x.Name))
             {
                 this.Write(this.Format(type));
             }
             foreach (var type in module.Types.OfType <TsInterface>().OrderBy(x => x.Name))
             {
                 this.Write(this.Format(type));
             }
         }
         this.WriteIndent();
         this.Write("}");
         this.WriteNewline();
         return(sbc.ToString());
     }
 }
예제 #12
0
 public virtual string Format(TsParameter parameter)
 {
     using (var sbc = new StringBuilderContext(this))
     {
         this.Write("{0}{1}: {2}", Format(parameter.Name), parameter.Optional ? "?" : string.Empty, Format(parameter.Type));
         return sbc.ToString();
     }
 }
예제 #13
0
 public virtual string Format(TsEnum tsEnum)
 {
     using (var sbc = new StringBuilderContext(this))
     {
         this.WriteIndent();
         this.Write("enum {0} {{", Format(tsEnum.Name));
         this.WriteNewline();
         using (Indent())
         {
             var values = tsEnum.Values.ToArray();
             for (int i = 0; i < values.Length; i++)
             {
                 var postFix = i < values.Length - 1 ? "," : string.Empty;
                 var entry = values[i];
                 this.WriteIndent();
                 if (entry.Value.HasValue)
                     this.Write("{0} = {1}{2}", entry.Key, entry.Value, postFix);
                 else
                     this.Write("{0}{1}", entry.Key, postFix);
                 this.WriteNewline();
             }
         }
         this.WriteIndent();
         this.Write("}");
         this.WriteNewline();
         return sbc.ToString();
     }
 }
예제 #14
0
 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();
     }
 }
예제 #15
0
        public virtual string Format(TsProperty property)
        {
            using (var sbc = new StringBuilderContext(this))
            {
                this.WriteIndent();

                this.Write("{0}{2}: {1};", Format(property.Name), Format(property.Type),property.Optional ? "?" : "");
                this.WriteNewline();
                return sbc.ToString();
            }
        }
예제 #16
0
        /// <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());
                }
            }
        }