Skip to content

Commit

Permalink
Update SA1618 to suppress messages when documentation for an element …
Browse files Browse the repository at this point in the history
…is optional

Fixes DotNetAnalyzers#2446
  • Loading branch information
sharwell committed Jun 20, 2017
1 parent e936b4f commit e0b4ec1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,25 @@ public void TestMethod<T>(T param1) { }
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(2446, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2446")]
public async Task TestPrivateMethodMissingGenericParametersAsync()
{
var testCode = @"
internal class ClassName
{
///
private void Test1<T>(int arg) { }
/**
*
*/
private void Test2<T>(int arg) { }
}";

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

protected override Project ApplyCompilationOptions(Project project)
{
var resolver = new TestXmlReferenceResolver();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace StyleCop.Analyzers.DocumentationRules
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using StyleCop.Analyzers.Helpers;
using StyleCop.Analyzers.Settings.ObjectModel;

/// <summary>
/// A generic C# element is missing documentation for one or more of its generic type parameters.
Expand Down Expand Up @@ -41,9 +42,9 @@ internal class SA1618GenericTypeParametersMustBeDocumented : DiagnosticAnalyzer
private static readonly DiagnosticDescriptor Descriptor =
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.DocumentationRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);

private static readonly Action<SyntaxNodeAnalysisContext> TypeDeclarationAction = HandleTypeDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext> MethodDeclarationAction = HandleMethodDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext> DelegateDeclarationAction = HandleDelegateDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> TypeDeclarationAction = HandleTypeDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> MethodDeclarationAction = HandleMethodDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> DelegateDeclarationAction = HandleDelegateDeclaration;

/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
Expand All @@ -60,7 +61,7 @@ public override void Initialize(AnalysisContext context)
context.RegisterSyntaxNodeAction(DelegateDeclarationAction, SyntaxKind.DelegateDeclaration);
}

private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context)
private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
{
TypeDeclarationSyntax typeDeclaration = (TypeDeclarationSyntax)context.Node;

Expand All @@ -70,25 +71,40 @@ private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context)
return;
}

HandleMemberDeclaration(context, typeDeclaration, typeDeclaration.TypeParameterList);
Accessibility declaredAccessibility = typeDeclaration.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
Accessibility effectiveAccessibility = typeDeclaration.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, typeDeclaration.Kind(), typeDeclaration.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
HandleMemberDeclaration(context, needsComment, typeDeclaration, typeDeclaration.TypeParameterList);
}

private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context)
private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
{
MethodDeclarationSyntax methodDeclaration = (MethodDeclarationSyntax)context.Node;

HandleMemberDeclaration(context, methodDeclaration, methodDeclaration.TypeParameterList);
Accessibility declaredAccessibility = methodDeclaration.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
Accessibility effectiveAccessibility = methodDeclaration.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, methodDeclaration.Kind(), methodDeclaration.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
HandleMemberDeclaration(context, needsComment, methodDeclaration, methodDeclaration.TypeParameterList);
}

private static void HandleDelegateDeclaration(SyntaxNodeAnalysisContext context)
private static void HandleDelegateDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
{
DelegateDeclarationSyntax delegateDeclaration = (DelegateDeclarationSyntax)context.Node;

HandleMemberDeclaration(context, delegateDeclaration, delegateDeclaration.TypeParameterList);
Accessibility declaredAccessibility = delegateDeclaration.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
Accessibility effectiveAccessibility = delegateDeclaration.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, delegateDeclaration.Kind(), delegateDeclaration.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
HandleMemberDeclaration(context, needsComment, delegateDeclaration, delegateDeclaration.TypeParameterList);
}

private static void HandleMemberDeclaration(SyntaxNodeAnalysisContext context, SyntaxNode node, TypeParameterListSyntax typeParameterList)
private static void HandleMemberDeclaration(SyntaxNodeAnalysisContext context, bool needsComment, SyntaxNode node, TypeParameterListSyntax typeParameterList)
{
if (!needsComment)
{
// Documentation is not required for this element.
return;
}

if (typeParameterList == null)
{
// The member does not have a type parameter list
Expand Down

0 comments on commit e0b4ec1

Please sign in to comment.