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; } } }
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); }
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()); }