From 65287cef28f811bd1bec31b05c8296f88f2230fb Mon Sep 17 00:00:00 2001 From: Ivan Maximov Date: Sun, 3 Dec 2023 15:17:48 +0300 Subject: [PATCH 1/2] Fix issue with dropped semicolon on NET472 --- src/PublicApiGenerator/CodeNormalizer.cs | 1 + .../Assembly_attributes.cs | 9 +- src/PublicApiGeneratorTests/Issue301.cs | 101 ++++++++++++++++++ .../PublicApiGeneratorTests.csproj | 6 +- 4 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 src/PublicApiGeneratorTests/Issue301.cs diff --git a/src/PublicApiGenerator/CodeNormalizer.cs b/src/PublicApiGenerator/CodeNormalizer.cs index 0da3064..ff188df 100644 --- a/src/PublicApiGenerator/CodeNormalizer.cs +++ b/src/PublicApiGenerator/CodeNormalizer.cs @@ -43,6 +43,7 @@ public static string NormalizeGeneratedCode(StringWriter writer) gennedClass = Regex.Replace(gennedClass, SET, " { set; }", RegexOptions.IgnorePatternWhitespace); gennedClass = Regex.Replace(gennedClass, @"\s+{\s+}", " { }", RegexOptions.IgnorePatternWhitespace); gennedClass = Regex.Replace(gennedClass, @"\)\s+;", ");", RegexOptions.IgnorePatternWhitespace); + gennedClass = Regex.Replace(gennedClass, @"\r\n\s+;\r\n", ";" + Environment.NewLine); // bug-fix for https://github.com/PublicApiGenerator/PublicApiGenerator/issues/301 var attributeMarkerEscaped = Regex.Escape(ATTRIBUTE_MARKER); gennedClass = Regex.Replace(gennedClass, $@" (Attribute)? # Delete this if present. Would create a clash for Attribute1, Attribute1Attribute but that is a very rare edge case diff --git a/src/PublicApiGeneratorTests/Assembly_attributes.cs b/src/PublicApiGeneratorTests/Assembly_attributes.cs index 4286023..2b2d2f6 100644 --- a/src/PublicApiGeneratorTests/Assembly_attributes.cs +++ b/src/PublicApiGeneratorTests/Assembly_attributes.cs @@ -7,8 +7,8 @@ [assembly: AttributeWithPositionalParameters1("Hello")] [assembly: AttributeWithPositionalParameters2(42)] [assembly: AttributeWithMultiplePositionalParameters(42, "Hello")] -[assembly: AttributeWithNamedParameterAttribute(StringValue = "Hello", IntValue = 42)] -[assembly: ComVisibleAttribute(false)] +[assembly: AttributeWithNamedParameter(StringValue = "Hello", IntValue = 42)] +[assembly: ComVisible(false)] namespace PublicApiGeneratorTests { @@ -17,7 +17,10 @@ public class Assembly_attributes : ApiGeneratorTestsBase [Fact] public void Attributes() { -#if NET8_0 +#if NET472 + const string TFM = ".NETFramework,Version=v4.7.2"; + const string TFMNAME = ".NET Framework 4.7.2"; +#elif NET8_0 const string TFM = ".NETCoreApp,Version=v8.0"; const string TFMNAME = ".NET 8.0"; #elif NET7_0 diff --git a/src/PublicApiGeneratorTests/Issue301.cs b/src/PublicApiGeneratorTests/Issue301.cs new file mode 100644 index 0000000..f79a23c --- /dev/null +++ b/src/PublicApiGeneratorTests/Issue301.cs @@ -0,0 +1,101 @@ +using PublicApiGeneratorTests.Examples; + +namespace PublicApiGeneratorTests +{ + public class Issue301 : ApiGeneratorTestsBase + { + [Fact] + public void Should_Render_Semicolon_Properly_1() + { + AssertPublicApi(typeof(Interface1), +""" +namespace PublicApiGeneratorTests.Examples +{ + public interface Interface1 + { + void ConfigureMapping() + where T : class; + } +} +"""); + } + + [Fact] + public void Should_Render_Semicolon_Properly_2() + { + AssertPublicApi(typeof(Interface2), +""" +namespace PublicApiGeneratorTests.Examples +{ + public interface Interface2 + { + void ConfigureMapping() + where T : class; + } +} +"""); + } + + [Fact] + public void Should_Render_Semicolon_Properly_3() + { + AssertPublicApi(typeof(Interface3), +""" +namespace PublicApiGeneratorTests.Examples +{ + public interface Interface3 + { + void ConfigureMapping() + where T : class; + } +} +"""); + } + + [Fact] + public void Should_Render_Semicolon_Properly_4() + { + AssertPublicApi(typeof(Interface4), +""" +namespace PublicApiGeneratorTests.Examples +{ + public interface Interface4 + { + void ConfigureMapping() + where T : class + where K : struct; + } +} +"""); + } + } + + + namespace Examples + { + public interface Interface1 + { + void ConfigureMapping() + where T : class; + } + + public interface Interface2 + { + void ConfigureMapping() + where T : class; + } + + public interface Interface3 + { + void ConfigureMapping() + where T : class; + } + + public interface Interface4 + { + void ConfigureMapping() + where T : class + where K : struct; + } + } +} diff --git a/src/PublicApiGeneratorTests/PublicApiGeneratorTests.csproj b/src/PublicApiGeneratorTests/PublicApiGeneratorTests.csproj index 84c500b..23232b5 100644 --- a/src/PublicApiGeneratorTests/PublicApiGeneratorTests.csproj +++ b/src/PublicApiGeneratorTests/PublicApiGeneratorTests.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1;net6.0;net7.0;net8.0 + netcoreapp3.1;net6.0;net7.0;net8.0;net472 $(NoWarn);CS0618;CS0067;CS8597;CS8603;CS8618;CS8653;IDE0051;IDE0060;IDE1006