Example #1
0
        private TypeScriptInterface BuildInterface(CodeClass codeClass, TypeScriptInterfaceAttributeValues attributeValues, TypeContext typeContext)
        {
            var tsInterface = new TypeScriptInterface
            {
                FullName = codeClass.FullName,
                Name     = GetInterfaceName(attributeValues)
            };

            TypescriptType indexedType;

            if (TryGetIndexedType(codeClass, typeContext, out indexedType))
            {
                tsInterface.IndexedType = indexedType;
            }

            new ClassTraverser(codeClass, (property) =>
            {
                TypeScriptInterfaceMember member;
                if (TryGetMember(property, typeContext, out member))
                {
                    tsInterface.Members.Add(member);
                }
            });

            return(tsInterface);
        }
        public void AppendOutputSubEnum(TypeScriptEnum tsEnum, TypeScriptInterface owner)
        {
            BeginInterface(tsEnum, owner);

            AppendMembers(tsEnum, owner);

            EndInterface(owner);
        }
 private void EndInterface(TypeScriptInterface owner = null)
 {
     while (owner != null)
     {
         AppendIndendation();
         AppendIndentedLine("}");
         owner = owner.Owner;
     }
     AppendIndentedLine("}");
 }
        private void AppendMembers(TypeScriptEnum tsEnum, TypeScriptInterface owner = null)
        {
            int identation = 4;

            while (owner != null)
            {
                identation += 4;
                owner       = owner.Owner;
            }
            var appender = new EnumMemberOutputAppender(Output, BaseIndentation + identation, Settings);

            foreach (TypeScriptEnumMember member in tsEnum.Members)
            {
                appender.AppendOutput(member);
            }
        }
Example #5
0
        public TypeScriptInterface GetOrCreateInterface(
            string moduleName,
            TypeName sourceType,
            string outputName,
            out bool created)
        {
            TypeScriptInterface result;

            if (!this.sourceToInterfaceMap.TryGetValue(
                    sourceType.UniversalName,
                    out result))
            {
                result = new TypeScriptInterface(
                    sourceType,
                    this.GetTypeArgumentReferences(
                        sourceType.TypeArguments,
                        contextTypeReference: null),
                    contextTypeReference: null);

                this.sourceToInterfaceMap.Add(
                    sourceType.UniversalName,
                    result);

                this.sourceNamesToOutputTypeMap.Add(
                    sourceType.UniversalName,
                    this.GenerateOutputName(
                        moduleName,
                        outputName,
                        (sourceType.TypeArguments != null)
                            ? sourceType.TypeArguments.Count
                            : 0));

                bool             moduleCreated;
                TypeScriptModule module = this.GetOrCreateModule(
                    moduleName,
                    out moduleCreated);
                module.Interfaces.Add(result);

                created = true;
            }
            else
            {
                created = false;
            }

            return(result);
        }
        public IEnumerable <TypeScriptModule> GetAllInterfaces()
        {
            TypeContext typeContext  = BuildContext();
            var         byModuleName = new Dictionary <string, TypeScriptModule>();
            var         tsMap        = new Dictionary <CodeClass, TypeScriptInterface>();
            var         tsEnumMap    = new Dictionary <CodeEnum, TypeScriptEnum>();

            new SolutionTraverser(Solution, ns =>
            {
                new NamespaceTraverser(ns,
                                       codeClass => ProcessCodeClass(typeContext, tsMap, byModuleName, codeClass),
                                       codeEnum => ProcessCodeEnum(typeContext, tsEnumMap, byModuleName, codeEnum)
                                       );
            });

            List <TypeScriptInterface> tsInterfaces = tsMap.Values.ToList();

            tsMap.Keys.ToList().ForEach(codeClass =>
            {
                CodeElements baseClasses = codeClass.Bases;
                if (baseClasses.Count > 0)
                {
                    CodeElement baseClass = baseClasses.Item(1);
                    if (baseClass != null)
                    {
                        // We must remove all text after < char, to compare generic types.
                        // It's not really correct, but must work in common cases.
                        var baseClassNonGenericFullName = baseClass.FullName.Split('<')[0];
                        TypeScriptInterface parent      = tsInterfaces.FirstOrDefault(intf => baseClassNonGenericFullName == intf.FullName.Split('<')[0]);
                        if (parent != null)
                        {
                            tsMap[codeClass].Parent = parent;
                        }
                    }
                }
            });

            return(byModuleName.Values
                   .OrderBy(m => m.QualifiedName)
                   .ToList());
        }
Example #7
0
        private TypeScriptInterface BuildInterface(
            CodeClass codeClass,
            TypeScriptInterfaceAttributeValues attributeValues,
            TypeContext typeContext,
            bool directlyMarkedInterface,
            bool indirectlyMarkedInterface
            )
        {
            var tsInterface = new TypeScriptInterface
            {
                FullName = codeClass.FullName,
                Name     = GetInterfaceName(attributeValues),
                IsIndirectlyMarkedInterface = indirectlyMarkedInterface,
                IsDirectlyMarkedInterface   = directlyMarkedInterface
            };

            try {
                tsInterface.DocComment = codeClass.DocComment;
            }
            catch (Exception exception) {
                tsInterface.DocComment = "Error resolving CodeClass.DocComment.";
            }

            TypescriptType indexedType;

            if (TryGetIndexedType(codeClass, typeContext, out indexedType))
            {
                tsInterface.IndexedType = indexedType;
            }

            new ClassTraverser(codeClass, (property) =>
            {
                TypeScriptInterfaceMember member;
                if (TryGetMember(property, typeContext, out member))
                {
                    tsInterface.Members.Add(member);
                }
            });
            return(tsInterface);
        }
Example #8
0
        private void AppendType(
            StringBuilder output,
            int indent,
            TypeReference typeReference,
            HashSet <string> outputTypeNames,
            InterfaceOutputAppender interfaceAppender,
            TypeScriptModule currentModule)
        {
            if (!outputTypeNames.Contains(typeReference.SourceType.UniversalName))
            {
                TypeScriptInterface interfaceType = this.TypeContext.GetInterface(typeReference.SourceType);
                if (interfaceType != null &&
                    currentModule.Interfaces.Contains(interfaceType))
                {
                    if (interfaceType.Parent != null)
                    {
                        this.AppendType(
                            output,
                            indent,
                            interfaceType.Parent,
                            outputTypeNames,
                            interfaceAppender,
                            currentModule);
                    }

                    // More reference handling...

                    if (!outputTypeNames.Contains(typeReference.SourceType.UniversalName))
                    {
                        interfaceAppender.AppendOutput(
                            output,
                            indent,
                            interfaceType);
                        outputTypeNames.Add(typeReference.SourceType.UniversalName);
                    }
                }
            }
        }
 private void BeginInterface(TypeScriptEnum tsEnum, TypeScriptInterface owner = null)
 {
     AppendIndentedLine("/** Generated from " + tsEnum.FullName + " **/");
     if (owner == null)
     {
         AppendIndented("const enum " + tsEnum.Name);
     }
     else
     {
         string   module   = owner.Name;
         string   enumName = tsEnum.Name;
         string[] arr      = tsEnum.Name.Split('.');
         if (arr.Length > 1)
         {
             module   = arr[0];
             enumName = arr[1];
         }
         AppendIndentedLine("export module " + module + " {");
         AppendIndendation();
         AppendIndented("const enum " + enumName);
     }
     Output.AppendLine(" {");
 }
        private void ProcessCodeClass(TypeContext typeContext, IDictionary <CodeClass, TypeScriptInterface> tsMap,
                                      IDictionary <string, TypeScriptModule> byModuleName, CodeClass codeClass)
        {
            InterfaceType interfaceType;

            if (typeContext.TryGetInterfaceType(codeClass.FullName, out interfaceType))
            {
                TypeScriptInterfaceAttributeValues values = interfaceType.AttributeValues;

                TypeScriptModule module;
                if (!byModuleName.TryGetValue(values.Module, out module))
                {
                    module = new TypeScriptModule {
                        QualifiedName = values.Module
                    };
                    byModuleName.Add(values.Module, module);
                }

                TypeScriptInterface tsInterface = BuildInterface(codeClass, values, typeContext);
                tsMap.Add(codeClass, tsInterface);
                tsInterface.Module = module;
                module.Interfaces.Add(tsInterface);
            }
        }
Example #11
0
        private TypeScriptInterface BuildInterface(CodeClass codeClass, TypeScriptInterfaceAttributeValues attributeValues, TypeContext typeContext)
        {
            var tsInterface = new TypeScriptInterface
            {
                FullName = codeClass.FullName,
                Name = GetInterfaceName(attributeValues)
            };

            TypescriptType indexedType;
            if (TryGetIndexedType(codeClass, typeContext, out indexedType))
                tsInterface.IndexedType = indexedType;

            new ClassTraverser(codeClass, (property) =>
            {
                TypeScriptInterfaceMember member;
                if (TryGetMember(property, typeContext, out member))
                    tsInterface.Members.Add(member);
            });

            return tsInterface;
        }
Example #12
0
        private void TraverseNamespace(
            CodeNamespace codeNamespace,
            TraverserSettings settings)
        {
            if (settings.NamespaceFilter == null ||
                settings.NamespaceFilter(codeNamespace))
            {
                if (settings.ClassToInterfaceBuilder != null)
                {
                    Traversal.TraverseClassesInNamespace(
                        codeNamespace,
                        (codeClass) =>
                    {
                        if (settings.ClassFilter == null ||
                            settings.ClassFilter(codeClass))
                        {
                            TypeScriptInterface builtType =
                                settings.ClassToInterfaceBuilder.Build(
                                    codeClass,
                                    this.context);

                            if (builtType != null &&
                                settings.TypeDecorators != null &&
                                settings.TypeDecorators.Any())
                            {
                                foreach (Action <TypeScriptType> typeDecorator in settings.TypeDecorators)
                                {
                                    typeDecorator(builtType);
                                }
                            }
                        }
                    });
                }

                if (settings.InterfaceToInterfaceBuilder != null)
                {
                    Traversal.TraverseInterfacesInNamespace(
                        codeNamespace,
                        (codeInterface) =>
                    {
                        if (settings.InterfaceFilter == null ||
                            settings.InterfaceFilter(codeInterface))
                        {
                            TypeScriptInterface builtType =
                                settings.InterfaceToInterfaceBuilder.Build(
                                    codeInterface,
                                    this.context);

                            if (builtType != null &&
                                settings.TypeDecorators != null &&
                                settings.TypeDecorators.Any())
                            {
                                foreach (Action <TypeScriptType> typeDecorator in settings.TypeDecorators)
                                {
                                    typeDecorator(builtType);
                                }
                            }
                        }
                    });
                }

                if (settings.EnumBuilder != null)
                {
                    Traversal.TraverseEnumsInNamespace(
                        codeNamespace,
                        (codeEnum) =>
                    {
                        if (settings.EnumFilter == null ||
                            settings.EnumFilter(codeEnum))
                        {
                            settings.EnumBuilder.Build(
                                codeEnum,
                                this.context);
                        }
                    });
                }
            }
        }
        private TypeScriptInterface BuildInterface(CodeClass codeClass,
                                                   TypeScriptInterfaceAttributeValues attributeValues, TypeContext typeContext)
        {
            var tsInterface = new TypeScriptInterface
            {
                FullName = codeClass.FullName,
                Name     = GetInterfaceName(attributeValues)
            };

            // Add sub-classes to the interface
            foreach (var codeSubClass in codeClass.Members.OfType <CodeClass>().Where(cc => cc.Access == vsCMAccess.vsCMAccessPublic))
            {
                var subAttributeValues = new TypeScriptInterfaceAttributeValues {
                    Name = codeSubClass.Name
                };
                InterfaceType interfaceType;
                if (typeContext.TryGetInterfaceType(codeSubClass.FullName, out interfaceType))
                {
                    subAttributeValues        = interfaceType.AttributeValues;
                    subAttributeValues.Module = attributeValues.Module + "." + tsInterface.Name;
                }

                TypeScriptInterface subInterface = BuildInterface(codeSubClass, subAttributeValues, typeContext);
                subInterface.Owner = tsInterface;
                tsInterface.SubClasses.Add(subInterface);
            }

            // Add sub-enums to the interface
            foreach (CodeEnum codeSubEnum in codeClass.Members.OfType <CodeEnum>().Where(cc => cc.Access == vsCMAccess.vsCMAccessPublic))
            {
                var subAttributeValues = new TypeScriptEnumAttributeValues {
                    Name = codeSubEnum.Name
                };
                EnumType enumType;
                if (typeContext.TryGetEnumType(codeSubEnum.FullName, out enumType))
                {
                    subAttributeValues        = enumType.AttributeValues;
                    subAttributeValues.Module = attributeValues.Module + "." + tsInterface.Name;
                }

                TypeScriptEnum subEnum = BuildEnum(codeSubEnum, subAttributeValues, typeContext);
                subEnum.Owner = tsInterface;
                tsInterface.SubEnums.Add(subEnum);
            }

            TypescriptType indexedType;

            if (TryGetIndexedType(codeClass, typeContext, out indexedType))
            {
                tsInterface.IndexedType = indexedType;
            }

            new ClassTraverser(codeClass, property =>
            {
                TypeScriptInterfaceMember member;
                if (TryGetMember(property, typeContext, out member))
                {
                    tsInterface.Members.Add(member);
                }
            });


            return(tsInterface);
        }
Example #14
0
        private TypeScriptInterface BuildInterface(CodeClass codeClass, TypeScriptInterfaceAttributeValues attributeValues, TypeContext typeContext)
        {
            var tsInterface = new TypeScriptInterface
            {
                FullName = codeClass.FullName,
                Name = GetInterfaceName(attributeValues),
                Comment = codeClass.Comment,
                DocComment = codeClass.DocComment
            };

            // Add sub-classes to the interface
            foreach (var codeSubClass in codeClass.Members.OfType<CodeClass>().Where(cc => cc.Access == vsCMAccess.vsCMAccessPublic))
            {
                var subAttributeValues = new TypeScriptInterfaceAttributeValues { Name = codeSubClass.Name };
                InterfaceType interfaceType;
                if (typeContext.TryGetInterfaceType(codeSubClass.FullName, out interfaceType))
                {
                    subAttributeValues = interfaceType.AttributeValues;
                    subAttributeValues.Module = attributeValues.Module + "." + tsInterface.Name;
                }

                var subInterface = BuildInterface(codeSubClass, subAttributeValues, typeContext);
                subInterface.Owner = tsInterface;
                tsInterface.SubClasses.Add(subInterface);
            }

            // Add sub-enums to the interface
            foreach (CodeEnum codeSubEnum in codeClass.Members.OfType<CodeEnum>().Where(cc => cc.Access == vsCMAccess.vsCMAccessPublic))
            {
                var subAttributeValues = new TypeScriptEnumAttributeValues { Name = codeSubEnum.Name };
                EnumType enumType;
                if (typeContext.TryGetEnumType(codeSubEnum.FullName, out enumType))
                {
                    subAttributeValues = enumType.AttributeValues;
                    subAttributeValues.Module = attributeValues.Module + "." + tsInterface.Name;
                }

                var subEnum = BuildEnum(codeSubEnum, subAttributeValues, typeContext);
                subEnum.Owner = tsInterface;
                tsInterface.SubEnums.Add(subEnum);
            }

            TypescriptType indexedType;
            if (TryGetIndexedType(codeClass, typeContext, out indexedType))
                tsInterface.IndexedType = indexedType;

            new ClassTraverser(codeClass, property =>
            {
                TypeScriptInterfaceMember member;
                if (TryGetMember(property, typeContext, out member))
                    tsInterface.Members.Add(member);
            });

            return tsInterface;
        }