From a4f8525a700dfe18380f893b8c7f1b62427bcb00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Fri, 31 Dec 2021 00:46:09 +0100 Subject: [PATCH] fix: correct generated syntax errors --- .../PacketListIndexAttributeGenerator.cs | 2 +- .../ConverterDeserializationGenerator.cs | 6 +++--- .../ConverterSerializationGenerator.cs | 9 ++++----- .../Extensions/AttributeArgumentSyntaxExtensions.cs | 2 +- .../Extensions/AttributeListSyntaxExtensions.cs | 2 +- .../SourceGenerator.cs | 11 ++++++++++- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketListIndexAttributeGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketListIndexAttributeGenerator.cs index ca6254d10029b107b6e245fc61365596053b11d8..05197cd34e62641481e81ba294a10a966d77ccab 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketListIndexAttributeGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketListIndexAttributeGenerator.cs @@ -22,7 +22,7 @@ public class PacketListIndexAttributeGenerator : IParameterGenerator /// /// Gets the full name of the packet index attribute. /// - public static string PacketListIndexAttributeFullName => "NosSmooth.Packets.Attributes.PacketListIndex"; + public static string PacketListIndexAttributeFullName => "NosSmooth.Packets.Attributes.PacketListIndexAttribute"; /// public bool ShouldHandle(ParameterInfo parameter) diff --git a/Core/NosSmooth.PacketSerializersGenerator/ConverterDeserializationGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/ConverterDeserializationGenerator.cs index 9dff5ad083a267e20974458dd2f9109904395e1e..723b7b1f685f08dd4e2785717d9d3eb73beed5b3 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/ConverterDeserializationGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/ConverterDeserializationGenerator.cs @@ -33,7 +33,7 @@ public class ConverterDeserializationGenerator /// The separator. public void SetAfterSeparatorOnce(char separator) { - _textWriter.WriteLine(@$"{_stringEnumeratorVariable}.SetAfterSeparatorOnce(""{separator}"");"); + _textWriter.WriteLine(@$"{_stringEnumeratorVariable}.SetAfterSeparatorOnce('{separator}');"); } /// @@ -43,7 +43,7 @@ public class ConverterDeserializationGenerator /// The maximum number of tokens to read. public void PushLevel(char separator, uint? maxTokens = default) { - _textWriter.WriteLine(@$"{_stringEnumeratorVariable}.PushLevel(""{separator}"", {maxTokens?.ToString() ?? "null"});"); + _textWriter.WriteLine(@$"{_stringEnumeratorVariable}.PushLevel('{separator}', {maxTokens?.ToString() ?? "null"});"); } /// @@ -61,7 +61,7 @@ public class ConverterDeserializationGenerator /// The maximum number of tokens to read. public void PrepareLevel(char separator, uint? maxTokens = default) { - _textWriter.WriteLine($@"{_stringEnumeratorVariable}.PrepareLevel(""{separator}"", {maxTokens?.ToString() ?? "null"});"); + _textWriter.WriteLine($@"{_stringEnumeratorVariable}.PrepareLevel('{separator}', {maxTokens?.ToString() ?? "null"});"); } /// diff --git a/Core/NosSmooth.PacketSerializersGenerator/ConverterSerializationGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/ConverterSerializationGenerator.cs index a3cab6406ecfe17358a992304abc502a081f03e2..6b5f1f3c46e76557f216d16aa67090d1566d14a8 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/ConverterSerializationGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/ConverterSerializationGenerator.cs @@ -33,7 +33,7 @@ public class ConverterSerializationGenerator /// The separator. public void SetAfterSeparatorOnce(char separator) { - _textWriter.WriteLine(@$"{_builderVariable}.SetAfterSeparatorOnce(""{separator}"");"); + _textWriter.WriteLine(@$"{_builderVariable}.SetAfterSeparatorOnce('{separator}');"); } /// @@ -43,7 +43,7 @@ public class ConverterSerializationGenerator public void PushLevel(char separator) { _textWriter.WriteLine - (@$"{_builderVariable}.PushLevel(""{separator}"");"); + (@$"{_builderVariable}.PushLevel('{separator}');"); } /// @@ -58,11 +58,10 @@ public class ConverterSerializationGenerator /// Prepare the level to the string enumerator. /// /// The separator. - /// The maximum number of tokens to read. - public void PrepareLevel(char separator, uint? maxTokens = default) + public void PrepareLevel(char separator) { _textWriter.WriteLine - ($@"{_builderVariable}.PrepareLevel(""{separator}"", {maxTokens?.ToString() ?? "null"});"); + ($@"{_builderVariable}.PrepareLevel('{separator}');"); } /// diff --git a/Core/NosSmooth.PacketSerializersGenerator/Extensions/AttributeArgumentSyntaxExtensions.cs b/Core/NosSmooth.PacketSerializersGenerator/Extensions/AttributeArgumentSyntaxExtensions.cs index f2e843370f07a5208aec8fd84902f105a6a6b71e..2b2b7ddd186604ed2762002c4e79dac85d1bd7d1 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/Extensions/AttributeArgumentSyntaxExtensions.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/Extensions/AttributeArgumentSyntaxExtensions.cs @@ -28,6 +28,6 @@ public static class AttributeArgumentSyntaxExtensions return null; } - return value; + return value.Value; } } \ No newline at end of file diff --git a/Core/NosSmooth.PacketSerializersGenerator/Extensions/AttributeListSyntaxExtensions.cs b/Core/NosSmooth.PacketSerializersGenerator/Extensions/AttributeListSyntaxExtensions.cs index 2c12e29e1ef4fcae9cb14e765c43ad6d11e5447f..9a035157f86a61b07a9ce5622aea985cb856ca98 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/Extensions/AttributeListSyntaxExtensions.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/Extensions/AttributeListSyntaxExtensions.cs @@ -24,6 +24,6 @@ public static class AttributeListSyntaxExtensions /// Whether the attribute is present. public static bool ContainsAttribute(this AttributeListSyntax attributeList, SemanticModel semanticModel, string attributeFullName) { - return attributeList.Attributes.Any(x => Regex.IsMatch(attributeFullName, semanticModel.GetTypeInfo(x).Type?.ToString()!)); + return attributeList.Attributes.Any(x => Regex.IsMatch(semanticModel.GetTypeInfo(x).Type?.ToString()!, attributeFullName)); } } \ No newline at end of file diff --git a/Core/NosSmooth.PacketSerializersGenerator/SourceGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/SourceGenerator.cs index 7afac6d100c16381e774768dd450e2c80f180b64..b5d0cbbfd84f7c3cc87a8b242a8610162074e31a 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/SourceGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/SourceGenerator.cs @@ -6,6 +6,7 @@ using System.CodeDom.Compiler; using System.Diagnostics; +using System.Text.RegularExpressions; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -204,7 +205,15 @@ public class SourceGenerator : ISourceGenerator parameterInfo = null; var attributes = parameter.AttributeLists .Where(x => x.ContainsAttribute(semanticModel, Constants.PacketAttributesClassRegex)) - .SelectMany(x => x.Attributes) + .SelectMany + ( + x + => x.Attributes.Where + ( + y => Regex.IsMatch + (semanticModel.GetTypeInfo(y).Type?.ToString()!, Constants.PacketAttributesClassRegex) + ) + ) .ToList(); if (attributes.Count == 0)