private static async Task <SyntaxNode> GetTransformedSyntaxRootAsync(Document document, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(root.SyntaxTree, cancellationToken);

            var        fileHeader = FileHeaderHelpers.ParseFileHeader(root);
            SyntaxNode newSyntaxRoot;

            if (fileHeader.IsMissing)
            {
                newSyntaxRoot = AddHeader(document, root, GetFileName(document), settings);
            }
            else
            {
                var trivia       = root.GetLeadingTrivia();
                var commentIndex = TriviaHelper.IndexOfFirstNonWhitespaceTrivia(trivia, false);

                // Safe to do this as fileHeader.IsMissing is false.
                var isMultiLineComment = trivia[commentIndex].IsKind(SyntaxKind.MultiLineCommentTrivia);

                var xmlFileHeader = FileHeaderHelpers.ParseXmlFileHeader(root);
                if (isMultiLineComment && !xmlFileHeader.IsMalformed)
                {
                    newSyntaxRoot = ReplaceWellFormedMultiLineCommentHeader(document, root, settings, commentIndex, xmlFileHeader);
                }
                else
                {
                    newSyntaxRoot = ReplaceHeader(document, root, settings, xmlFileHeader.IsMalformed);
                }
            }

            return(newSyntaxRoot);
        }
Example #2
0
        private static void HandleSyntaxTreeAxtion(SyntaxTreeAnalysisContext context)
        {
            var root = context.Tree.GetRoot(context.CancellationToken);

            var fileHeader = FileHeaderHelpers.ParseFileHeader(root);

            if (fileHeader.IsMissing || fileHeader.IsMalformed)
            {
                // this will be handled by SA1633
                return;
            }

            var summaryElement = fileHeader.GetElement("summary");

            if (summaryElement == null)
            {
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, fileHeader.GetLocation(context.Tree)));
                return;
            }

            if (string.IsNullOrWhiteSpace(summaryElement.Value))
            {
                var location = fileHeader.GetElementLocation(context.Tree, summaryElement);
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, location));
            }
        }
        private static void HandleSyntaxTreeAxtion(SyntaxTreeAnalysisContext context)
        {
            var root = context.Tree.GetRoot(context.CancellationToken);

            var fileHeader = FileHeaderHelpers.ParseFileHeader(root);

            if (fileHeader.IsMissing || fileHeader.IsMalformed)
            {
                // this will be handled by SA1633
                return;
            }

            var copyrightElement = fileHeader.GetElement("copyright");

            if (copyrightElement == null)
            {
                // this will be handled by SA1634
                return;
            }

            if (copyrightElement.Attribute("file") == null)
            {
                var location = fileHeader.GetElementLocation(context.Tree, copyrightElement);
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, location));
            }
        }
        private static void HandleSyntaxTreeAxtion(SyntaxTreeAnalysisContext context)
        {
            var root = context.Tree.GetRoot(context.CancellationToken);

            // don't process empty files
            if (root.FullSpan.IsEmpty)
            {
                return;
            }

            var fileHeader = FileHeaderHelpers.ParseFileHeader(root);

            if (fileHeader.IsMissing)
            {
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, fileHeader.GetLocation(context.Tree), "is missing or not located at the top of the file."));
            }
            else if (fileHeader.IsMalformed)
            {
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, fileHeader.GetLocation(context.Tree), "XML is invalid."));
            }
        }
            public static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCopSettings settings, Compilation compilation)
            {
                var root = context.Tree.GetRoot(context.CancellationToken);

                // don't process empty files
                if (root.FullSpan.IsEmpty)
                {
                    return;
                }

                if (settings.DocumentationRules.XmlHeader)
                {
                    var fileHeader = FileHeaderHelpers.ParseXmlFileHeader(root);
                    if (fileHeader.IsMissing)
                    {
                        context.ReportDiagnostic(Diagnostic.Create(SA1633DescriptorMissing, fileHeader.GetLocation(context.Tree)));
                        return;
                    }

                    if (fileHeader.IsMalformed)
                    {
                        context.ReportDiagnostic(Diagnostic.Create(SA1633DescriptorMalformed, fileHeader.GetLocation(context.Tree)));
                        return;
                    }

                    if (!compilation.IsAnalyzerSuppressed(SA1634Descriptor))
                    {
                        CheckCopyrightHeader(context, settings.DocumentationRules, compilation, fileHeader);
                    }

                    if (!compilation.IsAnalyzerSuppressed(SA1639Descriptor))
                    {
                        CheckSummaryHeader(context, compilation, fileHeader);
                    }
                }
                else
                {
                    var fileHeader = FileHeaderHelpers.ParseFileHeader(root);
                    if (fileHeader.IsMissing)
                    {
                        context.ReportDiagnostic(Diagnostic.Create(SA1633DescriptorMissing, fileHeader.GetLocation(context.Tree)));
                        return;
                    }

                    if (!compilation.IsAnalyzerSuppressed(SA1635Descriptor))
                    {
                        if (string.IsNullOrWhiteSpace(fileHeader.CopyrightText))
                        {
                            context.ReportDiagnostic(Diagnostic.Create(SA1635Descriptor, fileHeader.GetLocation(context.Tree)));
                            return;
                        }

                        if (compilation.IsAnalyzerSuppressed(SA1636Descriptor))
                        {
                            return;
                        }

                        if (!CompareCopyrightText(context, settings.DocumentationRules, fileHeader.CopyrightText))
                        {
                            context.ReportDiagnostic(Diagnostic.Create(SA1636Descriptor, fileHeader.GetLocation(context.Tree)));
                            return;
                        }
                    }
                }
            }
        private static void HandleSyntaxTreeAxtion(SyntaxTreeAnalysisContext context, Compilation compilation)
        {
            var root     = context.Tree.GetRoot(context.CancellationToken);
            var settings = context.GetStyleCopSettings();

            // don't process empty files
            if (root.FullSpan.IsEmpty)
            {
                return;
            }

            if (settings.DocumentationRules.XmlHeader)
            {
                var fileHeader = FileHeaderHelpers.ParseXmlFileHeader(root);
                if (fileHeader.IsMissing)
                {
                    context.ReportDiagnostic(Diagnostic.Create(SA1633DescriptorMissing, fileHeader.GetLocation(context.Tree)));
                    return;
                }

                if (fileHeader.IsMalformed)
                {
                    context.ReportDiagnostic(Diagnostic.Create(SA1633DescriptorMalformed, fileHeader.GetLocation(context.Tree)));
                    return;
                }

                if (!compilation.IsAnalyzerSuppressed(SA1634Identifier))
                {
                    CheckCopyrightHeader(context, compilation, settings, fileHeader);
                }

                if (!compilation.IsAnalyzerSuppressed(SA1639Identifier))
                {
                    CheckSummaryHeader(context, compilation, fileHeader);
                }
            }
            else
            {
                var fileHeader = FileHeaderHelpers.ParseFileHeader(root);
                if (fileHeader.IsMissing)
                {
                    context.ReportDiagnostic(Diagnostic.Create(SA1633DescriptorMissing, fileHeader.GetLocation(context.Tree)));
                    return;
                }

                if (!compilation.IsAnalyzerSuppressed(SA1635Identifier))
                {
                    if (string.IsNullOrWhiteSpace(fileHeader.CopyrightText))
                    {
                        context.ReportDiagnostic(Diagnostic.Create(SA1635Descriptor, fileHeader.GetLocation(context.Tree)));
                        return;
                    }

                    if (compilation.IsAnalyzerSuppressed(SA1636Identifier))
                    {
                        return;
                    }

                    // make sure that both \n and \r\n are accepted from the settings.
                    var reformattedCopyrightText = settings.DocumentationRules.CopyrightText.Replace("\r\n", "\n").Replace("\n", Environment.NewLine);
                    if (string.CompareOrdinal(fileHeader.CopyrightText, reformattedCopyrightText) != 0)
                    {
                        context.ReportDiagnostic(Diagnostic.Create(SA1636Descriptor, fileHeader.GetLocation(context.Tree)));
                    }
                }
            }
        }