diff --git a/README.md b/README.md index 82b30e0..d320df4 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ PM> Install-Package M31.FluentApi A package reference will be added to your `csproj` file. Moreover, since this library provides code via source code generation, consumers of your project don't need the reference to `M31.FluentApi`. Therefore, it is recommended to use the `PrivateAssets` metadata tag: ```xml - + ``` If you would like to examine the generated code, you may emit it by adding the following lines to your `csproj` file: @@ -586,4 +586,4 @@ In particular, if your IDE visually indicates that there are errors in your code This library is free. If you find it valuable and wish to express your support, please leave a star. You are kindly invited to contribute. If you see the possibility for enhancement, please create a GitHub issue and you will receive timely feedback. -Happy coding! +Happy coding! \ No newline at end of file diff --git a/src/M31.FluentApi.Generator/CodeBuilding/Parameter.cs b/src/M31.FluentApi.Generator/CodeBuilding/Parameter.cs index 35956c6..27c2aa8 100644 --- a/src/M31.FluentApi.Generator/CodeBuilding/Parameter.cs +++ b/src/M31.FluentApi.Generator/CodeBuilding/Parameter.cs @@ -1,3 +1,5 @@ +using M31.FluentApi.Generator.Commons; + namespace M31.FluentApi.Generator.CodeBuilding; internal class Parameter : ICode @@ -10,7 +12,7 @@ internal Parameter( ParameterAnnotations? parameterAnnotations = null) { Type = type; - Name = name; + Name = CSharpKeywords.IsCSharpKeyword(name) ? $"@{name}" : name; DefaultValue = defaultValue; GenericTypeParameterPosition = genericTypeParameterPosition; ParameterAnnotations = parameterAnnotations; diff --git a/src/M31.FluentApi.Generator/Commons/CSharpKeywords.cs b/src/M31.FluentApi.Generator/Commons/CSharpKeywords.cs new file mode 100644 index 0000000..bedc6c3 --- /dev/null +++ b/src/M31.FluentApi.Generator/Commons/CSharpKeywords.cs @@ -0,0 +1,11 @@ +using Microsoft.CodeAnalysis.CSharp; + +namespace M31.FluentApi.Generator.Commons; + +internal static class CSharpKeywords +{ + internal static bool IsCSharpKeyword(string name) + { + return SyntaxFacts.GetKeywordKind(name) != SyntaxKind.None; + } +} \ No newline at end of file diff --git a/src/M31.FluentApi.Generator/M31.FluentApi.Generator.csproj b/src/M31.FluentApi.Generator/M31.FluentApi.Generator.csproj index e8c74d4..7fd666f 100644 --- a/src/M31.FluentApi.Generator/M31.FluentApi.Generator.csproj +++ b/src/M31.FluentApi.Generator/M31.FluentApi.Generator.csproj @@ -11,7 +11,7 @@ true true true - 1.11.0 + 1.12.0 Kevin Schaal The generator package for M31.FluentAPI. Don't install this package explicitly, install M31.FluentAPI instead. fluentapi fluentbuilder fluentinterface fluentdesign fluent codegeneration diff --git a/src/M31.FluentApi.Tests/CodeGeneration/TestClasses/Abstract/KeywordClass/CreateStudent.expected.txt b/src/M31.FluentApi.Tests/CodeGeneration/TestClasses/Abstract/KeywordClass/CreateStudent.expected.txt new file mode 100644 index 0000000..ebe6abf --- /dev/null +++ b/src/M31.FluentApi.Tests/CodeGeneration/TestClasses/Abstract/KeywordClass/CreateStudent.expected.txt @@ -0,0 +1,73 @@ +// +// This code was generated by the library M31.FluentAPI. +// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member +#nullable enable + +using System; + +namespace M31.FluentApi.Tests.CodeGeneration.TestClasses.Abstract.KeywordClass; + +public class CreateStudent : + CreateStudent.ICreateStudent, + CreateStudent.IWithOperator, + CreateStudent.IWithClass, + CreateStudent.IWithVoid +{ + private readonly Student student; + + private CreateStudent() + { + student = new Student(); + } + + public static ICreateStudent InitialStep() + { + return new CreateStudent(); + } + + public static IWithClass WithOperator(string @operator) + { + CreateStudent createStudent = new CreateStudent(); + createStudent.student.Operator = @operator; + return createStudent; + } + + IWithClass IWithOperator.WithOperator(string @operator) + { + student.Operator = @operator; + return this; + } + + IWithVoid IWithClass.WithClass(string @class) + { + student.Class = @class; + return this; + } + + Student IWithVoid.WithVoid(int @void) + { + student.Void = @void; + return student; + } + + public interface ICreateStudent : IWithOperator + { + } + + public interface IWithOperator + { + IWithClass WithOperator(string @operator); + } + + public interface IWithClass + { + IWithVoid WithClass(string @class); + } + + public interface IWithVoid + { + Student WithVoid(int @void); + } +} \ No newline at end of file diff --git a/src/M31.FluentApi.Tests/CodeGeneration/TestClasses/Abstract/KeywordClass/CreateStudent.g.cs b/src/M31.FluentApi.Tests/CodeGeneration/TestClasses/Abstract/KeywordClass/CreateStudent.g.cs new file mode 100644 index 0000000..ebe6abf --- /dev/null +++ b/src/M31.FluentApi.Tests/CodeGeneration/TestClasses/Abstract/KeywordClass/CreateStudent.g.cs @@ -0,0 +1,73 @@ +// +// This code was generated by the library M31.FluentAPI. +// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member +#nullable enable + +using System; + +namespace M31.FluentApi.Tests.CodeGeneration.TestClasses.Abstract.KeywordClass; + +public class CreateStudent : + CreateStudent.ICreateStudent, + CreateStudent.IWithOperator, + CreateStudent.IWithClass, + CreateStudent.IWithVoid +{ + private readonly Student student; + + private CreateStudent() + { + student = new Student(); + } + + public static ICreateStudent InitialStep() + { + return new CreateStudent(); + } + + public static IWithClass WithOperator(string @operator) + { + CreateStudent createStudent = new CreateStudent(); + createStudent.student.Operator = @operator; + return createStudent; + } + + IWithClass IWithOperator.WithOperator(string @operator) + { + student.Operator = @operator; + return this; + } + + IWithVoid IWithClass.WithClass(string @class) + { + student.Class = @class; + return this; + } + + Student IWithVoid.WithVoid(int @void) + { + student.Void = @void; + return student; + } + + public interface ICreateStudent : IWithOperator + { + } + + public interface IWithOperator + { + IWithClass WithOperator(string @operator); + } + + public interface IWithClass + { + IWithVoid WithClass(string @class); + } + + public interface IWithVoid + { + Student WithVoid(int @void); + } +} \ No newline at end of file diff --git a/src/M31.FluentApi.Tests/CodeGeneration/TestClasses/Abstract/KeywordClass/Student.cs b/src/M31.FluentApi.Tests/CodeGeneration/TestClasses/Abstract/KeywordClass/Student.cs new file mode 100644 index 0000000..a9c55d3 --- /dev/null +++ b/src/M31.FluentApi.Tests/CodeGeneration/TestClasses/Abstract/KeywordClass/Student.cs @@ -0,0 +1,21 @@ +// Non-nullable member is uninitialized +#pragma warning disable CS8618 +// ReSharper disable All + +using System; +using M31.FluentApi.Attributes; + +namespace M31.FluentApi.Tests.CodeGeneration.TestClasses.Abstract.KeywordClass; + +[FluentApi] +public class Student +{ + [FluentMember(0)] + public string Operator { get; set; } + + [FluentMember(1, "With{Name}")] + public string Class { get; set; } + + [FluentMember(2)] + public int Void { get; set; } +} \ No newline at end of file diff --git a/src/M31.FluentApi.Tests/CodeGeneration/TestDataProvider.cs b/src/M31.FluentApi.Tests/CodeGeneration/TestDataProvider.cs index 00a65b8..7aa7d7a 100644 --- a/src/M31.FluentApi.Tests/CodeGeneration/TestDataProvider.cs +++ b/src/M31.FluentApi.Tests/CodeGeneration/TestDataProvider.cs @@ -73,6 +73,7 @@ internal class TestDataProvider : IEnumerable new object[] { "Abstract", "InheritedClassProtectedSetters", "Student|Person" }, new object[] { "Abstract", "InheritedRecord", "Student|Person" }, new object[] { "Abstract", "InternalClass", "Student" }, + new object[] { "Abstract", "KeywordClass", "Student"}, new object[] { "Abstract", "NonGenericCollectionMemberClass", "Student" }, new object[] { "Abstract", "NullablePredicateAndCollectionClass", "Student" }, new object[] { "Abstract", "OneMemberClass", "Student" }, diff --git a/src/M31.FluentApi/M31.FluentApi.csproj b/src/M31.FluentApi/M31.FluentApi.csproj index 95052a9..84d1d49 100644 --- a/src/M31.FluentApi/M31.FluentApi.csproj +++ b/src/M31.FluentApi/M31.FluentApi.csproj @@ -7,7 +7,7 @@ enable true true - 1.11.0 + 1.12.0 Kevin Schaal Generate fluent builders in C#. fluentapi fluentbuilder fluentinterface fluentdesign fluent codegeneration