From 98aa6bbb641447222736cc3e424f4173ef57f1e5 Mon Sep 17 00:00:00 2001 From: iperevoschikov Date: Thu, 14 Feb 2019 10:57:48 +0500 Subject: [PATCH 01/10] Typos fixes --- Common.DotSettings | 1 + .../DictionaryTypeBuildingContext.cs | 4 ++-- .../ListTypeBuildingContext.cs | 4 ++-- .../StringBuildingContext.cs | 4 ++-- TypeScript.ContractGenerator.sln.DotSettings | 1 + .../FlowTypeGenerator.cs | 18 +++++++++--------- .../TypeBuilders/ArrayTypeBuildingContext.cs | 6 +++--- .../TypeBuilders/BuildInTypeBuildingContext.cs | 6 +++--- .../CustomTypeTypeBuildingContextImpl.cs | 4 ++-- .../EnumTypeBuildingContextImpl.cs | 2 +- .../GenericParameterTypeBuildingContextImpl.cs | 4 ++-- .../GenericTypeTypeBuildingContextImpl.cs | 4 ++-- .../TypeBuilders/ITypeBuildingContext.cs | 4 ++-- .../TypeBuilders/TypeBuildingContext.cs | 13 +++++++------ 14 files changed, 39 insertions(+), 36 deletions(-) diff --git a/Common.DotSettings b/Common.DotSettings index 3db4f8b..a3d6674 100644 --- a/Common.DotSettings +++ b/Common.DotSettings @@ -170,6 +170,7 @@ True True True + True True True True diff --git a/TypeScript.ContractGenerator.Tests/CustomTypeGenerators/DictionaryTypeBuildingContext.cs b/TypeScript.ContractGenerator.Tests/CustomTypeGenerators/DictionaryTypeBuildingContext.cs index 0fe9724..ea83240 100644 --- a/TypeScript.ContractGenerator.Tests/CustomTypeGenerators/DictionaryTypeBuildingContext.cs +++ b/TypeScript.ContractGenerator.Tests/CustomTypeGenerators/DictionaryTypeBuildingContext.cs @@ -13,13 +13,13 @@ public DictionaryTypeBuildingContext(Type dictionaryType) valueType = dictionaryType.GetGenericArguments()[1]; } - public bool IsDefinitionBuilded => true; + public bool IsDefinitionBuilt => true; public void Initialize(ITypeGenerator typeGenerator) { } - public void BuildDefiniion(ITypeGenerator typeGenerator) + public void BuildDefinition(ITypeGenerator typeGenerator) { } diff --git a/TypeScript.ContractGenerator.Tests/CustomTypeGenerators/ListTypeBuildingContext.cs b/TypeScript.ContractGenerator.Tests/CustomTypeGenerators/ListTypeBuildingContext.cs index 0b6e767..6375d64 100644 --- a/TypeScript.ContractGenerator.Tests/CustomTypeGenerators/ListTypeBuildingContext.cs +++ b/TypeScript.ContractGenerator.Tests/CustomTypeGenerators/ListTypeBuildingContext.cs @@ -12,13 +12,13 @@ public ListTypeBuildingContext(Type listType) itemType = listType.GetGenericArguments()[0]; } - public bool IsDefinitionBuilded => true; + public bool IsDefinitionBuilt => true; public void Initialize(ITypeGenerator typeGenerator) { } - public void BuildDefiniion(ITypeGenerator typeGenerator) + public void BuildDefinition(ITypeGenerator typeGenerator) { } diff --git a/TypeScript.ContractGenerator.Tests/CustomTypeGenerators/StringBuildingContext.cs b/TypeScript.ContractGenerator.Tests/CustomTypeGenerators/StringBuildingContext.cs index a724601..0a9795c 100644 --- a/TypeScript.ContractGenerator.Tests/CustomTypeGenerators/StringBuildingContext.cs +++ b/TypeScript.ContractGenerator.Tests/CustomTypeGenerators/StringBuildingContext.cs @@ -5,13 +5,13 @@ namespace SkbKontur.TypeScript.ContractGenerator.Tests.CustomTypeGenerators { public class StringBuildingContext : ITypeBuildingContext { - public bool IsDefinitionBuilded => true; + public bool IsDefinitionBuilt => true; public void Initialize(ITypeGenerator typeGenerator) { } - public void BuildDefiniion(ITypeGenerator typeGenerator) + public void BuildDefinition(ITypeGenerator typeGenerator) { } diff --git a/TypeScript.ContractGenerator.sln.DotSettings b/TypeScript.ContractGenerator.sln.DotSettings index b97c6da..056172c 100644 --- a/TypeScript.ContractGenerator.sln.DotSettings +++ b/TypeScript.ContractGenerator.sln.DotSettings @@ -3,4 +3,5 @@ True True 1 + True \ No newline at end of file diff --git a/TypeScript.ContractGenerator/FlowTypeGenerator.cs b/TypeScript.ContractGenerator/FlowTypeGenerator.cs index aa356ec..0349562 100644 --- a/TypeScript.ContractGenerator/FlowTypeGenerator.cs +++ b/TypeScript.ContractGenerator/FlowTypeGenerator.cs @@ -23,12 +23,12 @@ public FlowTypeUnit[] Generate() { foreach (var type in rootTypes) RequestTypeBuild(type); - while (flowTypeDeclarations.Values.Any(x => !x.IsDefinitionBuilded)) + while (flowTypeDeclarations.Values.Any(x => !x.IsDefinitionBuilt)) { foreach (var currentType in flowTypeDeclarations.ToArray()) { - if (!currentType.Value.IsDefinitionBuilded) - currentType.Value.BuildDefiniion(this); + if (!currentType.Value.IsDefinitionBuilt) + currentType.Value.BuildDefinition(this); } } return flowTypeUnitFactory.Units; @@ -38,12 +38,12 @@ public void GenerateFiles(string targetPath) { foreach (var type in rootTypes) RequestTypeBuild(type); - while (flowTypeDeclarations.Values.Any(x => !x.IsDefinitionBuilded)) + while (flowTypeDeclarations.Values.Any(x => !x.IsDefinitionBuilt)) { foreach (var currentType in flowTypeDeclarations.ToArray()) { - if (!currentType.Value.IsDefinitionBuilded) - currentType.Value.BuildDefiniion(this); + if (!currentType.Value.IsDefinitionBuilt) + currentType.Value.BuildDefinition(this); } } FilesGenerator.GenerateFiles(targetPath, flowTypeUnitFactory); @@ -53,12 +53,12 @@ public void GenerateTypeScriptFiles(string targetPath) { foreach (var type in rootTypes) RequestTypeBuild(type); - while (flowTypeDeclarations.Values.Any(x => !x.IsDefinitionBuilded)) + while (flowTypeDeclarations.Values.Any(x => !x.IsDefinitionBuilt)) { foreach (var currentType in flowTypeDeclarations.ToArray()) { - if (!currentType.Value.IsDefinitionBuilded) - currentType.Value.BuildDefiniion(this); + if (!currentType.Value.IsDefinitionBuilt) + currentType.Value.BuildDefinition(this); } } FilesGenerator.DeleteFiles(targetPath, "*.js"); diff --git a/TypeScript.ContractGenerator/TypeBuilders/ArrayTypeBuildingContext.cs b/TypeScript.ContractGenerator/TypeBuilders/ArrayTypeBuildingContext.cs index 4c48f09..42b0581 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/ArrayTypeBuildingContext.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/ArrayTypeBuildingContext.cs @@ -1,4 +1,4 @@ -using System; +using System; using SkbKontur.TypeScript.ContractGenerator.CodeDom; @@ -11,13 +11,13 @@ public ArrayTypeBuildingContext(Type elementType) this.elementType = elementType; } - public bool IsDefinitionBuilded { get { return true; } } + public bool IsDefinitionBuilt { get { return true; } } public void Initialize(ITypeGenerator typeGenerator) { } - public void BuildDefiniion(ITypeGenerator typeGenerator) + public void BuildDefinition(ITypeGenerator typeGenerator) { } diff --git a/TypeScript.ContractGenerator/TypeBuilders/BuildInTypeBuildingContext.cs b/TypeScript.ContractGenerator/TypeBuilders/BuildInTypeBuildingContext.cs index 474bbe1..a8a355f 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/BuildInTypeBuildingContext.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/BuildInTypeBuildingContext.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using SkbKontur.TypeScript.ContractGenerator.CodeDom; @@ -38,13 +38,13 @@ public FlowTypeType ReferenceFrom(FlowTypeUnit targetUnit, ITypeGenerator typeGe throw new ArgumentOutOfRangeException(); } - public bool IsDefinitionBuilded { get { return true; } } + public bool IsDefinitionBuilt { get { return true; } } public void Initialize(ITypeGenerator typeGenerator) { } - public void BuildDefiniion(ITypeGenerator typeGenerator) + public void BuildDefinition(ITypeGenerator typeGenerator) { } diff --git a/TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContextImpl.cs b/TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContextImpl.cs index 1cec5d3..53595b4 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContextImpl.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContextImpl.cs @@ -16,7 +16,7 @@ public CustomTypeTypeBuildingContextImpl(FlowTypeUnit unit, Type type) { } - public override bool IsDefinitionBuilded => Declaration.Definition != null; + public override bool IsDefinitionBuilt => Declaration.Definition != null; private FlowTypeTypeDeclaration CreateComplexFlowTypeDeclarationWithoutDefintion(Type type) { @@ -39,7 +39,7 @@ public override void Initialize(ITypeGenerator typeGenerator) Unit.Body.Add(new FlowTypeExportTypeStatement {Declaration = Declaration}); } - public override void BuildDefiniion(ITypeGenerator typeGenerator) + public override void BuildDefinition(ITypeGenerator typeGenerator) { Declaration.Definition = CreateComplexFlowTypeDefintion(typeGenerator); } diff --git a/TypeScript.ContractGenerator/TypeBuilders/EnumTypeBuildingContextImpl.cs b/TypeScript.ContractGenerator/TypeBuilders/EnumTypeBuildingContextImpl.cs index a806be6..6bf8fce 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/EnumTypeBuildingContextImpl.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/EnumTypeBuildingContextImpl.cs @@ -5,7 +5,7 @@ namespace SkbKontur.TypeScript.ContractGenerator.TypeBuilders { - internal class EnumTypeBuildingContextImpl : TypeBuildingContext + public class EnumTypeBuildingContextImpl : TypeBuildingContext { public EnumTypeBuildingContextImpl(FlowTypeUnit unit, Type type) : base(unit, type) diff --git a/TypeScript.ContractGenerator/TypeBuilders/GenericParameterTypeBuildingContextImpl.cs b/TypeScript.ContractGenerator/TypeBuilders/GenericParameterTypeBuildingContextImpl.cs index 1607877..ca5affb 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/GenericParameterTypeBuildingContextImpl.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/GenericParameterTypeBuildingContextImpl.cs @@ -11,13 +11,13 @@ public GenericParameterTypeBuildingContextImpl(Type type) this.type = type; } - public bool IsDefinitionBuilded => true; + public bool IsDefinitionBuilt => true; public void Initialize(ITypeGenerator typeGenerator) { } - public void BuildDefiniion(ITypeGenerator typeGenerator) + public void BuildDefinition(ITypeGenerator typeGenerator) { } diff --git a/TypeScript.ContractGenerator/TypeBuilders/GenericTypeTypeBuildingContextImpl.cs b/TypeScript.ContractGenerator/TypeBuilders/GenericTypeTypeBuildingContextImpl.cs index 493b383..ec9d4e4 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/GenericTypeTypeBuildingContextImpl.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/GenericTypeTypeBuildingContextImpl.cs @@ -13,13 +13,13 @@ public GenericTypeTypeBuildingContextImpl(Type type) this.type = type; } - public bool IsDefinitionBuilded => true; + public bool IsDefinitionBuilt => true; public void Initialize(ITypeGenerator typeGenerator) { } - public void BuildDefiniion(ITypeGenerator typeGenerator) + public void BuildDefinition(ITypeGenerator typeGenerator) { } diff --git a/TypeScript.ContractGenerator/TypeBuilders/ITypeBuildingContext.cs b/TypeScript.ContractGenerator/TypeBuilders/ITypeBuildingContext.cs index 80324c6..857d8ff 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/ITypeBuildingContext.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/ITypeBuildingContext.cs @@ -4,9 +4,9 @@ namespace SkbKontur.TypeScript.ContractGenerator.TypeBuilders { public interface ITypeBuildingContext { - bool IsDefinitionBuilded { get; } + bool IsDefinitionBuilt { get; } void Initialize(ITypeGenerator typeGenerator); - void BuildDefiniion(ITypeGenerator typeGenerator); + void BuildDefinition(ITypeGenerator typeGenerator); FlowTypeType ReferenceFrom(FlowTypeUnit targetUnit, ITypeGenerator typeGenerator); } } \ No newline at end of file diff --git a/TypeScript.ContractGenerator/TypeBuilders/TypeBuildingContext.cs b/TypeScript.ContractGenerator/TypeBuilders/TypeBuildingContext.cs index 5149276..3fda2c3 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/TypeBuildingContext.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/TypeBuildingContext.cs @@ -6,7 +6,7 @@ namespace SkbKontur.TypeScript.ContractGenerator.TypeBuilders { public class TypeBuildingContext : ITypeBuildingContext { - public TypeBuildingContext(FlowTypeUnit unit, Type type) + protected TypeBuildingContext(FlowTypeUnit unit, Type type) { Unit = unit; Type = type; @@ -17,12 +17,13 @@ public virtual void Initialize(ITypeGenerator typeGenerator) Unit.Body.Add(new FlowTypeExportTypeStatement {Declaration = Declaration}); } - public FlowTypeTypeDeclaration Declaration { get; set; } - public FlowTypeUnit Unit { get; set; } - public Type Type { get; set; } - public virtual bool IsDefinitionBuilded { get { return true; } } + protected FlowTypeTypeDeclaration Declaration { get; set; } + protected FlowTypeUnit Unit { get; } + protected Type Type { get; } - public virtual void BuildDefiniion(ITypeGenerator typeGenerator) + public virtual bool IsDefinitionBuilt => true; + + public virtual void BuildDefinition(ITypeGenerator typeGenerator) { } From 4de146ab21a8d9452c9479241e0dedc16ab90e8c Mon Sep 17 00:00:00 2001 From: iperevoschikov Date: Thu, 14 Feb 2019 11:18:05 +0500 Subject: [PATCH 02/10] Convert all appliable to expression-bodied properties. Get rid off redundant Impl suffix in type builders --- TypeScript.ContractGenerator/Annotations.cs | 205 ++++++++++++++++++ .../CodeDom/FlowTypeClassDefinition.cs | 2 +- .../CodeDom/FlowTypeFunctionDefinition.cs | 4 +- .../CodeDom/FlowTypeFunctionType.cs | 2 +- .../CodeDom/FlowTypeInterfaceDefinition.cs | 2 +- .../FlowTypeInterfaceFunctionMember.cs | 2 +- .../CodeDom/FlowTypeMethodCallExpression.cs | 2 +- .../CodeDom/FlowTypeObjectLiteral.cs | 2 +- .../CodeDom/FlowTypeTypeDefintion.cs | 2 +- .../FlowTypeGenerator.cs | 10 +- TypeScript.ContractGenerator/FlowTypeUnit.cs | 10 +- .../DefaultFlowTypeGeneratorOutput.cs | 4 +- .../TypeBuilders/ArrayTypeBuildingContext.cs | 2 +- .../BuildInTypeBuildingContext.cs | 18 +- ...pl.cs => CustomTypeTypeBuildingContext.cs} | 21 +- ...textImpl.cs => EnumTypeBuildingContext.cs} | 4 +- ...=> GenericParameterTypeBuildingContext.cs} | 4 +- ...l.cs => GenericTypeTypeBuildingContext.cs} | 4 +- .../TypeBuilders/TypeBuildingContext.cs | 2 + 19 files changed, 255 insertions(+), 47 deletions(-) create mode 100644 TypeScript.ContractGenerator/Annotations.cs rename TypeScript.ContractGenerator/TypeBuilders/{CustomTypeTypeBuildingContextImpl.cs => CustomTypeTypeBuildingContext.cs} (81%) rename TypeScript.ContractGenerator/TypeBuilders/{EnumTypeBuildingContextImpl.cs => EnumTypeBuildingContext.cs} (92%) rename TypeScript.ContractGenerator/TypeBuilders/{GenericParameterTypeBuildingContextImpl.cs => GenericParameterTypeBuildingContext.cs} (81%) rename TypeScript.ContractGenerator/TypeBuilders/{GenericTypeTypeBuildingContextImpl.cs => GenericTypeTypeBuildingContext.cs} (93%) diff --git a/TypeScript.ContractGenerator/Annotations.cs b/TypeScript.ContractGenerator/Annotations.cs new file mode 100644 index 0000000..b500fde --- /dev/null +++ b/TypeScript.ContractGenerator/Annotations.cs @@ -0,0 +1,205 @@ +/* MIT License + +Copyright (c) 2016 JetBrains http://www.jetbrains.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. */ + +using System; + +#pragma warning disable 1591 +// ReSharper disable UnusedMember.Global +// ReSharper disable MemberCanBePrivate.Global +// ReSharper disable UnusedAutoPropertyAccessor.Global +// ReSharper disable IntroduceOptionalParameters.Global +// ReSharper disable MemberCanBeProtected.Global +// ReSharper disable InconsistentNaming +// ReSharper disable once CheckNamespace + +namespace JetBrains.Annotations +{ + /// + /// Indicates that the value of the marked element could be null sometimes, + /// so the check for null is necessary before its usage. + /// + /// + /// + /// [CanBeNull] object Test() => null; + /// + /// void UseTest() { + /// var p = Test(); + /// var s = p.ToString(); // Warning: Possible 'System.NullReferenceException' + /// } + /// + /// + [AttributeUsage( + AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property | + AttributeTargets.Delegate | AttributeTargets.Field | AttributeTargets.Event | + AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.GenericParameter)] + internal sealed class CanBeNullAttribute : Attribute + { + } + + /// + /// Indicates that the value of the marked element could never be null. + /// + /// + /// + /// [NotNull] object Foo() { + /// return null; // Warning: Possible 'null' assignment + /// } + /// + /// + [AttributeUsage( + AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property | + AttributeTargets.Delegate | AttributeTargets.Field | AttributeTargets.Event | + AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.GenericParameter)] + internal sealed class NotNullAttribute : Attribute + { + } + + /// + /// Can be appplied to symbols of types derived from IEnumerable as well as to symbols of Task + /// and Lazy classes to indicate that the value of a collection item, of the Task.Result property + /// or of the Lazy.Value property can never be null. + /// + [AttributeUsage( + AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property | + AttributeTargets.Delegate | AttributeTargets.Field)] + internal sealed class ItemNotNullAttribute : Attribute + { + } + + /// + /// Can be appplied to symbols of types derived from IEnumerable as well as to symbols of Task + /// and Lazy classes to indicate that the value of a collection item, of the Task.Result property + /// or of the Lazy.Value property can be null. + /// + [AttributeUsage( + AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property | + AttributeTargets.Delegate | AttributeTargets.Field)] + internal sealed class ItemCanBeNullAttribute : Attribute + { + } + + /// + /// Indicates that the marked symbol is used implicitly (e.g. via reflection, in external library), + /// so this symbol will not be marked as unused (as well as by other usage inspections). + /// + [AttributeUsage(AttributeTargets.All)] + internal sealed class UsedImplicitlyAttribute : Attribute + { + public UsedImplicitlyAttribute() + : this(ImplicitUseKindFlags.Default, ImplicitUseTargetFlags.Default) + { + } + + public UsedImplicitlyAttribute(ImplicitUseKindFlags useKindFlags) + : this(useKindFlags, ImplicitUseTargetFlags.Default) + { + } + + public UsedImplicitlyAttribute(ImplicitUseTargetFlags targetFlags) + : this(ImplicitUseKindFlags.Default, targetFlags) + { + } + + public UsedImplicitlyAttribute(ImplicitUseKindFlags useKindFlags, ImplicitUseTargetFlags targetFlags) + { + UseKindFlags = useKindFlags; + TargetFlags = targetFlags; + } + + public ImplicitUseKindFlags UseKindFlags { get; private set; } + + public ImplicitUseTargetFlags TargetFlags { get; private set; } + } + + /// + /// Should be used on attributes and causes ReSharper to not mark symbols marked with such attributes + /// as unused (as well as by other usage inspections) + /// + [AttributeUsage(AttributeTargets.Class | AttributeTargets.GenericParameter)] + internal sealed class MeansImplicitUseAttribute : Attribute + { + public MeansImplicitUseAttribute() + : this(ImplicitUseKindFlags.Default, ImplicitUseTargetFlags.Default) + { + } + + public MeansImplicitUseAttribute(ImplicitUseKindFlags useKindFlags) + : this(useKindFlags, ImplicitUseTargetFlags.Default) + { + } + + public MeansImplicitUseAttribute(ImplicitUseTargetFlags targetFlags) + : this(ImplicitUseKindFlags.Default, targetFlags) + { + } + + public MeansImplicitUseAttribute(ImplicitUseKindFlags useKindFlags, ImplicitUseTargetFlags targetFlags) + { + UseKindFlags = useKindFlags; + TargetFlags = targetFlags; + } + + [UsedImplicitly] + public ImplicitUseKindFlags UseKindFlags { get; private set; } + + [UsedImplicitly] + public ImplicitUseTargetFlags TargetFlags { get; private set; } + } + + [Flags] + internal enum ImplicitUseKindFlags + { + Default = Access | Assign | InstantiatedWithFixedConstructorSignature, + + /// Only entity marked with attribute considered used. + Access = 1, + + /// Indicates implicit assignment to a member. + Assign = 2, + + /// + /// Indicates implicit instantiation of a type with fixed constructor signature. + /// That means any unused constructor parameters won't be reported as such. + /// + InstantiatedWithFixedConstructorSignature = 4, + + /// Indicates implicit instantiation of a type. + InstantiatedNoFixedConstructorSignature = 8, + } + + /// + /// Specify what is considered used implicitly when marked + /// with or . + /// + [Flags] + internal enum ImplicitUseTargetFlags + { + Default = Itself, + Itself = 1, + + /// Members of entity marked with attribute are considered used. + Members = 2, + + /// Entity marked with attribute and all its members considered used. + WithMembers = Itself | Members + } +} \ No newline at end of file diff --git a/TypeScript.ContractGenerator/CodeDom/FlowTypeClassDefinition.cs b/TypeScript.ContractGenerator/CodeDom/FlowTypeClassDefinition.cs index 81259c9..d8eeefe 100644 --- a/TypeScript.ContractGenerator/CodeDom/FlowTypeClassDefinition.cs +++ b/TypeScript.ContractGenerator/CodeDom/FlowTypeClassDefinition.cs @@ -5,7 +5,7 @@ namespace SkbKontur.TypeScript.ContractGenerator.CodeDom { public class FlowTypeClassDefinition { - public List Members { get { return members; } } + public List Members => members; public FlowTypeType BaseClass { get; set; } diff --git a/TypeScript.ContractGenerator/CodeDom/FlowTypeFunctionDefinition.cs b/TypeScript.ContractGenerator/CodeDom/FlowTypeFunctionDefinition.cs index 4168313..bb24cc8 100644 --- a/TypeScript.ContractGenerator/CodeDom/FlowTypeFunctionDefinition.cs +++ b/TypeScript.ContractGenerator/CodeDom/FlowTypeFunctionDefinition.cs @@ -6,8 +6,8 @@ namespace SkbKontur.TypeScript.ContractGenerator.CodeDom { public class FlowTypeFunctionDefinition : FlowTypeDefinition { - public List Arguments { get { return arguments; } } - public List Body { get { return body; } } + public List Arguments => arguments; + public List Body => body; public FlowTypeType Result { get; set; } public bool IsAsync { get; set; } diff --git a/TypeScript.ContractGenerator/CodeDom/FlowTypeFunctionType.cs b/TypeScript.ContractGenerator/CodeDom/FlowTypeFunctionType.cs index 5e2a3cd..cb8a688 100644 --- a/TypeScript.ContractGenerator/CodeDom/FlowTypeFunctionType.cs +++ b/TypeScript.ContractGenerator/CodeDom/FlowTypeFunctionType.cs @@ -5,7 +5,7 @@ namespace SkbKontur.TypeScript.ContractGenerator.CodeDom { public class FlowTypeFunctionType : FlowTypeType { - public List Arguments { get { return arguments; } } + public List Arguments => arguments; public FlowTypeType Result { get; set; } public override string GenerateCode(ICodeGenerationContext context) diff --git a/TypeScript.ContractGenerator/CodeDom/FlowTypeInterfaceDefinition.cs b/TypeScript.ContractGenerator/CodeDom/FlowTypeInterfaceDefinition.cs index e14942b..cbabcff 100644 --- a/TypeScript.ContractGenerator/CodeDom/FlowTypeInterfaceDefinition.cs +++ b/TypeScript.ContractGenerator/CodeDom/FlowTypeInterfaceDefinition.cs @@ -5,7 +5,7 @@ namespace SkbKontur.TypeScript.ContractGenerator.CodeDom { public class FlowTypeInterfaceDefinition : FlowTypeType { - public List Members { get { return members; } } + public List Members => members; public override string GenerateCode(ICodeGenerationContext context) { diff --git a/TypeScript.ContractGenerator/CodeDom/FlowTypeInterfaceFunctionMember.cs b/TypeScript.ContractGenerator/CodeDom/FlowTypeInterfaceFunctionMember.cs index 75af361..6a9b1c0 100644 --- a/TypeScript.ContractGenerator/CodeDom/FlowTypeInterfaceFunctionMember.cs +++ b/TypeScript.ContractGenerator/CodeDom/FlowTypeInterfaceFunctionMember.cs @@ -6,7 +6,7 @@ namespace SkbKontur.TypeScript.ContractGenerator.CodeDom public class FlowTypeInterfaceFunctionMember { public string Name { get; set; } - public List Arguments { get { return arguments; } } + public List Arguments => arguments; public FlowTypeType Result { get; set; } public string GenerateCode(ICodeGenerationContext context) diff --git a/TypeScript.ContractGenerator/CodeDom/FlowTypeMethodCallExpression.cs b/TypeScript.ContractGenerator/CodeDom/FlowTypeMethodCallExpression.cs index de83429..4e37b3a 100644 --- a/TypeScript.ContractGenerator/CodeDom/FlowTypeMethodCallExpression.cs +++ b/TypeScript.ContractGenerator/CodeDom/FlowTypeMethodCallExpression.cs @@ -14,7 +14,7 @@ public FlowTypeMethodCallExpression(FlowTypeExpression subject, string methodNam public FlowTypeExpression Subject { get; set; } public string MethodName { get; set; } - public List Arguments { get { return arguments; } } + public List Arguments => arguments; public override string GenerateCode(ICodeGenerationContext context) { diff --git a/TypeScript.ContractGenerator/CodeDom/FlowTypeObjectLiteral.cs b/TypeScript.ContractGenerator/CodeDom/FlowTypeObjectLiteral.cs index 0c63a57..e8b8365 100644 --- a/TypeScript.ContractGenerator/CodeDom/FlowTypeObjectLiteral.cs +++ b/TypeScript.ContractGenerator/CodeDom/FlowTypeObjectLiteral.cs @@ -11,7 +11,7 @@ public FlowTypeObjectLiteral(IEnumerable prope this.properties = properties.ToList(); } - public List Properties { get { return properties; } } + public List Properties => properties; public override string GenerateCode(ICodeGenerationContext context) { diff --git a/TypeScript.ContractGenerator/CodeDom/FlowTypeTypeDefintion.cs b/TypeScript.ContractGenerator/CodeDom/FlowTypeTypeDefintion.cs index 2e4a782..9959fb3 100644 --- a/TypeScript.ContractGenerator/CodeDom/FlowTypeTypeDefintion.cs +++ b/TypeScript.ContractGenerator/CodeDom/FlowTypeTypeDefintion.cs @@ -5,7 +5,7 @@ namespace SkbKontur.TypeScript.ContractGenerator.CodeDom { public class FlowTypeTypeDefintion : FlowTypeType { - public List Members { get { return members; } } + public List Members => members; public override string GenerateCode(ICodeGenerationContext context) { diff --git a/TypeScript.ContractGenerator/FlowTypeGenerator.cs b/TypeScript.ContractGenerator/FlowTypeGenerator.cs index 0349562..166f09a 100644 --- a/TypeScript.ContractGenerator/FlowTypeGenerator.cs +++ b/TypeScript.ContractGenerator/FlowTypeGenerator.cs @@ -92,25 +92,25 @@ public ITypeBuildingContext ResolveType(Type type) if (type.IsEnum) { var targetUnit = flowTypeUnitFactory.GetOrCreateTypeUnit(typeLocation); - typeBuildingContext = new EnumTypeBuildingContextImpl(targetUnit, type); + typeBuildingContext = new EnumTypeBuildingContext(targetUnit, type); } if (type.IsGenericType && !type.IsGenericTypeDefinition) { - typeBuildingContext = new GenericTypeTypeBuildingContextImpl(type); + typeBuildingContext = new GenericTypeTypeBuildingContext(type); } if (type.IsGenericParameter) { - typeBuildingContext = new GenericParameterTypeBuildingContextImpl(type); + typeBuildingContext = new GenericParameterTypeBuildingContext(type); } if (type.IsGenericTypeDefinition) { var targetUnit = flowTypeUnitFactory.GetOrCreateTypeUnit(typeLocation); - typeBuildingContext = new CustomTypeTypeBuildingContextImpl(targetUnit, type); + typeBuildingContext = new CustomTypeTypeBuildingContext(targetUnit, type); } if (typeBuildingContext == null) { var targetUnit = flowTypeUnitFactory.GetOrCreateTypeUnit(typeLocation); - typeBuildingContext = new CustomTypeTypeBuildingContextImpl(targetUnit, type); + typeBuildingContext = new CustomTypeTypeBuildingContext(targetUnit, type); } } typeBuildingContext.Initialize(this); diff --git a/TypeScript.ContractGenerator/FlowTypeUnit.cs b/TypeScript.ContractGenerator/FlowTypeUnit.cs index d036526..92884b4 100644 --- a/TypeScript.ContractGenerator/FlowTypeUnit.cs +++ b/TypeScript.ContractGenerator/FlowTypeUnit.cs @@ -10,8 +10,10 @@ namespace SkbKontur.TypeScript.ContractGenerator public class FlowTypeUnit { public string Path { get; set; } - public IEnumerable Imports { get { return imports.Values; } } - public List Body { get { return body; } } + + public IEnumerable Imports => imports.Values; + + public List Body { get; } = new List(); public FlowTypeTypeReference AddTypeImport(Type sourceType, FlowTypeTypeDeclaration typeDeclaration, FlowTypeUnit sourceUnit) { @@ -56,7 +58,7 @@ public string GenerateCode(DefaultCodeGenerationContext context) } result.Append(context.NewLine); - foreach (var statement in body) + foreach (var statement in Body) { result.Append(statement.GenerateCode(context)).Append(context.NewLine); } @@ -66,7 +68,5 @@ public string GenerateCode(DefaultCodeGenerationContext context) private readonly Dictionary imports = new Dictionary(); private readonly Dictionary symbolImports = new Dictionary(); - - private readonly List body = new List(); } } \ No newline at end of file diff --git a/TypeScript.ContractGenerator/Internals/DefaultFlowTypeGeneratorOutput.cs b/TypeScript.ContractGenerator/Internals/DefaultFlowTypeGeneratorOutput.cs index 4c8201e..fb99fca 100644 --- a/TypeScript.ContractGenerator/Internals/DefaultFlowTypeGeneratorOutput.cs +++ b/TypeScript.ContractGenerator/Internals/DefaultFlowTypeGeneratorOutput.cs @@ -1,11 +1,11 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; namespace SkbKontur.TypeScript.ContractGenerator.Internals { internal class DefaultFlowTypeGeneratorOutput : IFlowTypeUnitFactory { - public FlowTypeUnit[] Units { get { return units.Values.ToArray(); } } + public FlowTypeUnit[] Units => units.Values.ToArray(); public FlowTypeUnit GetOrCreateTypeUnit(string path) { diff --git a/TypeScript.ContractGenerator/TypeBuilders/ArrayTypeBuildingContext.cs b/TypeScript.ContractGenerator/TypeBuilders/ArrayTypeBuildingContext.cs index 42b0581..10f72c0 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/ArrayTypeBuildingContext.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/ArrayTypeBuildingContext.cs @@ -11,7 +11,7 @@ public ArrayTypeBuildingContext(Type elementType) this.elementType = elementType; } - public bool IsDefinitionBuilt { get { return true; } } + public bool IsDefinitionBuilt => true; public void Initialize(ITypeGenerator typeGenerator) { diff --git a/TypeScript.ContractGenerator/TypeBuilders/BuildInTypeBuildingContext.cs b/TypeScript.ContractGenerator/TypeBuilders/BuildInTypeBuildingContext.cs index a8a355f..880ea35 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/BuildInTypeBuildingContext.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/BuildInTypeBuildingContext.cs @@ -21,24 +21,24 @@ public FlowTypeType ReferenceFrom(FlowTypeUnit targetUnit, ITypeGenerator typeGe { if (type == typeof(string)) return new FlowTypeBuildInType("string"); - else if (type == typeof(bool)) + if (type == typeof(bool)) return new FlowTypeBuildInType("boolean"); - else if (type == typeof(int)) + if (type == typeof(int)) return new FlowTypeBuildInType("number"); - else if (type == typeof(decimal)) + if (type == typeof(decimal)) return new FlowTypeBuildInType("number"); - else if (type == typeof(Int64)) + if (type == typeof(long)) return new FlowTypeBuildInType("string"); - else if (type == typeof(DateTime)) + if (type == typeof(DateTime)) return new FlowTypeBuildInType("(Date | string)"); - else if (type == typeof(byte[])) + if (type == typeof(byte[])) return new FlowTypeBuildInType("string"); - else if (type == typeof(void)) + if (type == typeof(void)) return new FlowTypeBuildInType("void"); throw new ArgumentOutOfRangeException(); } - public bool IsDefinitionBuilt { get { return true; } } + public bool IsDefinitionBuilt => true; public void Initialize(ITypeGenerator typeGenerator) { @@ -56,7 +56,7 @@ public void BuildDefinition(ITypeGenerator typeGenerator) typeof(bool), typeof(int), typeof(decimal), - typeof(Int64), + typeof(long), typeof(DateTime), typeof(void), typeof(byte[]), diff --git a/TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContextImpl.cs b/TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContext.cs similarity index 81% rename from TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContextImpl.cs rename to TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContext.cs index 53595b4..9e6b779 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContextImpl.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContext.cs @@ -1,24 +1,25 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; using SkbKontur.TypeScript.ContractGenerator.CodeDom; - using SkbKontur.TypeScript.ContractGenerator.Extensions; namespace SkbKontur.TypeScript.ContractGenerator.TypeBuilders { - public class CustomTypeTypeBuildingContextImpl : TypeBuildingContext + // ReSharper disable once ClassWithVirtualMembersNeverInherited.Global + public class CustomTypeTypeBuildingContext : TypeBuildingContext { - public CustomTypeTypeBuildingContextImpl(FlowTypeUnit unit, Type type) + public CustomTypeTypeBuildingContext(FlowTypeUnit unit, Type type) : base(unit, type) { } public override bool IsDefinitionBuilt => Declaration.Definition != null; - private FlowTypeTypeDeclaration CreateComplexFlowTypeDeclarationWithoutDefintion(Type type) + private FlowTypeTypeDeclaration CreateComplexFlowTypeDeclarationWithoutDefinition(Type type) { var result = new FlowTypeTypeDeclaration { @@ -35,16 +36,16 @@ public override void Initialize(ITypeGenerator typeGenerator) { typeGenerator.ResolveType(Type.BaseType); } - Declaration = CreateComplexFlowTypeDeclarationWithoutDefintion(Type); + Declaration = CreateComplexFlowTypeDeclarationWithoutDefinition(Type); Unit.Body.Add(new FlowTypeExportTypeStatement {Declaration = Declaration}); } public override void BuildDefinition(ITypeGenerator typeGenerator) { - Declaration.Definition = CreateComplexFlowTypeDefintion(typeGenerator); + Declaration.Definition = CreateComplexFlowTypeDefinition(typeGenerator); } - protected virtual FlowTypeTypeDefintion CreateComplexFlowTypeDefintion(ITypeGenerator typeGenerator) + protected virtual FlowTypeTypeDefintion CreateComplexFlowTypeDefinition(ITypeGenerator typeGenerator) { var result = new FlowTypeTypeDefintion(); var properties = CreateTypeProperties(Type); @@ -65,7 +66,7 @@ protected virtual FlowTypeTypeDefintion CreateComplexFlowTypeDefintion(ITypeGene return result; } - private FlowTypeUnionType OrNull(FlowTypeType buildAndImportType) + private static FlowTypeUnionType OrNull(FlowTypeType buildAndImportType) { return new FlowTypeUnionType( new[] @@ -76,12 +77,12 @@ private FlowTypeUnionType OrNull(FlowTypeType buildAndImportType) ); } - private string BuildPropertyName(string propertyName) + private static string BuildPropertyName(string propertyName) { return propertyName.ToLowerCamelCase(); } - protected virtual PropertyInfo[] CreateTypeProperties(Type type) + protected virtual IEnumerable CreateTypeProperties(Type type) { return type.GetProperties(BindingFlags.Instance | BindingFlags.Public); } diff --git a/TypeScript.ContractGenerator/TypeBuilders/EnumTypeBuildingContextImpl.cs b/TypeScript.ContractGenerator/TypeBuilders/EnumTypeBuildingContext.cs similarity index 92% rename from TypeScript.ContractGenerator/TypeBuilders/EnumTypeBuildingContextImpl.cs rename to TypeScript.ContractGenerator/TypeBuilders/EnumTypeBuildingContext.cs index 6bf8fce..094b674 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/EnumTypeBuildingContextImpl.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/EnumTypeBuildingContext.cs @@ -5,9 +5,9 @@ namespace SkbKontur.TypeScript.ContractGenerator.TypeBuilders { - public class EnumTypeBuildingContextImpl : TypeBuildingContext + public class EnumTypeBuildingContext : TypeBuildingContext { - public EnumTypeBuildingContextImpl(FlowTypeUnit unit, Type type) + public EnumTypeBuildingContext(FlowTypeUnit unit, Type type) : base(unit, type) { } diff --git a/TypeScript.ContractGenerator/TypeBuilders/GenericParameterTypeBuildingContextImpl.cs b/TypeScript.ContractGenerator/TypeBuilders/GenericParameterTypeBuildingContext.cs similarity index 81% rename from TypeScript.ContractGenerator/TypeBuilders/GenericParameterTypeBuildingContextImpl.cs rename to TypeScript.ContractGenerator/TypeBuilders/GenericParameterTypeBuildingContext.cs index ca5affb..c5b2ffb 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/GenericParameterTypeBuildingContextImpl.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/GenericParameterTypeBuildingContext.cs @@ -4,9 +4,9 @@ namespace SkbKontur.TypeScript.ContractGenerator.TypeBuilders { - public class GenericParameterTypeBuildingContextImpl : ITypeBuildingContext + public class GenericParameterTypeBuildingContext : ITypeBuildingContext { - public GenericParameterTypeBuildingContextImpl(Type type) + public GenericParameterTypeBuildingContext(Type type) { this.type = type; } diff --git a/TypeScript.ContractGenerator/TypeBuilders/GenericTypeTypeBuildingContextImpl.cs b/TypeScript.ContractGenerator/TypeBuilders/GenericTypeTypeBuildingContext.cs similarity index 93% rename from TypeScript.ContractGenerator/TypeBuilders/GenericTypeTypeBuildingContextImpl.cs rename to TypeScript.ContractGenerator/TypeBuilders/GenericTypeTypeBuildingContext.cs index ec9d4e4..cdc9ea1 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/GenericTypeTypeBuildingContextImpl.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/GenericTypeTypeBuildingContext.cs @@ -6,9 +6,9 @@ namespace SkbKontur.TypeScript.ContractGenerator.TypeBuilders { - public class GenericTypeTypeBuildingContextImpl : ITypeBuildingContext + public class GenericTypeTypeBuildingContext : ITypeBuildingContext { - public GenericTypeTypeBuildingContextImpl(Type type) + public GenericTypeTypeBuildingContext(Type type) { this.type = type; } diff --git a/TypeScript.ContractGenerator/TypeBuilders/TypeBuildingContext.cs b/TypeScript.ContractGenerator/TypeBuilders/TypeBuildingContext.cs index 3fda2c3..6c4babd 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/TypeBuildingContext.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/TypeBuildingContext.cs @@ -18,7 +18,9 @@ public virtual void Initialize(ITypeGenerator typeGenerator) } protected FlowTypeTypeDeclaration Declaration { get; set; } + protected FlowTypeUnit Unit { get; } + protected Type Type { get; } public virtual bool IsDefinitionBuilt => true; From 046fdd19f39cfa190a10462eebaac03f8d22a2c9 Mon Sep 17 00:00:00 2001 From: iperevoschikov Date: Thu, 14 Feb 2019 11:53:46 +0500 Subject: [PATCH 03/10] Generate typescript files with .ts extension --- .../{CommonType.tsx => CommonType.ts} | 0 ...ingRootType.tsx => CommonUsingRootType.ts} | 0 ...gRootType2.tsx => CommonUsingRootType2.ts} | 0 ...gRootType3.tsx => CommonUsingRootType3.ts} | 0 .../{CommonType.tsx => CommonType.ts} | 0 ...ingRootType.tsx => CommonUsingRootType.ts} | 0 ...gRootType2.tsx => CommonUsingRootType2.ts} | 0 ...gRootType3.tsx => CommonUsingRootType3.ts} | 0 .../TypeScript.ContractGenerator.Tests.csproj | 32 +++++++++---------- TypeScript.ContractGenerator.sln.DotSettings | 1 + .../Internals/FilesGenerator.cs | 17 ++++++++-- 11 files changed, 31 insertions(+), 19 deletions(-) rename TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/{CommonType.tsx => CommonType.ts} (100%) rename TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/{CommonUsingRootType.tsx => CommonUsingRootType.ts} (100%) rename TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/{CommonUsingRootType2.tsx => CommonUsingRootType2.ts} (100%) rename TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/{CommonUsingRootType3.tsx => CommonUsingRootType3.ts} (100%) rename TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonType/{CommonType.tsx => CommonType.ts} (100%) rename TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonUsingRootType/{CommonUsingRootType.tsx => CommonUsingRootType.ts} (100%) rename TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonUsingRootType2/{CommonUsingRootType2.tsx => CommonUsingRootType2.ts} (100%) rename TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonUsingRootType3/{CommonUsingRootType3.tsx => CommonUsingRootType3.ts} (100%) diff --git a/TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/CommonType.tsx b/TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/CommonType.ts similarity index 100% rename from TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/CommonType.tsx rename to TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/CommonType.ts diff --git a/TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/CommonUsingRootType.tsx b/TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/CommonUsingRootType.ts similarity index 100% rename from TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/CommonUsingRootType.tsx rename to TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/CommonUsingRootType.ts diff --git a/TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/CommonUsingRootType2.tsx b/TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/CommonUsingRootType2.ts similarity index 100% rename from TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/CommonUsingRootType2.tsx rename to TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/CommonUsingRootType2.ts diff --git a/TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/CommonUsingRootType3.tsx b/TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/CommonUsingRootType3.ts similarity index 100% rename from TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/CommonUsingRootType3.tsx rename to TypeScript.ContractGenerator.Tests/Files/FlatTypeLocator.Expected/TypeScript/CommonUsingRootType3.ts diff --git a/TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonType/CommonType.tsx b/TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonType/CommonType.ts similarity index 100% rename from TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonType/CommonType.tsx rename to TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonType/CommonType.ts diff --git a/TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonUsingRootType/CommonUsingRootType.tsx b/TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonUsingRootType/CommonUsingRootType.ts similarity index 100% rename from TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonUsingRootType/CommonUsingRootType.tsx rename to TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonUsingRootType/CommonUsingRootType.ts diff --git a/TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonUsingRootType2/CommonUsingRootType2.tsx b/TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonUsingRootType2/CommonUsingRootType2.ts similarity index 100% rename from TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonUsingRootType2/CommonUsingRootType2.tsx rename to TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonUsingRootType2/CommonUsingRootType2.ts diff --git a/TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonUsingRootType3/CommonUsingRootType3.tsx b/TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonUsingRootType3/CommonUsingRootType3.ts similarity index 100% rename from TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonUsingRootType3/CommonUsingRootType3.tsx rename to TypeScript.ContractGenerator.Tests/Files/SimpleStructureTypeLocator.Expected/TypeScript/CommonUsingRootType3/CommonUsingRootType3.ts diff --git a/TypeScript.ContractGenerator.Tests/TypeScript.ContractGenerator.Tests.csproj b/TypeScript.ContractGenerator.Tests/TypeScript.ContractGenerator.Tests.csproj index 0b52a2a..6b5811b 100644 --- a/TypeScript.ContractGenerator.Tests/TypeScript.ContractGenerator.Tests.csproj +++ b/TypeScript.ContractGenerator.Tests/TypeScript.ContractGenerator.Tests.csproj @@ -20,6 +20,14 @@ + + + + + + + + Always @@ -59,20 +67,16 @@ Always - - + Always - - + Always - - + Always - - + Always @@ -163,20 +167,16 @@ Always - - + Always - - + Always - - + Always - - + Always diff --git a/TypeScript.ContractGenerator.sln.DotSettings b/TypeScript.ContractGenerator.sln.DotSettings index 056172c..67dfc9f 100644 --- a/TypeScript.ContractGenerator.sln.DotSettings +++ b/TypeScript.ContractGenerator.sln.DotSettings @@ -4,4 +4,5 @@ True 1 True + True \ No newline at end of file diff --git a/TypeScript.ContractGenerator/Internals/FilesGenerator.cs b/TypeScript.ContractGenerator/Internals/FilesGenerator.cs index b5f4284..789a3fc 100644 --- a/TypeScript.ContractGenerator/Internals/FilesGenerator.cs +++ b/TypeScript.ContractGenerator/Internals/FilesGenerator.cs @@ -20,7 +20,9 @@ public static void GenerateFiles(string targetDir, DefaultFlowTypeGeneratorOutpu foreach (var unit in output.Units) { var targetFileName = Path.Combine(targetDir, unit.Path + ".js"); - Directory.CreateDirectory(Path.GetDirectoryName(targetFileName)); + + EnsureDirectoryExists(targetFileName); + File.WriteAllText(targetFileName, "// @flow" + "\n"); File.AppendAllText(targetFileName, generatedContentMarkerString + "\n"); File.AppendAllText(targetFileName, unit.GenerateCode(new DefaultCodeGenerationContext(JavaScriptTypeChecker.Flow))); @@ -32,14 +34,23 @@ public static void GenerateTypeScriptFiles(string targetDir, DefaultFlowTypeGene Directory.CreateDirectory(targetDir); foreach (var unit in output.Units) { - var targetFileName = Path.Combine(targetDir, unit.Path + ".tsx"); - Directory.CreateDirectory(Path.GetDirectoryName(targetFileName)); + var targetFileName = Path.Combine(targetDir, unit.Path + ".ts"); + + EnsureDirectoryExists(targetFileName); + File.AppendAllText(targetFileName, generatedContentMarkerString + "\n"); File.AppendAllText(targetFileName, "// tslint:disable" + "\n"); File.AppendAllText(targetFileName, unit.GenerateCode(new DefaultCodeGenerationContext(JavaScriptTypeChecker.TypeScript))); } } + private static void EnsureDirectoryExists(string targetFileName) + { + var targetDirectoryName = Path.GetDirectoryName(targetFileName); + if (!string.IsNullOrEmpty(targetDirectoryName)) + Directory.CreateDirectory(targetDirectoryName); + } + public static void DeleteFiles(string targetDir, string searchPattern) { var files = Directory.GetFiles(targetDir, searchPattern, SearchOption.AllDirectories); From 2b25e251883a85993f9d364a5911727fc45405c3 Mon Sep 17 00:00:00 2001 From: iperevoschikov Date: Thu, 14 Feb 2019 12:03:55 +0500 Subject: [PATCH 04/10] make js and ts files extensions constants --- TypeScript.ContractGenerator/FlowTypeGenerator.cs | 4 ++-- .../Internals/FilesGenerator.cs | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/TypeScript.ContractGenerator/FlowTypeGenerator.cs b/TypeScript.ContractGenerator/FlowTypeGenerator.cs index 166f09a..fa682c0 100644 --- a/TypeScript.ContractGenerator/FlowTypeGenerator.cs +++ b/TypeScript.ContractGenerator/FlowTypeGenerator.cs @@ -61,8 +61,8 @@ public void GenerateTypeScriptFiles(string targetPath) currentType.Value.BuildDefinition(this); } } - FilesGenerator.DeleteFiles(targetPath, "*.js"); - FilesGenerator.DeleteFiles(targetPath, "*.tsx"); + FilesGenerator.DeleteFiles(targetPath, $"*.{FilesGenerator.JavaScriptFilesExtension}"); + FilesGenerator.DeleteFiles(targetPath, $"*.{FilesGenerator.TypeScriptFilesExtension}"); FilesGenerator.GenerateTypeScriptFiles(targetPath, flowTypeUnitFactory); } diff --git a/TypeScript.ContractGenerator/Internals/FilesGenerator.cs b/TypeScript.ContractGenerator/Internals/FilesGenerator.cs index 789a3fc..6109843 100644 --- a/TypeScript.ContractGenerator/Internals/FilesGenerator.cs +++ b/TypeScript.ContractGenerator/Internals/FilesGenerator.cs @@ -6,10 +6,13 @@ namespace SkbKontur.TypeScript.ContractGenerator.Internals { internal static class FilesGenerator { + public const string JavaScriptFilesExtension = "js"; + public const string TypeScriptFilesExtension = "ts"; + public static void GenerateFiles(string targetDir, DefaultFlowTypeGeneratorOutput output) { Directory.CreateDirectory(targetDir); - var files = Directory.GetFiles(targetDir, "*.js", SearchOption.AllDirectories); + var files = Directory.GetFiles(targetDir, $"*.{JavaScriptFilesExtension}", SearchOption.AllDirectories); foreach (var file in files) { if (File.ReadAllText(file).Contains(generatedContentMarkerString)) @@ -19,7 +22,7 @@ public static void GenerateFiles(string targetDir, DefaultFlowTypeGeneratorOutpu } foreach (var unit in output.Units) { - var targetFileName = Path.Combine(targetDir, unit.Path + ".js"); + var targetFileName = GetUnitTargetFileName(targetDir, unit, JavaScriptFilesExtension); EnsureDirectoryExists(targetFileName); @@ -34,7 +37,7 @@ public static void GenerateTypeScriptFiles(string targetDir, DefaultFlowTypeGene Directory.CreateDirectory(targetDir); foreach (var unit in output.Units) { - var targetFileName = Path.Combine(targetDir, unit.Path + ".ts"); + var targetFileName = GetUnitTargetFileName(targetDir, unit, TypeScriptFilesExtension); EnsureDirectoryExists(targetFileName); @@ -44,6 +47,12 @@ public static void GenerateTypeScriptFiles(string targetDir, DefaultFlowTypeGene } } + private static string GetUnitTargetFileName(string targetDir, FlowTypeUnit unit, string fileExtension) + { + var targetFileName = Path.Combine(targetDir, unit.Path + $".{fileExtension}"); + return targetFileName; + } + private static void EnsureDirectoryExists(string targetFileName) { var targetDirectoryName = Path.GetDirectoryName(targetFileName); From 13c7db1be5e4ff4af878841de8462379b8279edb Mon Sep 17 00:00:00 2001 From: iperevoschikov Date: Thu, 14 Feb 2019 15:52:36 +0500 Subject: [PATCH 05/10] Two enum generation modes: enum or fixed strings range; Toggle options for explicit nullablility and optional properties --- ...enerator.cs => TestCustomTypeGenerator.cs} | 2 +- .../EndToEndTests.cs | 2 +- .../FlowTypeTestBase.cs | 8 ++--- .../GenericTypesTest.cs | 2 +- .../CodeDom/JavaScriptEnumDeclaration.cs | 15 +++++++++ .../CodeDom/JavaScriptEnumDefinition.cs | 27 ++++++++++++++++ .../CodeDom/JavaScriptEnumMember.cs | 18 +++++++++++ .../CustomTypeGenerator.cs | 9 ++++++ .../EnumGenerationMode.cs | 8 +++++ .../FlowTypeGenerationOptions.cs | 13 ++++++++ .../FlowTypeGenerator.cs | 24 ++++++++------ .../FlowTypeGeneratorHelpers.cs | 4 +-- .../IRootTypesProvider.cs | 12 +++++++ .../Internals/FilesExtensions.cs | 8 +++++ .../Internals/FilesGenerator.cs | 9 ++---- .../RootTypesProvider.cs | 25 +++++++++++++++ .../CustomTypeTypeBuildingContext.cs | 11 +++++-- ...tringsAndDictionaryTypeBuildingContext.cs} | 4 +-- .../TypeScriptEnumTypeBuildingContext.cs | 32 +++++++++++++++++++ 19 files changed, 204 insertions(+), 29 deletions(-) rename TypeScript.ContractGenerator.Tests/CustomTypeGenerators/{CustomTypeGenerator.cs => TestCustomTypeGenerator.cs} (93%) create mode 100644 TypeScript.ContractGenerator/CodeDom/JavaScriptEnumDeclaration.cs create mode 100644 TypeScript.ContractGenerator/CodeDom/JavaScriptEnumDefinition.cs create mode 100644 TypeScript.ContractGenerator/CodeDom/JavaScriptEnumMember.cs create mode 100644 TypeScript.ContractGenerator/CustomTypeGenerator.cs create mode 100644 TypeScript.ContractGenerator/EnumGenerationMode.cs create mode 100644 TypeScript.ContractGenerator/FlowTypeGenerationOptions.cs create mode 100644 TypeScript.ContractGenerator/IRootTypesProvider.cs create mode 100644 TypeScript.ContractGenerator/Internals/FilesExtensions.cs create mode 100644 TypeScript.ContractGenerator/RootTypesProvider.cs rename TypeScript.ContractGenerator/TypeBuilders/{EnumTypeBuildingContext.cs => FixedStringsAndDictionaryTypeBuildingContext.cs} (90%) create mode 100644 TypeScript.ContractGenerator/TypeBuilders/TypeScriptEnumTypeBuildingContext.cs diff --git a/TypeScript.ContractGenerator.Tests/CustomTypeGenerators/CustomTypeGenerator.cs b/TypeScript.ContractGenerator.Tests/CustomTypeGenerators/TestCustomTypeGenerator.cs similarity index 93% rename from TypeScript.ContractGenerator.Tests/CustomTypeGenerators/CustomTypeGenerator.cs rename to TypeScript.ContractGenerator.Tests/CustomTypeGenerators/TestCustomTypeGenerator.cs index 01d48cd..66c71dd 100644 --- a/TypeScript.ContractGenerator.Tests/CustomTypeGenerators/CustomTypeGenerator.cs +++ b/TypeScript.ContractGenerator.Tests/CustomTypeGenerators/TestCustomTypeGenerator.cs @@ -5,7 +5,7 @@ namespace SkbKontur.TypeScript.ContractGenerator.Tests.CustomTypeGenerators { - public class CustomTypeGenerator : ICustomTypeGenerator + public class TestCustomTypeGenerator : ICustomTypeGenerator { public string GetTypeLocation(Type type) { diff --git a/TypeScript.ContractGenerator.Tests/EndToEndTests.cs b/TypeScript.ContractGenerator.Tests/EndToEndTests.cs index 4ff331d..924bd27 100644 --- a/TypeScript.ContractGenerator.Tests/EndToEndTests.cs +++ b/TypeScript.ContractGenerator.Tests/EndToEndTests.cs @@ -49,7 +49,7 @@ public void GenerateFilesTest(Type type) [TestCase(typeof(ArrayRootType), "array-types.expected")] public void CustomGeneratorTest(Type rootType, string expectedFileName) { - var generatedCode = GenerateCode(new CustomTypeGenerator(), rootType).Single().Replace("\r\n", "\n"); + var generatedCode = GenerateCode(new TestCustomTypeGenerator(), rootType).Single().Replace("\r\n", "\n"); var expectedCode = File.ReadAllText(GetFilePath($"CustomGenerator/{expectedFileName}")).Replace("\r\n", "\n"); generatedCode.Should().Be(expectedCode); } diff --git a/TypeScript.ContractGenerator.Tests/FlowTypeTestBase.cs b/TypeScript.ContractGenerator.Tests/FlowTypeTestBase.cs index 57a2f75..7440c9a 100644 --- a/TypeScript.ContractGenerator.Tests/FlowTypeTestBase.cs +++ b/TypeScript.ContractGenerator.Tests/FlowTypeTestBase.cs @@ -18,17 +18,17 @@ public abstract class FlowTypeTestBase protected FlowTypeTestBase(JavaScriptTypeChecker javaScriptTypeChecker) { this.javaScriptTypeChecker = javaScriptTypeChecker; - fileExtension = javaScriptTypeChecker == JavaScriptTypeChecker.TypeScript ? "ts" : "js"; + fileExtension = javaScriptTypeChecker == JavaScriptTypeChecker.TypeScript ? FilesExtensions.TypeScriptFilesExtension : FilesExtensions.JavaScriptFilesExtension; } protected string[] GenerateCode(Type rootType) { - return GenerateCode(null, rootType); + return GenerateCode(CustomTypeGenerator.Null, rootType); } protected string[] GenerateCode(ICustomTypeGenerator customTypeGenerator, Type rootType) { - var generator = new FlowTypeGenerator(customTypeGenerator, new[] {rootType}); + var generator = new FlowTypeGenerator(FlowTypeGenerationOptions.Default, customTypeGenerator, new RootTypesProvider(rootType)); return generator.Generate().Select(x => x.GenerateCode(new DefaultCodeGenerationContext(javaScriptTypeChecker))).ToArray(); } @@ -39,7 +39,7 @@ protected void GenerateFiles(ICustomTypeGenerator customTypeGenerator, string fo Directory.Delete(path, recursive : true); Directory.CreateDirectory(path); - var generator = new FlowTypeGenerator(customTypeGenerator, rootTypes); + var generator = new FlowTypeGenerator(FlowTypeGenerationOptions.Default, customTypeGenerator, new RootTypesProvider(rootTypes)); if (javaScriptTypeChecker == JavaScriptTypeChecker.Flow) generator.GenerateFiles(path); else diff --git a/TypeScript.ContractGenerator.Tests/GenericTypesTest.cs b/TypeScript.ContractGenerator.Tests/GenericTypesTest.cs index e6c88d5..cf08e2b 100644 --- a/TypeScript.ContractGenerator.Tests/GenericTypesTest.cs +++ b/TypeScript.ContractGenerator.Tests/GenericTypesTest.cs @@ -11,7 +11,7 @@ public class GenericTypesTest [Test] public void RootCantBeGenericType() { - new FlowTypeGenerator(null, new[] {typeof(GenericRootType)}).Generate().Should().BeEmpty(); + new FlowTypeGenerator(FlowTypeGenerationOptions.Default, CustomTypeGenerator.Null, new RootTypesProvider(typeof(GenericRootType))).Generate().Should().BeEmpty(); } } } \ No newline at end of file diff --git a/TypeScript.ContractGenerator/CodeDom/JavaScriptEnumDeclaration.cs b/TypeScript.ContractGenerator/CodeDom/JavaScriptEnumDeclaration.cs new file mode 100644 index 0000000..e0aec4c --- /dev/null +++ b/TypeScript.ContractGenerator/CodeDom/JavaScriptEnumDeclaration.cs @@ -0,0 +1,15 @@ +using System.Text; + +namespace SkbKontur.TypeScript.ContractGenerator.CodeDom +{ + public class JavaScriptEnumDeclaration : FlowTypeTypeDeclaration + { + public override string GenerateCode(ICodeGenerationContext context) + { + var result = new StringBuilder(); + result.AppendFormat("enum {0} ", Name); + result.Append(Definition.GenerateCode(context)); + return result.ToString(); + } + } +} \ No newline at end of file diff --git a/TypeScript.ContractGenerator/CodeDom/JavaScriptEnumDefinition.cs b/TypeScript.ContractGenerator/CodeDom/JavaScriptEnumDefinition.cs new file mode 100644 index 0000000..150e443 --- /dev/null +++ b/TypeScript.ContractGenerator/CodeDom/JavaScriptEnumDefinition.cs @@ -0,0 +1,27 @@ +using System.Collections.Generic; +using System.Text; + +namespace SkbKontur.TypeScript.ContractGenerator.CodeDom +{ + public class JavaScriptEnumDefinition : FlowTypeType + { + public JavaScriptEnumDefinition(IEnumerable members) + { + Members.AddRange(members); + } + + public List Members { get; } = new List(); + + public override string GenerateCode(ICodeGenerationContext context) + { + var result = new StringBuilder(); + result.AppendFormat("{{").Append(context.NewLine); + foreach (var member in Members) + { + result.AppendWithTab(context.Tab, member.GenerateCode(context) + ",", context.NewLine).Append(context.NewLine); + } + result.Append("}"); + return result.ToString(); + } + } +} \ No newline at end of file diff --git a/TypeScript.ContractGenerator/CodeDom/JavaScriptEnumMember.cs b/TypeScript.ContractGenerator/CodeDom/JavaScriptEnumMember.cs new file mode 100644 index 0000000..5d1e144 --- /dev/null +++ b/TypeScript.ContractGenerator/CodeDom/JavaScriptEnumMember.cs @@ -0,0 +1,18 @@ +namespace SkbKontur.TypeScript.ContractGenerator.CodeDom +{ + public class JavaScriptEnumMember + { + public string Name { get; set; } + public FlowTypeExpression ValueLiteral { get; set; } + + public string GenerateCode(ICodeGenerationContext context) + { + if (ValueLiteral == null) + { + return Name; + } + return $"{Name} = {ValueLiteral.GenerateCode(context)}"; + } + + } +} \ No newline at end of file diff --git a/TypeScript.ContractGenerator/CustomTypeGenerator.cs b/TypeScript.ContractGenerator/CustomTypeGenerator.cs new file mode 100644 index 0000000..dedd1a8 --- /dev/null +++ b/TypeScript.ContractGenerator/CustomTypeGenerator.cs @@ -0,0 +1,9 @@ +using SkbKontur.TypeScript.ContractGenerator.Internals; + +namespace SkbKontur.TypeScript.ContractGenerator +{ + public static class CustomTypeGenerator + { + public static ICustomTypeGenerator Null => new NullCustomTypeGenerator(); + } +} \ No newline at end of file diff --git a/TypeScript.ContractGenerator/EnumGenerationMode.cs b/TypeScript.ContractGenerator/EnumGenerationMode.cs new file mode 100644 index 0000000..fe6ec94 --- /dev/null +++ b/TypeScript.ContractGenerator/EnumGenerationMode.cs @@ -0,0 +1,8 @@ +namespace SkbKontur.TypeScript.ContractGenerator +{ + public enum EnumGenerationMode + { + FixedStringsAndDictionary = 0, + TypeScriptEnum = 1, + } +} \ No newline at end of file diff --git a/TypeScript.ContractGenerator/FlowTypeGenerationOptions.cs b/TypeScript.ContractGenerator/FlowTypeGenerationOptions.cs new file mode 100644 index 0000000..8ead213 --- /dev/null +++ b/TypeScript.ContractGenerator/FlowTypeGenerationOptions.cs @@ -0,0 +1,13 @@ +namespace SkbKontur.TypeScript.ContractGenerator +{ + public class FlowTypeGenerationOptions + { + public EnumGenerationMode EnumGenerationMode { get; set; } = EnumGenerationMode.FixedStringsAndDictionary; + + public bool EnableOptionalProperties { get; set; } = true; + + public bool EnableExplicitNullability { get; set; } = true; + + public static FlowTypeGenerationOptions Default { get; } = new FlowTypeGenerationOptions(); + } +} \ No newline at end of file diff --git a/TypeScript.ContractGenerator/FlowTypeGenerator.cs b/TypeScript.ContractGenerator/FlowTypeGenerator.cs index fa682c0..afe0d3d 100644 --- a/TypeScript.ContractGenerator/FlowTypeGenerator.cs +++ b/TypeScript.ContractGenerator/FlowTypeGenerator.cs @@ -3,6 +3,8 @@ using System.Linq; using System.Reflection; +using JetBrains.Annotations; + using SkbKontur.TypeScript.ContractGenerator.CodeDom; using SkbKontur.TypeScript.ContractGenerator.Internals; using SkbKontur.TypeScript.ContractGenerator.TypeBuilders; @@ -11,11 +13,12 @@ namespace SkbKontur.TypeScript.ContractGenerator { public class FlowTypeGenerator : ITypeGenerator { - public FlowTypeGenerator(ICustomTypeGenerator customTypeGenerator, Type[] rootTypes) + public FlowTypeGenerator([NotNull] FlowTypeGenerationOptions options, [NotNull] ICustomTypeGenerator customTypeGenerator, [NotNull] IRootTypesProvider rootTypesProvider) { - this.rootTypes = rootTypes; + this.options = options ?? throw new ArgumentNullException(nameof(options)); + rootTypes = rootTypesProvider.GetRootTypes(); flowTypeUnitFactory = new DefaultFlowTypeGeneratorOutput(); - this.customTypeGenerator = customTypeGenerator ?? new NullCustomTypeGenerator(); + this.customTypeGenerator = customTypeGenerator; flowTypeDeclarations = new Dictionary(); } @@ -61,8 +64,8 @@ public void GenerateTypeScriptFiles(string targetPath) currentType.Value.BuildDefinition(this); } } - FilesGenerator.DeleteFiles(targetPath, $"*.{FilesGenerator.JavaScriptFilesExtension}"); - FilesGenerator.DeleteFiles(targetPath, $"*.{FilesGenerator.TypeScriptFilesExtension}"); + FilesGenerator.DeleteFiles(targetPath, $"*.{FilesExtensions.JavaScriptFilesExtension}"); + FilesGenerator.DeleteFiles(targetPath, $"*.{FilesExtensions.TypeScriptFilesExtension}"); FilesGenerator.GenerateTypeScriptFiles(targetPath, flowTypeUnitFactory); } @@ -92,7 +95,9 @@ public ITypeBuildingContext ResolveType(Type type) if (type.IsEnum) { var targetUnit = flowTypeUnitFactory.GetOrCreateTypeUnit(typeLocation); - typeBuildingContext = new EnumTypeBuildingContext(targetUnit, type); + typeBuildingContext = options.EnumGenerationMode == EnumGenerationMode.FixedStringsAndDictionary + ? (ITypeBuildingContext)new FixedStringsAndDictionaryTypeBuildingContext(targetUnit, type) + : new TypeScriptEnumTypeBuildingContext(targetUnit, type); } if (type.IsGenericType && !type.IsGenericTypeDefinition) { @@ -105,12 +110,12 @@ public ITypeBuildingContext ResolveType(Type type) if (type.IsGenericTypeDefinition) { var targetUnit = flowTypeUnitFactory.GetOrCreateTypeUnit(typeLocation); - typeBuildingContext = new CustomTypeTypeBuildingContext(targetUnit, type); + typeBuildingContext = new CustomTypeTypeBuildingContext(targetUnit, type, options); } if (typeBuildingContext == null) { var targetUnit = flowTypeUnitFactory.GetOrCreateTypeUnit(typeLocation); - typeBuildingContext = new CustomTypeTypeBuildingContext(targetUnit, type); + typeBuildingContext = new CustomTypeTypeBuildingContext(targetUnit, type, options); } } typeBuildingContext.Initialize(this); @@ -122,7 +127,7 @@ public FlowTypeType BuildAndImportType(FlowTypeUnit targetUnit, ICustomAttribute { var (isNullable, resultType) = FlowTypeGeneratorHelpers.ProcessNullable(attributeProvider, type); var result = GetFlowTypeType(targetUnit, resultType); - if (isNullable) + if (isNullable && options.EnableExplicitNullability) result = new FlowTypeNullableType(result); return result; } @@ -137,6 +142,7 @@ private FlowTypeType GetFlowTypeType(FlowTypeUnit targetUnit, Type type) return context.ReferenceFrom(targetUnit, this); } + private readonly FlowTypeGenerationOptions options; private readonly Type[] rootTypes; private readonly DefaultFlowTypeGeneratorOutput flowTypeUnitFactory; private readonly ICustomTypeGenerator customTypeGenerator; diff --git a/TypeScript.ContractGenerator/FlowTypeGeneratorHelpers.cs b/TypeScript.ContractGenerator/FlowTypeGeneratorHelpers.cs index 0ba4e68..6cc00b4 100644 --- a/TypeScript.ContractGenerator/FlowTypeGeneratorHelpers.cs +++ b/TypeScript.ContractGenerator/FlowTypeGeneratorHelpers.cs @@ -10,8 +10,8 @@ public static (bool, Type) ProcessNullable(ICustomAttributeProvider attributeCon { if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) { - var undelayingType = type.GetGenericArguments()[0]; - return (true, undelayingType); + var underlyingType = type.GetGenericArguments()[0]; + return (true, underlyingType); } if (attributeContainer != null && type.IsClass && attributeContainer.GetCustomAttributes(true).All(x => x.GetType().Name != "NotNullAttribute")) return (true, type); diff --git a/TypeScript.ContractGenerator/IRootTypesProvider.cs b/TypeScript.ContractGenerator/IRootTypesProvider.cs new file mode 100644 index 0000000..39f8810 --- /dev/null +++ b/TypeScript.ContractGenerator/IRootTypesProvider.cs @@ -0,0 +1,12 @@ +using System; + +using JetBrains.Annotations; + +namespace SkbKontur.TypeScript.ContractGenerator +{ + public interface IRootTypesProvider + { + [NotNull] + Type[] GetRootTypes(); + } +} \ No newline at end of file diff --git a/TypeScript.ContractGenerator/Internals/FilesExtensions.cs b/TypeScript.ContractGenerator/Internals/FilesExtensions.cs new file mode 100644 index 0000000..e1bdac6 --- /dev/null +++ b/TypeScript.ContractGenerator/Internals/FilesExtensions.cs @@ -0,0 +1,8 @@ +namespace SkbKontur.TypeScript.ContractGenerator.Internals +{ + public static class FilesExtensions + { + public const string JavaScriptFilesExtension = "js"; + public const string TypeScriptFilesExtension = "ts"; + } +} \ No newline at end of file diff --git a/TypeScript.ContractGenerator/Internals/FilesGenerator.cs b/TypeScript.ContractGenerator/Internals/FilesGenerator.cs index 6109843..1e7aebe 100644 --- a/TypeScript.ContractGenerator/Internals/FilesGenerator.cs +++ b/TypeScript.ContractGenerator/Internals/FilesGenerator.cs @@ -6,13 +6,10 @@ namespace SkbKontur.TypeScript.ContractGenerator.Internals { internal static class FilesGenerator { - public const string JavaScriptFilesExtension = "js"; - public const string TypeScriptFilesExtension = "ts"; - public static void GenerateFiles(string targetDir, DefaultFlowTypeGeneratorOutput output) { Directory.CreateDirectory(targetDir); - var files = Directory.GetFiles(targetDir, $"*.{JavaScriptFilesExtension}", SearchOption.AllDirectories); + var files = Directory.GetFiles(targetDir, $"*.{FilesExtensions.JavaScriptFilesExtension}", SearchOption.AllDirectories); foreach (var file in files) { if (File.ReadAllText(file).Contains(generatedContentMarkerString)) @@ -22,7 +19,7 @@ public static void GenerateFiles(string targetDir, DefaultFlowTypeGeneratorOutpu } foreach (var unit in output.Units) { - var targetFileName = GetUnitTargetFileName(targetDir, unit, JavaScriptFilesExtension); + var targetFileName = GetUnitTargetFileName(targetDir, unit, FilesExtensions.JavaScriptFilesExtension); EnsureDirectoryExists(targetFileName); @@ -37,7 +34,7 @@ public static void GenerateTypeScriptFiles(string targetDir, DefaultFlowTypeGene Directory.CreateDirectory(targetDir); foreach (var unit in output.Units) { - var targetFileName = GetUnitTargetFileName(targetDir, unit, TypeScriptFilesExtension); + var targetFileName = GetUnitTargetFileName(targetDir, unit, FilesExtensions.TypeScriptFilesExtension); EnsureDirectoryExists(targetFileName); diff --git a/TypeScript.ContractGenerator/RootTypesProvider.cs b/TypeScript.ContractGenerator/RootTypesProvider.cs new file mode 100644 index 0000000..5f322d4 --- /dev/null +++ b/TypeScript.ContractGenerator/RootTypesProvider.cs @@ -0,0 +1,25 @@ +using System; + +using JetBrains.Annotations; + +namespace SkbKontur.TypeScript.ContractGenerator +{ + public class RootTypesProvider : IRootTypesProvider + { + public RootTypesProvider(params Type[] rootTypes) + { + this.rootTypes = rootTypes ?? new Type[0]; + } + + [NotNull, ItemNotNull] + public Type[] GetRootTypes() + { + return rootTypes; + } + + [NotNull] + public static IRootTypesProvider Default = new RootTypesProvider(); + + private readonly Type[] rootTypes; + } +} \ No newline at end of file diff --git a/TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContext.cs b/TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContext.cs index 9e6b779..97775bc 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContext.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContext.cs @@ -4,6 +4,8 @@ using System.Reflection; using System.Text.RegularExpressions; +using JetBrains.Annotations; + using SkbKontur.TypeScript.ContractGenerator.CodeDom; using SkbKontur.TypeScript.ContractGenerator.Extensions; @@ -12,9 +14,10 @@ namespace SkbKontur.TypeScript.ContractGenerator.TypeBuilders // ReSharper disable once ClassWithVirtualMembersNeverInherited.Global public class CustomTypeTypeBuildingContext : TypeBuildingContext { - public CustomTypeTypeBuildingContext(FlowTypeUnit unit, Type type) + public CustomTypeTypeBuildingContext([NotNull] FlowTypeUnit unit, [NotNull] Type type, [NotNull] FlowTypeGenerationOptions options) : base(unit, type) { + this.options = options; } public override bool IsDefinitionBuilt => Declaration.Definition != null; @@ -57,10 +60,10 @@ protected virtual FlowTypeTypeDefintion CreateComplexFlowTypeDefinition(ITypeGen result.Members.Add(new FlowTypeTypeMemberDeclaration { Name = BuildPropertyName(property.Name), - Optional = isNullable, + Optional = isNullable && options.EnableOptionalProperties, Type = property.PropertyType.IsGenericParameter ? new FlowTypeTypeReference(property.PropertyType.Name) - : isNullable ? OrNull(propertyType) : propertyType, + : isNullable && options.EnableExplicitNullability ? OrNull(propertyType) : propertyType, }); } return result; @@ -86,5 +89,7 @@ protected virtual IEnumerable CreateTypeProperties(Type type) { return type.GetProperties(BindingFlags.Instance | BindingFlags.Public); } + + private readonly FlowTypeGenerationOptions options; } } \ No newline at end of file diff --git a/TypeScript.ContractGenerator/TypeBuilders/EnumTypeBuildingContext.cs b/TypeScript.ContractGenerator/TypeBuilders/FixedStringsAndDictionaryTypeBuildingContext.cs similarity index 90% rename from TypeScript.ContractGenerator/TypeBuilders/EnumTypeBuildingContext.cs rename to TypeScript.ContractGenerator/TypeBuilders/FixedStringsAndDictionaryTypeBuildingContext.cs index 094b674..8302f8e 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/EnumTypeBuildingContext.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/FixedStringsAndDictionaryTypeBuildingContext.cs @@ -5,9 +5,9 @@ namespace SkbKontur.TypeScript.ContractGenerator.TypeBuilders { - public class EnumTypeBuildingContext : TypeBuildingContext + public class FixedStringsAndDictionaryTypeBuildingContext : TypeBuildingContext { - public EnumTypeBuildingContext(FlowTypeUnit unit, Type type) + public FixedStringsAndDictionaryTypeBuildingContext(FlowTypeUnit unit, Type type) : base(unit, type) { } diff --git a/TypeScript.ContractGenerator/TypeBuilders/TypeScriptEnumTypeBuildingContext.cs b/TypeScript.ContractGenerator/TypeBuilders/TypeScriptEnumTypeBuildingContext.cs new file mode 100644 index 0000000..2053dd1 --- /dev/null +++ b/TypeScript.ContractGenerator/TypeBuilders/TypeScriptEnumTypeBuildingContext.cs @@ -0,0 +1,32 @@ +using System; +using System.Linq; + +using SkbKontur.TypeScript.ContractGenerator.CodeDom; + +namespace SkbKontur.TypeScript.ContractGenerator.TypeBuilders +{ + public class TypeScriptEnumTypeBuildingContext : TypeBuildingContext + { + public TypeScriptEnumTypeBuildingContext(FlowTypeUnit unit, Type type) + : base(unit, type) + { + } + + public override void Initialize(ITypeGenerator typeGenerator) + { + var values = Type.GetEnumNames(); + var enumResult = new JavaScriptEnumDeclaration + { + Name = Type.Name, + Definition = new JavaScriptEnumDefinition( + values.Select(x => new JavaScriptEnumMember {Name = x, ValueLiteral = new FlowTypeStringLiteral(x)})), + }; + Unit.Body.Add( + new FlowTypeExportTypeStatement + { + Declaration = enumResult + }); + Declaration = enumResult; + } + } +} \ No newline at end of file From 753c58bf54d5d878a7ecac969468120ede5000be Mon Sep 17 00:00:00 2001 From: iperevoschikov Date: Thu, 14 Feb 2019 16:48:45 +0500 Subject: [PATCH 06/10] Add options tests --- .../EndToEndTests.cs | 5 +- .../enum-generation-fixed-strings.js | 6 +++ .../enum-generation-fixed-strings.ts | 6 +++ .../enum-generation-typescript-enum.js | 5 ++ .../enum-generation-typescript-enum.ts | 5 ++ .../explicit-nullability-disabled.js | 9 ++++ .../explicit-nullability-disabled.ts | 9 ++++ .../explicit-nullability-enabled.js | 9 ++++ .../explicit-nullability-enabled.ts | 9 ++++ .../optional-properties-disabled.js | 4 ++ .../optional-properties-disabled.ts | 4 ++ .../optional-properties-enabled.js | 4 ++ .../optional-properties-enabled.ts | 4 ++ .../FlowTypeTestBase.cs | 12 ++++- .../GenericTypesTest.cs | 3 +- .../OptionsTests.cs | 46 +++++++++++++++++++ .../TypeScript.ContractGenerator.Tests.csproj | 39 ++++++++++++++++ .../Types/SingleNullablePropertyType.cs | 7 +++ .../FlowTypeGenerationOptions.cs | 4 ++ 19 files changed, 185 insertions(+), 5 deletions(-) create mode 100644 TypeScript.ContractGenerator.Tests/Files/Options.Expected/enum-generation-fixed-strings.js create mode 100644 TypeScript.ContractGenerator.Tests/Files/Options.Expected/enum-generation-fixed-strings.ts create mode 100644 TypeScript.ContractGenerator.Tests/Files/Options.Expected/enum-generation-typescript-enum.js create mode 100644 TypeScript.ContractGenerator.Tests/Files/Options.Expected/enum-generation-typescript-enum.ts create mode 100644 TypeScript.ContractGenerator.Tests/Files/Options.Expected/explicit-nullability-disabled.js create mode 100644 TypeScript.ContractGenerator.Tests/Files/Options.Expected/explicit-nullability-disabled.ts create mode 100644 TypeScript.ContractGenerator.Tests/Files/Options.Expected/explicit-nullability-enabled.js create mode 100644 TypeScript.ContractGenerator.Tests/Files/Options.Expected/explicit-nullability-enabled.ts create mode 100644 TypeScript.ContractGenerator.Tests/Files/Options.Expected/optional-properties-disabled.js create mode 100644 TypeScript.ContractGenerator.Tests/Files/Options.Expected/optional-properties-disabled.ts create mode 100644 TypeScript.ContractGenerator.Tests/Files/Options.Expected/optional-properties-enabled.js create mode 100644 TypeScript.ContractGenerator.Tests/Files/Options.Expected/optional-properties-enabled.ts create mode 100644 TypeScript.ContractGenerator.Tests/OptionsTests.cs create mode 100644 TypeScript.ContractGenerator.Tests/Types/SingleNullablePropertyType.cs diff --git a/TypeScript.ContractGenerator.Tests/EndToEndTests.cs b/TypeScript.ContractGenerator.Tests/EndToEndTests.cs index 924bd27..0a521c3 100644 --- a/TypeScript.ContractGenerator.Tests/EndToEndTests.cs +++ b/TypeScript.ContractGenerator.Tests/EndToEndTests.cs @@ -1,5 +1,4 @@ using System; -using System.IO; using System.Linq; using FluentAssertions; @@ -31,7 +30,7 @@ public EndToEndTests(JavaScriptTypeChecker javaScriptTypeChecker) public void GenerateCodeTest(Type rootType, string expectedFileName) { var generatedCode = GenerateCode(rootType).Single().Replace("\r\n", "\n"); - var expectedCode = File.ReadAllText(GetFilePath($"SimpleGenerator/{expectedFileName}")).Replace("\r\n", "\n"); + var expectedCode = GetExpectedCode($"SimpleGenerator/{expectedFileName}"); generatedCode.Should().Be(expectedCode); } @@ -50,7 +49,7 @@ public void GenerateFilesTest(Type type) public void CustomGeneratorTest(Type rootType, string expectedFileName) { var generatedCode = GenerateCode(new TestCustomTypeGenerator(), rootType).Single().Replace("\r\n", "\n"); - var expectedCode = File.ReadAllText(GetFilePath($"CustomGenerator/{expectedFileName}")).Replace("\r\n", "\n"); + var expectedCode = GetExpectedCode($"CustomGenerator/{expectedFileName}"); generatedCode.Should().Be(expectedCode); } } diff --git a/TypeScript.ContractGenerator.Tests/Files/Options.Expected/enum-generation-fixed-strings.js b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/enum-generation-fixed-strings.js new file mode 100644 index 0000000..b7ef6c3 --- /dev/null +++ b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/enum-generation-fixed-strings.js @@ -0,0 +1,6 @@ + +export type DefaultEnum = 'A' | 'B'; +export const DefaultEnums = { + ['A']: (('A'): DefaultEnum), + ['B']: (('B'): DefaultEnum), +}; diff --git a/TypeScript.ContractGenerator.Tests/Files/Options.Expected/enum-generation-fixed-strings.ts b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/enum-generation-fixed-strings.ts new file mode 100644 index 0000000..713ec86 --- /dev/null +++ b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/enum-generation-fixed-strings.ts @@ -0,0 +1,6 @@ + +export type DefaultEnum = 'A' | 'B'; +export const DefaultEnums = { + ['A']: ('A') as DefaultEnum, + ['B']: ('B') as DefaultEnum, +}; diff --git a/TypeScript.ContractGenerator.Tests/Files/Options.Expected/enum-generation-typescript-enum.js b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/enum-generation-typescript-enum.js new file mode 100644 index 0000000..49d262d --- /dev/null +++ b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/enum-generation-typescript-enum.js @@ -0,0 +1,5 @@ + +export enum DefaultEnum { + A = 'A', + B = 'B', +} diff --git a/TypeScript.ContractGenerator.Tests/Files/Options.Expected/enum-generation-typescript-enum.ts b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/enum-generation-typescript-enum.ts new file mode 100644 index 0000000..49d262d --- /dev/null +++ b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/enum-generation-typescript-enum.ts @@ -0,0 +1,5 @@ + +export enum DefaultEnum { + A = 'A', + B = 'B', +} diff --git a/TypeScript.ContractGenerator.Tests/Files/Options.Expected/explicit-nullability-disabled.js b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/explicit-nullability-disabled.js new file mode 100644 index 0000000..4ea18c0 --- /dev/null +++ b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/explicit-nullability-disabled.js @@ -0,0 +1,9 @@ + +export type NotNullRootType = { + someNotNullClass: SomeClass; + someNullableClass?: SomeClass; + notNullString: string; +}; +export type SomeClass = { + a: number; +}; diff --git a/TypeScript.ContractGenerator.Tests/Files/Options.Expected/explicit-nullability-disabled.ts b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/explicit-nullability-disabled.ts new file mode 100644 index 0000000..4ea18c0 --- /dev/null +++ b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/explicit-nullability-disabled.ts @@ -0,0 +1,9 @@ + +export type NotNullRootType = { + someNotNullClass: SomeClass; + someNullableClass?: SomeClass; + notNullString: string; +}; +export type SomeClass = { + a: number; +}; diff --git a/TypeScript.ContractGenerator.Tests/Files/Options.Expected/explicit-nullability-enabled.js b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/explicit-nullability-enabled.js new file mode 100644 index 0000000..477480c --- /dev/null +++ b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/explicit-nullability-enabled.js @@ -0,0 +1,9 @@ + +export type NotNullRootType = { + someNotNullClass: SomeClass; + someNullableClass?: null | SomeClass; + notNullString: string; +}; +export type SomeClass = { + a: number; +}; diff --git a/TypeScript.ContractGenerator.Tests/Files/Options.Expected/explicit-nullability-enabled.ts b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/explicit-nullability-enabled.ts new file mode 100644 index 0000000..477480c --- /dev/null +++ b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/explicit-nullability-enabled.ts @@ -0,0 +1,9 @@ + +export type NotNullRootType = { + someNotNullClass: SomeClass; + someNullableClass?: null | SomeClass; + notNullString: string; +}; +export type SomeClass = { + a: number; +}; diff --git a/TypeScript.ContractGenerator.Tests/Files/Options.Expected/optional-properties-disabled.js b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/optional-properties-disabled.js new file mode 100644 index 0000000..c9d76e1 --- /dev/null +++ b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/optional-properties-disabled.js @@ -0,0 +1,4 @@ + +export type SingleNullablePropertyType = { + a: null | number; +}; diff --git a/TypeScript.ContractGenerator.Tests/Files/Options.Expected/optional-properties-disabled.ts b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/optional-properties-disabled.ts new file mode 100644 index 0000000..c9d76e1 --- /dev/null +++ b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/optional-properties-disabled.ts @@ -0,0 +1,4 @@ + +export type SingleNullablePropertyType = { + a: null | number; +}; diff --git a/TypeScript.ContractGenerator.Tests/Files/Options.Expected/optional-properties-enabled.js b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/optional-properties-enabled.js new file mode 100644 index 0000000..86f1354 --- /dev/null +++ b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/optional-properties-enabled.js @@ -0,0 +1,4 @@ + +export type SingleNullablePropertyType = { + a?: null | number; +}; diff --git a/TypeScript.ContractGenerator.Tests/Files/Options.Expected/optional-properties-enabled.ts b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/optional-properties-enabled.ts new file mode 100644 index 0000000..86f1354 --- /dev/null +++ b/TypeScript.ContractGenerator.Tests/Files/Options.Expected/optional-properties-enabled.ts @@ -0,0 +1,4 @@ + +export type SingleNullablePropertyType = { + a?: null | number; +}; diff --git a/TypeScript.ContractGenerator.Tests/FlowTypeTestBase.cs b/TypeScript.ContractGenerator.Tests/FlowTypeTestBase.cs index 7440c9a..d8e6f06 100644 --- a/TypeScript.ContractGenerator.Tests/FlowTypeTestBase.cs +++ b/TypeScript.ContractGenerator.Tests/FlowTypeTestBase.cs @@ -28,7 +28,12 @@ protected string[] GenerateCode(Type rootType) protected string[] GenerateCode(ICustomTypeGenerator customTypeGenerator, Type rootType) { - var generator = new FlowTypeGenerator(FlowTypeGenerationOptions.Default, customTypeGenerator, new RootTypesProvider(rootType)); + return GenerateCode(FlowTypeGenerationOptions.Default, customTypeGenerator, rootType); + } + + protected string[] GenerateCode(FlowTypeGenerationOptions options, ICustomTypeGenerator customTypeGenerator, Type rootType) + { + var generator = new FlowTypeGenerator(options, customTypeGenerator, new RootTypesProvider(rootType)); return generator.Generate().Select(x => x.GenerateCode(new DefaultCodeGenerationContext(javaScriptTypeChecker))).ToArray(); } @@ -80,6 +85,11 @@ private static void CheckDirectoriesEquivalenceInner(string expectedDirectory, s CheckDirectoriesEquivalenceInner($"{expectedDirectory}/{directory}", $"{actualDirectory}/{directory}"); } + protected string GetExpectedCode(string expectedCodeFilePath) + { + return File.ReadAllText(GetFilePath(expectedCodeFilePath)).Replace("\r\n", "\n"); + } + protected string GetFilePath(string filename) { return $"{TestContext.CurrentContext.TestDirectory}/Files/{filename}.{fileExtension}"; diff --git a/TypeScript.ContractGenerator.Tests/GenericTypesTest.cs b/TypeScript.ContractGenerator.Tests/GenericTypesTest.cs index cf08e2b..8b42d5b 100644 --- a/TypeScript.ContractGenerator.Tests/GenericTypesTest.cs +++ b/TypeScript.ContractGenerator.Tests/GenericTypesTest.cs @@ -11,7 +11,8 @@ public class GenericTypesTest [Test] public void RootCantBeGenericType() { - new FlowTypeGenerator(FlowTypeGenerationOptions.Default, CustomTypeGenerator.Null, new RootTypesProvider(typeof(GenericRootType))).Generate().Should().BeEmpty(); + new FlowTypeGenerator(FlowTypeGenerationOptions.Default, CustomTypeGenerator.Null, new RootTypesProvider(typeof(GenericRootType))) + .Generate().Should().BeEmpty(); } } } \ No newline at end of file diff --git a/TypeScript.ContractGenerator.Tests/OptionsTests.cs b/TypeScript.ContractGenerator.Tests/OptionsTests.cs new file mode 100644 index 0000000..ec9db3e --- /dev/null +++ b/TypeScript.ContractGenerator.Tests/OptionsTests.cs @@ -0,0 +1,46 @@ +using System.Linq; + +using FluentAssertions; + +using NUnit.Framework; + +using SkbKontur.TypeScript.ContractGenerator.CodeDom; +using SkbKontur.TypeScript.ContractGenerator.Tests.Types; + +namespace SkbKontur.TypeScript.ContractGenerator.Tests +{ + public class OptionsTests : FlowTypeTestBase + { + public OptionsTests(JavaScriptTypeChecker javaScriptTypeChecker) + : base(javaScriptTypeChecker) + { + } + + [TestCase(EnumGenerationMode.FixedStringsAndDictionary, "enum-generation-fixed-strings")] + [TestCase(EnumGenerationMode.TypeScriptEnum, "enum-generation-typescript-enum")] + public void EnumGenerationModeTest(EnumGenerationMode enumGenerationMode, string expectedFileName) + { + var generatedCode = GenerateCode(new FlowTypeGenerationOptions {EnumGenerationMode = enumGenerationMode}, CustomTypeGenerator.Null, typeof(DefaultEnum)).Single().Replace("\r\n", "\n"); + var expectedCode = GetExpectedCode($"Options.Expected/{expectedFileName}"); + generatedCode.Should().Be(expectedCode); + } + + [TestCase(true, "optional-properties-enabled")] + [TestCase(false, "optional-properties-disabled")] + public void OptionalPropertiesTest(bool optionalPropertiesEnabled, string expectedFileName) + { + var generatedCode = GenerateCode(new FlowTypeGenerationOptions {EnableOptionalProperties = optionalPropertiesEnabled}, CustomTypeGenerator.Null, typeof(SingleNullablePropertyType)).Single().Replace("\r\n", "\n"); + var expectedCode = GetExpectedCode($"Options.Expected/{expectedFileName}"); + generatedCode.Should().Be(expectedCode); + } + + [TestCase(true, "explicit-nullability-enabled")] + [TestCase(false, "explicit-nullability-disabled")] + public void ExplicitNullabilityTest(bool explicitNullabilityEnabled, string expectedFileName) + { + var generatedCode = GenerateCode(new FlowTypeGenerationOptions {EnableExplicitNullability = explicitNullabilityEnabled}, CustomTypeGenerator.Null, typeof(NotNullRootType)).Single().Replace("\r\n", "\n"); + var expectedCode = GetExpectedCode($"Options.Expected/{expectedFileName}"); + generatedCode.Should().Be(expectedCode); + } + } +} \ No newline at end of file diff --git a/TypeScript.ContractGenerator.Tests/TypeScript.ContractGenerator.Tests.csproj b/TypeScript.ContractGenerator.Tests/TypeScript.ContractGenerator.Tests.csproj index 6b5811b..d267cc5 100644 --- a/TypeScript.ContractGenerator.Tests/TypeScript.ContractGenerator.Tests.csproj +++ b/TypeScript.ContractGenerator.Tests/TypeScript.ContractGenerator.Tests.csproj @@ -181,4 +181,43 @@ + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + diff --git a/TypeScript.ContractGenerator.Tests/Types/SingleNullablePropertyType.cs b/TypeScript.ContractGenerator.Tests/Types/SingleNullablePropertyType.cs new file mode 100644 index 0000000..7b7436d --- /dev/null +++ b/TypeScript.ContractGenerator.Tests/Types/SingleNullablePropertyType.cs @@ -0,0 +1,7 @@ +namespace SkbKontur.TypeScript.ContractGenerator.Tests.Types +{ + public class SingleNullablePropertyType + { + public int? A { get; set; } + } +} \ No newline at end of file diff --git a/TypeScript.ContractGenerator/FlowTypeGenerationOptions.cs b/TypeScript.ContractGenerator/FlowTypeGenerationOptions.cs index 8ead213..2b3b846 100644 --- a/TypeScript.ContractGenerator/FlowTypeGenerationOptions.cs +++ b/TypeScript.ContractGenerator/FlowTypeGenerationOptions.cs @@ -1,5 +1,8 @@ +using JetBrains.Annotations; + namespace SkbKontur.TypeScript.ContractGenerator { + [UsedImplicitly(ImplicitUseTargetFlags.WithMembers)] public class FlowTypeGenerationOptions { public EnumGenerationMode EnumGenerationMode { get; set; } = EnumGenerationMode.FixedStringsAndDictionary; @@ -8,6 +11,7 @@ public class FlowTypeGenerationOptions public bool EnableExplicitNullability { get; set; } = true; + [NotNull] public static FlowTypeGenerationOptions Default { get; } = new FlowTypeGenerationOptions(); } } \ No newline at end of file From 8ea47ee0f5e32c1b5ccf6b31325d680c0c47e180 Mon Sep 17 00:00:00 2001 From: iperevoschikov Date: Fri, 15 Feb 2019 22:14:42 +0500 Subject: [PATCH 07/10] Review fixes. Added FilesGenerationContext containing file extension header factory and JavaScriptTypeChecker. --- .../FlowTypeTestBase.cs | 24 ++++------ .../OptionsValidationTests.cs | 22 +++++++++ .../CustomTypeGenerator.cs | 3 ++ .../FlowTypeGenerator.cs | 31 +++++------- .../DefaultFlowTypeGeneratorOutput.cs | 3 +- .../Internals/FilesExtensions.cs | 8 ---- .../Internals/FilesGenerationContext.cs | 47 +++++++++++++++++++ .../Internals/FilesGenerator.cs | 39 +++------------ .../CustomTypeTypeBuildingContext.cs | 3 +- 9 files changed, 104 insertions(+), 76 deletions(-) create mode 100644 TypeScript.ContractGenerator.Tests/OptionsValidationTests.cs delete mode 100644 TypeScript.ContractGenerator/Internals/FilesExtensions.cs create mode 100644 TypeScript.ContractGenerator/Internals/FilesGenerationContext.cs diff --git a/TypeScript.ContractGenerator.Tests/FlowTypeTestBase.cs b/TypeScript.ContractGenerator.Tests/FlowTypeTestBase.cs index d8e6f06..8b66553 100644 --- a/TypeScript.ContractGenerator.Tests/FlowTypeTestBase.cs +++ b/TypeScript.ContractGenerator.Tests/FlowTypeTestBase.cs @@ -17,8 +17,7 @@ public abstract class FlowTypeTestBase { protected FlowTypeTestBase(JavaScriptTypeChecker javaScriptTypeChecker) { - this.javaScriptTypeChecker = javaScriptTypeChecker; - fileExtension = javaScriptTypeChecker == JavaScriptTypeChecker.TypeScript ? FilesExtensions.TypeScriptFilesExtension : FilesExtensions.JavaScriptFilesExtension; + filesGenerationContext = FilesGenerationContext.Create(javaScriptTypeChecker); } protected string[] GenerateCode(Type rootType) @@ -34,27 +33,24 @@ protected string[] GenerateCode(ICustomTypeGenerator customTypeGenerator, Type r protected string[] GenerateCode(FlowTypeGenerationOptions options, ICustomTypeGenerator customTypeGenerator, Type rootType) { var generator = new FlowTypeGenerator(options, customTypeGenerator, new RootTypesProvider(rootType)); - return generator.Generate().Select(x => x.GenerateCode(new DefaultCodeGenerationContext(javaScriptTypeChecker))).ToArray(); + return generator.Generate().Select(x => x.GenerateCode(new DefaultCodeGenerationContext(JavaScriptTypeChecker))).ToArray(); } protected void GenerateFiles(ICustomTypeGenerator customTypeGenerator, string folderName, params Type[] rootTypes) { - var path = $"{TestContext.CurrentContext.TestDirectory}/{folderName}/{javaScriptTypeChecker}"; + var path = $"{TestContext.CurrentContext.TestDirectory}/{folderName}/{JavaScriptTypeChecker}"; if (Directory.Exists(path)) Directory.Delete(path, recursive : true); Directory.CreateDirectory(path); var generator = new FlowTypeGenerator(FlowTypeGenerationOptions.Default, customTypeGenerator, new RootTypesProvider(rootTypes)); - if (javaScriptTypeChecker == JavaScriptTypeChecker.Flow) - generator.GenerateFiles(path); - else - generator.GenerateTypeScriptFiles(path); + generator.GenerateFiles(path, JavaScriptTypeChecker); } protected void CheckDirectoriesEquivalence(string expectedDirectory, string actualDirectory) { - expectedDirectory = $"{TestContext.CurrentContext.TestDirectory}/{expectedDirectory}/{javaScriptTypeChecker}"; - actualDirectory = $"{TestContext.CurrentContext.TestDirectory}/{actualDirectory}/{javaScriptTypeChecker}"; + expectedDirectory = $"{TestContext.CurrentContext.TestDirectory}/{expectedDirectory}/{JavaScriptTypeChecker}"; + actualDirectory = $"{TestContext.CurrentContext.TestDirectory}/{actualDirectory}/{JavaScriptTypeChecker}"; CheckDirectoriesEquivalenceInner(expectedDirectory, actualDirectory); } @@ -90,12 +86,12 @@ protected string GetExpectedCode(string expectedCodeFilePath) return File.ReadAllText(GetFilePath(expectedCodeFilePath)).Replace("\r\n", "\n"); } - protected string GetFilePath(string filename) + private string GetFilePath(string filename) { - return $"{TestContext.CurrentContext.TestDirectory}/Files/{filename}.{fileExtension}"; + return $"{TestContext.CurrentContext.TestDirectory}/Files/{filename}.{filesGenerationContext.FileExtension}"; } - private readonly JavaScriptTypeChecker javaScriptTypeChecker; - private readonly string fileExtension; + private readonly FilesGenerationContext filesGenerationContext; + private JavaScriptTypeChecker JavaScriptTypeChecker => filesGenerationContext.JavaScriptTypeChecker; } } \ No newline at end of file diff --git a/TypeScript.ContractGenerator.Tests/OptionsValidationTests.cs b/TypeScript.ContractGenerator.Tests/OptionsValidationTests.cs new file mode 100644 index 0000000..270bb22 --- /dev/null +++ b/TypeScript.ContractGenerator.Tests/OptionsValidationTests.cs @@ -0,0 +1,22 @@ +using System; + +using FluentAssertions; + +using NUnit.Framework; + +using SkbKontur.TypeScript.ContractGenerator.CodeDom; + +namespace SkbKontur.TypeScript.ContractGenerator.Tests +{ + public class OptionsValidationTests + { + [Test] + public void FlowAndTypeScriptEnumIncompatibilityTest() + { + var options = new FlowTypeGenerationOptions {EnumGenerationMode = EnumGenerationMode.TypeScriptEnum}; + var generator = new FlowTypeGenerator(options, CustomTypeGenerator.Null, RootTypesProvider.Default); + Action filesGeneration = () => generator.GenerateFiles("", JavaScriptTypeChecker.Flow); + filesGeneration.Should().Throw().And.Message.Should().Be("Flow is not compatible with TypeScript enums"); + } + } +} \ No newline at end of file diff --git a/TypeScript.ContractGenerator/CustomTypeGenerator.cs b/TypeScript.ContractGenerator/CustomTypeGenerator.cs index dedd1a8..cad1887 100644 --- a/TypeScript.ContractGenerator/CustomTypeGenerator.cs +++ b/TypeScript.ContractGenerator/CustomTypeGenerator.cs @@ -1,9 +1,12 @@ +using JetBrains.Annotations; + using SkbKontur.TypeScript.ContractGenerator.Internals; namespace SkbKontur.TypeScript.ContractGenerator { public static class CustomTypeGenerator { + [NotNull] public static ICustomTypeGenerator Null => new NullCustomTypeGenerator(); } } \ No newline at end of file diff --git a/TypeScript.ContractGenerator/FlowTypeGenerator.cs b/TypeScript.ContractGenerator/FlowTypeGenerator.cs index afe0d3d..063a970 100644 --- a/TypeScript.ContractGenerator/FlowTypeGenerator.cs +++ b/TypeScript.ContractGenerator/FlowTypeGenerator.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; @@ -13,12 +14,14 @@ namespace SkbKontur.TypeScript.ContractGenerator { public class FlowTypeGenerator : ITypeGenerator { + [SuppressMessage("ReSharper", "ConstantNullCoalescingCondition")] + [SuppressMessage("ReSharper", "ConstantConditionalAccessQualifier")] public FlowTypeGenerator([NotNull] FlowTypeGenerationOptions options, [NotNull] ICustomTypeGenerator customTypeGenerator, [NotNull] IRootTypesProvider rootTypesProvider) { this.options = options ?? throw new ArgumentNullException(nameof(options)); - rootTypes = rootTypesProvider.GetRootTypes(); + this.customTypeGenerator = customTypeGenerator ?? throw new ArgumentNullException(nameof(customTypeGenerator)); + rootTypes = rootTypesProvider?.GetRootTypes() ?? throw new ArgumentNullException(nameof(rootTypesProvider)); flowTypeUnitFactory = new DefaultFlowTypeGeneratorOutput(); - this.customTypeGenerator = customTypeGenerator; flowTypeDeclarations = new Dictionary(); } @@ -37,8 +40,10 @@ public FlowTypeUnit[] Generate() return flowTypeUnitFactory.Units; } - public void GenerateFiles(string targetPath) + public void GenerateFiles(string targetPath, JavaScriptTypeChecker javaScriptTypeChecker) { + ValidateOptions(javaScriptTypeChecker, options); + foreach (var type in rootTypes) RequestTypeBuild(type); while (flowTypeDeclarations.Values.Any(x => !x.IsDefinitionBuilt)) @@ -49,24 +54,14 @@ public void GenerateFiles(string targetPath) currentType.Value.BuildDefinition(this); } } - FilesGenerator.GenerateFiles(targetPath, flowTypeUnitFactory); + FilesGenerator.GenerateFiles(targetPath, flowTypeUnitFactory, FilesGenerationContext.Create(javaScriptTypeChecker)); } - public void GenerateTypeScriptFiles(string targetPath) + [SuppressMessage("ReSharper", "ParameterOnlyUsedForPreconditionCheck.Local")] + private static void ValidateOptions(JavaScriptTypeChecker javaScriptTypeChecker, FlowTypeGenerationOptions flowTypeGenerationOptions) { - foreach (var type in rootTypes) - RequestTypeBuild(type); - while (flowTypeDeclarations.Values.Any(x => !x.IsDefinitionBuilt)) - { - foreach (var currentType in flowTypeDeclarations.ToArray()) - { - if (!currentType.Value.IsDefinitionBuilt) - currentType.Value.BuildDefinition(this); - } - } - FilesGenerator.DeleteFiles(targetPath, $"*.{FilesExtensions.JavaScriptFilesExtension}"); - FilesGenerator.DeleteFiles(targetPath, $"*.{FilesExtensions.TypeScriptFilesExtension}"); - FilesGenerator.GenerateTypeScriptFiles(targetPath, flowTypeUnitFactory); + if (javaScriptTypeChecker == JavaScriptTypeChecker.Flow && flowTypeGenerationOptions.EnumGenerationMode == EnumGenerationMode.TypeScriptEnum) + throw new ArgumentException("Flow is not compatible with TypeScript enums"); } private void RequestTypeBuild(Type type) diff --git a/TypeScript.ContractGenerator/Internals/DefaultFlowTypeGeneratorOutput.cs b/TypeScript.ContractGenerator/Internals/DefaultFlowTypeGeneratorOutput.cs index fb99fca..1423dfd 100644 --- a/TypeScript.ContractGenerator/Internals/DefaultFlowTypeGeneratorOutput.cs +++ b/TypeScript.ContractGenerator/Internals/DefaultFlowTypeGeneratorOutput.cs @@ -9,8 +9,7 @@ internal class DefaultFlowTypeGeneratorOutput : IFlowTypeUnitFactory public FlowTypeUnit GetOrCreateTypeUnit(string path) { - FlowTypeUnit result; - if (units.TryGetValue(path, out result)) + if (units.TryGetValue(path, out var result)) return result; result = new FlowTypeUnit { diff --git a/TypeScript.ContractGenerator/Internals/FilesExtensions.cs b/TypeScript.ContractGenerator/Internals/FilesExtensions.cs deleted file mode 100644 index e1bdac6..0000000 --- a/TypeScript.ContractGenerator/Internals/FilesExtensions.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace SkbKontur.TypeScript.ContractGenerator.Internals -{ - public static class FilesExtensions - { - public const string JavaScriptFilesExtension = "js"; - public const string TypeScriptFilesExtension = "ts"; - } -} \ No newline at end of file diff --git a/TypeScript.ContractGenerator/Internals/FilesGenerationContext.cs b/TypeScript.ContractGenerator/Internals/FilesGenerationContext.cs new file mode 100644 index 0000000..23834da --- /dev/null +++ b/TypeScript.ContractGenerator/Internals/FilesGenerationContext.cs @@ -0,0 +1,47 @@ +using System; + +using JetBrains.Annotations; + +using SkbKontur.TypeScript.ContractGenerator.CodeDom; + +namespace SkbKontur.TypeScript.ContractGenerator.Internals +{ + public class FilesGenerationContext + { + private FilesGenerationContext() + { + } + + [NotNull] + public static FilesGenerationContext Create(JavaScriptTypeChecker javaScriptTypeChecker) + { + switch (javaScriptTypeChecker) + { + case JavaScriptTypeChecker.Flow: + return new FilesGenerationContext + { + FileExtension = "js", + HeaderGenerationFunc = marker => $"// @flow\n{marker}\n", + JavaScriptTypeChecker = javaScriptTypeChecker + }; + case JavaScriptTypeChecker.TypeScript: + return new FilesGenerationContext + { + FileExtension = "ts", + HeaderGenerationFunc = marker => $"{marker}\n// tslint:disable\n", + JavaScriptTypeChecker = javaScriptTypeChecker, + }; + default: + throw new ArgumentOutOfRangeException(nameof(javaScriptTypeChecker), javaScriptTypeChecker, null); + } + } + + [NotNull] + public string FileExtension { get; private set; } + + [NotNull] + public Func HeaderGenerationFunc { get; private set; } + + public JavaScriptTypeChecker JavaScriptTypeChecker { get; private set; } + } +} \ No newline at end of file diff --git a/TypeScript.ContractGenerator/Internals/FilesGenerator.cs b/TypeScript.ContractGenerator/Internals/FilesGenerator.cs index 1e7aebe..bad491d 100644 --- a/TypeScript.ContractGenerator/Internals/FilesGenerator.cs +++ b/TypeScript.ContractGenerator/Internals/FilesGenerator.cs @@ -1,52 +1,27 @@ using System.IO; -using SkbKontur.TypeScript.ContractGenerator.CodeDom; - namespace SkbKontur.TypeScript.ContractGenerator.Internals { internal static class FilesGenerator { - public static void GenerateFiles(string targetDir, DefaultFlowTypeGeneratorOutput output) - { - Directory.CreateDirectory(targetDir); - var files = Directory.GetFiles(targetDir, $"*.{FilesExtensions.JavaScriptFilesExtension}", SearchOption.AllDirectories); - foreach (var file in files) - { - if (File.ReadAllText(file).Contains(generatedContentMarkerString)) - { - File.Delete(file); - } - } - foreach (var unit in output.Units) - { - var targetFileName = GetUnitTargetFileName(targetDir, unit, FilesExtensions.JavaScriptFilesExtension); - - EnsureDirectoryExists(targetFileName); - - File.WriteAllText(targetFileName, "// @flow" + "\n"); - File.AppendAllText(targetFileName, generatedContentMarkerString + "\n"); - File.AppendAllText(targetFileName, unit.GenerateCode(new DefaultCodeGenerationContext(JavaScriptTypeChecker.Flow))); - } - } - - public static void GenerateTypeScriptFiles(string targetDir, DefaultFlowTypeGeneratorOutput output) + public static void GenerateFiles(string targetDir, DefaultFlowTypeGeneratorOutput output, FilesGenerationContext filesGenerationContext) { + DeleteFiles(targetDir, $"*.{filesGenerationContext.FileExtension}"); Directory.CreateDirectory(targetDir); foreach (var unit in output.Units) { - var targetFileName = GetUnitTargetFileName(targetDir, unit, FilesExtensions.TypeScriptFilesExtension); + var targetFileName = GetUnitTargetFileName(targetDir, unit, filesGenerationContext.FileExtension); EnsureDirectoryExists(targetFileName); - File.AppendAllText(targetFileName, generatedContentMarkerString + "\n"); - File.AppendAllText(targetFileName, "// tslint:disable" + "\n"); - File.AppendAllText(targetFileName, unit.GenerateCode(new DefaultCodeGenerationContext(JavaScriptTypeChecker.TypeScript))); + File.WriteAllText(targetFileName, filesGenerationContext.HeaderGenerationFunc(generatedContentMarkerString)); + File.AppendAllText(targetFileName, unit.GenerateCode(new DefaultCodeGenerationContext(filesGenerationContext.JavaScriptTypeChecker))); } } private static string GetUnitTargetFileName(string targetDir, FlowTypeUnit unit, string fileExtension) { - var targetFileName = Path.Combine(targetDir, unit.Path + $".{fileExtension}"); + var targetFileName = Path.Combine(targetDir, $"{unit.Path}.{fileExtension}"); return targetFileName; } @@ -57,7 +32,7 @@ private static void EnsureDirectoryExists(string targetFileName) Directory.CreateDirectory(targetDirectoryName); } - public static void DeleteFiles(string targetDir, string searchPattern) + private static void DeleteFiles(string targetDir, string searchPattern) { var files = Directory.GetFiles(targetDir, searchPattern, SearchOption.AllDirectories); foreach (var file in files) diff --git a/TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContext.cs b/TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContext.cs index 97775bc..2b56138 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContext.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/CustomTypeTypeBuildingContext.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; @@ -85,7 +84,7 @@ private static string BuildPropertyName(string propertyName) return propertyName.ToLowerCamelCase(); } - protected virtual IEnumerable CreateTypeProperties(Type type) + protected virtual PropertyInfo[] CreateTypeProperties(Type type) { return type.GetProperties(BindingFlags.Instance | BindingFlags.Public); } From 1e7d7009b9ff6ffefa71814ec3b272db4ac9d5f8 Mon Sep 17 00:00:00 2001 From: "p.vostretsov" Date: Sat, 16 Feb 2019 11:35:33 +0500 Subject: [PATCH 08/10] up version --- version.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.json b/version.json index 09a0a98..b6f5416 100644 --- a/version.json +++ b/version.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", - "version": "1.0", + "version": "1.1", "assemblyVersion": { "precision": "build" }, From ac65d56764a50bfb5b6e2c45775a70cc4761803d Mon Sep 17 00:00:00 2001 From: iperevoschikov Date: Mon, 18 Feb 2019 13:00:48 +0500 Subject: [PATCH 09/10] Add v1.1 to changelog.md --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f23db4b..2b00513 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## v1.1 - 2019.02.18 +- TypeScript files generated with '.ts' extension +- Generation options: EnumGenerationMode: FixedStringsAndDictionary | TypeScriptEnum, EnableOptionalProperties, EnableExplicitNullability +- IRootTypes provider interface (preparation to build in executable generator) +- Add internal JetBrains.Annotations +- Typos fixes at ITypeBuildingContext: IDefinitionBuilded -> IsDefinitionBuilt, BuildDefiniion -> BuildDefinition + ## v1.0 - 2019.01.30 - Support .NET Standard 2.0. - Switch to SDK-style project format and dotnet core build tooling. From 3dc913461cd1cc6e5f26c64f4984e55625d0cda3 Mon Sep 17 00:00:00 2001 From: iperevoschikov Date: Mon, 18 Feb 2019 14:11:53 +0500 Subject: [PATCH 10/10] Change readme.md to correspond new contracts. Add options description section. --- README.md | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5934807..24d40c6 100644 --- a/README.md +++ b/README.md @@ -30,11 +30,11 @@ public class SecondType Then generate TypeScript files with: ```csharp -var generator = new FlowTypeGenerator(customTypeGenerator : null, rootTypes : new[] {typeof(SecondType)}); -generator.GenerateTypeScriptFiles("./output"); +var generator = new FlowTypeGenerator(FlowTypeGenerationOptions.Default, CustomTypeGenerator.Null, new RootTypesProvider(typeof(SecondType))); +generator.GenerateFiles("./output", JavaScriptTypeChecker.TypeScript); ``` -By default, this will generate file with name `.tsx` with following content: +By default, this will generate file with name `.ts` with following content: ```ts // TypeScriptContractGenerator's generated content @@ -52,6 +52,65 @@ export type FirstType = { If you want generated files to have different name or to generate some typings differently, you should pass your own implementation of `ICustomTypeGenerator` to `FlowTypeGenerator`. +## Generation options + +### EnumGenerationMode + +This options is set to `FixedStringsAndDictionary` by default. + +```csharp +public enum EnumGenerationMode +{ + FixedStringsAndDictionary = 0, + TypeScriptEnum = 1, +} +``` + +Setting option value equal to `FixedStringsAndDictionary` produces following output: + +```ts +export type SomeEnum = 'A' | 'B'; +export const SomeEnums = { + ['A']: ('A') as SomeEnum, + ['B']: ('B') as SomeEnum, +}; +``` + +Option value `TypeScriptEnum` produces following: + +```ts +export enum SomeEnum { + A = 'A', + B = 'B', +} +``` + +### EnableOptionalProperties + +This option is **enabled** by default. When **enabled** produces optional properties for members which may contain nulls. + +```ts +export type SomeType = { + somePropertyWithNullableValue?: typeDefinition; + somePropertyWithNonNullableValue: typeDefinition; +}; + +``` +When **disabled**, all properties produced as required. + +### EnableExplicitNullability + +This option is **enabled** by default. When **enabled** produces nullable types for members which may contain nulls. + +```ts +export type SomeType = { + nullablePropertyDefinition: null | string; + nonNullablePropertyDefinition: string; +}; +``` + +When **disabled** produces all types as-is. + ## Known bugs See [this issue](https://github.com/skbkontur/TypeScript.ContractGenerator/issues/1)