Exemplo n.º 1
0
        private static void InitType(JsDocComment comment, IType type, IEmitter emitter)
        {
            if (!emitter.JsDoc.Namespaces.Contains(type.Namespace))
            {
                emitter.JsDoc.Namespaces.Add(type.Namespace);
                comment.Namespace = type.Namespace;
            }

            comment.Class    = XmlToJsDoc.ToJavascriptName(type, emitter);
            comment.Augments = XmlToJsDoc.GetTypeHierarchy(type, emitter);

            if (type is IHasAccessibility access)
            {
                comment.IsPublic    = access.IsPublic;
                comment.IsPrivate   = access.IsPrivate;
                comment.IsProtected = access.IsProtected;
            }

            if (type is ICSharpCode.NRefactory.TypeSystem.Implementation.DefaultResolvedTypeDefinition typeDef)
            {
                comment.IsAbstract = typeDef.IsAbstract;
                comment.IsStatic   = typeDef.IsStatic;
            }

            if (type.Kind == TypeKind.Enum)
            {
                comment.Enum = true;
            }
        }
Exemplo n.º 2
0
        public static string Convert(string source, ResolveResult rr, IEmitter emitter, object value)
        {
            var comment = new JsDocComment();

            XmlToJsDoc.InitComment(comment, rr, emitter, value);

            return(ReadComment(source, rr, emitter, comment));
        }
Exemplo n.º 3
0
 private static void InitComment(JsDocComment comment, ResolveResult rr, IEmitter emitter, object value)
 {
     if (rr is MemberResolveResult)
     {
         InitMember(comment, ((MemberResolveResult)rr).Member, emitter, value);
     }
     else if (rr is TypeResolveResult)
     {
         InitType(comment, rr.Type, emitter);
     }
 }
Exemplo n.º 4
0
        public static string ToJavascriptName(IType type, IEmitter emitter)
        {
            if (type.Kind == TypeKind.Delegate)
            {
                var delegateName = H5Types.ConvertName(type.FullName);

                if (!emitter.JsDoc.Callbacks.Contains(delegateName))
                {
                    var          method  = type.GetDelegateInvokeMethod();
                    JsDocComment comment = new JsDocComment();

                    var parameters = method.Parameters;

                    if (parameters != null && parameters.Count > 0)
                    {
                        foreach (var param in parameters)
                        {
                            var jsParam = new JsDocParam();
                            jsParam.Name = param.Name;
                            jsParam.Type = XmlToJsDoc.ToJavascriptName(param.Type, emitter);

                            comment.Parameters.Add(jsParam);
                        }
                    }

                    comment.Returns.Add(new JsDocParam
                    {
                        Type = XmlToJsDoc.ToJavascriptName(method.ReturnType, emitter)
                    });

                    comment.Callback = delegateName;
                    comment.MemberOf = type.Namespace;

                    if (!emitter.JsDoc.Namespaces.Contains(type.Namespace))
                    {
                        emitter.JsDoc.Namespaces.Add(type.Namespace);
                        comment.Namespace = type.Namespace;
                    }

                    emitter.JsDoc.Callbacks.Add(delegateName);

                    emitter.WriteIndented(comment.ToString() + newLine + newLine, 0);
                }

                return(delegateName);
            }

            if (type.IsKnownType(KnownTypeCode.String))
            {
                return("string");
            }

            if (type.IsKnownType(KnownTypeCode.Boolean))
            {
                return("boolean");
            }

            if (type.IsKnownType(KnownTypeCode.Void))
            {
                return("void");
            }

            if (type.IsKnownType(KnownTypeCode.Byte) ||
                type.IsKnownType(KnownTypeCode.Char) ||
                type.IsKnownType(KnownTypeCode.Double) ||
                type.IsKnownType(KnownTypeCode.Int16) ||
                type.IsKnownType(KnownTypeCode.Int32) ||
                type.IsKnownType(KnownTypeCode.SByte) ||
                type.IsKnownType(KnownTypeCode.Single) ||
                type.IsKnownType(KnownTypeCode.UInt16) ||
                type.IsKnownType(KnownTypeCode.UInt32))
            {
                return("number");
            }

            if (type.Kind == TypeKind.Array)
            {
                ICSharpCode.NRefactory.TypeSystem.ArrayType arrayType = (ICSharpCode.NRefactory.TypeSystem.ArrayType)type;
                return(JS.Types.ARRAY + ".<" + XmlToJsDoc.ToJavascriptName(arrayType.ElementType, emitter) + ">");
            }

            if (type.Kind == TypeKind.Dynamic)
            {
                return("object");
            }

            if (type.Kind == TypeKind.Enum && type.DeclaringType != null)
            {
                return("number");
            }

            if (NullableType.IsNullable(type))
            {
                return("?" + XmlToJsDoc.ToJavascriptName(NullableType.GetUnderlyingType(type), emitter));
            }

            H5Type h5Type = emitter.H5Types.Get(type, true);
            //string name = H5Types.ConvertName(type.FullName);

            var name = type.Namespace;

            var  hasTypeDef = h5Type != null && h5Type.TypeDefinition != null;
            bool isNested   = false;

            if (hasTypeDef)
            {
                var typeDef = h5Type.TypeDefinition;
                if (typeDef.IsNested)
                {
                    name     = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + H5Types.GetParentNames(emitter, typeDef);
                    isNested = true;
                }

                name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + H5Types.ConvertName(typeDef.Name);
            }
            else
            {
                if (type.DeclaringType != null)
                {
                    name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + H5Types.GetParentNames(emitter, type);

                    if (type.DeclaringType.TypeArguments.Count > 0)
                    {
                        name += Helpers.PrefixDollar(type.TypeArguments.Count);
                    }
                    isNested = true;
                }

                name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + H5Types.ConvertName(type.Name);
            }

            bool isCustomName = false;

            if (h5Type != null)
            {
                name = H5Types.AddModule(name, h5Type, false, isNested, out isCustomName);
            }

            if (!hasTypeDef && !isCustomName && type.TypeArguments.Count > 0)
            {
                name += Helpers.PrefixDollar(type.TypeArguments.Count);
            }

            return(name);
        }
Exemplo n.º 5
0
        private static void InitMember(JsDocComment comment, IMember member, IEmitter emitter, object value)
        {
            if (member != null)
            {
                if (member is IMethod method)
                {
                    comment.This = XmlToJsDoc.ToJavascriptName(member.DeclaringType, emitter);
                    if (method.IsConstructor)
                    {
                        comment.Constructs = comment.This;
                    }

                    if (method.TypeParameters != null && method.TypeParameters.Count > 0)
                    {
                        foreach (var param in method.TypeParameters)
                        {
                            var jsParam = new JsDocParam();
                            jsParam.Name = param.Name;
                            jsParam.Type = "Function";

                            comment.Parameters.Add(jsParam);
                        }
                    }
                }

                comment.Override = member.IsOverride;

                if (member is IParameterizedMember)
                {
                    var parameters = ((IParameterizedMember)member).Parameters;

                    if (parameters != null && parameters.Count > 0)
                    {
                        foreach (var param in parameters)
                        {
                            var jsParam = new JsDocParam();
                            jsParam.Name = param.Name;
                            jsParam.Type = XmlToJsDoc.ToJavascriptName(param.Type, emitter);

                            comment.Parameters.Add(jsParam);
                        }
                    }
                }

                if (member is IVariable variable)
                {
                    comment.MemberType = XmlToJsDoc.ToJavascriptName(variable.Type, emitter);
                }
                else if (!(member is IField || member is IProperty))
                {
                    comment.Returns.Add(new JsDocParam
                    {
                        Type = XmlToJsDoc.ToJavascriptName(member.ReturnType, emitter)
                    });
                }

                if (member is IField field)
                {
                    comment.ReadOnly   = field.IsReadOnly;
                    comment.Const      = field.IsConst;
                    comment.Default    = value ?? field.ConstantValue;
                    comment.MemberType = XmlToJsDoc.ToJavascriptName(field.Type, emitter);
                }

                if (member is IProperty property)
                {
                    comment.ReadOnly   = !property.CanSet;
                    comment.Default    = value;
                    comment.MemberType = XmlToJsDoc.ToJavascriptName(property.ReturnType, emitter);
                }

                if (member is IEvent ev)
                {
                    comment.Event = XmlToJsDoc.ToJavascriptName(member.DeclaringType, emitter) + "#" + member.Name;
                }

                comment.MemberOf    = XmlToJsDoc.ToJavascriptName(member.DeclaringType, emitter);
                comment.IsPublic    = member.IsPublic;
                comment.IsPrivate   = member.IsPrivate;
                comment.IsProtected = member.IsProtected;

                if (member is ICSharpCode.NRefactory.TypeSystem.Implementation.AbstractResolvedEntity entity)
                {
                    comment.IsAbstract = entity.IsAbstract;
                    comment.IsStatic   = entity.IsStatic;
                }
            }
        }
Exemplo n.º 6
0
        public static void EmitComment(IAbstractEmitterBlock block, AstNode node, bool?getter = null, VariableInitializer varInitializer = null)
        {
            if (block.Emitter.AssemblyInfo.GenerateDocumentation == H5.Contract.DocumentationMode.None || node.Parent == null)
            {
                return;
            }

            var visitor = new DocumentationCommentVisitor();

            node.AcceptChildren(visitor);

            if (block.Emitter.AssemblyInfo.GenerateDocumentation == H5.Contract.DocumentationMode.Basic && visitor.Comments.Count == 0)
            {
                return;
            }

            object value  = null;
            var    rr     = block.Emitter.Resolver.ResolveNode(varInitializer ?? node);
            string source = BuildCommentString(visitor.Comments);

            if (node is FieldDeclaration)
            {
                if (varInitializer.Initializer is PrimitiveExpression initializer)
                {
                    value = initializer.Value;
                }
            }
            else if (node is EventDeclaration eventDecl)
            {
                foreach (var evVar in eventDecl.Variables)
                {
                    var ev_rr = block.Emitter.Resolver.ResolveNode(evVar);
                    var memberResolveResult = ev_rr as MemberResolveResult;
                    var ev = memberResolveResult.Member as IEvent;

                    if (!getter.HasValue || getter.Value)
                    {
                        var comment = new JsDocComment();
                        InitMember(comment, ev.AddAccessor, block.Emitter, null);
                        comment.Function = Helpers.GetEventRef(ev, block.Emitter, false);
                        block.Write(
                            block.WriteIndentToString(XmlToJsDoc.ReadComment(source, ev_rr, block.Emitter, comment)));
                        block.WriteNewLine();
                    }

                    if (!getter.HasValue || !getter.Value)
                    {
                        var comment = new JsDocComment();
                        InitMember(comment, ev.RemoveAccessor, block.Emitter, null);
                        comment.Function = Helpers.GetEventRef(ev, block.Emitter, true);
                        block.Write(
                            block.WriteIndentToString(XmlToJsDoc.ReadComment(source, ev_rr, block.Emitter, comment)));
                        block.WriteNewLine();
                    }
                }

                return;
            }

            if (node is PropertyDeclaration prop)
            {
                var memberResolveResult = rr as MemberResolveResult;
                var rProp = memberResolveResult.Member as IProperty;

                var comment = new JsDocComment();
                InitMember(comment, rProp, block.Emitter, null);
                comment.Function = Helpers.GetPropertyRef(rProp, block.Emitter);
                block.Write(block.WriteIndentToString(XmlToJsDoc.ReadComment(source, rr, block.Emitter, comment)));
                block.WriteNewLine();
                return;
            }

            if (node is IndexerDeclaration indexer)
            {
                var memberResolveResult = rr as MemberResolveResult;
                var rProp = memberResolveResult.Member as IProperty;

                var comment = new JsDocComment();
                InitMember(comment, getter.HasValue && getter.Value ? rProp.Getter : rProp.Setter, block.Emitter, null);
                block.Write(block.WriteIndentToString(XmlToJsDoc.ReadComment(source, rr, block.Emitter, comment)));
                block.WriteNewLine();
                return;
            }

            block.Write(block.WriteIndentToString(XmlToJsDoc.Convert(source, rr, block.Emitter, value)));

            block.WriteNewLine();
        }
Exemplo n.º 7
0
        private static string ReadComment(string source, ResolveResult rr, IEmitter emitter, JsDocComment comment)
        {
            var xml = new StringBuilder("<comment>" + newLine);

            if (source != null)
            {
                foreach (var line in source.Split(newLine))
                {
                    var trimmedLine = line.Trim();

                    if (string.IsNullOrEmpty(trimmedLine))
                    {
                        continue;
                    }

                    xml.Append(System.Text.RegularExpressions.Regex.Replace(line, @"\/\/\/\s*", "") + newLine);
                }

                xml.Append("</comment>");

                var doc = new System.Xml.XmlDocument();

                try
                {
                    doc.LoadXml(xml.ToString());
                }
                catch (XmlException)
                {
                    return("");
                }

                foreach (XmlNode node in doc.GetElementsByTagName("summary"))
                {
                    comment.Descriptions.Add(HandleNode(node));
                }

                foreach (XmlNode node in doc.GetElementsByTagName("remark"))
                {
                    comment.Remarks.Add(HandleNode(node));
                }

                foreach (XmlNode node in doc.GetElementsByTagName("typeparam"))
                {
                    string name = null;
                    var    attr = node.Attributes["name"];
                    if (attr != null)
                    {
                        name = attr.Value.Trim();
                    }

                    var param = comment.Parameters.FirstOrDefault(p => p.Name == name);
                    if (param == null)
                    {
                        param = new JsDocParam
                        {
                            Name = "[name]",
                            Type = "[type]"
                        };

                        comment.Parameters.Add(param);
                    }

                    attr = node.Attributes["type"];
                    if (attr != null)
                    {
                        param.Type = attr.Value;
                    }
                    else if (rr != null)
                    {
                        param.Type = "Function";
                    }

                    var text = HandleNode(node);
                    if (!string.IsNullOrEmpty(text))
                    {
                        param.Desc = text;
                    }
                }

                foreach (XmlNode node in doc.GetElementsByTagName("param"))
                {
                    string name = null;
                    var    attr = node.Attributes["name"];
                    if (attr != null)
                    {
                        name = attr.Value.Trim();
                    }

                    var param = comment.Parameters.FirstOrDefault(p => p.Name == name);
                    if (param == null)
                    {
                        continue;
                    }

                    attr = node.Attributes["type"];
                    if (attr != null)
                    {
                        param.Type = attr.Value;
                    }
                    else if (rr != null)
                    {
                        param.Type = XmlToJsDoc.GetParamTypeName(param.Name, rr, emitter);
                    }

                    var text = HandleNode(node);
                    if (!string.IsNullOrEmpty(text))
                    {
                        param.Desc = text;
                    }
                }

                foreach (XmlNode node in doc.GetElementsByTagName("returns"))
                {
                    JsDocParam param = null;
                    if (comment.Returns.Any())
                    {
                        param = comment.Returns.FirstOrDefault();
                    }
                    else
                    {
                        param = new JsDocParam
                        {
                            Type = "[type]"
                        };

                        comment.Returns.Add(param);
                    }

                    var attr = node.Attributes["name"];
                    if (attr != null)
                    {
                        param.Name = attr.Value.Trim();
                    }

                    if (string.IsNullOrWhiteSpace(param.Type))
                    {
                        attr = node.Attributes["type"];
                        if (attr != null)
                        {
                            param.Type = attr.Value.Trim();
                        }
                        else if (rr != null)
                        {
                            param.Type = XmlToJsDoc.GetParamTypeName(null, rr, emitter);
                        }
                    }

                    var text = HandleNode(node);
                    if (!string.IsNullOrEmpty(text))
                    {
                        param.Desc = text;
                    }
                }

                foreach (XmlNode node in doc.GetElementsByTagName("example"))
                {
                    var           codeNodes = node.SelectNodes("code");
                    StringBuilder sb        = new StringBuilder();

                    foreach (XmlNode codeNode in codeNodes)
                    {
                        sb.Append(codeNode.InnerText + newLine);
                        node.RemoveChild(codeNode);
                    }

                    var code    = sb.ToString();
                    var caption = HandleNode(node);
                    comment.Examples.Add(new Tuple <string, string>(caption, code));
                }

                foreach (XmlNode node in doc.GetElementsByTagName("exception"))
                {
                    var attr          = node.Attributes["cref"];
                    var exceptionType = "";

                    if (attr != null)
                    {
                        try
                        {
                            exceptionType = XmlToJsDoc.ToJavascriptName(emitter.H5Types.Get(attr.InnerText).Type, emitter);
                        }
                        catch
                        {
                            // ignored
                        }
                    }

                    var caption = HandleNode(node);
                    comment.Throws.Add(new Tuple <string, string>(caption, exceptionType));
                }

                foreach (XmlNode node in doc.GetElementsByTagName("seealso"))
                {
                    var attr = node.Attributes["cref"];
                    var cref = "";

                    if (attr != null)
                    {
                        cref = attr.InnerText;
                    }

                    comment.SeeAlso.Add(cref);
                }

                foreach (XmlNode node in doc.GetElementsByTagName("value"))
                {
                    var valueParam = comment.Parameters.FirstOrDefault(p => p.Name == "value");
                    if (valueParam != null)
                    {
                        valueParam.Desc = HandleNode(node);
                    }
                }
            }

            return(comment.ToString());
        }