Skip to content

Commit

Permalink
Don't crash on a doc comment before a pattern variable statement.
Browse files Browse the repository at this point in the history
This is a funny corner of the analyzer AST API where a
PatternVariableDeclarationStatement wraps an inner
PatternVariableDeclaration. The statement class isn't itself an
AnnotatedNode so doesn't get caught by the first couple of cases. But
its beginToken comes from the inner PatternVariableDeclaration, which
*is* an AnnotatedNode, so we need to recurse into that.

Fix #1586.
  • Loading branch information
munificent committed Oct 24, 2024
1 parent 89577e7 commit 922d809
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
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;
}

0 comments on commit 922d809

Please sign in to comment.