|
| 1 | +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +namespace StyleCop.Analyzers.DocumentationRules |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.Collections.Generic; |
| 8 | + using System.Linq; |
| 9 | + using System.Xml.Linq; |
| 10 | + using Microsoft.CodeAnalysis; |
| 11 | + using Microsoft.CodeAnalysis.CSharp; |
| 12 | + using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 13 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 14 | + using StyleCop.Analyzers.Helpers; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// This is the base class for analyzers which examine the <c><param></c> text of a documentation comment on an element declaration. |
| 18 | + /// </summary> |
| 19 | + internal abstract class ElementDocumentationParameterBase : DiagnosticAnalyzer |
| 20 | + { |
| 21 | + private readonly Action<CompilationStartAnalysisContext> compilationStartAction; |
| 22 | + private readonly Action<SyntaxNodeAnalysisContext> methodDeclarationAction; |
| 23 | + private readonly Action<SyntaxNodeAnalysisContext> constructorDeclarationAction; |
| 24 | + private readonly Action<SyntaxNodeAnalysisContext> delegateDeclarationAction; |
| 25 | + private readonly Action<SyntaxNodeAnalysisContext> indexerDeclarationAction; |
| 26 | + private readonly Action<SyntaxNodeAnalysisContext> operatorDeclarationAction; |
| 27 | + private readonly Action<SyntaxNodeAnalysisContext> conversionOperatorDeclarationAction; |
| 28 | + |
| 29 | + protected ElementDocumentationParameterBase() |
| 30 | + { |
| 31 | + this.compilationStartAction = this.HandleCompilationStart; |
| 32 | + this.methodDeclarationAction = this.HandleMethodDeclaration; |
| 33 | + this.constructorDeclarationAction = this.HandleConstructorDeclaration; |
| 34 | + this.delegateDeclarationAction = this.HandleDelegateDeclaration; |
| 35 | + this.indexerDeclarationAction = this.HandleIndexerDeclaration; |
| 36 | + this.operatorDeclarationAction = this.HandleOperatorDeclaration; |
| 37 | + this.conversionOperatorDeclarationAction = this.HandleConversionOperatorDeclaration; |
| 38 | + } |
| 39 | + |
| 40 | + /// <inheritdoc/> |
| 41 | + public override void Initialize(AnalysisContext context) |
| 42 | + { |
| 43 | + context.RegisterCompilationStartAction(this.compilationStartAction); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Analyzes the top-level <c><param></c> elements of a documentation comment. |
| 48 | + /// </summary> |
| 49 | + /// <param name="context">The current analysis context.</param> |
| 50 | + /// <param name="syntaxList">The <see cref="XmlElementSyntax"/> or <see cref="XmlEmptyElementSyntax"/> of the node |
| 51 | + /// to examine.</param> |
| 52 | + /// <param name="completeDocumentation">The complete documentation for the declared symbol, with any |
| 53 | + /// <c><include></c> elements expanded. If the XML documentation comment included a <c><param></c> |
| 54 | + /// element, this value will be <see langword="null"/>, even if the XML documentation comment also included an |
| 55 | + /// <c><include></c> element.</param> |
| 56 | + /// <param name="diagnosticLocations">The location(s) where diagnostics, if any, should be reported.</param> |
| 57 | + protected abstract void HandleXmlElement(SyntaxNodeAnalysisContext context, IEnumerable<XmlNodeSyntax> syntaxList, XElement completeDocumentation, params Location[] diagnosticLocations); |
| 58 | + |
| 59 | + private void HandleCompilationStart(CompilationStartAnalysisContext context) |
| 60 | + { |
| 61 | + context.RegisterSyntaxNodeActionHonorExclusions(this.methodDeclarationAction, SyntaxKind.MethodDeclaration); |
| 62 | + context.RegisterSyntaxNodeActionHonorExclusions(this.constructorDeclarationAction, SyntaxKind.ConstructorDeclaration); |
| 63 | + context.RegisterSyntaxNodeActionHonorExclusions(this.delegateDeclarationAction, SyntaxKind.DelegateDeclaration); |
| 64 | + context.RegisterSyntaxNodeActionHonorExclusions(this.indexerDeclarationAction, SyntaxKind.IndexerDeclaration); |
| 65 | + context.RegisterSyntaxNodeActionHonorExclusions(this.operatorDeclarationAction, SyntaxKind.OperatorDeclaration); |
| 66 | + context.RegisterSyntaxNodeActionHonorExclusions(this.conversionOperatorDeclarationAction, SyntaxKind.ConversionOperatorDeclaration); |
| 67 | + } |
| 68 | + |
| 69 | + private void HandleMethodDeclaration(SyntaxNodeAnalysisContext context) |
| 70 | + { |
| 71 | + var node = (MethodDeclarationSyntax)context.Node; |
| 72 | + if (node.Identifier.IsMissing) |
| 73 | + { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + this.HandleDeclaration(context, node, node.Identifier.GetLocation()); |
| 78 | + } |
| 79 | + |
| 80 | + private void HandleConstructorDeclaration(SyntaxNodeAnalysisContext context) |
| 81 | + { |
| 82 | + var node = (ConstructorDeclarationSyntax)context.Node; |
| 83 | + if (node.Identifier.IsMissing) |
| 84 | + { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + this.HandleDeclaration(context, node, node.Identifier.GetLocation()); |
| 89 | + } |
| 90 | + |
| 91 | + private void HandleDelegateDeclaration(SyntaxNodeAnalysisContext context) |
| 92 | + { |
| 93 | + var node = (DelegateDeclarationSyntax)context.Node; |
| 94 | + if (node.Identifier.IsMissing) |
| 95 | + { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + this.HandleDeclaration(context, node, node.Identifier.GetLocation()); |
| 100 | + } |
| 101 | + |
| 102 | + private void HandleIndexerDeclaration(SyntaxNodeAnalysisContext context) |
| 103 | + { |
| 104 | + var node = (IndexerDeclarationSyntax)context.Node; |
| 105 | + if (node.ThisKeyword.IsMissing) |
| 106 | + { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + this.HandleDeclaration(context, node, node.ThisKeyword.GetLocation()); |
| 111 | + } |
| 112 | + |
| 113 | + private void HandleOperatorDeclaration(SyntaxNodeAnalysisContext context) |
| 114 | + { |
| 115 | + var node = (OperatorDeclarationSyntax)context.Node; |
| 116 | + if (node.OperatorToken.IsMissing) |
| 117 | + { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + this.HandleDeclaration(context, node, node.OperatorToken.GetLocation()); |
| 122 | + } |
| 123 | + |
| 124 | + private void HandleConversionOperatorDeclaration(SyntaxNodeAnalysisContext context) |
| 125 | + { |
| 126 | + var node = (ConversionOperatorDeclarationSyntax)context.Node; |
| 127 | + |
| 128 | + this.HandleDeclaration(context, node, node.GetLocation()); |
| 129 | + } |
| 130 | + |
| 131 | + private void HandleDeclaration(SyntaxNodeAnalysisContext context, SyntaxNode node, params Location[] locations) |
| 132 | + { |
| 133 | + var documentation = node.GetDocumentationCommentTriviaSyntax(); |
| 134 | + if (documentation == null) |
| 135 | + { |
| 136 | + // missing documentation is reported by SA1600, SA1601, and SA1602 |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + if (documentation.Content.GetFirstXmlElement(XmlCommentHelper.InheritdocXmlTag) != null) |
| 141 | + { |
| 142 | + // Ignore nodes with an <inheritdoc/> tag. |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + XElement completeDocumentation = null; |
| 147 | + var paramXmlElements = documentation.Content.GetXmlElements(XmlCommentHelper.ParamXmlTag); |
| 148 | + if (!paramXmlElements.Any()) |
| 149 | + { |
| 150 | + var includedDocumentation = documentation.Content.GetFirstXmlElement(XmlCommentHelper.IncludeXmlTag); |
| 151 | + if (includedDocumentation != null) |
| 152 | + { |
| 153 | + var declaration = context.SemanticModel.GetDeclaredSymbol(node, context.CancellationToken); |
| 154 | + var rawDocumentation = declaration?.GetDocumentationCommentXml(expandIncludes: true, cancellationToken: context.CancellationToken); |
| 155 | + completeDocumentation = XElement.Parse(rawDocumentation, LoadOptions.None); |
| 156 | + if (completeDocumentation.Nodes().OfType<XElement>().Any(element => element.Name == XmlCommentHelper.InheritdocXmlTag)) |
| 157 | + { |
| 158 | + // Ignore nodes with an <inheritdoc/> tag in the included XML. |
| 159 | + return; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + this.HandleXmlElement(context, paramXmlElements, completeDocumentation, locations); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments