Exemple #1
0
        public override Element Build()
        {
            var @interface = new Interface(SourceType.Name.Split('`').First())
            {
                Modifiers = { Modifier.Export }
            };

            foreach (var @base in _interfaces)
            {
                @interface.Extends.Add(@base.TypescriptName());
            }

            var classDocumentation = Documentation?.ForClass(SourceType);

            if (classDocumentation != null)
            {
                @interface.Comment = classDocumentation.Summary;
            }

            foreach (var typeArgument in _typeArguments)
            {
                @interface.TypeArguments.Add(typeArgument.TypescriptName());
            }

            foreach (var member in _properties)
            {
                var property = new Property(member.Name.CamelCase(), member.PropertyType.TypescriptName());
                var propertyDocumentation = Documentation?.ForMember(member);

                if (propertyDocumentation != null)
                {
                    property.Comment = propertyDocumentation.Summary;
                }

                @interface.Members.Add(property);
            }

            WriteLine(ConsoleColor.Magenta, "interface", @interface.Name);
            return(@interface);
        }
        public override Element Build()
        {
            var @class = new Class(SourceType.Name.SanitizeTypeName())
            {
                Modifiers = { Modifier.Export }
            };

            WriteLine(ConsoleColor.Green, "class", @class.Name);

            if (Inherits != null)
            {
                @class.Extends = Inherits.TypescriptName();
            }

            var classDocumentation = Documentation?.ForClass(SourceType);

            if (classDocumentation != null)
            {
                @class.Comment = classDocumentation.Summary;
            }

            if (SourceType.IsAbstract)
            {
                @class.Modifiers.Add(Modifier.Abstract);
            }

            foreach (var @interface in _interfaces)
            {
                @class.Implements.Add(@interface.TypescriptName());
            }

            foreach (var typeArgument in _typeArguments)
            {
                @class.TypeArguments.Add(typeArgument.TypescriptName());
            }

            if (!SourceType.IsAbstract)
            {
                foreach (var attribute in _attributes)
                {
                    var name      = attribute.GetType().ClassDecoratorName();
                    var arguments = new ObjectLiteral(attribute);
                    @class.Decorators.Add(new Decorator(name, new[] { arguments }));
                }
            }

            foreach (var source in _properties)
            {
                var getMethod  = source.GetMethod;
                var target     = new Property(source.Name.CamelCase(), source.PropertyType.TypescriptName());
                var attributes = source.GetCustomAttributes(false).Where(t => t.GetType().IsPublic);

                if (getMethod.IsAbstract)
                {
                    target.Modifiers.Add(Modifier.Abstract);
                }

                if (getMethod.IsFamily)
                {
                    target.Modifiers.Add(Modifier.Protected);
                }
                else
                {
                    target.Modifiers.Add(Modifier.Public);
                }

                var propertyDocumentation = Documentation?.ForMember(source);

                if (propertyDocumentation != null)
                {
                    target.Comment = propertyDocumentation.Summary;
                }

                if (OutputContext.Properties?.Initialize ?? false)
                {
                    SetDefaultValue(source, target);
                }

                foreach (var attribute in attributes)
                {
                    var name      = attribute.GetType().PropertyDecoratorName();
                    var arguments = new ObjectLiteral(attribute);
                    target.Decorators.Add(new Decorator(name, new[] { arguments }));
                }

                @class.Members.Add(target);
            }

            var illegalProp = @class.Properties.SingleOrDefault(p => p.Name == "constructor");

            {
                if (illegalProp != null)
                {
                    const string prefix  = "_";
                    var          newName = prefix + illegalProp.Name;
                    while (@class.Properties.Any(p => p.Name == newName))
                    {
                        newName = prefix + newName;
                    }

                    illegalProp.Name = newName;
                }
            }
            return(@class);
        }
Exemple #3
0
        public override Element Build()
        {
            var @enum = new Enum(SourceType.Name)
            {
                Modifiers = { Modifier.Export }
            };

            var classDocumentation = Documentation?.ForClass(SourceType);

            if (classDocumentation != null)
            {
                @enum.Comment = classDocumentation.Summary;
            }

            var names = System.Enum.GetNames(SourceType);

            foreach (var name in names)
            {
                object value;

                var member = SourceType.GetMember(name);
                //If we've specified we want number enum values, just add the value it finds.
                if (OutputContext.EnumSettings?.ValueType == EnumValueType.Number)
                {
                    value = System.Enum.Parse(SourceType, name);
                }
                //Use string enum values by default
                else
                {
                    var enumMemberAttribute = member.Single()
                                              .GetCustomAttributes(true)
                                              .FirstOrDefault(a => a.GetType().FullName == "System.Runtime.Serialization.EnumMemberAttribute");

                    var label = enumMemberAttribute
                                ?.GetType()
                                .GetProperty("Value")
                                ?.GetValue(enumMemberAttribute)
                                ?.ToString();

                    // No EnumMemberAttribute, try for a DisplayAttribute instead.
                    if (label == null)
                    {
                        var displayAttribute = member.Single()
                                               .GetCustomAttributes(true)
                                               .FirstOrDefault(a => a.GetType().FullName == "System.ComponentModel.DataAnnotations.DisplayAttribute");

                        label = displayAttribute?.GetType()
                                .GetProperty("Name")
                                ?.GetValue(displayAttribute)
                                ?.ToString();
                    }

                    value = $"\"{label ?? name}\"";
                }

                @enum.Members.Add(name.CamelCase(), value);
            }

            WriteLine(ConsoleColor.DarkYellow, "enum", @enum.Name);
            return(@enum);
        }