Skip to content

Commit 7f744ea

Browse files
committed
Enable language version 3.10 and format dot shorthands.
This is using a workaround for dart-lang/sdk#60840. That issue is fixed and a new version of analyzer is published, but unfortunately there is no corresponding version of test that works with it so I can't upgrade yet. The workaround is pretty harmless, so I'm fine with it. (The formatter uses similar logic to handle commas which also aren't represented in the AST nodes.)
1 parent 6c4166a commit 7f744ea

File tree

5 files changed

+133
-2
lines changed

5 files changed

+133
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## 3.1.1-wip
22

3-
* Update to latest analyzer and enable language version 3.9.
3+
* Support dot shorthand syntax.
4+
* Enable language version 3.10.
5+
* Update to latest analyzer.
46

57
## 3.1.0
68

lib/src/dart_formatter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final RegExp _widthCommentPattern = RegExp(r'^// dart format width=(\d+)$');
3434
final class DartFormatter {
3535
/// The latest Dart language version that can be parsed and formatted by this
3636
/// version of the formatter.
37-
static final latestLanguageVersion = Version(3, 9, 0);
37+
static final latestLanguageVersion = Version(3, 10, 0);
3838

3939
/// The latest Dart language version that will be formatted using the older
4040
/// "short" style.

lib/src/front_end/ast_node_visitor.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,29 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
589589
pieces.token(node.semicolon);
590590
}
591591

592+
@override
593+
void visitDotShorthandInvocation(DotShorthandInvocation node) {
594+
// TODO(rnystrom): Get this directly from the AST once the formatter can
595+
// use analyzer 8.0.0.
596+
// See: https://github.com/dart-lang/sdk/issues/60840
597+
if (node.period.previous case var token?
598+
when token.keyword == Keyword.CONST) {
599+
pieces.token(token);
600+
pieces.space();
601+
}
602+
603+
pieces.token(node.period);
604+
pieces.visit(node.memberName);
605+
pieces.visit(node.typeArguments);
606+
pieces.visit(node.argumentList);
607+
}
608+
609+
@override
610+
void visitDotShorthandPropertyAccess(DotShorthandPropertyAccess node) {
611+
pieces.token(node.period);
612+
pieces.visit(node.propertyName);
613+
}
614+
592615
@override
593616
void visitDottedName(DottedName node) {
594617
writeDotted(node.components);
@@ -2316,6 +2339,17 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
23162339
// Instead, we hoist the comment out of all of those and then have comment
23172340
// precede them all so that they don't split.
23182341
var firstToken = node.firstNonCommentToken;
2342+
2343+
// TODO(rnystrom): The AST node for DotShorthandInvocation in analyzer
2344+
// before 8.0.0 doesn't include a leading `const` as part of the node. If
2345+
// we ignore that, then a comment before the `.` gets incorrectly hoisted
2346+
// before the `const`. Remove this when we can upgrade to 8.0.0.
2347+
// See: https://github.com/dart-lang/sdk/issues/60840
2348+
if (node is DotShorthandInvocation &&
2349+
firstToken.previous?.keyword == Keyword.CONST) {
2350+
firstToken = firstToken.previous!;
2351+
}
2352+
23192353
if (firstToken.precedingComments != null) {
23202354
var comments = pieces.takeCommentsBefore(firstToken);
23212355
var piece = pieces.build(() {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
40 columns |
2+
(experiment dot-shorthands)
3+
>>> Getter.
4+
variable = . getter;
5+
<<< 3.9
6+
variable = .getter;
7+
>>> Method call with unsplit arguments.
8+
variable = .method(1,x:2,3,y:4);
9+
<<< 3.9
10+
variable = .method(1, x: 2, 3, y: 4);
11+
>>> Method call with split arguments.
12+
variable = .method(one, x: two, three, y: four);
13+
<<< 3.9
14+
variable = .method(
15+
one,
16+
x: two,
17+
three,
18+
y: four,
19+
);
20+
>>> Generic method call.
21+
variable = . method < int , String > ( ) ;
22+
<<< 3.9
23+
variable = .method<int, String>();
24+
>>> Constructor.
25+
variable = .new(1);
26+
<<< 3.9
27+
variable = .new(1);
28+
>>> Const constructor.
29+
variable = const . new ( );
30+
<<< 3.9
31+
variable = const .new();
32+
>>> Const named constructor.
33+
variable = const . named ( );
34+
<<< 3.9
35+
variable = const .named();
36+
>>> Unsplit selector chain.
37+
v = . property . method() . x . another();
38+
<<< 3.9
39+
v = .property.method().x.another();
40+
>>> Split selector chain on shorthand getter.
41+
variable = .shorthand.method().another().third();
42+
<<< 3.9
43+
variable = .shorthand
44+
.method()
45+
.another()
46+
.third();
47+
>>> Split selector chain on shorthand method.
48+
variable = .shorthand().method().getter.another().third();
49+
<<< 3.9
50+
variable = .shorthand()
51+
.method()
52+
.getter
53+
.another()
54+
.third();
55+
>>> Split in shorthand method call argument list.
56+
context(.shorthand(argument, anotherArgument, thirdArgument)
57+
.chain().another().third().fourthOne());
58+
<<< 3.9
59+
context(
60+
.shorthand(
61+
argument,
62+
anotherArgument,
63+
thirdArgument,
64+
)
65+
.chain()
66+
.another()
67+
.third()
68+
.fourthOne(),
69+
);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
40 columns |
2+
(experiment dot-shorthands)
3+
>>> Line comment after dot.
4+
variable = . // Comment.
5+
whoDoesThis();
6+
<<< 3.9
7+
variable =
8+
. // Comment.
9+
whoDoesThis();
10+
>>> Block comment after dot.
11+
variable = ./* Comment. */whoDoesThis();
12+
<<< 3.9
13+
variable =
14+
. /* Comment. */ whoDoesThis();
15+
>>> Line comment after `const`.
16+
variable = const // Comment.
17+
. whoDoesThis();
18+
<<< 3.9
19+
variable =
20+
const // Comment.
21+
.whoDoesThis();
22+
>>> Block comment after `const`.
23+
variable = const/* Comment. */.whoDoesThis();
24+
<<< 3.9
25+
variable =
26+
const /* Comment. */ .whoDoesThis();

0 commit comments

Comments
 (0)