Skip to content

Commit e9bd213

Browse files
authored
Don't crash on doc comments inside local variable declarations. (#1623)
Fix #1621.
1 parent 7c4a960 commit e9bd213

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Ensure comment formatting is idempotent (#1606).
77
* Better indentation of leading comments on property accesses in binary operator
88
operands (#1611).
9+
* Don't crash on doc comments in local variable declarations (#1621).
910

1011
## 3.0.0
1112

lib/src/ast_extensions.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,18 @@ extension AstNodeExtensions on AstNode {
3131
AnnotatedNode(metadata: [var annotation, ...]) => annotation.beginToken,
3232
AnnotatedNode(firstTokenAfterCommentAndMetadata: var token) => token,
3333

34-
// DefaultFormalParameter is not an AnnotatedNode, but its first child
35-
// (parameter) *is* an AnnotatedNode, so we can't just use beginToken.
34+
// The inner [NormalFormalParameter] is an [AnnotatedNode].
3635
DefaultFormalParameter(:var parameter) => parameter.firstNonCommentToken,
3736

38-
// A pattern variable statement isn't itself an AnnotatedNode, but the
39-
// [PatternVariableDeclaration] that it wraps is.
37+
// The inner [PatternVariableDeclaration] is an [AnnotatedNode].
4038
PatternVariableDeclarationStatement(:var declaration) =>
4139
declaration.firstNonCommentToken,
40+
41+
// The inner [VariableDeclarationList] is an [AnnotatedNode].
42+
VariableDeclarationStatement(:var variables) =>
43+
variables.firstNonCommentToken,
44+
45+
// Otherwise, we don't have to worry about doc comments.
4246
_ => beginToken
4347
};
4448
}

test/tall/regression/1600/1621.unit

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
>>>
2+
/*member: readLocalInAnonymousClosure:*/
3+
readLocalInAnonymousClosure(/**/ parameter) {
4+
var /**/ local = parameter;
5+
return /*fields=[local],free=[local]*/ () => local;
6+
}
7+
<<<
8+
/*member: readLocalInAnonymousClosure:*/
9+
readLocalInAnonymousClosure(/**/ parameter) {
10+
var /**/ local = parameter;
11+
return /*fields=[local],free=[local]*/ () => local;
12+
}
13+
>>>
14+
class C {
15+
@override
16+
// ignore: overridden_fields
17+
final FunctionEntity _member;
18+
}
19+
<<<
20+
class C {
21+
@override
22+
// ignore: overridden_fields
23+
final FunctionEntity _member;
24+
}

0 commit comments

Comments
 (0)