diff --git a/converter.js b/converter.js index 5935cff..58a945b 100644 --- a/converter.js +++ b/converter.js @@ -113,9 +113,13 @@ const createConverter = config => { } else { rows.push(`export enum ${enum_.Identifier} {`); - entries.forEach(([key, value], i) => { + entries.forEach(([key, value]) => { if (config.numericEnums) { - rows.push(` ${key} = ${value != null ? value : i},`); + if (value == null) { + rows.push(` ${key},`); + } else { + rows.push(` ${key} = ${value != null ? value : i},`); + } } else { rows.push(` ${key} = '${getEnumStringValue(key)}',`); } diff --git a/lib/csharp-models-to-json/EnumCollector.cs b/lib/csharp-models-to-json/EnumCollector.cs index a446433..a1a37b5 100644 --- a/lib/csharp-models-to-json/EnumCollector.cs +++ b/lib/csharp-models-to-json/EnumCollector.cs @@ -5,13 +5,13 @@ namespace CSharpModelsToJson { - class Enum + public class Enum { public string Identifier { get; set; } public Dictionary Values { get; set; } } - class EnumCollector: CSharpSyntaxWalker + public class EnumCollector: CSharpSyntaxWalker { public readonly List Enums = new List(); diff --git a/lib/csharp-models-to-json_test/EnumCollector_test.cs b/lib/csharp-models-to-json_test/EnumCollector_test.cs new file mode 100644 index 0000000..b7e9883 --- /dev/null +++ b/lib/csharp-models-to-json_test/EnumCollector_test.cs @@ -0,0 +1,43 @@ +using System.Linq; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using NUnit.Framework; + +namespace CSharpModelsToJson.Tests +{ + [TestFixture] + public class EnumCollectorTest + { + [Test] + public void ReturnEnumWithMissingValues() + { + var tree = CSharpSyntaxTree.ParseText(@" + public enum SampleEnum + { + A, + B = 7, + C, + D = 4, + E + }" + ); + + var root = (CompilationUnitSyntax)tree.GetRoot(); + + var enumCollector = new EnumCollector(); + enumCollector.VisitEnumDeclaration(root.DescendantNodes().OfType().First()); + + var model = enumCollector.Enums.First(); + + Assert.That(model, Is.Not.Null); + Assert.That(model.Values, Is.Not.Null); + + var values = model.Values.ToArray(); + Assert.That(values[0].Value, Is.Null); + Assert.That(values[1].Value, Is.EqualTo("7")); + Assert.That(values[2].Value, Is.Null); + Assert.That(values[3].Value, Is.EqualTo("4")); + Assert.That(values[4].Value, Is.Null); + } + } +} \ No newline at end of file