Skip to content

Commit 38b7dd4

Browse files
authored
fix: escape keywords in method parameters (#47)
* fix: escape keywords in method parameters * fix: KeywordClass
1 parent 6854d18 commit 38b7dd4

File tree

9 files changed

+186
-5
lines changed

9 files changed

+186
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ PM> Install-Package M31.FluentApi
3737
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:
3838

3939
```xml
40-
<PackageReference Include="M31.FluentApi" Version="1.11.0" PrivateAssets="all"/>
40+
<PackageReference Include="M31.FluentApi" Version="1.12.0" PrivateAssets="all"/>
4141
```
4242

4343
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
586586

587587
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.
588588

589-
Happy coding!
589+
Happy coding!

src/M31.FluentApi.Generator/CodeBuilding/Parameter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using M31.FluentApi.Generator.Commons;
2+
13
namespace M31.FluentApi.Generator.CodeBuilding;
24

35
internal class Parameter : ICode
@@ -10,7 +12,7 @@ internal Parameter(
1012
ParameterAnnotations? parameterAnnotations = null)
1113
{
1214
Type = type;
13-
Name = name;
15+
Name = CSharpKeywords.IsCSharpKeyword(name) ? $"@{name}" : name;
1416
DefaultValue = defaultValue;
1517
GenericTypeParameterPosition = genericTypeParameterPosition;
1618
ParameterAnnotations = parameterAnnotations;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.CodeAnalysis.CSharp;
2+
3+
namespace M31.FluentApi.Generator.Commons;
4+
5+
internal static class CSharpKeywords
6+
{
7+
internal static bool IsCSharpKeyword(string name)
8+
{
9+
return SyntaxFacts.GetKeywordKind(name) != SyntaxKind.None;
10+
}
11+
}

src/M31.FluentApi.Generator/M31.FluentApi.Generator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1212
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1313
<SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>
14-
<PackageVersion>1.11.0</PackageVersion>
14+
<PackageVersion>1.12.0</PackageVersion>
1515
<Authors>Kevin Schaal</Authors>
1616
<Description>The generator package for M31.FluentAPI. Don't install this package explicitly, install M31.FluentAPI instead.</Description>
1717
<PackageTags>fluentapi fluentbuilder fluentinterface fluentdesign fluent codegeneration</PackageTags>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// <auto-generated/>
2+
// This code was generated by the library M31.FluentAPI.
3+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
4+
5+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
6+
#nullable enable
7+
8+
using System;
9+
10+
namespace M31.FluentApi.Tests.CodeGeneration.TestClasses.Abstract.KeywordClass;
11+
12+
public class CreateStudent :
13+
CreateStudent.ICreateStudent,
14+
CreateStudent.IWithOperator,
15+
CreateStudent.IWithClass,
16+
CreateStudent.IWithVoid
17+
{
18+
private readonly Student student;
19+
20+
private CreateStudent()
21+
{
22+
student = new Student();
23+
}
24+
25+
public static ICreateStudent InitialStep()
26+
{
27+
return new CreateStudent();
28+
}
29+
30+
public static IWithClass WithOperator(string @operator)
31+
{
32+
CreateStudent createStudent = new CreateStudent();
33+
createStudent.student.Operator = @operator;
34+
return createStudent;
35+
}
36+
37+
IWithClass IWithOperator.WithOperator(string @operator)
38+
{
39+
student.Operator = @operator;
40+
return this;
41+
}
42+
43+
IWithVoid IWithClass.WithClass(string @class)
44+
{
45+
student.Class = @class;
46+
return this;
47+
}
48+
49+
Student IWithVoid.WithVoid(int @void)
50+
{
51+
student.Void = @void;
52+
return student;
53+
}
54+
55+
public interface ICreateStudent : IWithOperator
56+
{
57+
}
58+
59+
public interface IWithOperator
60+
{
61+
IWithClass WithOperator(string @operator);
62+
}
63+
64+
public interface IWithClass
65+
{
66+
IWithVoid WithClass(string @class);
67+
}
68+
69+
public interface IWithVoid
70+
{
71+
Student WithVoid(int @void);
72+
}
73+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// <auto-generated/>
2+
// This code was generated by the library M31.FluentAPI.
3+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
4+
5+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
6+
#nullable enable
7+
8+
using System;
9+
10+
namespace M31.FluentApi.Tests.CodeGeneration.TestClasses.Abstract.KeywordClass;
11+
12+
public class CreateStudent :
13+
CreateStudent.ICreateStudent,
14+
CreateStudent.IWithOperator,
15+
CreateStudent.IWithClass,
16+
CreateStudent.IWithVoid
17+
{
18+
private readonly Student student;
19+
20+
private CreateStudent()
21+
{
22+
student = new Student();
23+
}
24+
25+
public static ICreateStudent InitialStep()
26+
{
27+
return new CreateStudent();
28+
}
29+
30+
public static IWithClass WithOperator(string @operator)
31+
{
32+
CreateStudent createStudent = new CreateStudent();
33+
createStudent.student.Operator = @operator;
34+
return createStudent;
35+
}
36+
37+
IWithClass IWithOperator.WithOperator(string @operator)
38+
{
39+
student.Operator = @operator;
40+
return this;
41+
}
42+
43+
IWithVoid IWithClass.WithClass(string @class)
44+
{
45+
student.Class = @class;
46+
return this;
47+
}
48+
49+
Student IWithVoid.WithVoid(int @void)
50+
{
51+
student.Void = @void;
52+
return student;
53+
}
54+
55+
public interface ICreateStudent : IWithOperator
56+
{
57+
}
58+
59+
public interface IWithOperator
60+
{
61+
IWithClass WithOperator(string @operator);
62+
}
63+
64+
public interface IWithClass
65+
{
66+
IWithVoid WithClass(string @class);
67+
}
68+
69+
public interface IWithVoid
70+
{
71+
Student WithVoid(int @void);
72+
}
73+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Non-nullable member is uninitialized
2+
#pragma warning disable CS8618
3+
// ReSharper disable All
4+
5+
using System;
6+
using M31.FluentApi.Attributes;
7+
8+
namespace M31.FluentApi.Tests.CodeGeneration.TestClasses.Abstract.KeywordClass;
9+
10+
[FluentApi]
11+
public class Student
12+
{
13+
[FluentMember(0)]
14+
public string Operator { get; set; }
15+
16+
[FluentMember(1, "With{Name}")]
17+
public string Class { get; set; }
18+
19+
[FluentMember(2)]
20+
public int Void { get; set; }
21+
}

src/M31.FluentApi.Tests/CodeGeneration/TestDataProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ internal class TestDataProvider : IEnumerable<object[]>
7373
new object[] { "Abstract", "InheritedClassProtectedSetters", "Student|Person" },
7474
new object[] { "Abstract", "InheritedRecord", "Student|Person" },
7575
new object[] { "Abstract", "InternalClass", "Student" },
76+
new object[] { "Abstract", "KeywordClass", "Student"},
7677
new object[] { "Abstract", "NonGenericCollectionMemberClass", "Student" },
7778
new object[] { "Abstract", "NullablePredicateAndCollectionClass", "Student" },
7879
new object[] { "Abstract", "OneMemberClass", "Student" },

src/M31.FluentApi/M31.FluentApi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<ImplicitUsings>enable</ImplicitUsings>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
10-
<PackageVersion>1.11.0</PackageVersion>
10+
<PackageVersion>1.12.0</PackageVersion>
1111
<Authors>Kevin Schaal</Authors>
1212
<Description>Generate fluent builders in C#.</Description>
1313
<PackageTags>fluentapi fluentbuilder fluentinterface fluentdesign fluent codegeneration</PackageTags>

0 commit comments

Comments
 (0)