Skip to content

Commit

Permalink
Remove declare modifiers from interfaces and add support for enums.
Browse files Browse the repository at this point in the history
Enums only work if you are not generating a declarations file.
  • Loading branch information
alexperovich committed May 22, 2015
1 parent f5e1362 commit e6bb765
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 19 deletions.
4 changes: 2 additions & 2 deletions CS2TS.Tests/Arrays.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ namespace Foo

var processor = new TypeScriptProcessor(new []{input}, new[] {typeof(HashSet<>).Assembly.Location});
var output = processor.GetTypescriptAsString(generateDeclarations);
Assert.Equal(string.Format(@"{1} interface Test {{
Assert.Equal(string.Format(@"{1}interface Test {{
Prop?: {0}[];
}}
", expectedTypescriptType, generateDeclarations ? "declare" : "export"), output);
", expectedTypescriptType, generateDeclarations ? "" : "export "), output);
}
}
}
1 change: 1 addition & 0 deletions CS2TS.Tests/CS2TS.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Compile Include="Arrays.cs" />
<Compile Include="ClassTypeMembers.cs" />
<Compile Include="DerivedTypes.cs" />
<Compile Include="EnumTests.cs" />
<Compile Include="PlainStruct.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TypeInformation.cs" />
Expand Down
6 changes: 3 additions & 3 deletions CS2TS.Tests/ClassTypeMembers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ namespace Foo

var processor = new TypeScriptProcessor(input);
var output = processor.GetTypescriptAsString(generateDeclarations);
Assert.Equal(string.Format(@"{2} interface Test {{
Assert.Equal(string.Format(@"{2}interface Test {{
Prop{1}: {0};
}}
{2} interface Test2 {{
{2}interface Test2 {{
Prop{3}: Test;
}}
", typescriptType, cSharpType.ToLower().Contains("string") ? "?" : "", generateDeclarations ? "declare" : "export", typeType == "struct" ? "" : "?"), output);
", typescriptType, cSharpType.ToLower().Contains("string") ? "?" : "", generateDeclarations ? "" : "export ", typeType == "struct" ? "" : "?"), output);
}
}
}
8 changes: 4 additions & 4 deletions CS2TS.Tests/DerivedTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ public class Test3 : Test2

var processor = new TypeScriptProcessor(input);
var output = processor.GetTypescriptAsString(generateDeclarations);
Assert.Equal(string.Format(@"{2} interface Test {{
Assert.Equal(string.Format(@"{2}interface Test {{
[key: number]: {0};
}}
{2} interface Test2 {{
{2}interface Test2 {{
[key: string]: {0};
}}
{2} interface Test3 extends Test2 {{
{2}interface Test3 extends Test2 {{
Prop{1}: {0};
Dict?: {{ [key: string]: {0}; }};
}}
", typescriptType, cSharpType.ToLower().Contains("string") ? "?" : "", generateDeclarations ? "declare" : "export"), output);
", typescriptType, cSharpType.ToLower().Contains("string") ? "?" : "", generateDeclarations ? "" : "export "), output);
}
}
}
39 changes: 39 additions & 0 deletions CS2TS.Tests/EnumTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace CS2TS.Tests
{
public class EnumTests
{
[Fact]
public void Enum()
{
var input = @"
public enum Foo
{
Bar,
B,
Baz = 4,
Boo = 5,
Foo = 4,
A
}";
var processor = new TypeScriptProcessor(input);
var output = processor.GetTypescriptAsString(false);
Assert.Equal(@"export enum Foo {
Bar = 0,
B = 1,
Baz = 4,
Boo = 5,
Foo = 4,
A = 5
}
", output);
}
}
}
16 changes: 8 additions & 8 deletions CS2TS.Tests/PlainStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ namespace Foo

var processor = new TypeScriptProcessor(input);
var output = processor.GetTypescriptAsString(generateDeclarations);
Assert.Equal(string.Format(@"{2} interface Test {{
Assert.Equal(string.Format(@"{2}interface Test {{
Prop{1}: {0};
}}
", typescriptType, cSharpType.ToLower().Contains("string") ? "?" : "", generateDeclarations ? "declare" : "export"), output);
", typescriptType, cSharpType.ToLower().Contains("string") ? "?" : "", generateDeclarations ? "" : "export "), output);
}

[Theory]
Expand All @@ -70,12 +70,12 @@ namespace Foo

var processor = new TypeScriptProcessor(input);
var output = processor.GetTypescriptAsString(generateDeclarations);
Assert.Equal(string.Format(@"{2} interface Test {{
Assert.Equal(string.Format(@"{2}interface Test {{
x{1}: {0};
y{1}: {0};
}}
", typescriptType, cSharpType.ToLower().Contains("string") ? "?" : "", generateDeclarations ? "declare" : "export"), output);
", typescriptType, cSharpType.ToLower().Contains("string") ? "?" : "", generateDeclarations ? "" : "export "), output);
}

[Theory]
Expand All @@ -98,11 +98,11 @@ namespace Foo

var processor = new TypeScriptProcessor(new[] {input}, new[] {typeof (JsonPropertyAttribute).Assembly.Location});
var output = processor.GetTypescriptAsString(generateDeclarations);
Assert.Equal(string.Format(@"{2} interface Test {{
Assert.Equal(string.Format(@"{2}interface Test {{
foo{1}: {0};
}}
", typescriptType, cSharpType.ToLower().Contains("string") ? "?" : "", generateDeclarations ? "declare" : "export"), output);
", typescriptType, cSharpType.ToLower().Contains("string") ? "?" : "", generateDeclarations ? "" : "export "), output);
}

[Theory]
Expand All @@ -126,11 +126,11 @@ namespace Foo

var processor = new TypeScriptProcessor(new[] {input}, new[] {typeof (DataMemberAttribute).Assembly.Location});
var output = processor.GetTypescriptAsString(generateDeclarations);
Assert.Equal(string.Format(@"{2} interface Test {{
Assert.Equal(string.Format(@"{2}interface Test {{
foo{1}: {0};
}}
", typescriptType, cSharpType.ToLower().Contains("string") ? "?" : "", generateDeclarations ? "declare" : "export"), output);
", typescriptType, cSharpType.ToLower().Contains("string") ? "?" : "", generateDeclarations ? "" : "export "), output);
}
}
}
7 changes: 7 additions & 0 deletions CS2TS/TypeCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace CS2TS
internal class TypeCollector : CSharpSyntaxWalker
{
public readonly List<TypeDeclarationSyntax> Types = new List<TypeDeclarationSyntax>();
public readonly List<EnumDeclarationSyntax> Enums = new List<EnumDeclarationSyntax>();

public override void VisitClassDeclaration(ClassDeclarationSyntax node)
{
Expand All @@ -27,5 +28,11 @@ public override void VisitInterfaceDeclaration(InterfaceDeclarationSyntax node)
Types.Add(node);
base.VisitInterfaceDeclaration(node);
}

public override void VisitEnumDeclaration(EnumDeclarationSyntax node)
{
Enums.Add(node);
base.VisitEnumDeclaration(node);
}
}
}
27 changes: 25 additions & 2 deletions CS2TS/TypeScriptEmitter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -26,14 +27,36 @@ public void Emit(TypeDeclarationSyntax typeDeclarationSyntax)
var emitter = new TypeScriptMemberEmitter(propertyWriter, _semanticModel, _isDeclaration);
emitter.Visit(typeDeclarationSyntax);
_output.WriteLine(
"{1} interface {0}{2} {{",
"{1}interface {0}{2} {{",
typeDeclarationSyntax.Identifier.Text,
_isDeclaration ? "declare" : "export",
_isDeclaration ? "" : "export ",
emitter.Extends);
_output.Write(propertyWriter.ToString());
_output.WriteLine("}");
_output.WriteLine();
}
}

public void Emit(EnumDeclarationSyntax enumDeclarationSyntax)
{
if (_isDeclaration)
throw new InvalidOperationException();
_output.WriteLine("export enum {0} {{", enumDeclarationSyntax.Identifier.Text);
var currentValue = 0;
var members = new List<string>();
foreach (var member in enumDeclarationSyntax.Members)
{
var value = currentValue++;
if (member.EqualsValue != null)
{
value = (int) _semanticModel.GetConstantValue(member.EqualsValue.Value).Value;
currentValue = value + 1;
}
members.Add(string.Format(" {0} = {1}", member.Identifier.Text, value));
}
_output.WriteLine(string.Join(",\r\n", members));
_output.WriteLine("}");
_output.WriteLine();
}
}
}
4 changes: 4 additions & 0 deletions CS2TS/TypeScriptProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public void Write(TextWriter writer, bool declarations)
{
emitter.Emit(type);
}
foreach (var e in collector.Enums)
{
emitter.Emit(e);
}
}
}

Expand Down

0 comments on commit e6bb765

Please sign in to comment.