コード例 #1
0
        /// <summary>
        ///     Exports entire class to specified writer
        /// </summary>
        /// <param name="declType">
        ///     Declaration type. Used in "export $gt;class&lt; ... " line. This parameter allows switch it to
        ///     "interface"
        /// </param>
        /// <param name="type">Exporting class type</param>
        /// <param name="resolver">Type resolver</param>
        /// <param name="sw">Output writer</param>
        /// <param name="swtch">Pass here type attribute inherited from IAutoexportSwitchAttribute</param>
        protected virtual void Export(string declType, Type type, TypeResolver resolver, WriterWrapper sw,
                                      IAutoexportSwitchAttribute swtch)
        {
            var name = type.GetName();

            Settings.Documentation.WriteDocumentation(type, sw);
            sw.Indent();


            sw.Write(Settings.GetDeclarationFormat(type), declType);
            sw.Write(name);

            var ifaces = type.GetInterfaces();
            var bs     = type.BaseType;
            var baseClassIsExportedAsInterface = false;

            if (bs != null && bs != typeof(object))
            {
                if (ConfigurationRepository.Instance.ForType <TsDeclarationAttributeBase>(bs) != null)
                {
                    if (bs.IsExportingAsInterface())
                    {
                        baseClassIsExportedAsInterface = true;
                    }
                    else
                    {
                        sw.Write(" extends {0} ", resolver.ResolveTypeName(bs));
                    }
                }
            }
            var ifacesStrings =
                ifaces.Where(c => ConfigurationRepository.Instance.ForType <TsInterfaceAttribute>(c) != null)
                .Select(resolver.ResolveTypeName).ToList();

            if (baseClassIsExportedAsInterface)
            {
                ifacesStrings.Add(resolver.ResolveTypeName(bs));
            }
            if (ifacesStrings.Any())
            {
                var implemets = string.Join(", ", ifacesStrings);
                if (type.IsExportingAsInterface())
                {
                    sw.Write(" extends {0}", implemets);
                }
                else
                {
                    sw.Write(" implements {0}", implemets);
                }
            }

            sw.Write(" {{");
            sw.WriteLine();
            ExportMembers(type, resolver, sw, swtch);
            sw.WriteLine("}");
        }
コード例 #2
0
 private void Line(WriterWrapper sw, string line = null)
 {
     if (string.IsNullOrEmpty(line))
     {
         sw.WriteLine("*");
     }
     else
     {
         sw.WriteLine("* {0}", line);
     }
 }
コード例 #3
0
 /// <summary>
 ///     Writes to output file opening namespace declaration
 /// </summary>
 /// <param name="namespaceName">Namespace name</param>
 /// <param name="sw">Output writer</param>
 public virtual void WriteNamespaceBegin(string namespaceName, WriterWrapper sw)
 {
     if (string.IsNullOrEmpty(namespaceName))
     {
         return;
     }
     if (Settings.ExportPureTypings)
     {
         sw.WriteLine("declare module {0} {{", namespaceName);
     }
     else
     {
         sw.WriteLine("module {0} {{", namespaceName);
     }
     sw.Tab();
 }
コード例 #4
0
 /// <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();
 }
コード例 #5
0
        /// <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();
            }
        }
コード例 #6
0
        /// <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("}");
        }
コード例 #7
0
        /// <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();
                }
            }
        }
コード例 #8
0
 private void End(WriterWrapper sw)
 {
     sw.WriteLine("*/");
 }
コード例 #9
0
 private void Begin(WriterWrapper sw)
 {
     sw.WriteLine("/**");
 }
コード例 #10
0
 private void End(WriterWrapper sw)
 {
     sw.WriteLine("*/");
 }
コード例 #11
0
 /// <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();
     }
 }
コード例 #12
0
 private void Line(WriterWrapper sw, string line = null)
 {
     if (string.IsNullOrEmpty(line)) sw.WriteLine("*");
     else sw.WriteLine("* {0}", line);
 }
コード例 #13
0
 private void Begin(WriterWrapper sw)
 {
     sw.WriteLine("/**");
 }