dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.04k stars 4.03k forks source link

'DocumentationCommentTriviaSyntax.Ancestors(true)' does not return any ancestors but 'AncestorsAndSelf(true)' does #46964

Open RalfKoban opened 4 years ago

RalfKoban commented 4 years ago

Version Used: 3.6

Steps to Reproduce:

  1. Get the DocumentationCommentTriviaSyntax that represents the comment in following snippet:
    public class TestMe
    {
    /// <summary>
    /// Some summary.
    /// </summary>
    /// <value>
    /// Some value.
    /// </value>
    private int i;
    }
  2. Invoke SyntaxNode.Ancestors() and SyntaxNode.AncestorsAndSelf(true) on the DocumentationCommentTriviaSyntax.
  3. Compare the results and find out that Ancestors() does neither return the FieldDeclarationSyntax, nor the ClassDeclarationSyntax, nor the CompilationUnitSyntax. In contrast, AncestorsAndSelf() returns them all.

Expected Behavior: Invoking SyntaxNode.Ancestors(true) on a DocumentationCommentTriviaSyntax returns all ancestors, similar to SyntaxNode.AncestorsAndSelf(true), except the trivia (no self).

Actual Behavior: SyntaxNode.Ancestors(true) invoked on a DocumentationCommentTriviaSyntax returns no ancestors.

SyntaxNode.AncestorsAndSelf(true) invoked on the same DocumentationCommentTriviaSyntax not only returns the trivia as expected, but also all the other ancestors that SyntaxNode.Ancestors(true) is missing to return.

CyrusNajmabadi commented 4 years ago
        public IEnumerable<SyntaxNode> Ancestors(bool ascendOutOfTrivia = true)
        {
            return this.Parent?
                .AncestorsAndSelf(ascendOutOfTrivia) ??
                SpecializedCollections.EmptyEnumerable<SyntaxNode>();
        }

should likely be updated to be:

        public IEnumerable<SyntaxNode> Ancestors(bool ascendOutOfTrivia = true)
        {
            return GetParent(this, ascendOutOfTrivia)?
                .AncestorsAndSelf(ascendOutOfTrivia) ??
                SpecializedCollections.EmptyEnumerable<SyntaxNode>();
        }