示例#1
0
        private void HandleMethodDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
        {
            var node = (MethodDeclarationSyntax)context.Node;

            if (node.Identifier.IsMissing)
            {
                return;
            }

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

            this.HandleDeclaration(context, settings, needsComment, node, node.Identifier.GetLocation());
        }
示例#2
0
        private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
        {
            TypeDeclarationSyntax typeDeclaration = (TypeDeclarationSyntax)context.Node;

            if (typeDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword))
            {
                // This is handled by SA1619
                return;
            }

            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);
        }
            public static void HandleEnumMemberDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
            {
                if (context.GetDocumentationMode() == DocumentationMode.None)
                {
                    return;
                }

                EnumMemberDeclarationSyntax declaration = (EnumMemberDeclarationSyntax)context.Node;
                Accessibility declaredAccessibility     = declaration.GetDeclaredAccessibility();
                Accessibility effectiveAccessibility    = declaration.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);

                if (SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, declaration.Kind(), declaration.Parent.Kind(), declaredAccessibility, effectiveAccessibility))
                {
                    if (!XmlCommentHelper.HasDocumentation(declaration))
                    {
                        context.ReportDiagnostic(Diagnostic.Create(Descriptor, declaration.Identifier.GetLocation()));
                    }
                }
            }
        private void HandleTypeDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
        {
            var node = (BaseTypeDeclarationSyntax)context.Node;

            if (node.Identifier.IsMissing)
            {
                return;
            }

            if (node.Modifiers.Any(SyntaxKind.PartialKeyword))
            {
                // partial elements are handled by PartialElementDocumentationSummaryBase
                return;
            }

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

            this.HandleDeclaration(context, needsComment, node, node.Identifier.GetLocation());
        }
示例#5
0
        private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
        {
            TypeDeclarationSyntax typeDeclaration = (TypeDeclarationSyntax)context.Node;

            if (typeDeclaration.TypeParameterList == null)
            {
                // We are only interested in generic types
                return;
            }

            if (!typeDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword))
            {
                return;
            }

            var documentation = typeDeclaration.GetDocumentationCommentTriviaSyntax();

            if (documentation == null)
            {
                // Don't report if the documentation is missing
                return;
            }

            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);

            if (!needsComment)
            {
                // Omitting documentation is allowed for this element.
                return;
            }

            var includeElement = documentation.Content.GetFirstXmlElement(XmlCommentHelper.IncludeXmlTag);

            if (includeElement != null)
            {
                string rawDocumentation;

                var declaration = context.SemanticModel.GetDeclaredSymbol(typeDeclaration, context.CancellationToken);
                if (declaration == null)
                {
                    return;
                }

                rawDocumentation = declaration.GetDocumentationCommentXml(expandIncludes: true, cancellationToken: context.CancellationToken);
                var completeDocumentation = XElement.Parse(rawDocumentation, LoadOptions.None);
                if (completeDocumentation.Nodes().OfType <XElement>().Any(element => element.Name == XmlCommentHelper.InheritdocXmlTag))
                {
                    // Ignore nodes with an <inheritdoc/> tag in the included XML.
                    return;
                }

                if (completeDocumentation.Nodes().OfType <XElement>().All(element => element.Name != XmlCommentHelper.SummaryXmlTag))
                {
                    // Ignore nodes without a <summary> tag.
                    return;
                }

                var typeParameterAttributes = completeDocumentation.Nodes()
                                              .OfType <XElement>()
                                              .Where(element => element.Name == XmlCommentHelper.TypeParamXmlTag)
                                              .Select(element => element.Attribute(XmlCommentHelper.NameArgumentName))
                                              .Where(x => x != null);

                foreach (var parameter in typeDeclaration.TypeParameterList.Parameters)
                {
                    if (!typeParameterAttributes.Any(x => x.Value == parameter.Identifier.ValueText))
                    {
                        context.ReportDiagnostic(Diagnostic.Create(Descriptor, parameter.Identifier.GetLocation(), parameter.Identifier.ValueText));
                    }
                }
            }
            else
            {
                if (documentation.Content.GetFirstXmlElement(XmlCommentHelper.InheritdocXmlTag) != null)
                {
                    // Ignore nodes with an <inheritdoc/> tag.
                    return;
                }

                if (documentation.Content.GetFirstXmlElement(XmlCommentHelper.SummaryXmlTag) == null)
                {
                    // Ignore nodes without a <summary> tag.
                    return;
                }

                var xmlParameterNames = documentation.Content.GetXmlElements(XmlCommentHelper.TypeParamXmlTag)
                                        .Select(XmlCommentHelper.GetFirstAttributeOrDefault <XmlNameAttributeSyntax>)
                                        .Where(x => x != null)
                                        .ToImmutableArray();

                foreach (var parameter in typeDeclaration.TypeParameterList.Parameters)
                {
                    if (!xmlParameterNames.Any(x => x.Identifier.Identifier.ValueText == parameter.Identifier.ValueText))
                    {
                        context.ReportDiagnostic(Diagnostic.Create(Descriptor, parameter.Identifier.GetLocation(), parameter.Identifier.ValueText));
                    }
                }
            }
        }