Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't move a comment between a modifier and return type. #1588

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) ??
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[optional] You could use a non-null filter instead of writing it as a callback.

Suggested change
var firstToken = modifiers.firstWhereOrNull((token) => token != null) ??
var firstToken = modifiers.nonNulls.firstOrNull ??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well crap. I landed this before I saw your suggestion. Today I learned about nonNulls.

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;
}