Skip to content

Commit

Permalink
Don't move a comment between a modifier and return type.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
munificent committed Oct 24, 2024
1 parent 89577e7 commit 57c9d76
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/src/front_end/piece_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:collection/collection.dart';

import '../ast_extensions.dart';
import '../piece/adjacent.dart';
Expand Down Expand Up @@ -626,8 +627,9 @@ mixin PieceFactory {
// @meta
// // Weird place for comment.
// int f() {}
var leadingComments =
pieces.takeCommentsBefore(returnType.firstNonCommentToken);
var firstToken = modifiers.firstWhereOrNull((token) => token != null) ??
returnType.firstNonCommentToken;
var leadingComments = pieces.takeCommentsBefore(firstToken);

var returnTypePiece = pieces.build(() {
for (var keyword in modifiers) {
Expand Down
30 changes: 30 additions & 0 deletions test/tall/declaration/member_comment.unit
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,34 @@ class Foo {
void d() {
;
}
}
>>> Comment after `external`.
class C {
external /* c */ int x;
external /* c */ int f();
external /* c */ int get g;
}
<<<
class C {
external /* c */ int x;
external /* c */ int f();
external /* c */ int get g;
}
>>> Comment after `covariant` on function typed parameter.
class C {
method(covariant /* c */ int Function() f) {}
}
<<<
class C {
method(
covariant /* c */ int Function() f,
) {}
}
>>> Comment after `static` on method.
class C {
static /* c */ int method() {}
}
<<<
class C {
static /* c */ int method() {}
}
8 changes: 7 additions & 1 deletion test/tall/function/parameter.unit
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ function(
>>> Required old style function typed parameter.
f({ required callback()}) {}
<<<
f({required callback()}) {}
f({required callback()}) {}
>>> Comment after `required` on function type.
f({required /* c */ int Function() f}) {}
<<<
f({
required /* c */ int Function() f,
}) {}
18 changes: 18 additions & 0 deletions test/tall/regression/1500/1585.unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
>>>
class AaaaAaaaa {
external Aaaaaa get aa;
external Aaaaaa get aaAaa;
external Aaaaaa get aa;
external aaaa get aaaa;
external /*AaaaaaAaaa*/ aaa get aa;
external Aaaaaa get aaa;
}
<<<
class AaaaAaaaa {
external Aaaaaa get aa;
external Aaaaaa get aaAaa;
external Aaaaaa get aa;
external aaaa get aaaa;
external /*AaaaaaAaaa*/ aaa get aa;
external Aaaaaa get aaa;
}

0 comments on commit 57c9d76

Please sign in to comment.