From cb647217cf39800cf4750677ec0a5fc5502015f7 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Fri, 16 Aug 2024 12:23:21 -0500 Subject: [PATCH] =?UTF-8?q?When=20an=20#if=20directive=20has=20a=20comma?= =?UTF-8?q?=20added=20with=20it,=20then=20the=20syntax=20node=E2=80=A6=20(?= =?UTF-8?q?#1317)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … validation fails because it is not comparing the syntax nodes. closes #1312 --- .../DisabledTextComparerTests.cs | 136 ++++++++++-------- .../SyntaxNodeComparerTests.cs | 125 ++++++++-------- Src/CSharpier/DisabledTextComparer.cs | 11 +- 3 files changed, 157 insertions(+), 115 deletions(-) diff --git a/Src/CSharpier.Tests/DisabledTextComparerTests.cs b/Src/CSharpier.Tests/DisabledTextComparerTests.cs index c076b543f..49c5136d3 100644 --- a/Src/CSharpier.Tests/DisabledTextComparerTests.cs +++ b/Src/CSharpier.Tests/DisabledTextComparerTests.cs @@ -12,10 +12,11 @@ public void IsCodeBasicallyEqual_Should_Return_True_For_Basic_Case() { var before = "public string Tester;"; - var after = - @"public -string Tester; -"; + var after = """ + public + string Tester; + + """; DisabledTextComparer.IsCodeBasicallyEqual(before, after).Should().BeTrue(); } @@ -30,74 +31,95 @@ public void Squash_Should_Work_For_Trailing_Space_And_New_Line() Squash(before).Should().Be(Squash(after)); } + [Test] + public void Squash_Should_Work_For_Trailing_Comma() + { + var before = """ + public enum Enum + { + Foo = 1, + } + """; + + var after = "public enum Enum {Foo=1}\n"; + + Squash(before).Should().Be(Squash(after)); + } + [Test] public void Squash_Should_Work_With_Pointer_Stuff() { - var before = - @" [MethodImpl (MethodImplOptions.InternalCall)] - private static unsafe extern void ApplyUpdate_internal (IntPtr base_assm, byte* dmeta_bytes, int dmeta_length, byte *dil_bytes, int dil_length, byte *dpdb_bytes, int dpdb_length);"; - - var after = - @"[MethodImpl(MethodImplOptions.InternalCall)] - private static unsafe extern void ApplyUpdate_internal( - IntPtr base_assm, - byte* dmeta_bytes, - int dmeta_length, - byte* dil_bytes, - int dil_length, - byte* dpdb_bytes, - int dpdb_length - ); -"; + var before = """ + [MethodImpl (MethodImplOptions.InternalCall)] + private static unsafe extern void ApplyUpdate_internal (IntPtr base_assm, byte* dmeta_bytes, int dmeta_length, byte *dil_bytes, int dil_length, byte *dpdb_bytes, int dpdb_length); + """; + + var after = """ + [MethodImpl(MethodImplOptions.InternalCall)] + private static unsafe extern void ApplyUpdate_internal( + IntPtr base_assm, + byte* dmeta_bytes, + int dmeta_length, + byte* dil_bytes, + int dil_length, + byte* dpdb_bytes, + int dpdb_length + ); + + """; Squash(before).Should().Be(Squash(after)); } [Test] public void Squash_Should_Work_With_Commas() { - var before = - @" - TypeBuilder typeBuilder = moduleBuilder.DefineType(assemblyName.FullName - , TypeAttributes.Public | - TypeAttributes.Class | - TypeAttributes.AutoClass | - TypeAttributes.AnsiClass | - TypeAttributes.BeforeFieldInit | - TypeAttributes.AutoLayout - , null); -"; - - var after = - @" -TypeBuilder typeBuilder = moduleBuilder.DefineType( - assemblyName.FullName, - TypeAttributes.Public - | TypeAttributes.Class - | TypeAttributes.AutoClass - | TypeAttributes.AnsiClass - | TypeAttributes.BeforeFieldInit - | TypeAttributes.AutoLayout, - null - ); -"; + var before = """ + + TypeBuilder typeBuilder = moduleBuilder.DefineType(assemblyName.FullName + , TypeAttributes.Public | + TypeAttributes.Class | + TypeAttributes.AutoClass | + TypeAttributes.AnsiClass | + TypeAttributes.BeforeFieldInit | + TypeAttributes.AutoLayout + , null); + + """; + + var after = """ + + TypeBuilder typeBuilder = moduleBuilder.DefineType( + assemblyName.FullName, + TypeAttributes.Public + | TypeAttributes.Class + | TypeAttributes.AutoClass + | TypeAttributes.AnsiClass + | TypeAttributes.BeforeFieldInit + | TypeAttributes.AutoLayout, + null + ); + + """; Squash(before).Should().Be(Squash(after)); } [Test] public void Squash_Should_Work_With_Period() { - var before = - @" - var options2 = (ProxyGenerationOptions)proxy.GetType(). - GetField(""proxyGenerationOptions"", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null); -"; - - var after = - @" - var options2 = (ProxyGenerationOptions)proxy.GetType() - .GetField(""proxyGenerationOptions"", BindingFlags.Static | BindingFlags.NonPublic) - .GetValue(null); -"; + var before = """ + + var options2 = (ProxyGenerationOptions)proxy.GetType(). + GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null); + + """; + + var after = """ + + var options2 = (ProxyGenerationOptions)proxy.GetType() + .GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic) + .GetValue(null); + + """; Squash(before).Should().Be(Squash(after)); } diff --git a/Src/CSharpier.Tests/SyntaxNodeComparerTests.cs b/Src/CSharpier.Tests/SyntaxNodeComparerTests.cs index ac29a86be..b4ae576e2 100644 --- a/Src/CSharpier.Tests/SyntaxNodeComparerTests.cs +++ b/Src/CSharpier.Tests/SyntaxNodeComparerTests.cs @@ -649,24 +649,24 @@ public void RawStringLiterals_Error_With_Adding_Indentation_When_There_Was_None( } [Test] - public void CollectionExpression_Work_With_Adding_Trailing_Comma_When_There_Was_None() + public void CollectionExpression_Works_With_Adding_Trailing_Comma_When_There_Was_None() { - var left = """" + var left = """ int[][] a = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; - """"; - var right = """" + """; + var right = """ int[][] a = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ]; - """"; + """; var result = CompareSource(left, right); @@ -674,14 +674,10 @@ public void CollectionExpression_Work_With_Adding_Trailing_Comma_When_There_Was_ } [Test] - public void CollectionExpression_Work_With_Removing_Trailing_Comma_When_There_Was_One() + public void CollectionExpression_Works_With_Removing_Trailing_Comma_When_There_Was_One() { - var left = """" - int[] a = [1, 2, 3,]; - """"; - var right = """" - int[] a = [1, 2, 3]; - """"; + var left = "int[] a = [1, 2, 3,];"; + var right = "int[] a = [1, 2, 3];"; var result = CompareSource(left, right); @@ -689,24 +685,24 @@ public void CollectionExpression_Work_With_Removing_Trailing_Comma_When_There_Wa } [Test] - public void AnonymousObjectCreationExpression_Work_With_Adding_Trailing_Comma_When_There_Was_None() + public void AnonymousObjectCreationExpression_Works_With_Adding_Trailing_Comma_When_There_Was_None() { - var left = """" + var left = """ public dynamic a = new { One = "One", Two = "Two", ThreeThreeThree = "ThreeThreeThree" }; - """"; - var right = """" + """; + var right = """ public dynamic a = new { One = "One", Two = "Two", ThreeThreeThree = "ThreeThreeThree", }; - """"; + """; var result = CompareSource(left, right); @@ -714,14 +710,10 @@ public void AnonymousObjectCreationExpression_Work_With_Adding_Trailing_Comma_Wh } [Test] - public void AnonymousObjectCreationExpression_Work_With_Removing_Trailing_Comma_When_There_Was_One() + public void AnonymousObjectCreationExpression_Works_With_Removing_Trailing_Comma_When_There_Was_One() { - var left = """" - public dynamic a = new { Property = true, } - """"; - var right = """" - public dynamic a = new { Property = true } - """"; + var left = "public dynamic a = new { Property = true, }"; + var right = "public dynamic a = new { Property = true }"; var result = CompareSource(left, right); @@ -729,22 +721,22 @@ public void AnonymousObjectCreationExpression_Work_With_Removing_Trailing_Comma_ } [Test] - public void InitializerExpression_Work_With_Adding_Trailing_Comma_When_There_Was_None() + public void InitializerExpression_Works_With_Adding_Trailing_Comma_When_There_Was_None() { - var left = """" + var left = """ string[] a = { "someLongValue_____________________________________", "someLongValue_____________________________________" }; - """"; - var right = """" + """; + var right = """ string[] a = { "someLongValue_____________________________________", "someLongValue_____________________________________", }; - """"; + """; var result = CompareSource(left, right); @@ -752,14 +744,10 @@ public void InitializerExpression_Work_With_Adding_Trailing_Comma_When_There_Was } [Test] - public void InitializerExpression_Work_With_Removing_Trailing_Comma_When_There_Was_One() + public void InitializerExpression_Works_With_Removing_Trailing_Comma_When_There_Was_One() { - var left = """" - int[] a = { 1, 2, }; - """"; - var right = """" - int[] a = { 1, 2 }; - """"; + var left = "int[] a = { 1, 2, };"; + var right = "int[] a = { 1, 2 };"; var result = CompareSource(left, right); @@ -767,15 +755,15 @@ public void InitializerExpression_Work_With_Removing_Trailing_Comma_When_There_W } [Test] - public void SwitchExpression_Work_With_Adding_Trailing_Comma_When_There_Was_None() + public void SwitchExpression_Works_With_Adding_Trailing_Comma_When_There_Was_None() { - var left = """" + var left = """ int switchExpressionNoTrailingComma() { return 1 switch { 1 => 100, _ => throw new global::System.Exception() }; } - """"; - var right = """" + """; + var right = """ int switchExpressionNoTrailingComma() { return 1 switch @@ -784,7 +772,7 @@ int switchExpressionNoTrailingComma() _ => throw new global::System.Exception(), }; } - """"; + """; var result = CompareSource(left, right); @@ -792,9 +780,9 @@ int switchExpressionNoTrailingComma() } [Test] - public void ListPattern_Work_With_Adding_Trailing_Comma_When_There_Was_None() + public void ListPattern_Works_With_Adding_Trailing_Comma_When_There_Was_None() { - var left = """" + var left = """ object listPatternTrailingComma(object list) { return list switch @@ -804,8 +792,8 @@ object listPatternTrailingComma(object list) [..] elems => elems.Sum(e => e + e) }; } - """"; - var right = """" + """; + var right = """ object listPatternTrailingComma(object list) { return list switch @@ -815,7 +803,7 @@ object listPatternTrailingComma(object list) [..] elems => elems.Sum(e => e + e), }; } - """"; + """; var result = CompareSource(left, right); @@ -823,20 +811,20 @@ object listPatternTrailingComma(object list) } [Test] - public void EnumDeclaration_Work_With_Adding_Trailing_Comma_When_There_Was_None() + public void EnumDeclaration_Works_With_Adding_Trailing_Comma_When_There_Was_None() { - var left = """" + var left = """ public enum Enum { Foo = 1 } - """"; - var right = """" + """; + var right = """ public enum Enum { Foo = 1, } - """"; + """; var result = CompareSource(left, right); @@ -844,17 +832,42 @@ public enum Enum } [Test] - public void EnumDeclaration_Work_Without_Removing_Trailing_Comma_When_There_Already_Was_One() + public void EnumDeclaration_Works_Without_Removing_Trailing_Comma_When_There_Already_Was_One() { - var left = """" + var left = """ public enum Enum { Foo = 1, } - """"; - var right = """" + """; + var right = """ public enum Enum { Foo = 1, } - """"; + """; + + var result = CompareSource(left, right); + + result.Should().BeEmpty(); + } + + [Test] + public void EnumDeclaration_Works_With_Adding_Trailing_Comma_When_There_Was_None_Inside_If_Directive() + { + var left = """ + #if DEBUG + public enum Enum + { + Foo = 1 + } + #endif + """; + var right = """ + #if DEBUG + public enum Enum + { + Foo = 1, + } + #endif + """; var result = CompareSource(left, right); diff --git a/Src/CSharpier/DisabledTextComparer.cs b/Src/CSharpier/DisabledTextComparer.cs index 5b37807b8..0ea78a60d 100644 --- a/Src/CSharpier/DisabledTextComparer.cs +++ b/Src/CSharpier/DisabledTextComparer.cs @@ -21,9 +21,8 @@ public static bool IsCodeBasicallyEqual(string code, string formattedCode) protected static string Squash(string code) { var result = new StringBuilder(); - for (var index = 0; index < code.Length; index++) + foreach (var nextChar in code) { - var nextChar = code[index]; if (nextChar is ' ' or '\t' or '\r' or '\n') { if ( @@ -93,6 +92,14 @@ is not ( { result.Append(nextChar); } + + // this removes trailing commas so that added trailing commas inside #if blocks don't result + // in validation failures + if (nextChar is '}' && result.Length > 1 && result[^2] is ',') + { + result[^2] = nextChar; + result.Length -= 1; + } } return result.ToString();