Пример #1
0
            public override string GetSource(string outputFilePath, Config config, GeneratorContext generatorContext)
            {
                var indent = "  ";

                var result = new StringBuilder();

                var name = Name;

                if (_parentToAugument != null)
                {
                    name = _parentToAugument.Name;
                }

                var typeScriptClassComment = generatorContext.GetTypeScriptComment(_type);

                if (typeScriptClassComment == null)
                {
                    var interfaces = _type.GetInterfaces();
                    foreach (var iface in interfaces)
                    {
                        typeScriptClassComment = generatorContext.GetTypeScriptComment(iface);
                        if (typeScriptClassComment != null)
                        {
                            break;
                        }
                    }
                }

                if (TypeBuilder.ShouldGenerateDotNetTypeNamesAsJsDocComment(_type))
                {
                    var dotNetTypeAttr = TypeUtils.GetCustomAttributesData(_type).FirstOrDefault(a =>
                                                                                                 a.AttributeType.Name == Constants.GenerateTypeScriptDotNetNameAttributeName &&
                                                                                                 a.ConstructorArguments.Count == 1
                                                                                                 );

                    var type = _type;
                    if (dotNetTypeAttr?.ConstructorArguments[0].Value is Type t)
                    {
                        type = t;
                    }

                    var canonicalType     = TypeBuilder.GetCanonicalDotNetType(type);
                    var dotNetTypeComment = $"@DotNetTypeName {TypeUtils.GetFullName(type)},{type.Assembly.GetName().Name}";

                    result.Append($"{indent}/**");

                    if (typeScriptClassComment != null)
                    {
                        result.Append(config.NewLine);
                        result.Append(FormatTypeScriptComment(typeScriptClassComment, indent, config.NewLine));
                        result.Append(config.NewLine);
                        result.Append($"{indent} *");
                        result.Append(config.NewLine);
                    }

                    if (canonicalType != null)
                    {
                        result.Append(config.NewLine);
                        result.Append($"{indent} * {dotNetTypeComment}");
                        result.Append(config.NewLine);
                        result.Append($"{indent} * @DotNetCanonicalTypeName {TypeUtils.GetFullName(canonicalType)},{canonicalType.Assembly.GetName().Name}");
                        result.Append(config.NewLine);
                        result.Append($"{indent} */");
                    }
                    else
                    {
                        result.Append($" {dotNetTypeComment} */");
                    }

                    result.Append(config.NewLine);
                }
                else
                {
                    if (typeScriptClassComment != null)
                    {
                        result.Append($"{indent}/**");
                        result.Append(config.NewLine);
                        result.Append(FormatTypeScriptComment(typeScriptClassComment, indent, config.NewLine));
                        result.Append(config.NewLine);
                        result.Append($"{indent} */");
                        result.Append(config.NewLine);
                    }
                }

                result.Append($"{indent}interface {name}");
                if (_typeParameters.Length > 0)
                {
                    result.Append("<")
                    .Append(string.Join(", ", _typeParameters))
                    .Append(">");
                }

                if (_extends.Length > 0)
                {
                    var extends = _parentToAugument != null?_extends.Where(e => !e.Equals(_parentToAugument)).ToList() : _extends.ToList();

                    if (extends.Any())
                    {
                        result.Append(" extends ").Append(extends[0].GetSource());
                        for (int i = 1; i < extends.Count; i++)
                        {
                            result.Append(", ").Append(extends[i].GetSource());
                        }
                    }
                }

                result.Append(" {");
                result.Append(config.NewLine);

                if (_typeMemberName != null)
                {
                    // TODO: Should the value here be configurable? Perhaps you want the FQN?
                    result.Append($"{indent}  {_typeMemberName}: '{name}';");
                    result.Append(config.NewLine);
                }

                var memberTypeWrapper = TypeBuilder.GetWrapperTypeForMembers(_type, config);

                foreach (var m in _members)
                {
                    var optional = "";
                    if (m.IsOptional || m.Type.IsOptional)
                    {
                        if (m.IsOptional || m.Type.IsOptional)
                        {
                            optional = "?";
                        }
                    }

                    var typeScriptMemberComment = generatorContext.GetTypeScriptComment(m.MemberInfo);
                    if (typeScriptMemberComment == null)
                    {
                        var interfaceMembers = m.MemberInfo.DeclaringType.GetInterfaces().SelectMany(i => i.GetMember(m.MemberInfo.Name)).Where(m => m != null).ToList();
                        foreach (var interfaceMember in interfaceMembers)
                        {
                            typeScriptMemberComment = generatorContext.GetTypeScriptComment(interfaceMember);
                            if (typeScriptMemberComment != null)
                            {
                                break;
                            }
                        }
                    }

                    if (typeScriptMemberComment != null)
                    {
                        result.Append($"{indent}  /**");
                        result.Append(config.NewLine);
                        result.Append(FormatTypeScriptComment(typeScriptMemberComment, indent + "  ", config.NewLine));
                        result.Append(config.NewLine);
                        result.Append($"{indent}   */");
                        result.Append(config.NewLine);
                    }
                    result.Append($"{indent}  {FixName(m.Name)}{optional}: {WrapType(m.Type.GetSource(), memberTypeWrapper)};");
                    result.Append(config.NewLine);
                }

                result.Append(indent).Append("}");
                result.Append(config.NewLine);

                if (_derivedTypesUnionGeneration?.DerivedTypeReferences.Length > 0)
                {
                    result.Append(config.NewLine);
                    result.Append($"{indent}type {_derivedTypesUnionGeneration.DerivedTypesUnionName} ={config.NewLine}{indent}  | {string.Join($"{config.NewLine}{indent}  | ", _derivedTypesUnionGeneration.DerivedTypeReferences.Select(t => t.GetSource()))};");
                    result.Append(config.NewLine);
                }

                return(result.ToString());
            }