예제 #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);

            var access = type as IHasAccessibility;

            if (access != null)
            {
                comment.IsPublic    = access.IsPublic;
                comment.IsPrivate   = access.IsPrivate;
                comment.IsProtected = access.IsProtected;
            }

            var typeDef = type as ICSharpCode.NRefactory.TypeSystem.Implementation.DefaultResolvedTypeDefinition;

            if (typeDef != null)
            {
                comment.IsAbstract = typeDef.IsAbstract;
                comment.IsStatic   = typeDef.IsStatic;
            }

            if (type.Kind == TypeKind.Enum)
            {
                comment.Enum = true;
            }
        }
예제 #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));
        }
예제 #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);
     }
 }
예제 #4
0
        public static string ToJavascriptName(IType type, IEmitter emitter)
        {
            if (type.Kind == TypeKind.Delegate)
            {
                var delegateName = BridgeTypes.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));
            }

            BridgeType bridgeType = emitter.BridgeTypes.Get(type, true);
            //string name = BridgeTypes.ConvertName(type.FullName);

            var name = type.Namespace;

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

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

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

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

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

            bool isCustomName = false;

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

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

            return(name);
        }
예제 #5
0
        private static void InitMember(JsDocComment comment, IMember member, IEmitter emitter, object value)
        {
            if (member != null)
            {
                var method = member as IMethod;
                if (method != null)
                {
                    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);
                        }
                    }
                }

                var variable = member as IVariable;
                if (variable != null)
                {
                    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)
                    });
                }

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

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

                var ev = member as IEvent;
                if (ev != null)
                {
                    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;

                var entity = member as ICSharpCode.NRefactory.TypeSystem.Implementation.AbstractResolvedEntity;
                if (entity != null)
                {
                    comment.IsAbstract = entity.IsAbstract;
                    comment.IsStatic   = entity.IsStatic;
                }
            }
        }
예제 #6
0
        public static void EmitComment(IAbstractEmitterBlock block, AstNode node, bool?getter = null, VariableInitializer varInitializer = null)
        {
            if (block.Emitter.AssemblyInfo.GenerateDocumentation == Bridge.Contract.DocumentationMode.None || node.Parent == null)
            {
                return;
            }

            var visitor = new DocumentationCommentVisitor();

            node.AcceptChildren(visitor);

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

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

            if (node is FieldDeclaration)
            {
                var initializer = varInitializer.Initializer as PrimitiveExpression;

                if (initializer != null)
                {
                    value = initializer.Value;
                }
            }
            else if (node is EventDeclaration)
            {
                var eventDecl = (EventDeclaration)node;
                foreach (var evVar in eventDecl.Variables)
                {
                    var ev_rr = block.Emitter.Resolver.ResolveNode(evVar, block.Emitter);
                    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;
            }

            var prop = node as PropertyDeclaration;

            if (prop != null)
            {
                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;
            }

            var indexer = node as IndexerDeclaration;

            if (indexer != null)
            {
                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();
        }
예제 #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.BridgeTypes.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());
        }
예제 #8
0
        public static string ToJavascriptName(IType type, IEmitter emitter)
        {
            if (type.Kind == TypeKind.Delegate)
            {
                var delegateName = BridgeTypes.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.Output.Insert(0, comment.ToString() + newLine + newLine);
                }

                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.Decimal) ||
                type.IsKnownType(KnownTypeCode.Double) ||
                type.IsKnownType(KnownTypeCode.Int16) ||
                type.IsKnownType(KnownTypeCode.Int32) ||
                type.IsKnownType(KnownTypeCode.Int64) ||
                type.IsKnownType(KnownTypeCode.SByte) ||
                type.IsKnownType(KnownTypeCode.Single) ||
                type.IsKnownType(KnownTypeCode.UInt16) ||
                type.IsKnownType(KnownTypeCode.UInt32) ||
                type.IsKnownType(KnownTypeCode.UInt64))
            {
                return "number";
            }

            if (type.Kind == TypeKind.Array)
            {
                ICSharpCode.NRefactory.TypeSystem.ArrayType arrayType = (ICSharpCode.NRefactory.TypeSystem.ArrayType)type;
                return "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);
            }

            BridgeType bridgeType = emitter.BridgeTypes.Get(type, true);
            //string name = BridgeTypes.ConvertName(type.FullName);

            var name = type.Namespace;

            var hasTypeDef = bridgeType != null && bridgeType.TypeDefinition != null;
            if (hasTypeDef)
            {
                var typeDef = bridgeType.TypeDefinition;
                if (typeDef.IsNested)
                {
                    name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + BridgeTypes.GetParentNames(typeDef);
                }

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

                    if (type.DeclaringType.TypeArguments.Count > 0)
                    {
                        name += "$" + type.TypeArguments.Count;
                    }
                }

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

            bool isCustomName = false;
            if (bridgeType != null)
            {
                name = BridgeTypes.AddModule(name, bridgeType, out isCustomName);
            }

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

            return name;
        }
예제 #9
0
        public static void EmitComment(IAbstractEmitterBlock block, AstNode node)
        {
            if (block.Emitter.AssemblyInfo.GenerateDocumentation == Bridge.Contract.DocumentationMode.None || node.Parent == null)
            {
                return;
            }

            var visitor = new DocumentationCommentVisitor();

            node.AcceptChildren(visitor);

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

            object value = null;

            if (node is FieldDeclaration)
            {
                var fieldDecl = (FieldDeclaration)node;
                node = fieldDecl.Variables.First();
                var initializer = fieldDecl.Variables.First().Initializer as PrimitiveExpression;

                if (initializer != null)
                {
                    value = initializer.Value;
                }
            }
            else if (node is EventDeclaration)
            {
                var eventDecl = (EventDeclaration)node;
                node = eventDecl.Variables.First();
                var initializer = eventDecl.Variables.First().Initializer as PrimitiveExpression;

                if (initializer != null)
                {
                    value = initializer.Value;
                }
            }

            var    rr     = block.Emitter.Resolver.ResolveNode(node, block.Emitter);
            string source = BuildCommentString(visitor.Comments);

            var prop = node as PropertyDeclaration;

            if (prop != null)
            {
                var memberResolveResult = rr as MemberResolveResult;
                var rProp = memberResolveResult.Member as DefaultResolvedProperty;

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

                comment = new JsDocComment();
                InitMember(comment, rProp.Setter, block.Emitter, null);
                comment.Function = Helpers.GetPropertyRef(rProp, block.Emitter, true);
                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();
        }
예제 #10
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);

            var access = type as IHasAccessibility;
            if (access != null)
            {
                comment.IsPublic = access.IsPublic;
                comment.IsPrivate = access.IsPrivate;
                comment.IsProtected = access.IsProtected;
            }

            var typeDef = type as ICSharpCode.NRefactory.TypeSystem.Implementation.DefaultResolvedTypeDefinition;
            if (typeDef != null)
            {
                comment.IsAbstract = typeDef.IsAbstract;
                comment.IsStatic = typeDef.IsStatic;
            }

            if (type.Kind == TypeKind.Enum)
            {
                comment.Enum = true;
            }
        }
예제 #11
0
        private static void InitMember(JsDocComment comment, IMember member, IEmitter emitter, object value)
        {
            if (member != null)
            {
                var method = member as IMethod;
                if (method != null)
                {
                    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);
                        }
                    }
                }

                var variable = member as IVariable;
                if (variable != null)
                {
                    comment.MemberType = XmlToJsDoc.ToJavascriptName(variable.Type, emitter);
                }
                else
                {
                    comment.Returns.Add(new JsDocParam
                    {
                        Type = XmlToJsDoc.ToJavascriptName(member.ReturnType, emitter)
                    });
                }

                var field = member as DefaultResolvedField;
                if (field != null)
                {
                    comment.ReadOnly = field.IsReadOnly;
                    comment.Const = field.IsConst;
                    comment.Default = value ?? field.ConstantValue;
                }

                var ev = member as IEvent;
                if (ev != null)
                {
                    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;

                var entity = member as ICSharpCode.NRefactory.TypeSystem.Implementation.AbstractResolvedEntity;
                if (entity != null)
                {
                    comment.IsAbstract = entity.IsAbstract;
                    comment.IsStatic = entity.IsStatic;
                }
            }
        }
예제 #12
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);
     }
 }
예제 #13
0
        public static void EmitComment(IAbstractEmitterBlock block, AstNode node)
        {
            if (block.Emitter.AssemblyInfo.GenerateDocumentation == Bridge.Contract.DocumentationMode.None || node.Parent == null)
            {
                return;
            }

            var visitor = new DocumentationCommentVisitor();
            node.AcceptChildren(visitor);

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

            object value = null;
            if (node is FieldDeclaration)
            {
                var fieldDecl = (FieldDeclaration)node;
                node = fieldDecl.Variables.First();
                var initializer = fieldDecl.Variables.First().Initializer as PrimitiveExpression;

                if (initializer != null)
                {
                    value = initializer.Value;
                }
            }
            else if (node is EventDeclaration)
            {
                var eventDecl = (EventDeclaration)node;
                node = eventDecl.Variables.First();
                var initializer = eventDecl.Variables.First().Initializer as PrimitiveExpression;

                if (initializer != null)
                {
                    value = initializer.Value;
                }
            }

            var rr = block.Emitter.Resolver.ResolveNode(node, block.Emitter);
            string source = BuildCommentString(visitor.Comments);

            var prop = node as PropertyDeclaration;
            if (prop != null)
            {
                var memberResolveResult = rr as MemberResolveResult;
                var rProp = memberResolveResult.Member as DefaultResolvedProperty;

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

                comment = new JsDocComment();
                InitMember(comment, rProp.Setter, block.Emitter, null);
                comment.Function = Helpers.GetPropertyRef(rProp, block.Emitter, true);
                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();
        }
예제 #14
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)
                    {
                        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 = 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();
                    }

                    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.BridgeTypes.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();
        }
예제 #15
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);
        }
예제 #16
0
        public static string ToJavascriptName(IType type, IEmitter emitter)
        {
            if (type.Kind == TypeKind.Delegate)
            {
                var delegateName = BridgeTypes.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.Output.Insert(0, comment.ToString() + newLine + newLine);
                }

                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.Decimal) ||
                type.IsKnownType(KnownTypeCode.Double) ||
                type.IsKnownType(KnownTypeCode.Int16) ||
                type.IsKnownType(KnownTypeCode.Int32) ||
                type.IsKnownType(KnownTypeCode.Int64) ||
                type.IsKnownType(KnownTypeCode.SByte) ||
                type.IsKnownType(KnownTypeCode.Single) ||
                type.IsKnownType(KnownTypeCode.UInt16) ||
                type.IsKnownType(KnownTypeCode.UInt32) ||
                type.IsKnownType(KnownTypeCode.UInt64))
            {
                return("number");
            }

            if (type.Kind == TypeKind.Array)
            {
                ICSharpCode.NRefactory.TypeSystem.ArrayType arrayType = (ICSharpCode.NRefactory.TypeSystem.ArrayType)type;
                return("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));
            }

            BridgeType bridgeType   = emitter.BridgeTypes.Get(type, true);
            string     name         = BridgeTypes.ConvertName(type.FullName);
            bool       isCustomName = false;

            if (bridgeType != null)
            {
                name = BridgeTypes.AddModule(name, bridgeType, out isCustomName);
            }

            if (!isCustomName && type.TypeArguments.Count > 0)
            {
                name += "$" + type.TypeArguments.Count;
            }

            return(name);
        }