Skip to content
This repository has been archived by the owner on May 21, 2018. It is now read-only.

new feature: generate comments from C# source to the TS definitions #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions T4TS.Example/Models/Barfoo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@

namespace T4TS.Example.Models
{
/// <summary>
/// Barfoo has some comments!
/// <example>var bar = new Barfoo();</example>
/// </summary>
[TypeScriptInterface(Module = "")]
public class Barfoo
{
/// <summary>
/// Well, this is a number
/// And has multiple lines of comment
/// "Nicely" formated
/// </summary>
public int Number { get; set; }
/// <summary>
/// Okay, this has a single line of comment
/// </summary>
public Inherited Complex { get; set; }
public string Name { get; set; }
public DateTime DateTime { get; set; }
Expand Down
19 changes: 19 additions & 0 deletions T4TS.Example/T4TS.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,20 @@

// -- Begin global interfaces
/** Generated from T4TS.Example.Models.Barfoo **/
/** <summary>
* Barfoo has some comments!
* <example>var bar = new Barfoo();</example>
* </summary> */
interface Barfoo {
/** <summary>
* Well, this is a number
* And has multiple lines of comment
* "Nicely" formated
* </summary> */
Number: number;
/** <summary>
* Okay, this has a single line of comment
* </summary> */
Complex: T4TS.OverridenName;
Name: string;
DateTime: string;
Expand Down Expand Up @@ -111,6 +123,12 @@ declare module T4TS {
MyProperty: number;
SomeDateTime: string;
}
/** Generated from T4TS.Tests.Fixtures.Dictionary.DictionaryModel **/
export interface DictionaryModel {
IntKey: { [name: number]: T4TS.BasicModel};
StringKey: { [name: string]: T4TS.BasicModel};
[index: number]: T4TS.BasicModel;
}
/** Generated from T4TS.Tests.Fixtures.Enumerable.EnumerableModel **/
export interface EnumerableModel {
NormalProperty: number;
Expand All @@ -120,6 +138,7 @@ declare module T4TS {
InterfaceList: T4TS.BasicModel[];
DeepArray: number[][];
DeepList: number[][];
Generic: string[];
}
/** Generated from T4TS.Tests.Fixtures.ExternalProp.ExternalPropModel **/
export interface ExternalPropModel {
Expand Down
12 changes: 8 additions & 4 deletions T4TS/CodeTraverser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ private TypeScriptInterface BuildInterface(CodeClass codeClass, TypeScriptInterf
{
var tsInterface = new TypeScriptInterface
{
FullName = codeClass.FullName,
Name = GetInterfaceName(attributeValues)
FullName = codeClass.FullName,
Name = GetInterfaceName(attributeValues),
Comment = codeClass.Comment,
DocComment = codeClass.DocComment
};

TypescriptType indexedType;
Expand Down Expand Up @@ -209,8 +211,10 @@ private bool TryGetMember(CodeProperty property, TypeContext typeContext, out Ty
Optional = values.Optional,
Ignore = values.Ignore,
Type = (string.IsNullOrWhiteSpace(values.Type))
? typeContext.GetTypeScriptType(getter.Type)
: new InterfaceType(values.Type)
? typeContext.GetTypeScriptType(getter.Type)
: new InterfaceType(values.Type),
Comment = property.Comment,
DocComment = property.DocComment
};

if (member.Ignore)
Expand Down
134 changes: 69 additions & 65 deletions T4TS/Outputs/InterfaceOutputAppender.cs
Original file line number Diff line number Diff line change
@@ -1,65 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T4TS
{
public class InterfaceOutputAppender : OutputAppender<TypeScriptInterface>
{
private bool InGlobalModule { get; set; }

public InterfaceOutputAppender(StringBuilder output, int baseIndentation, Settings settings, bool inGlobalModule)
: base(output, baseIndentation, settings)
{
this.InGlobalModule = inGlobalModule;
}

public override void AppendOutput(TypeScriptInterface tsInterface)
{
BeginInterface(tsInterface);

AppendMembers(tsInterface);

if (tsInterface.IndexedType != null)
AppendIndexer(tsInterface);

EndInterface();
}

private void AppendMembers(TypeScriptInterface tsInterface)
{
var appender = new MemberOutputAppender(Output, BaseIndentation + 4, Settings);
foreach (var member in tsInterface.Members)
appender.AppendOutput(member);
}

private void BeginInterface(TypeScriptInterface tsInterface)
{
AppendIndentedLine("/** Generated from " + tsInterface.FullName + " **/");

if (InGlobalModule)
AppendIndented("interface " + tsInterface.Name);
else
AppendIndented("export interface " + tsInterface.Name);

if (tsInterface.Parent != null)
Output.Append(" extends " + (tsInterface.Parent.Module.IsGlobal ? "" : tsInterface.Parent.Module.QualifiedName + ".") + tsInterface.Parent.Name);

Output.AppendLine(" {");
}

private void EndInterface()
{
AppendIndentedLine("}");
}

private void AppendIndexer(TypeScriptInterface tsInterface)
{
AppendIndendation();
Output.AppendFormat(" [index: number]: {0};", tsInterface.IndexedType);
Output.AppendLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T4TS
{
public class InterfaceOutputAppender : OutputAppender<TypeScriptInterface>
{
private bool InGlobalModule { get; set; }

public InterfaceOutputAppender(StringBuilder output, int baseIndentation, Settings settings, bool inGlobalModule)
: base(output, baseIndentation, settings)
{
this.InGlobalModule = inGlobalModule;
}

public override void AppendOutput(TypeScriptInterface tsInterface)
{
BeginInterface(tsInterface);

AppendMembers(tsInterface);

if (tsInterface.IndexedType != null)
AppendIndexer(tsInterface);

EndInterface();
}

private void AppendMembers(TypeScriptInterface tsInterface)
{
var appender = new MemberOutputAppender(Output, BaseIndentation + 4, Settings);
foreach (var member in tsInterface.Members)
appender.AppendOutput(member);
}

private void BeginInterface(TypeScriptInterface tsInterface)
{
AppendIndentedLine("/** Generated from " + tsInterface.FullName + " **/");

//replace NewLine characters, so multiline comments will align nicely, and don't stick to the begining of the lines...
if(!string.IsNullOrWhiteSpace(tsInterface.Comment)) { AppendIndentedLine("/** " + tsInterface.Comment.Replace(Environment.NewLine, Environment.NewLine + new string(' ', BaseIndentation) + "* ") + " */"); }
if(!string.IsNullOrWhiteSpace(tsInterface.DocComment)) { AppendIndentedLine("/** " + tsInterface.DocComment.Replace(Environment.NewLine, Environment.NewLine + new string(' ', BaseIndentation) + "* ") + " */"); }

if (InGlobalModule)
AppendIndented("interface " + tsInterface.Name);
else
AppendIndented("export interface " + tsInterface.Name);

if (tsInterface.Parent != null)
Output.Append(" extends " + (tsInterface.Parent.Module.IsGlobal ? "" : tsInterface.Parent.Module.QualifiedName + ".") + tsInterface.Parent.Name);

Output.AppendLine(" {");
}

private void EndInterface()
{
AppendIndentedLine("}");
}

private void AppendIndexer(TypeScriptInterface tsInterface)
{
AppendIndendation();
Output.AppendFormat(" [index: number]: {0};", tsInterface.IndexedType);
Output.AppendLine();
}
}
}
7 changes: 5 additions & 2 deletions T4TS/Outputs/MemberOutputAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public MemberOutputAppender(StringBuilder output, int baseIndentation, Settings

public override void AppendOutput(TypeScriptInterfaceMember member)
{
//replace NewLine characters, so multiline comments will align nicely, and don't stick to the begining of the lines...
if(!string.IsNullOrWhiteSpace(member.Comment)) { AppendIndentedLine("/** " + member.Comment.Replace(Environment.NewLine, Environment.NewLine + new string(' ', BaseIndentation) + "* ") + " */"); }
if(!string.IsNullOrWhiteSpace(member.DocComment)) { AppendIndentedLine("/** " + member.DocComment.Replace(Environment.NewLine, Environment.NewLine + new string(' ', BaseIndentation) + "* ") + " */"); }

AppendIndendation();

bool isOptional = member.Optional;
Expand All @@ -26,8 +30,7 @@ public override void AppendOutput(TypeScriptInterfaceMember member)
type = "bool";
else
type = "boolean";
}

}
Output.AppendFormat("{0}{1}: {2}",
member.Name,
(isOptional ? "?" : ""),
Expand Down
17 changes: 16 additions & 1 deletion T4TS/Outputs/TypeScriptInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,22 @@ namespace T4TS
public class TypeScriptInterface
{
public string Name { get; set; }
public string FullName { get; set; }
public string FullName { get; set; }

public string Comment { get; set; }

private string _docComment;
public string DocComment {
get { return _docComment; }
set {
_docComment = value;
if(value == null) { return; }
//strip the '<doc> </doc>' xml tags
if(value.StartsWith("<doc>") && value.EndsWith("</doc>")) {
_docComment = value.Substring(5, value.Length - 11).Trim();
} else { _docComment = value; }
}
}

public List<TypeScriptInterfaceMember> Members { get; set; }
public TypescriptType IndexedType { get; set; }
Expand Down
17 changes: 16 additions & 1 deletion T4TS/Outputs/TypeScriptInterfaceMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,22 @@ namespace T4TS
{
public class TypeScriptInterfaceMember
{
public string Name { get; set; }
public string Name { get; set; }

public string Comment { get; set; }

private string _docComment;
public string DocComment {
get { return _docComment; }
set {
_docComment = value;
if(value == null) { return; }
//strip the '<doc> </doc>' xml tags
if(value.StartsWith("<doc>") && value.EndsWith("</doc>")) {
_docComment = value.Substring(5, value.Length - 11).Trim();
} else { _docComment = value; }
}
}
public TypescriptType Type { get; set; }
public bool Optional { get; set; }
//public string FullName { get; set; }
Expand Down