Skip to content

Commit

Permalink
Don't crash on doc comments inside local variable declarations. (#1623)
Browse files Browse the repository at this point in the history
Fix #1621.
  • Loading branch information
munificent authored Dec 12, 2024
1 parent 7c4a960 commit e9bd213
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Ensure comment formatting is idempotent (#1606).
* Better indentation of leading comments on property accesses in binary operator
operands (#1611).
* Don't crash on doc comments in local variable declarations (#1621).

## 3.0.0

Expand Down
12 changes: 8 additions & 4 deletions lib/src/ast_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ extension AstNodeExtensions on AstNode {
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.
// The inner [NormalFormalParameter] is an [AnnotatedNode].
DefaultFormalParameter(:var parameter) => parameter.firstNonCommentToken,

// A pattern variable statement isn't itself an AnnotatedNode, but the
// [PatternVariableDeclaration] that it wraps is.
// The inner [PatternVariableDeclaration] is an [AnnotatedNode].
PatternVariableDeclarationStatement(:var declaration) =>
declaration.firstNonCommentToken,

// The inner [VariableDeclarationList] is an [AnnotatedNode].
VariableDeclarationStatement(:var variables) =>
variables.firstNonCommentToken,

// Otherwise, we don't have to worry about doc comments.
_ => beginToken
};
}
Expand Down
24 changes: 24 additions & 0 deletions test/tall/regression/1600/1621.unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
>>>
/*member: readLocalInAnonymousClosure:*/
readLocalInAnonymousClosure(/**/ parameter) {
var /**/ local = parameter;
return /*fields=[local],free=[local]*/ () => local;
}
<<<
/*member: readLocalInAnonymousClosure:*/
readLocalInAnonymousClosure(/**/ parameter) {
var /**/ local = parameter;
return /*fields=[local],free=[local]*/ () => local;
}
>>>
class C {
@override
// ignore: overridden_fields
final FunctionEntity _member;
}
<<<
class C {
@override
// ignore: overridden_fields
final FunctionEntity _member;
}

0 comments on commit e9bd213

Please sign in to comment.