/// <summary>
        /// Inspects the syntax tree and reports the comments starting with the expected prefixes.
        /// </summary>
        /// <param name="tree">Parsed syntax tree</param>
        /// <param name="rules">Rules of the inspection</param>
        /// <returns>List of comments starting with the given prefixes</returns>
        public static IEnumerable <Record> Inspect(SyntaxTree tree, Rules rules)
        {
            var root = (CompilationUnitSyntax)tree.GetRoot();

            var irrelevantRecord = new Record("", "", 0, 0, Status.Ok);

            IEnumerable <Record> records =
                root.DescendantTrivia()
                // Beware: ToString() is an expensive operation on Syntax Nodes and
                // involves some complex logic and a string builder!
                // Hence we convert the trivia to string only at this single place.
                .Select((trivia) => (trivia, trivia.ToString()))
                .Select(
                    ((SyntaxTrivia, string)t) =>
            {
                var(trivia, triviaAsString) = t;

                var result = Text.Inspect(triviaAsString, rules);
                if (result == null)
                {
                    return(irrelevantRecord);
                }

                var span     = tree.GetLineSpan(trivia.Span);
                var position = span.StartLinePosition;
                var line     = position.Line;
                var column   = position.Character;

                return(new Record(result.Prefix, result.Suffix, line, column, result.Status));
            })
예제 #2
0
        /// <summary>
        /// Inspects the syntax tree and reports the comments which seem to be dead code.
        /// </summary>
        /// <param name="tree">Parsed syntax tree</param>
        /// <returns>List of problematic comments</returns>
        public static IEnumerable <Suspect> Inspect(SyntaxTree tree)
        {
            IEnumerable <SuspectTrivia> suspectTrivias = InspectTrivias(tree);

            return(suspectTrivias.Select(
                       (suspectTrivia) =>
            {
                var span = tree.GetLineSpan(suspectTrivia.Trivia.Span);
                var position = span.StartLinePosition;

                return new Suspect(position.Line, position.Character, suspectTrivia.Cues);
            }));
        }
예제 #3
0
        private static IEnumerable <SuspectTrivia> InspectTrivias(SyntaxTree tree)
        {
            var root = (CompilationUnitSyntax)tree.GetRoot();

            var tracker = new OnOffTracker();

            var relevantTrivias =
                root.DescendantTrivia()
                // Beware: ToString() is an expensive operation on Syntax Nodes and
                // involves some complex logic and a string builder!
                // Hence we convert the trivia to string only at this single place.
                .Select((trivia) => (trivia, trivia.ToString()))
                .Where(
                    (t) =>
            {
                var(_, triviaAsString) = t;
                tracker.Feed(triviaAsString);

                return(tracker.IsOn &&
                       TriviaIsComment(triviaAsString) &&
                       !ShouldSkipTrivia(triviaAsString));
            });

            foreach (var(trivia, triviaAsString) in relevantTrivias)
            {
                var span     = tree.GetLineSpan(trivia.Span);
                var position = span.StartLinePosition;

                List <Cue>?cues = InspectComment(position.Line, position.Character, triviaAsString);

                if (cues != null)
                {
                    yield return(new SuspectTrivia(trivia, cues));
                }
            }
        }