Fix SyntaxNode.Ancestors() to work correctly with structured trivia#80786
Merged
CyrusNajmabadi merged 4 commits intomainfrom Oct 31, 2025
Merged
Fix SyntaxNode.Ancestors() to work correctly with structured trivia#80786CyrusNajmabadi merged 4 commits intomainfrom
CyrusNajmabadi merged 4 commits intomainfrom
Conversation
- Changed Ancestors() to use GetParent() instead of just Parent property - This allows it to properly ascend out of structured trivia (like DocumentationCommentTriviaSyntax) - Added tests for DocumentationCommentTriviaSyntax and DefineDirectiveTriviaSyntax Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix ancestors method to return expected ancestors
Fix SyntaxNode.Ancestors() to work correctly with structured trivia
Oct 16, 2025
| Assert.Equal(SyntaxKind.AddExpression, nodes[7].Kind()); | ||
| } | ||
|
|
||
| [Fact] |
Contributor
There was a problem hiding this comment.
@copilot add a WorkItem attribute. And use a raw string literal for the test. This applies to all tests you added.
Contributor
Author
There was a problem hiding this comment.
Done in commit e8d5c67. Added [WorkItem("https://github.com/dotnet/roslyn/issues/46964")] attribute to both tests and converted the verbatim strings to raw string literals.
- Added [WorkItem] attribute referencing issue #46964 to both test methods - Converted verbatim strings to raw string literals (""") for better formatting Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Contributor
|
@dotnet/roslyn-compiler ptal. |
333fred
approved these changes
Oct 30, 2025
RikkiGibson
approved these changes
Oct 31, 2025
Comment on lines
+803
to
804
| return parent? | ||
| .AncestorsAndSelf(ascendOutOfTrivia) ?? |
Member
There was a problem hiding this comment.
Suggested change
| return parent? | |
| .AncestorsAndSelf(ascendOutOfTrivia) ?? | |
| return parent | |
| ?.AncestorsAndSelf(ascendOutOfTrivia) ?? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: DocumentationCommentTriviaSyntax.Ancestors() returns empty collection
Problem
Ancestors(ascendOutOfTrivia: true)onDocumentationCommentTriviaSyntaxreturned no ancestorsAncestorsAndSelf(ascendOutOfTrivia: true)correctly returned all ancestors including selfRoot Cause
The
Ancestorsmethod inSyntaxNode.cswas usingthis.Parentdirectly, which isnullfor structured trivia nodes. It should use theGetParenthelper method (likeAncestorsAndSelfdoes) which knows how to ascend out of structured trivia whenascendOutOfTriviaistrue.Solution
Changed
Ancestorsto useGetParent(this, ascendOutOfTrivia)instead of justthis.Parent.Changes Made
Ancestorsmethod in SyntaxNode.csTesting
TestAncestorsOfDocumentationCommentTrivia- tests doc comment triviaTestAncestorsOfDirectiveTrivia- tests preprocessor directive triviaFixes #46964
Original prompt
Fixes #46964
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.