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


C# SyntaxTreeAnalysisContext类代码示例

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


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

示例1: 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

示例2: CheckTrivias

        private static void CheckTrivias(IEnumerable<SyntaxTrivia> trivias, SyntaxTreeAnalysisContext context)
        {
            var shouldReport = true;
            foreach (var trivia in trivias)
            {
                // comment start is checked because of  https://github.com/dotnet/roslyn/issues/10003

                if (!trivia.ToFullString().TrimStart().StartsWith("/**", StringComparison.Ordinal) &&
                    trivia.IsKind(SyntaxKind.MultiLineCommentTrivia))
                {
                    CheckMultilineComment(context, trivia);
                    shouldReport = true;
                    continue;
                }

                if (!trivia.ToFullString().TrimStart().StartsWith("///", StringComparison.Ordinal) &&
                    trivia.IsKind(SyntaxKind.SingleLineCommentTrivia) &&
                    shouldReport)
                {
                    var triviaContent = GetTriviaContent(trivia);
                    if (!IsCode(triviaContent))
                    {
                        continue;
                    }

                    context.ReportDiagnostic(Diagnostic.Create(Rule, trivia.GetLocation()));
                    shouldReport = false;
                }
            }
        }
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:30,代码来源:CommentedOutCode.cs

示例3: 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

示例4: HandleSyntaxTree

 public void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
 {
     if (context.Tree.Options.DocumentationMode == DocumentationMode.None)
     {
         Volatile.Write(ref this.documentationAnalysisDisabled, true);
     }
 }
开发者ID:neugenes,项目名称:StyleCopAnalyzers,代码行数:7,代码来源:SA0001XmlCommentAnalysisDisabled.cs

示例5: 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 fileAttribute = copyrightElement.Attribute("file");
            if (fileAttribute == null)
            {
                // this will be handled by SA1637
                return;
            }

            var fileName = Path.GetFileName(context.Tree.FilePath);

            if (!fileAttribute.Value.Equals(fileName, StringComparison.Ordinal))
            {
                var location = fileHeader.GetElementLocation(context.Tree, copyrightElement);
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, location));
            }
        }
开发者ID:nvincent,项目名称:StyleCopAnalyzers,代码行数:33,代码来源:SA1638FileHeaderFileNameDocumentationMustMatchFileName.cs

示例6: 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

示例7: HandleLessThanToken

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

            switch (token.Parent.Kind())
            {
            case SyntaxKind.TypeArgumentList:
            case SyntaxKind.TypeParameterList:
                break;

            default:
                // not a generic bracket
                return;
            }

            bool firstInLine = token.IsFirstInLine();
            bool precededBySpace = firstInLine || token.IsPrecededByWhitespace();
            bool followedBySpace = token.IsFollowedByWhitespace();

            if (!firstInLine && precededBySpace)
            {
                // Opening generic brackets must not be {preceded} by a space.
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), "preceded"));
            }

            if (followedBySpace)
            {
                // Opening generic brackets must not be {followed} by a space.
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), "followed"));
            }
        }
开发者ID:Akkenar,项目名称:StyleCopAnalyzers,代码行数:34,代码来源:SA1014OpeningGenericBracketsMustBeSpacedCorrectly.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: AnalyzeOpenBrace

        private static void AnalyzeOpenBrace(SyntaxTreeAnalysisContext context, SyntaxToken openBrace)
        {
            var prevToken = openBrace.GetPreviousToken();
            var triviaList = TriviaHelper.MergeTriviaLists(prevToken.TrailingTrivia, openBrace.LeadingTrivia);

            var done = false;
            var eolCount = 0;
            for (var i = triviaList.Count - 1; !done && (i >= 0); i--)
            {
                switch (triviaList[i].Kind())
                {
                case SyntaxKind.WhitespaceTrivia:
                    break;
                case SyntaxKind.EndOfLineTrivia:
                    eolCount++;
                    break;
                default:
                    if (triviaList[i].IsDirective)
                    {
                        // These have a built-in end of line
                        eolCount++;
                    }

                    done = true;
                    break;
                }
            }

            if (eolCount < 2)
            {
                return;
            }

            context.ReportDiagnostic(Diagnostic.Create(Descriptor, openBrace.GetLocation()));
        }
开发者ID:ursenzler,项目名称:StyleCopAnalyzers,代码行数:35,代码来源:SA1509OpeningBracesMustNotBePrecededByBlankLine.cs

示例10: HandleCloseBracketToken

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

            if (!token.Parent.IsKind(SyntaxKind.AttributeList))
            {
                return;
            }

            if (token.IsFirstInLine())
            {
                return;
            }

            SyntaxToken precedingToken = token.GetPreviousToken();
            if (!precedingToken.HasTrailingTrivia)
            {
                return;
            }

            if (!precedingToken.TrailingTrivia.Last().IsKind(SyntaxKind.WhitespaceTrivia))
            {
                return;
            }

            // Closing attribute brackets must not be preceded by a space.
            context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingProperties.RemoveImmediatePreceding));
        }
开发者ID:EdwinEngelen,项目名称:StyleCopAnalyzers,代码行数:31,代码来源:SA1017ClosingAttributeBracketsMustBeSpacedCorrectly.cs

示例11: 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

示例12: 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));

            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:endjin,项目名称:StyleCopAnalyzers,代码行数:35,代码来源:SA1402FileMayOnlyContainASingleClass.cs

示例13: HandleOpenBracketToken

        private static void HandleOpenBracketToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
        {
            bool firstInLine = token.IsFirstInLine();
            bool precededBySpace = true;
            bool ignorePrecedingSpaceProblem = false;

            if (!firstInLine)
            {
                precededBySpace = token.IsPrecededByWhitespace();

                // ignore if handled by SA1026
                ignorePrecedingSpaceProblem = precededBySpace && token.GetPreviousToken().IsKind(SyntaxKind.NewKeyword);
            }

            bool followedBySpace = token.IsFollowedByWhitespace();
            bool lastInLine = token.IsLastInLine();

            if (!firstInLine && precededBySpace && !ignorePrecedingSpaceProblem && !lastInLine && followedBySpace)
            {
                // Opening square bracket must {neither preceded nor followed} by a space.
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), "neither preceded nor followed"));
            }
            else if (!firstInLine && precededBySpace && !ignorePrecedingSpaceProblem)
            {
                // Opening square bracket must {not be preceded} by a space.
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), "not be preceded"));
            }
            else if (!lastInLine && followedBySpace)
            {
                // Opening square bracket must {not be followed} by a space.
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), "not be followed"));
            }
        }
开发者ID:nvincent,项目名称:StyleCopAnalyzers,代码行数:33,代码来源:SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs

示例14: 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

示例15: 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


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