Skip to content

Commit dc13a2f

Browse files
authored
Don't crash on a doc comment before a pattern variable statement. (#1589)
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.
1 parent 0f448ba commit dc13a2f

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/src/ast_extensions.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@ extension AstNodeExtensions on AstNode {
2626
/// (if there is any), or the beginning of the code.
2727
Token get firstNonCommentToken {
2828
return switch (this) {
29+
// If the node is annotated, skip past the doc comments, but not the
30+
// metadata.
2931
AnnotatedNode(metadata: [var annotation, ...]) => annotation.beginToken,
3032
AnnotatedNode(firstTokenAfterCommentAndMetadata: var token) => token,
33+
3134
// DefaultFormalParameter is not an AnnotatedNode, but its first child
3235
// (parameter) *is* an AnnotatedNode, so we can't just use beginToken.
3336
DefaultFormalParameter(:var parameter) => parameter.firstNonCommentToken,
37+
38+
// A pattern variable statement isn't itself an AnnotatedNode, but the
39+
// [PatternVariableDeclaration] that it wraps is.
40+
PatternVariableDeclarationStatement(:var declaration) =>
41+
declaration.firstNonCommentToken,
3442
_ => beginToken
3543
};
3644
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
>>>
2+
main() {
3+
/// Doc comment.
4+
final (a, b) = c;
5+
}
6+
<<<
7+
main() {
8+
/// Doc comment.
9+
final (a, b) = c;
10+
}

0 commit comments

Comments
 (0)