Skip to content

Commit

Permalink
Update SA1111 to also check the primary constructor argument list in …
Browse files Browse the repository at this point in the history
…a base list

#3785
  • Loading branch information
bjornhellander committed Mar 20, 2024
1 parent 92e8a2c commit 5f00381
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,52 @@ public async Task TestPrimaryConstructorWithoutParameterAsync(string typeKeyword
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

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

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

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

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

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

protected virtual DiagnosticResult[] GetExpectedResultTestPrimaryConstructorWithParameter()
{
return new[]
Expand All @@ -58,5 +104,15 @@ protected virtual DiagnosticResult[] GetExpectedResultTestPrimaryConstructorWith
Diagnostic().WithLocation(0),
};
}

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

private static readonly Action<SyntaxNodeAnalysisContext> TypeDeclarationAction = HandleTypeDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext> PrimaryConstructorBaseTypeAction = HandlePrimaryConstructorBaseType;
private static readonly Action<SyntaxNodeAnalysisContext> BaseMethodDeclarationAction = HandleBaseMethodDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext> LocalFunctionStatementAction = HandleLocalFunctionStatement;
private static readonly Action<SyntaxNodeAnalysisContext> InvocationExpressionAction = HandleInvocationExpression;
Expand All @@ -84,6 +85,7 @@ public override void Initialize(AnalysisContext context)
context.EnableConcurrentExecution();

context.RegisterSyntaxNodeAction(TypeDeclarationAction, SyntaxKinds.TypeDeclaration);
context.RegisterSyntaxNodeAction(PrimaryConstructorBaseTypeAction, SyntaxKindEx.PrimaryConstructorBaseType);
context.RegisterSyntaxNodeAction(BaseMethodDeclarationAction, HandledMethodSyntaxKinds);
context.RegisterSyntaxNodeAction(LocalFunctionStatementAction, SyntaxKindEx.LocalFunctionStatement);
context.RegisterSyntaxNodeAction(InvocationExpressionAction, SyntaxKind.InvocationExpression);
Expand Down Expand Up @@ -224,6 +226,12 @@ private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context)
CheckParameterList(context, typeDeclarationSyntax.ParameterList());
}

private static void HandlePrimaryConstructorBaseType(SyntaxNodeAnalysisContext context)
{
var typeDeclarationSyntax = (PrimaryConstructorBaseTypeSyntaxWrapper)context.Node;
CheckArgumentList(context, typeDeclarationSyntax.ArgumentList);
}

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

0 comments on commit 5f00381

Please sign in to comment.