diff --git a/CS2TS.Tests/Arrays.cs b/CS2TS.Tests/Arrays.cs index bd53b14..617e984 100644 --- a/CS2TS.Tests/Arrays.cs +++ b/CS2TS.Tests/Arrays.cs @@ -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); } } } diff --git a/CS2TS.Tests/CS2TS.Tests.csproj b/CS2TS.Tests/CS2TS.Tests.csproj index d031b3a..1def41b 100644 --- a/CS2TS.Tests/CS2TS.Tests.csproj +++ b/CS2TS.Tests/CS2TS.Tests.csproj @@ -61,6 +61,7 @@ + diff --git a/CS2TS.Tests/ClassTypeMembers.cs b/CS2TS.Tests/ClassTypeMembers.cs index 1ddb5eb..7a67730 100644 --- a/CS2TS.Tests/ClassTypeMembers.cs +++ b/CS2TS.Tests/ClassTypeMembers.cs @@ -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); } } } diff --git a/CS2TS.Tests/DerivedTypes.cs b/CS2TS.Tests/DerivedTypes.cs index 9eb361a..a9d7f19 100644 --- a/CS2TS.Tests/DerivedTypes.cs +++ b/CS2TS.Tests/DerivedTypes.cs @@ -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); } } } diff --git a/CS2TS.Tests/EnumTests.cs b/CS2TS.Tests/EnumTests.cs new file mode 100644 index 0000000..ac95a2a --- /dev/null +++ b/CS2TS.Tests/EnumTests.cs @@ -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); + } + } +} diff --git a/CS2TS.Tests/PlainStruct.cs b/CS2TS.Tests/PlainStruct.cs index 56d5549..3918ace 100644 --- a/CS2TS.Tests/PlainStruct.cs +++ b/CS2TS.Tests/PlainStruct.cs @@ -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] @@ -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] @@ -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] @@ -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); } } } diff --git a/CS2TS/TypeCollector.cs b/CS2TS/TypeCollector.cs index 6e0be4e..41b501c 100644 --- a/CS2TS/TypeCollector.cs +++ b/CS2TS/TypeCollector.cs @@ -9,6 +9,7 @@ namespace CS2TS internal class TypeCollector : CSharpSyntaxWalker { public readonly List Types = new List(); + public readonly List Enums = new List(); public override void VisitClassDeclaration(ClassDeclarationSyntax node) { @@ -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); + } } } \ No newline at end of file diff --git a/CS2TS/TypeScriptEmitter.cs b/CS2TS/TypeScriptEmitter.cs index fc1512e..b7cf23c 100644 --- a/CS2TS/TypeScriptEmitter.cs +++ b/CS2TS/TypeScriptEmitter.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using System.Linq; using Microsoft.CodeAnalysis; @@ -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(); + 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(); + } } } \ No newline at end of file diff --git a/CS2TS/TypeScriptProcessor.cs b/CS2TS/TypeScriptProcessor.cs index 53e0b28..ffc0fd4 100644 --- a/CS2TS/TypeScriptProcessor.cs +++ b/CS2TS/TypeScriptProcessor.cs @@ -51,6 +51,10 @@ public void Write(TextWriter writer, bool declarations) { emitter.Emit(type); } + foreach (var e in collector.Enums) + { + emitter.Emit(e); + } } }