private static void CheckTrivias(SyntaxTriviaList triviaList, SyntaxTreeAnalysisContext c)
        {
            var pragmaWarnings = triviaList
                                 .Where(t => t.HasStructure)
                                 .Select(t => t.GetStructure())
                                 .OfType <PragmaWarningDirectiveTriviaSyntax>()
                                 .Where(t => t.DisableOrRestoreKeyword.IsKind(SyntaxKind.DisableKeyword));

            foreach (var pragmaWarning in pragmaWarnings)
            {
                var location = Location.Create(pragmaWarning.SyntaxTree,
                                               TextSpan.FromBounds(pragmaWarning.SpanStart, pragmaWarning.DisableOrRestoreKeyword.Span.End));
                c.ReportDiagnosticWhenActive(Diagnostic.Create(rule, location));
            }
        }
Beispiel #2
0
        private void CheckTokenComments(SyntaxToken token, SyntaxTreeAnalysisContext context)
        {
            var tokenLine = token.GetLocation().GetLineSpan().StartLinePosition.Line;

            var comments = token.TrailingTrivia
                           .Where(tr => tr.IsKind(SyntaxKind.CommentTrivia));

            foreach (var comment in comments)
            {
                var location = comment.GetLocation();
                if (location.GetLineSpan().StartLinePosition.Line == tokenLine &&
                    !Regex.IsMatch(comment.ToString(), LegalCommentPattern))
                {
                    context.ReportDiagnosticWhenActive(Diagnostic.Create(rule, location));
                }
            }
        }
        private static void CheckMultilineComment(SyntaxTreeAnalysisContext context, SyntaxTrivia comment)
        {
            var triviaContent = GetTriviaContent(comment);
            var triviaLines   = triviaContent.Split(MetricsBase.LineTerminators, StringSplitOptions.None);

            for (var triviaLineNumber = 0; triviaLineNumber < triviaLines.Length; triviaLineNumber++)
            {
                if (!IsCode(triviaLines[triviaLineNumber]))
                {
                    continue;
                }

                var triviaStartingLineNumber = comment.GetLocation().GetLineSpan().StartLinePosition.Line;
                var lineNumber      = triviaStartingLineNumber + triviaLineNumber;
                var lineSpan        = context.Tree.GetText().Lines[lineNumber].Span;
                var commentLineSpan = lineSpan.Intersection(comment.GetLocation().SourceSpan);

                var location = Location.Create(context.Tree, commentLineSpan ?? lineSpan);
                context.ReportDiagnosticWhenActive(Diagnostic.Create(rule, location));
                return;
            }
        }