From 390b565e8c630e5bc2ac93c9acfb1288645cb3fe Mon Sep 17 00:00:00 2001 From: David Grieser Date: Wed, 20 Mar 2024 10:36:13 +0100 Subject: [PATCH 1/2] Update SA1513 to not require a blank line before linq 'into' keyword --- .../LayoutRules/SA1513CSharp12UnitTests.cs | 71 +++++++++++++++++++ ...13ClosingBraceMustBeFollowedByBlankLine.cs | 3 +- 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/LayoutRules/SA1513CSharp12UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/LayoutRules/SA1513CSharp12UnitTests.cs index 47cc412e4..5477cf504 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/LayoutRules/SA1513CSharp12UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/LayoutRules/SA1513CSharp12UnitTests.cs @@ -32,5 +32,76 @@ public Foo[] TestMethod() => await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false); } + + [Fact] + public async Task TestJoinIntoClauseSyntaxQueryExpressionAsync() + { + var testCode = @" +using System.Collections.Generic; +using System.Linq; + +public class JoinIntoClauseSyntaxQueryExpressionTest +{ + public JoinIntoClauseSyntaxQueryExpressionTest() + { + List fooList = + [ + new(1, ""First""), + new(2, ""Second""), + new(3, ""Third""), + new(1, ""Fourth"") + ]; + + List barList = + [ + new(1, ""First""), + new(2, ""Second""), + new(3, ""Third"") + ]; + + var query = + from foo in fooList + join bar in barList + on new + { + foo.Id, + foo.Name + } + equals new + { + bar.Id, + bar.Name + } + into grouping + from joined in grouping.DefaultIfEmpty() + select new + { + joined.Id, + BarName = joined.Name, + FooName = foo.Name + }; + } + + private class Foo( + int id, + string name) + { + public int Id { get; } = id; + + public string Name { get; } = name; + } + + private class Bar( + int id, + string name) + { + public int Id { get; } = id; + + public string Name { get; } = name; + } +}"; + + await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1513ClosingBraceMustBeFollowedByBlankLine.cs b/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1513ClosingBraceMustBeFollowedByBlankLine.cs index 97dd481f1..46ed408fb 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1513ClosingBraceMustBeFollowedByBlankLine.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1513ClosingBraceMustBeFollowedByBlankLine.cs @@ -238,7 +238,8 @@ private void AnalyzeCloseBrace(SyntaxToken token) { if (nextToken.Parent is QueryClauseSyntax || nextToken.Parent is SelectOrGroupClauseSyntax - || nextToken.Parent is QueryContinuationSyntax) + || nextToken.Parent is QueryContinuationSyntax + || nextToken.Parent is JoinIntoClauseSyntax) { // the close brace is part of a query expression return; From 297d44ce29074d2f14970c609aeb734079a7f0d5 Mon Sep 17 00:00:00 2001 From: David Grieser Date: Wed, 20 Mar 2024 15:18:20 +0100 Subject: [PATCH 2/2] Moved 'TestJoinIntoClauseSyntaxQueryExpressionAsync' test case --- .../LayoutRules/SA1513CSharp12UnitTests.cs | 72 ------------------- .../LayoutRules/SA1513UnitTests.cs | 67 +++++++++++++++++ 2 files changed, 67 insertions(+), 72 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/LayoutRules/SA1513CSharp12UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/LayoutRules/SA1513CSharp12UnitTests.cs index 5477cf504..077b32bc5 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/LayoutRules/SA1513CSharp12UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/LayoutRules/SA1513CSharp12UnitTests.cs @@ -8,7 +8,6 @@ namespace StyleCop.Analyzers.Test.CSharp12.LayoutRules using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.CSharp11.LayoutRules; using Xunit; - using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.LayoutRules.SA1513ClosingBraceMustBeFollowedByBlankLine, StyleCop.Analyzers.LayoutRules.SA1513CodeFixProvider>; @@ -32,76 +31,5 @@ public Foo[] TestMethod() => await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false); } - - [Fact] - public async Task TestJoinIntoClauseSyntaxQueryExpressionAsync() - { - var testCode = @" -using System.Collections.Generic; -using System.Linq; - -public class JoinIntoClauseSyntaxQueryExpressionTest -{ - public JoinIntoClauseSyntaxQueryExpressionTest() - { - List fooList = - [ - new(1, ""First""), - new(2, ""Second""), - new(3, ""Third""), - new(1, ""Fourth"") - ]; - - List barList = - [ - new(1, ""First""), - new(2, ""Second""), - new(3, ""Third"") - ]; - - var query = - from foo in fooList - join bar in barList - on new - { - foo.Id, - foo.Name - } - equals new - { - bar.Id, - bar.Name - } - into grouping - from joined in grouping.DefaultIfEmpty() - select new - { - joined.Id, - BarName = joined.Name, - FooName = foo.Name - }; - } - - private class Foo( - int id, - string name) - { - public int Id { get; } = id; - - public string Name { get; } = name; - } - - private class Bar( - int id, - string name) - { - public int Id { get; } = id; - - public string Name { get; } = name; - } -}"; - - await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false); - } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1513UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1513UnitTests.cs index 267095142..effb3d3d3 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1513UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1513UnitTests.cs @@ -1034,5 +1034,72 @@ public class TestClass await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false); } + + [Fact] + public async Task TestJoinIntoClauseSyntaxQueryExpressionAsync() + { + var testCode = @" +using System.Collections.Generic; +using System.Linq; + +public class JoinIntoClauseSyntaxQueryExpressionTest +{ + public JoinIntoClauseSyntaxQueryExpressionTest() + { + List fooList = new List(); + List barList = new List(); + + var query = + from foo in fooList + join bar in barList + on new + { + foo.Id, + foo.Name + } + equals new + { + bar.Id, + bar.Name + } + into grouping + from joined in grouping.DefaultIfEmpty() + select new + { + joined.Id, + BarName = joined.Name, + FooName = foo.Name + }; + } + + private class Foo + { + public Foo(int id, string name) + { + this.Id = id; + this.Name = name; + } + + public int Id { get; } + + public string Name { get; } + } + + private class Bar + { + public Bar(int id, string name) + { + this.Id = id; + this.Name = name; + } + + public int Id { get; } + + public string Name { get; } + } +}"; + + await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false); + } } }