Skip to content

Commit

Permalink
Update SA1111 to also check the parameter list in primary constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornhellander committed Mar 19, 2024
1 parent b5992ef commit 92e8a2c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@

namespace StyleCop.Analyzers.Test.CSharp11.ReadabilityRules
{
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp10.ReadabilityRules;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.ReadabilityRules.SA1111ClosingParenthesisMustBeOnLineOfLastParameter,
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;

public partial class SA1111CSharp11UnitTests : SA1111CSharp10UnitTests
{
protected override DiagnosticResult[] GetExpectedResultTestPrimaryConstructorWithParameter()
{
return new[]
{
Diagnostic().WithLocation(0),
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,60 @@

namespace StyleCop.Analyzers.Test.CSharp9.ReadabilityRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp8.ReadabilityRules;
using StyleCop.Analyzers.Test.Helpers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.ReadabilityRules.SA1111ClosingParenthesisMustBeOnLineOfLastParameter,
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;

public partial class SA1111CSharp9UnitTests : SA1111CSharp8UnitTests
{
[Theory]
[MemberData(nameof(CommonMemberData.TypeKeywordsWhichSupportPrimaryConstructors), MemberType = typeof(CommonMemberData))]
[WorkItem(3785, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3785")]
public async Task TestPrimaryConstructorWithParameterAsync(string typeKeyword)
{
var testCode = $@"
{typeKeyword} Foo(int x
{{|#0:)|}}
{{
}}";

var fixedCode = $@"
{typeKeyword} Foo(int x)
{{
}}";

var expected = this.GetExpectedResultTestPrimaryConstructorWithParameter();
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[MemberData(nameof(CommonMemberData.TypeKeywordsWhichSupportPrimaryConstructors), MemberType = typeof(CommonMemberData))]
[WorkItem(3785, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3785")]
public async Task TestPrimaryConstructorWithoutParameterAsync(string typeKeyword)
{
var testCode = $@"
{typeKeyword} Foo(
)
{{
}}";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

protected virtual DiagnosticResult[] GetExpectedResultTestPrimaryConstructorWithParameter()
{
return new[]
{
// Diagnostic issued twice because of https://github.com/dotnet/roslyn/issues/53136
Diagnostic().WithLocation(0),
Diagnostic().WithLocation(0),
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ internal class SA1111ClosingParenthesisMustBeOnLineOfLastParameter : DiagnosticA
SyntaxKind.OperatorDeclaration,
SyntaxKind.ConversionOperatorDeclaration);

private static readonly Action<SyntaxNodeAnalysisContext> TypeDeclarationAction = HandleTypeDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext> BaseMethodDeclarationAction = HandleBaseMethodDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext> LocalFunctionStatementAction = HandleLocalFunctionStatement;
private static readonly Action<SyntaxNodeAnalysisContext> InvocationExpressionAction = HandleInvocationExpression;
Expand All @@ -82,6 +83,7 @@ public override void Initialize(AnalysisContext context)
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();

context.RegisterSyntaxNodeAction(TypeDeclarationAction, SyntaxKinds.TypeDeclaration);
context.RegisterSyntaxNodeAction(BaseMethodDeclarationAction, HandledMethodSyntaxKinds);
context.RegisterSyntaxNodeAction(LocalFunctionStatementAction, SyntaxKindEx.LocalFunctionStatement);
context.RegisterSyntaxNodeAction(InvocationExpressionAction, SyntaxKind.InvocationExpression);
Expand Down Expand Up @@ -216,6 +218,12 @@ private static void HandleLocalFunctionStatement(SyntaxNodeAnalysisContext conte
CheckParameterList(context, localFunctionStatementSyntax.ParameterList);
}

private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context)
{
var typeDeclarationSyntax = (TypeDeclarationSyntax)context.Node;
CheckParameterList(context, typeDeclarationSyntax.ParameterList());
}

private static void CheckParameterList(SyntaxNodeAnalysisContext context, ParameterListSyntax parameterList)
{
if (parameterList == null || parameterList.IsMissing || !parameterList.Parameters.Any())
Expand Down

0 comments on commit 92e8a2c

Please sign in to comment.