Skip to content

Commit 57c9d76

Browse files
committed
Don't move a comment between a modifier and return type.
When formatting a function, method, getter, or function type, the formatter hoists any leading comments out so that they don't force a split between the return type and body. However, it failed to take into account modifiers that may occur before the return type but after the comment. If that happened, the comment would get moved before the modifiers. Fix #1585.
1 parent 89577e7 commit 57c9d76

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

lib/src/front_end/piece_factory.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44
import 'package:analyzer/dart/ast/ast.dart';
55
import 'package:analyzer/dart/ast/token.dart';
6+
import 'package:collection/collection.dart';
67

78
import '../ast_extensions.dart';
89
import '../piece/adjacent.dart';
@@ -626,8 +627,9 @@ mixin PieceFactory {
626627
// @meta
627628
// // Weird place for comment.
628629
// int f() {}
629-
var leadingComments =
630-
pieces.takeCommentsBefore(returnType.firstNonCommentToken);
630+
var firstToken = modifiers.firstWhereOrNull((token) => token != null) ??
631+
returnType.firstNonCommentToken;
632+
var leadingComments = pieces.takeCommentsBefore(firstToken);
631633

632634
var returnTypePiece = pieces.build(() {
633635
for (var keyword in modifiers) {

test/tall/declaration/member_comment.unit

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,34 @@ class Foo {
2121
void d() {
2222
;
2323
}
24+
}
25+
>>> Comment after `external`.
26+
class C {
27+
external /* c */ int x;
28+
external /* c */ int f();
29+
external /* c */ int get g;
30+
}
31+
<<<
32+
class C {
33+
external /* c */ int x;
34+
external /* c */ int f();
35+
external /* c */ int get g;
36+
}
37+
>>> Comment after `covariant` on function typed parameter.
38+
class C {
39+
method(covariant /* c */ int Function() f) {}
40+
}
41+
<<<
42+
class C {
43+
method(
44+
covariant /* c */ int Function() f,
45+
) {}
46+
}
47+
>>> Comment after `static` on method.
48+
class C {
49+
static /* c */ int method() {}
50+
}
51+
<<<
52+
class C {
53+
static /* c */ int method() {}
2454
}

test/tall/function/parameter.unit

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,10 @@ function(
3737
>>> Required old style function typed parameter.
3838
f({ required callback()}) {}
3939
<<<
40-
f({required callback()}) {}
40+
f({required callback()}) {}
41+
>>> Comment after `required` on function type.
42+
f({required /* c */ int Function() f}) {}
43+
<<<
44+
f({
45+
required /* c */ int Function() f,
46+
}) {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
>>>
2+
class AaaaAaaaa {
3+
external Aaaaaa get aa;
4+
external Aaaaaa get aaAaa;
5+
external Aaaaaa get aa;
6+
external aaaa get aaaa;
7+
external /*AaaaaaAaaa*/ aaa get aa;
8+
external Aaaaaa get aaa;
9+
}
10+
<<<
11+
class AaaaAaaaa {
12+
external Aaaaaa get aa;
13+
external Aaaaaa get aaAaa;
14+
external Aaaaaa get aa;
15+
external aaaa get aaaa;
16+
external /*AaaaaaAaaa*/ aaa get aa;
17+
external Aaaaaa get aaa;
18+
}

0 commit comments

Comments
 (0)