当前位置: 首页>>代码示例>>C#>>正文


C# SyntaxTreeAnalysisContext.ReportDiagnostic方法代码示例

本文整理汇总了C#中SyntaxTreeAnalysisContext.ReportDiagnostic方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxTreeAnalysisContext.ReportDiagnostic方法的具体用法?C# SyntaxTreeAnalysisContext.ReportDiagnostic怎么用?C# SyntaxTreeAnalysisContext.ReportDiagnostic使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SyntaxTreeAnalysisContext的用法示例。


在下文中一共展示了SyntaxTreeAnalysisContext.ReportDiagnostic方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: HandleSyntaxTree

        private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
        {
            if (context.Tree.IsWhitespaceOnly(context.CancellationToken))
            {
                // Handling of empty documents is now the responsibility of the analyzers
                return;
            }

            var firstToken = context.Tree.GetRoot().GetFirstToken(includeZeroWidth: true);

            if (firstToken.HasLeadingTrivia)
            {
                var leadingTrivia = firstToken.LeadingTrivia;

                var firstNonBlankLineTriviaIndex = TriviaHelper.IndexOfFirstNonBlankLineTrivia(leadingTrivia);
                switch (firstNonBlankLineTriviaIndex)
                {
                case 0:
                    // no blank lines
                    break;

                case -1:
                    // only blank lines
                    context.ReportDiagnostic(Diagnostic.Create(Descriptor, Location.Create(context.Tree, leadingTrivia.Span)));
                    break;

                default:
                    var textSpan = TextSpan.FromBounds(leadingTrivia[0].Span.Start, leadingTrivia[firstNonBlankLineTriviaIndex].Span.Start);
                    context.ReportDiagnostic(Diagnostic.Create(Descriptor, Location.Create(context.Tree, textSpan)));
                    break;
                }
            }
        }
开发者ID:EdwinEngelen,项目名称:StyleCopAnalyzers,代码行数:33,代码来源:SA1517CodeMustNotContainBlankLinesAtStartOfFile.cs

示例2: HandleSyntaxTree

        private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
        {
            var firstToken = context.Tree.GetRoot().GetFirstToken(includeZeroWidth: true);

            if (firstToken.HasLeadingTrivia)
            {
                var leadingTrivia = firstToken.LeadingTrivia;

                var firstNonBlankLineTriviaIndex = TriviaHelper.IndexOfFirstNonBlankLineTrivia(leadingTrivia);
                switch (firstNonBlankLineTriviaIndex)
                {
                case 0:
                    // no blank lines
                    break;

                case -1:
                    // only blank lines
                    context.ReportDiagnostic(Diagnostic.Create(Descriptor, Location.Create(context.Tree, leadingTrivia.Span)));
                    break;

                default:
                    var textSpan = TextSpan.FromBounds(leadingTrivia[0].Span.Start, leadingTrivia[firstNonBlankLineTriviaIndex].Span.Start);
                    context.ReportDiagnostic(Diagnostic.Create(Descriptor, Location.Create(context.Tree, textSpan)));
                    break;
                }
            }
        }
开发者ID:neugenes,项目名称:StyleCopAnalyzers,代码行数:27,代码来源:SA1517CodeMustNotContainBlankLinesAtStartOfFile.cs

示例3: HandleSyntaxTreeAxtion

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

            var companyAttribute = copyrightElement.Attribute("company");
            if (string.IsNullOrWhiteSpace(companyAttribute?.Value))
            {
                var location = fileHeader.GetElementLocation(context.Tree, copyrightElement);
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, location));
            }
        }
开发者ID:nvincent,项目名称:StyleCopAnalyzers,代码行数:25,代码来源:SA1640FileHeaderMustHaveValidCompanyText.cs

示例4: AnalyzeTree

        private static void AnalyzeTree(SyntaxTreeAnalysisContext context)
        {
            var tree = context.Tree;
            var emptyStrings = tree.GetRoot().DescendantTokens()
                .Where(x => x.RawKind == (int)SyntaxKind.StringLiteralToken
                       && string.IsNullOrEmpty(x.ValueText)).ToList();

            foreach (var s in emptyStrings)
            {
                // Skip if it is inside method parameter definition or as case switch or a attribute argument.
                if (s.Parent.Parent.Parent.IsKind(SyntaxKind.Parameter) ||
                    s.Parent.Parent.IsKind(SyntaxKind.CaseSwitchLabel) ||
                    s.Parent.Parent.IsKind(SyntaxKind.AttributeArgument))
                {
                    continue;
                }

                FieldDeclarationSyntax fieldSyntax = s.Parent.Parent.Parent.Parent.Parent as FieldDeclarationSyntax;
                if (fieldSyntax != null && fieldSyntax.DescendantTokens().Any(x => x.IsKind(SyntaxKind.ConstKeyword)))
                {
                    continue;
                }

                var line = s.SyntaxTree.GetLineSpan(s.FullSpan);
                var diagnostic = Diagnostic.Create(Rule, s.GetLocation());

                context.ReportDiagnostic(diagnostic);
            }
        }
开发者ID:jernejk,项目名称:Roslyn-simple-demos,代码行数:29,代码来源:DiagnosticAnalyzer.cs

示例5: HandleOpenBraceToken

        private static void HandleOpenBraceToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
        {
            if (token.IsMissing)
            {
                return;
            }

            bool followedBySpace = token.IsFollowedByWhitespace();

            if (token.Parent is InterpolationSyntax)
            {
                if (followedBySpace)
                {
                    // Opening curly bracket must{} be {followed} by a space.
                    context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), " not", "followed"));
                }

                return;
            }

            bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace();

            if (!precededBySpace)
            {
                // Opening curly bracket must{} be {preceded} by a space.
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), string.Empty, "preceded"));
            }

            if (!token.IsLastInLine() && !followedBySpace)
            {
                // Opening curly bracket must{} be {followed} by a space.
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), string.Empty, "followed"));
            }
        }
开发者ID:nukefusion,项目名称:StyleCopAnalyzers,代码行数:34,代码来源:SA1012OpeningCurlyBracketsMustBeSpacedCorrectly.cs

示例6: HandleSyntaxTree

        // If you want a full implementation of this analyzer with system tests and a code fix, go to
        // https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1120CommentsMustContainText.cs
        private void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
        {
            SyntaxNode root = context.Tree.GetCompilationUnitRoot(context.CancellationToken);

            foreach (var node in root.DescendantTrivia())
            {
                switch (node.Kind())
                {
                    case SyntaxKind.SingleLineCommentTrivia:
                        // Remove the leading // from the comment
                        var commentText = node.ToString().Substring(2);
                        int index = 0;

                        var list = TriviaHelper.GetContainingTriviaList(node, out index);
                        bool isFirst = IsFirstComment(list, index);
                        bool isLast = IsLastComment(list, index);

                        if (string.IsNullOrWhiteSpace(commentText) && (isFirst || isLast))
                        {
                            var diagnostic = Diagnostic.Create(Rule, node.GetLocation());
                            context.ReportDiagnostic(diagnostic);
                        }

                        break;
                }
            }
        }
开发者ID:johnkoerner,项目名称:AnalyzerSamples,代码行数:29,代码来源:EmptyCommentAnalyzer.cs

示例7: HandleSyntaxTree

        private void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
        {
            var syntaxRoot = context.Tree.GetRoot(context.CancellationToken);

            var descentNodes = syntaxRoot.DescendantNodes(descendIntoChildren: node => node != null && !node.IsKind(SyntaxKind.ClassDeclaration));

            string foundClassName = null;
            bool isPartialClass = false;

            foreach (var node in descentNodes)
            {
                if (node.IsKind(SyntaxKind.ClassDeclaration))
                {
                    ClassDeclarationSyntax classDeclaration = node as ClassDeclarationSyntax;
                    if (foundClassName != null)
                    {
                        if (isPartialClass && foundClassName == classDeclaration.Identifier.Text)
                        {
                            continue;
                        }

                        var location = NamedTypeHelpers.GetNameOrIdentifierLocation(node);
                        if (location != null)
                        {
                            context.ReportDiagnostic(Diagnostic.Create(Descriptor, location));
                        }
                    }
                    else
                    {
                        foundClassName = classDeclaration.Identifier.Text;
                        isPartialClass = classDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword);
                    }
                }
            }
        }
开发者ID:nvincent,项目名称:StyleCopAnalyzers,代码行数:35,代码来源:SA1402FileMayOnlyContainASingleClass.cs

示例8: HandleSyntaxTree

        private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
        {
            var syntaxRoot = context.Tree.GetRoot(context.CancellationToken);

            var descentNodes = syntaxRoot.DescendantNodes(descendIntoChildren: node => node != null && !node.IsKind(SyntaxKind.ClassDeclaration));

            bool foundNode = false;

            foreach (var node in descentNodes)
            {
                if (node.IsKind(SyntaxKind.NamespaceDeclaration))
                {
                    if (foundNode)
                    {
                        var location = NamedTypeHelpers.GetNameOrIdentifierLocation(node);
                        if (location != null)
                        {
                            context.ReportDiagnostic(Diagnostic.Create(Descriptor, location));
                        }
                    }
                    else
                    {
                        foundNode = true;
                    }
                }
            }
        }
开发者ID:JaRau,项目名称:StyleCopAnalyzers,代码行数:27,代码来源:SA1403FileMayOnlyContainASingleNamespace.cs

示例9: HandleSyntaxTree

 private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
 {
     if (context.Tree.Options.DocumentationMode != DocumentationMode.Diagnose)
     {
         context.ReportDiagnostic(Diagnostic.Create(Descriptor, context.Tree.GetLocation(new TextSpan(0, 0))));
     }
 }
开发者ID:journeyman,项目名称:StyleCopAnalyzers,代码行数:7,代码来源:SA1652EnableXmlDocumentationOutput.cs

示例10: AnalyzeType

 private void AnalyzeType(SyntaxTreeAnalysisContext context, TypeDeclarationSyntax typeDeclaration)
 {
     var numberOfFields = typeDeclaration.Members.Count(member => member is FieldDeclarationSyntax);
     if (numberOfFields > MaximumNumberOfFields)
     {
         context.ReportDiagnostic(Diagnostic.Create(Rule, typeDeclaration.Identifier.GetLocation(), typeDeclaration.Identifier.Text, numberOfFields));
     }
 }
开发者ID:johannes-schmitt,项目名称:DaVinci,代码行数:8,代码来源:Oc8NoClassesWithMoreThanTwoFields.cs

示例11: AnalyzeSyntaxTree

        private void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context)
        {
            var diagnostics = RenameTrackingTaggerProvider.GetDiagnosticsAsync(context.Tree, DiagnosticDescriptor, context.CancellationToken).WaitAndGetResult(context.CancellationToken);

            foreach (var diagnostic in diagnostics)
            {
                context.ReportDiagnostic(diagnostic);
            }
        }
开发者ID:noahstein,项目名称:roslyn,代码行数:9,代码来源:RenameTrackingDiagnosticAnalyzer.cs

示例12: HandleSyntaxTree

        private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
        {
            byte[] preamble = context.Tree.Encoding.GetPreamble();

            if (!IsUtf8Preamble(preamble))
            {
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, Location.Create(context.Tree, TextSpan.FromBounds(0, 0))));
            }
        }
开发者ID:nvincent,项目名称:StyleCopAnalyzers,代码行数:9,代码来源:SA1412StoreFilesAsUtf8.cs

示例13: HandleWhitespaceTrivia

        private void HandleWhitespaceTrivia(SyntaxTreeAnalysisContext context, SyntaxTrivia trivia)
        {
            if (trivia.ToFullString().IndexOf('\t') < 0)
            {
                return;
            }

            // Tabs must not be used.
            context.ReportDiagnostic(Diagnostic.Create(Descriptor, trivia.GetLocation()));
        }
开发者ID:nvincent,项目名称:StyleCopAnalyzers,代码行数:10,代码来源:SA1027TabsMustNotBeUsed.cs

示例14: HandleSyntaxTree

        private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
        {
            byte[] preamble = context.Tree.Encoding.GetPreamble();

            if (!IsUtf8Preamble(preamble))
            {
                ImmutableDictionary<string, string> properties = ImmutableDictionary<string, string>.Empty.SetItem(EncodingProperty, context.Tree.Encoding?.WebName ?? "<null>");
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, Location.Create(context.Tree, TextSpan.FromBounds(0, 0)), properties));
            }
        }
开发者ID:Noryoko,项目名称:StyleCopAnalyzers,代码行数:10,代码来源:SA1412StoreFilesAsUtf8.cs

示例15: AnalyzeSyntaxTree

 private static void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context)
 {
     // Find source files with documentation comment diagnostics turned off.
     if (context.Tree.Options.DocumentationMode != DocumentationMode.Diagnose)
     {
         // For all such files, produce a diagnostic.
         var diagnostic = Diagnostic.Create(Rule, Location.None, Path.GetFileName(context.Tree.FilePath));
         context.ReportDiagnostic(diagnostic);
     }
 }
开发者ID:CAPCHIK,项目名称:roslyn,代码行数:10,代码来源:SyntaxTreeAnalyzer.cs


注:本文中的SyntaxTreeAnalysisContext.ReportDiagnostic方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。