示例#1
0
 /// <summary>
 ///     Exports all type members sequentially
 /// </summary>
 /// <param name="element">Type itself</param>
 /// <param name="resolver">Type resolver</param>
 /// <param name="typeMember">Placeholder for members</param>
 /// <param name="swtch">Pass here type attribute inherited from IAutoexportSwitchAttribute</param>
 protected virtual void ExportMembers(Type element, TypeResolver resolver, ITypeMember typeMember,
                                      IAutoexportSwitchAttribute swtch)
 {
     ExportConstructors(typeMember, element, resolver, swtch);
     ExportFields(typeMember, element, resolver, swtch);
     ExportProperties(typeMember, element, resolver, swtch);
     ExportMethods(typeMember, element, resolver, swtch);
     HandleBaseClassExportingAsInterface(typeMember, element, resolver, swtch);
 }
示例#2
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("}");
        }
示例#3
0
 /// <summary>
 ///     Exports type constructors
 /// </summary>
 /// <param name="element">Type itself</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 ExportConstructors(Type element, TypeResolver resolver, WriterWrapper sw,
                                           IAutoexportSwitchAttribute swtch)
 {
     if (swtch.AutoExportConstructors)
     {
         if (!element.IsExportingAsInterface()) // constructors are not allowed on interfaces
         {
             var constructors =
                 element.GetConstructors(TypeExtensions.MembersFlags)
                 .Where(c => TypeExtensions.TypeScriptMemberSearchPredicate(c));
             GenerateMembers(element, resolver, sw, constructors);
         }
     }
 }
示例#4
0
        /// <summary>
        ///     Here you can customize what to export when base class is class but exporting as interface
        /// </summary>
        /// <param name="element">Type itself</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 HandleBaseClassExportingAsInterface(Type element, TypeResolver resolver, WriterWrapper sw,
                                                                   IAutoexportSwitchAttribute swtch)
        {
            if (element.BaseType != null)
            {
                if (
                    element.BaseType.IsExportingAsInterface() && !element.IsExportingAsInterface())
                {
                    // well.. bad but often case.
                    // Here we should export members also for base class
                    // we do not export methods - just properties and fields
                    // but still. It is better thatn nothing

                    Settings.Documentation.WriteComment(sw,
                                                        string.Format("Automatically implemented from {0}", resolver.ResolveTypeName(element.BaseType)));
                    var basExSwtch = ConfigurationRepository.Instance.ForType <TsInterfaceAttribute>(element.BaseType);
                    Settings.SpecialCase = true;
                    ExportFields(element.BaseType, resolver, sw, basExSwtch);
                    ExportMethods(element.BaseType, resolver, sw, basExSwtch);
                    Settings.SpecialCase = false;
                }
            }
        }
示例#5
0
        /// <summary>
        ///     Exports type constructors
        /// </summary>
        /// <param name="typeMember">Output writer</param>
        /// <param name="element">Type itself</param>
        /// <param name="resolver">Type resolver</param>
        /// <param name="swtch">Pass here type attribute inherited from IAutoexportSwitchAttribute</param>
        protected virtual void ExportConstructors(ITypeMember typeMember, Type element, TypeResolver resolver, IAutoexportSwitchAttribute swtch)
        {
            var bp = Context.Project.Blueprint(element);

            if (swtch.AutoExportConstructors)
            {
                if (!bp.IsExportingAsInterface()) // constructors are not allowed on interfaces
                {
                    var constructors =
                        element._GetConstructors(TypeExtensions.MembersFlags)
                        .Where(c => (c.GetCustomAttribute <CompilerGeneratedAttribute>() == null) && !bp.IsIgnored(c));
                    if (!constructors.Any())
                    {
                        constructors =
                            element._GetConstructors(TypeExtensions.MembersFlags)
                            .Where(c => !bp.IsIgnored(c));
                    }
                    GenerateMembers(element, resolver, typeMember, constructors.Take(1));
                }
            }
        }
示例#6
0
 /// <summary>
 ///     Exports type methods
 /// </summary>
 /// <param name="typeMember">Output writer</param>
 /// <param name="element">Type itself</param>
 /// <param name="resolver">Type resolver</param>
 /// <param name="swtch">Pass here type attribute inherited from IAutoexportSwitchAttribute</param>
 protected virtual void ExportMethods(ITypeMember typeMember, Type element, TypeResolver resolver, IAutoexportSwitchAttribute swtch)
 {
     GenerateMembers(element, resolver, typeMember, Context.Project.Blueprint(element).GetExportedMethods());
 }
示例#7
0
        /// <summary>
        ///     Exports entire class to specified writer
        /// </summary>
        /// <param name="result">Exporting result</param>
        /// <param name="type">Exporting class type</param>
        /// <param name="resolver">Type resolver</param>
        /// <param name="swtch">Pass here type attribute inherited from IAutoexportSwitchAttribute</param>
        protected virtual void Export(ITypeMember result, Type type, TypeResolver resolver, IAutoexportSwitchAttribute swtch)
        {
            var bp = Context.Project.Blueprint(type);

            result.Name  = bp.GetName();
            result.Order = bp.GetOrder();

            var doc = Context.Documentation.GetDocumentationMember(type);

            if (doc != null)
            {
                RtJsdocNode docNode = new RtJsdocNode();
                if (doc.HasSummary())
                {
                    docNode.Description = doc.Summary.Text;
                }
                result.Documentation = docNode;
            }

            var materializedGenericParameters = type._GetGenericArguments()
                                                .Where(c => c.GetCustomAttribute <TsGenericAttribute>() != null)
                                                .ToDictionary(c => c.Name, resolver.ResolveTypeName);

            if (materializedGenericParameters.Count == 0)
            {
                materializedGenericParameters = null;
            }

            if (!bp.IsFlatten())
            {
                var bs = type._BaseType();
                var baseClassIsExportedAsInterface = false;
                if (bs != null && bs != typeof(object))
                {
                    bool       baseAsInterface  = false;
                    RtTypeName inferredBaseType = null;
                    if (bs._IsGenericType())
                    {
                        var genericBase   = bs.GetGenericTypeDefinition();
                        var genericBaseBp = Context.Project.Blueprint(genericBase);
                        if (genericBaseBp.TypeAttribute != null || genericBaseBp.ThirdParty != null)
                        {
                            inferredBaseType = resolver.ResolveTypeName(bs,
                                                                        MergeMaterializedGenerics(bs, resolver, materializedGenericParameters));
                            baseAsInterface = Context.Project.Blueprint(genericBase).IsExportingAsInterface();
                        }
                    }
                    if (inferredBaseType == null || !bs._IsGenericType())
                    {
                        var bsBp = Context.Project.Blueprint(bs);
                        if (bsBp.TypeAttribute != null || bsBp.ThirdParty != null)
                        {
                            baseAsInterface  = Context.Project.Blueprint(bs).IsExportingAsInterface();
                            inferredBaseType = resolver.ResolveTypeName(bs,
                                                                        MergeMaterializedGenerics(bs, resolver, materializedGenericParameters));
                        }
                    }

                    if (inferredBaseType != null)
                    {
                        if (baseAsInterface)
                        {
                            baseClassIsExportedAsInterface = true;
                        }
                        else
                        {
                            ((RtClass)result).Extendee = inferredBaseType;
                        }
                    }
                }
                var implementees = ExtractImplementees(type, resolver, materializedGenericParameters).ToList();

                if (baseClassIsExportedAsInterface)
                {
                    implementees.Add(resolver.ResolveTypeName(bs, materializedGenericParameters));
                }
                result.Implementees.AddRange(implementees.OfType <RtSimpleTypeName>());
            }

            ExportMembers(type, resolver, result, swtch);
        }
示例#8
0
        /// <summary>
        ///     Here you can customize what to export when base class is class but exporting as interface
        /// </summary>
        /// <param name="sw">Output writer</param>
        /// <param name="element">Type itself</param>
        /// <param name="resolver">Type resolver</param>
        /// <param name="swtch">Pass here type attribute inherited from IAutoexportSwitchAttribute</param>
        protected virtual void HandleBaseClassExportingAsInterface(ITypeMember sw, Type element, TypeResolver resolver, IAutoexportSwitchAttribute swtch)
        {
            if (element._BaseType() != null)
            {
                var baseBp = Context.Project.Blueprint(element._BaseType());
                var bp     = Context.Project.Blueprint(element);
                if (baseBp.IsExportingAsInterface() && !bp.IsExportingAsInterface())
                {
                    // well.. bad but often case.
                    // Here we should export members also for base class
                    // we do not export methods - just properties and fields
                    // but still. It is better thatn nothing

                    if (sw.Documentation == null)
                    {
                        sw.Documentation = new RtJsdocNode();
                    }
                    sw.Documentation.TagToDescription.Add(new Tuple <DocTag, string>(DocTag.Todo,
                                                                                     string.Format("Automatically implemented from {0}", resolver.ResolveTypeName(element._BaseType()))));

                    var baseBlueprint = Context.Project.Blueprint(element._BaseType());
                    var basExSwtch    = baseBlueprint.Attr <TsInterfaceAttribute>();
                    Context.SpecialCase = true;
                    ExportFields(sw, element._BaseType(), resolver, basExSwtch);
                    ExportProperties(sw, element._BaseType(), resolver, basExSwtch);
                    ExportMethods(sw, element._BaseType(), resolver, basExSwtch);
                    Context.SpecialCase = false;
                    Context.Warnings.Add(ErrorMessages.RTW0005_BaseClassExportingAsInterface.Warn(element._BaseType().FullName, element.FullName));
                }
            }
        }
示例#9
0
        /// <summary>
        ///     Exports entire class to specified writer
        /// </summary>
        /// <param name="result">Exporting result</param>
        /// <param name="type">Exporting class type</param>
        /// <param name="resolver">Type resolver</param>
        /// <param name="swtch">Pass here type attribute inherited from IAutoexportSwitchAttribute</param>
        protected virtual void Export(ITypeMember result, Type type, TypeResolver resolver, IAutoexportSwitchAttribute swtch)
        {
            Context.Location.SetCurrentType(type);
            result.Name  = type.GetName();
            result.Order = type.GetOrder();

            var doc = Context.Documentation.GetDocumentationMember(type);

            if (doc != null)
            {
                RtJsdocNode docNode = new RtJsdocNode();
                if (doc.HasSummary())
                {
                    docNode.Description = doc.Summary.Text;
                }
                result.Documentation = docNode;
            }

            var materializedGenericParameters = type.GetGenericArguments()
                                                .Where(c => c.GetCustomAttribute <TsGenericAttribute>() != null)
                                                .ToDictionary(c => c.Name, resolver.ResolveTypeName);

            if (materializedGenericParameters.Count == 0)
            {
                materializedGenericParameters = null;
            }

            var bs = type.BaseType;
            var baseClassIsExportedAsInterface = false;

            if (bs != null && bs != typeof(object))
            {
                TsDeclarationAttributeBase attr = null;
                bool baseAsInterface            = false;
                if (bs.IsGenericType)
                {
                    var genericBase = bs.GetGenericTypeDefinition();
                    attr            = ConfigurationRepository.Instance.ForType <TsDeclarationAttributeBase>(genericBase);
                    baseAsInterface = genericBase.IsExportingAsInterface();
                }
                else
                {
                    attr            = ConfigurationRepository.Instance.ForType <TsDeclarationAttributeBase>(bs);
                    baseAsInterface = bs.IsExportingAsInterface();
                }

                if (attr != null)
                {
                    if (baseAsInterface)
                    {
                        baseClassIsExportedAsInterface = true;
                    }
                    else
                    {
                        ((RtClass)result).Extendee = resolver.ResolveTypeName(bs, MergeMaterializedGenerics(bs, resolver, materializedGenericParameters));
                    }
                }
            }
            var implementees = ExtractImplementees(type, resolver, materializedGenericParameters).ToList();

            if (baseClassIsExportedAsInterface)
            {
                implementees.Add(resolver.ResolveTypeName(bs, materializedGenericParameters));
            }
            result.Implementees.AddRange(implementees.OfType <RtSimpleTypeName>());
            ExportMembers(type, resolver, result, swtch);
            Context.Location.ResetCurrentType();
        }
示例#10
0
 /// <summary>
 ///     Exports type methods
 /// </summary>
 /// <param name="typeMember">Output writer</param>
 /// <param name="element">Type itself</param>
 /// <param name="resolver">Type resolver</param>
 /// <param name="swtch">Pass here type attribute inherited from IAutoexportSwitchAttribute</param>
 protected virtual void ExportMethods(ITypeMember typeMember, Type element, TypeResolver resolver, IAutoexportSwitchAttribute swtch)
 {
     GenerateMembers(element, resolver, typeMember, element.GetExportedMethods());
 }
示例#11
0
 /// <summary>
 ///     Exports type methods
 /// </summary>
 /// <param name="element">Type itself</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 ExportMethods(Type element, TypeResolver resolver, WriterWrapper sw,
                                      IAutoexportSwitchAttribute swtch)
 {
     GenerateMembers(element, resolver, sw, element.GetExportedMethods());
 }
        /// <summary>
        ///     Exports entire class to specified writer
        /// </summary>
        /// <param name="result">Exporting result</param>
        /// <param name="type">Exporting class type</param>
        /// <param name="resolver">Type resolver</param>
        /// <param name="swtch">Pass here type attribute inherited from IAutoexportSwitchAttribute</param>
        protected virtual void Export(ITypeMember result, Type type, TypeResolver resolver, IAutoexportSwitchAttribute swtch)
        {
            result.Name = type.GetName();

            var doc = Context.Documentation.GetDocumentationMember(type);

            if (doc != null)
            {
                RtJsdocNode docNode = new RtJsdocNode();
                if (doc.HasSummary())
                {
                    docNode.Description = doc.Summary.Text;
                }
                result.Documentation = docNode;
            }

            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
                    {
                        ((RtClass)result).Extendee = resolver.ResolveTypeName(bs);
                    }
                }
            }
            var implementees =
                ifaces.Where(c => ConfigurationRepository.Instance.ForType <TsInterfaceAttribute>(c) != null)
                .Select(resolver.ResolveTypeName).ToList();

            if (baseClassIsExportedAsInterface)
            {
                implementees.Add(resolver.ResolveTypeName(bs));
            }
            result.Implementees.AddRange(implementees.OfType <RtSimpleTypeName>());
            ExportMembers(type, resolver, result, swtch);
        }