Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't crash on a doc comment before a pattern variable statement. #1589

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/src/ast_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ extension AstNodeExtensions on AstNode {
/// (if there is any), or the beginning of the code.
Token get firstNonCommentToken {
return switch (this) {
// If the node is annotated, skip past the doc comments, but not the
// metadata.
AnnotatedNode(metadata: [var annotation, ...]) => annotation.beginToken,
AnnotatedNode(firstTokenAfterCommentAndMetadata: var token) => token,

// DefaultFormalParameter is not an AnnotatedNode, but its first child
// (parameter) *is* an AnnotatedNode, so we can't just use beginToken.
DefaultFormalParameter(:var parameter) => parameter.firstNonCommentToken,

// A pattern variable statement isn't itself an AnnotatedNode, but the
// [PatternVariableDeclaration] that it wraps is.
PatternVariableDeclarationStatement(:var declaration) =>
declaration.firstNonCommentToken,
_ => beginToken
};
}
Expand Down
10 changes: 10 additions & 0 deletions test/tall/regression/1500/1586.unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
>>>
main() {
/// Doc comment.
final (a, b) = c;
}
<<<
main() {
/// Doc comment.
final (a, b) = c;
}