Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation Generation #132

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions src/TypedSignalR.Client.TypeScript/InterfaceTranspiler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Logging;
using Tapper;
Expand Down Expand Up @@ -90,6 +92,17 @@ private static void AddInterface(
ITypedSignalRTranspilationOptions options,
ref CodeWriter codeWriter)
{
var doc = GetDocumentationFromSymbol(interfaceSymbol);
var summaryList = doc?.GetElementsByTagName("summary");
var summary = summaryList?.Count > 0 ? summaryList[0]?.InnerText.Trim() : null;

if (!string.IsNullOrEmpty(summary))
{
codeWriter.AppendLine("/**");
codeWriter.AppendLine($"* {summary}");
codeWriter.AppendLine($"*/");
}

codeWriter.AppendLine($"export type {interfaceSymbol.Name} = {{");

foreach (var method in interfaceSymbol.GetMethods())
Expand All @@ -108,14 +121,30 @@ private static void AddInterface(

private static void WriteJSDoc(IMethodSymbol methodSymbol, ref CodeWriter codeWriter)
{
var doc = GetDocumentationFromSymbol(methodSymbol);

codeWriter.AppendLine(" /**");

// Write method summary if available
var summaryList = doc?.GetElementsByTagName("summary");
var summary = summaryList?.Count > 0 ? summaryList[0]?.InnerText.Trim() : null;

if (doc != null) codeWriter.AppendLine($" * {summary ?? "Documentation unavailable."}");
var parameterSummaries = doc?.GetElementsByTagName("param").Cast<XmlElement>().ToDictionary(x => x.GetAttribute("name"), x => x.InnerText.Trim()) ?? new();

foreach (var parameter in methodSymbol.Parameters)
{
codeWriter.AppendLine($" * @param {parameter.Name} Transpiled from {parameter.Type.ToDisplayString()}");
codeWriter.AppendLine(parameterSummaries.TryGetValue(parameter.Name, out string? paramSummary)
? $" * @param {parameter.Name} {paramSummary} (Transpiled from {parameter.Type.ToDisplayString()})"
: $" * @param {parameter.Name} Transpiled from {parameter.Type.ToDisplayString()}");
}

codeWriter.AppendLine($" * @returns Transpiled from {methodSymbol.ReturnType.ToDisplayString()}");
var returnList = doc?.GetElementsByTagName("returns");
var returnSummary = returnList?.Count > 0 ? returnList[0]?.InnerText.Trim() : null;

codeWriter.AppendLine(string.IsNullOrEmpty(returnSummary)
? $" * @returns Transpiled from {methodSymbol.ReturnType.ToDisplayString()}"
: $" * @returns {returnSummary} (Transpiled from {methodSymbol.ReturnType.ToDisplayString()})");
codeWriter.AppendLine(" */");
}

Expand Down Expand Up @@ -203,4 +232,22 @@ private static void WriteReturnType(

codeWriter.Append(TypeMapper.MapTo(returnType, options));
}

///-------------------------------------------------------------------------------------------------
/// <summary> Gets the XML documentation from a symbol. </summary>
///
/// <param name="symbol"> The symbol. </param>
///
/// <returns> The documentation from symbol. </returns>
///-------------------------------------------------------------------------------------------------
private static XmlDocument? GetDocumentationFromSymbol(ISymbol symbol)
{
var xmlDoc = symbol.GetDocumentationCommentXml();
if (string.IsNullOrEmpty(xmlDoc))
return null;

XmlDocument? doc = new();
doc.LoadXml(xmlDoc);
return doc;
}
}