Esempio n. 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);
        }