public static ImmutableArray <string> GetAttributeValues(DocumentationCommentTriviaSyntax comment, string elementName1, string elementName2, string attributeName)
        {
            HashSet <string> values = null;
            bool             containsIncludeOrExclude = false;
            bool             isFirst = true;

            foreach (XmlNodeSyntax node in comment.Content)
            {
                XmlElementInfo info;
                if (XmlElementInfo.TryCreate(node, out info))
                {
                    switch (info.ElementKind)
                    {
                    case XmlElementKind.Include:
                    case XmlElementKind.Exclude:
                    {
                        if (isFirst)
                        {
                            containsIncludeOrExclude = true;
                        }

                        break;
                    }

                    case XmlElementKind.InheritDoc:
                    {
                        return(default(ImmutableArray <string>));
                    }

                    default:
                    {
                        if (info.IsXmlElement &&
                            info.IsLocalName(elementName1, elementName2))
                        {
                            string value = GetAttributeValue((XmlElementSyntax)info.Element, attributeName);

                            if (value != null)
                            {
                                (values ?? (values = new HashSet <string>())).Add(value);
                            }
                        }

                        break;
                    }
                    }

                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        containsIncludeOrExclude = false;
                    }
                }
            }

            if (!containsIncludeOrExclude)
            {
                return(values?.ToImmutableArray() ?? ImmutableArray <string> .Empty);
            }

            return(default(ImmutableArray <string>));
        }
Exemplo n.º 2
0
        public static void AnalyzeSingleLineDocumentationCommentTrivia(SyntaxNodeAnalysisContext context)
        {
            var documentationComment = (DocumentationCommentTriviaSyntax)context.Node;

            if (!IsPartOfMemberDeclaration(documentationComment))
            {
                return;
            }

            bool containsInheritDoc       = false;
            bool containsIncludeOrExclude = false;
            bool containsSummaryElement   = false;
            bool isFirst = true;

            foreach (XmlNodeSyntax node in documentationComment.Content)
            {
                XmlElementInfo info;
                if (XmlElementInfo.TryCreate(node, out info))
                {
                    switch (info.ElementKind)
                    {
                    case XmlElementKind.Include:
                    case XmlElementKind.Exclude:
                    {
                        if (isFirst)
                        {
                            containsIncludeOrExclude = true;
                        }

                        break;
                    }

                    case XmlElementKind.InheritDoc:
                    {
                        containsInheritDoc = true;
                        break;
                    }

                    case XmlElementKind.Summary:
                    {
                        if (info.IsXmlEmptyElement || IsSummaryMissing((XmlElementSyntax)info.Element))
                        {
                            context.ReportDiagnostic(
                                DiagnosticDescriptors.AddSummaryToDocumentationComment,
                                info.Element);
                        }

                        containsSummaryElement = true;
                        break;
                    }
                    }

                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        containsIncludeOrExclude = false;
                    }

                    if (containsInheritDoc && containsSummaryElement)
                    {
                        break;
                    }
                }
            }

            if (!containsSummaryElement &&
                !containsInheritDoc &&
                !containsIncludeOrExclude)
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.AddSummaryElementToDocumentationComment,
                    documentationComment);
            }
        }
Exemplo n.º 3
0
        private static bool CanAddExceptionToComment(
            DocumentationCommentTriviaSyntax comment,
            ITypeSymbol exceptionSymbol,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            bool containsException        = false;
            bool containsIncludeOrExclude = false;
            bool isFirst = true;

            foreach (XmlNodeSyntax node in comment.Content)
            {
                XmlElementInfo info;
                if (XmlElementInfo.TryCreate(node, out info))
                {
                    switch (info.ElementKind)
                    {
                    case XmlElementKind.Include:
                    case XmlElementKind.Exclude:
                    {
                        if (isFirst)
                        {
                            containsIncludeOrExclude = true;
                        }

                        break;
                    }

                    case XmlElementKind.InheritDoc:
                    {
                        return(false);
                    }

                    case XmlElementKind.Exception:
                    {
                        if (!containsException)
                        {
                            if (info.IsXmlElement)
                            {
                                containsException = ContainsException((XmlElementSyntax)info.Element, exceptionSymbol, semanticModel, cancellationToken);
                            }
                            else
                            {
                                containsException = ContainsException((XmlEmptyElementSyntax)info.Element, exceptionSymbol, semanticModel, cancellationToken);
                            }
                        }

                        break;
                    }
                    }

                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        containsIncludeOrExclude = false;
                    }
                }
            }

            return(!containsIncludeOrExclude &&
                   !containsException);
        }