Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.1.2-wip

* Support dot shorthand syntax.
* Enable language version 3.10.

## 3.1.1

* Update to latest analyzer and enable language version 3.9.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/cli/formatter_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'show.dart';
import 'summary.dart';

// Note: The following line of code is modified by tool/grind.dart.
const dartStyleVersion = '3.1.1';
const dartStyleVersion = '3.1.2-wip';

/// Global options parsed from the command line that affect how the formatter
/// produces and uses its outputs.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/dart_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final RegExp _widthCommentPattern = RegExp(r'^// dart format width=(\d+)$');
final class DartFormatter {
/// The latest Dart language version that can be parsed and formatted by this
/// version of the formatter.
static final latestLanguageVersion = Version(3, 9, 0);
static final latestLanguageVersion = Version(3, 10, 0);

/// The latest Dart language version that will be formatted using the older
/// "short" style.
Expand Down
34 changes: 34 additions & 0 deletions lib/src/front_end/ast_node_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,29 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
pieces.token(node.semicolon);
}

@override
void visitDotShorthandInvocation(DotShorthandInvocation node) {
// TODO(rnystrom): Get this directly from the AST once the formatter can
// use analyzer 8.0.0.
// See: https://github.com/dart-lang/sdk/issues/60840
if (node.period.previous case var token?
when token.keyword == Keyword.CONST) {
pieces.token(token);
pieces.space();
}

pieces.token(node.period);
pieces.visit(node.memberName);
pieces.visit(node.typeArguments);
pieces.visit(node.argumentList);
}

@override
void visitDotShorthandPropertyAccess(DotShorthandPropertyAccess node) {
pieces.token(node.period);
pieces.visit(node.propertyName);
}

@override
void visitDottedName(DottedName node) {
writeDotted(node.components);
Expand Down Expand Up @@ -2316,6 +2339,17 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
// Instead, we hoist the comment out of all of those and then have comment
// precede them all so that they don't split.
var firstToken = node.firstNonCommentToken;

// TODO(rnystrom): The AST node for DotShorthandInvocation in analyzer
// before 8.0.0 doesn't include a leading `const` as part of the node. If
// we ignore that, then a comment before the `.` gets incorrectly hoisted
// before the `const`. Remove this when we can upgrade to 8.0.0.
// See: https://github.com/dart-lang/sdk/issues/60840
if (node is DotShorthandInvocation &&
firstToken.previous?.keyword == Keyword.CONST) {
firstToken = firstToken.previous!;
}

if (firstToken.precedingComments != null) {
var comments = pieces.takeCommentsBefore(firstToken);
var piece = pieces.build(() {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dart_style
# Note: See tool/grind.dart for how to bump the version.
version: 3.1.1
version: 3.1.2-wip
description: >-
Opinionated, automatic Dart source code formatter.
Provides an API and a CLI tool.
Expand Down
69 changes: 69 additions & 0 deletions test/tall/invocation/dot_shorthand.stmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
40 columns |
(experiment dot-shorthands)
>>> Getter.
variable = . getter;
<<< 3.9
variable = .getter;
>>> Method call with unsplit arguments.
variable = .method(1,x:2,3,y:4);
<<< 3.9
variable = .method(1, x: 2, 3, y: 4);
>>> Method call with split arguments.
variable = .method(one, x: two, three, y: four);
<<< 3.9
variable = .method(
one,
x: two,
three,
y: four,
);
>>> Generic method call.
variable = . method < int , String > ( ) ;
<<< 3.9
variable = .method<int, String>();
>>> Constructor.
variable = .new(1);
<<< 3.9
variable = .new(1);
>>> Const constructor.
variable = const . new ( );
<<< 3.9
variable = const .new();
>>> Const named constructor.
variable = const . named ( );
<<< 3.9
variable = const .named();
>>> Unsplit selector chain.
v = . property . method() . x . another();
<<< 3.9
v = .property.method().x.another();
>>> Split selector chain on shorthand getter.
variable = .shorthand.method().another().third();
<<< 3.9
variable = .shorthand
.method()
.another()
.third();
>>> Split selector chain on shorthand method.
variable = .shorthand().method().getter.another().third();
<<< 3.9
variable = .shorthand()
.method()
.getter
.another()
.third();
>>> Split in shorthand method call argument list.
context(.shorthand(argument, anotherArgument, thirdArgument)
.chain().another().third().fourthOne());
<<< 3.9
context(
.shorthand(
argument,
anotherArgument,
thirdArgument,
)
.chain()
.another()
.third()
.fourthOne(),
);
26 changes: 26 additions & 0 deletions test/tall/invocation/dot_shorthand_comment.stmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
40 columns |
(experiment dot-shorthands)
>>> Line comment after dot.
variable = . // Comment.
whoDoesThis();
<<< 3.9
variable =
. // Comment.
whoDoesThis();
>>> Block comment after dot.
variable = ./* Comment. */whoDoesThis();
<<< 3.9
variable =
. /* Comment. */ whoDoesThis();
>>> Line comment after `const`.
variable = const // Comment.
. whoDoesThis();
<<< 3.9
variable =
const // Comment.
.whoDoesThis();
>>> Block comment after `const`.
variable = const/* Comment. */.whoDoesThis();
<<< 3.9
variable =
const /* Comment. */ .whoDoesThis();