Skip to content

Commit ca4bdb8

Browse files
committed
Enable language version 3.10 and format dot shorthands. (#1745)
Enable language version 3.10 and format dot shorthands.
1 parent fcf426c commit ca4bdb8

File tree

9 files changed

+151
-19
lines changed

9 files changed

+151
-19
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 3.1.2-wip
2+
3+
* Support dot shorthand syntax.
4+
* Enable language version 3.10.
5+
16
## 3.1.1
27

38
* Update to latest analyzer and enable language version 3.9.

lib/src/cli/formatter_options.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import 'show.dart';
1313
import 'summary.dart';
1414

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

1818
/// Global options parsed from the command line that affect how the formatter
1919
/// produces and uses its outputs.

lib/src/dart_formatter.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:analyzer/dart/analysis/features.dart';
77
import 'package:analyzer/dart/analysis/utilities.dart';
88
import 'package:analyzer/dart/ast/ast.dart';
99
import 'package:analyzer/dart/ast/token.dart';
10+
import 'package:analyzer/diagnostic/diagnostic.dart';
1011
import 'package:analyzer/error/error.dart';
1112
// ignore: implementation_imports
1213
import 'package:analyzer/src/dart/scanner/scanner.dart';
@@ -34,7 +35,7 @@ final RegExp _widthCommentPattern = RegExp(r'^// dart format width=(\d+)$');
3435
final class DartFormatter {
3536
/// The latest Dart language version that can be parsed and formatted by this
3637
/// version of the formatter.
37-
static final latestLanguageVersion = Version(3, 9, 0);
38+
static final latestLanguageVersion = Version(3, 10, 0);
3839

3940
/// The latest Dart language version that will be formatted using the older
4041
/// "short" style.
@@ -176,7 +177,7 @@ final class DartFormatter {
176177
// Throw if there are syntactic errors.
177178
var syntacticErrors =
178179
parseResult.errors.where((error) {
179-
return error.errorCode.type == ErrorType.SYNTACTIC_ERROR;
180+
return error.diagnosticCode.type == DiagnosticType.SYNTACTIC_ERROR;
180181
}).toList();
181182
if (syntacticErrors.isNotEmpty) {
182183
throw FormatterException(syntacticErrors);
@@ -194,11 +195,11 @@ final class DartFormatter {
194195
var token = node.endToken.next!;
195196
if (token.type != TokenType.CLOSE_CURLY_BRACKET) {
196197
var stringSource = StringSource(text, source.uri);
197-
var error = AnalysisError.tmp(
198+
var error = Diagnostic.tmp(
198199
source: stringSource,
199200
offset: token.offset - inputOffset,
200201
length: math.max(token.length, 1),
201-
errorCode: ParserErrorCode.UNEXPECTED_TOKEN,
202+
diagnosticCode: ParserErrorCode.UNEXPECTED_TOKEN,
202203
arguments: [token.lexeme],
203204
);
204205
throw FormatterException([error]);

lib/src/exceptions.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/error/error.dart';
5+
import 'package:analyzer/diagnostic/diagnostic.dart';
66
import 'package:source_span/source_span.dart';
77

88
/// Thrown when one or more errors occurs while parsing the code to be
99
/// formatted.
1010
final class FormatterException implements Exception {
11-
/// The [AnalysisError]s that occurred.
12-
final List<AnalysisError> errors;
11+
/// The [Diagnostic]s that occurred.
12+
final List<Diagnostic> errors;
1313

1414
/// Creates a new FormatterException with an optional error [message].
1515
const FormatterException(this.errors);

lib/src/front_end/ast_node_visitor.dart

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
531531

532532
// The name of the type being constructed.
533533
var type = node.type;
534-
pieces.token(type.name2);
534+
pieces.token(type.name);
535535
pieces.visit(type.typeArguments);
536536
pieces.token(type.question);
537537

@@ -589,6 +589,31 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
589589
pieces.token(node.semicolon);
590590
}
591591

592+
@override
593+
void visitDotShorthandConstructorInvocation(
594+
DotShorthandConstructorInvocation node,
595+
) {
596+
pieces.modifier(node.constKeyword);
597+
pieces.token(node.period);
598+
pieces.visit(node.constructorName);
599+
pieces.visit(node.typeArguments);
600+
pieces.visit(node.argumentList);
601+
}
602+
603+
@override
604+
void visitDotShorthandInvocation(DotShorthandInvocation node) {
605+
pieces.token(node.period);
606+
pieces.visit(node.memberName);
607+
pieces.visit(node.typeArguments);
608+
pieces.visit(node.argumentList);
609+
}
610+
611+
@override
612+
void visitDotShorthandPropertyAccess(DotShorthandPropertyAccess node) {
613+
pieces.token(node.period);
614+
pieces.visit(node.propertyName);
615+
}
616+
592617
@override
593618
void visitDottedName(DottedName node) {
594619
writeDotted(node.components);
@@ -1241,7 +1266,7 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
12411266

12421267
// The type being constructed.
12431268
var type = constructor.type;
1244-
pieces.token(type.name2);
1269+
pieces.token(type.name);
12451270
pieces.visit(type.typeArguments);
12461271

12471272
// If this is a named constructor call, the name.
@@ -1346,7 +1371,7 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
13461371
void visitLibraryDirective(LibraryDirective node) {
13471372
pieces.withMetadata(node.metadata, () {
13481373
pieces.token(node.libraryKeyword);
1349-
pieces.visit(node.name2, spaceBefore: true);
1374+
pieces.visit(node.name, spaceBefore: true);
13501375
pieces.token(node.semicolon);
13511376
});
13521377
}
@@ -1548,7 +1573,7 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
15481573
void visitNamedType(NamedType node) {
15491574
pieces.token(node.importPrefix?.name);
15501575
pieces.token(node.importPrefix?.period);
1551-
pieces.token(node.name2);
1576+
pieces.token(node.name);
15521577
pieces.visit(node.typeArguments);
15531578
pieces.token(node.question);
15541579
}

lib/src/short/source_visitor.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,9 +1970,7 @@ final class SourceVisitor extends ThrowingAstVisitor {
19701970
_visitDirectiveMetadata(node);
19711971
_simpleStatement(node, () {
19721972
token(node.libraryKeyword);
1973-
if (node.name2 != null) {
1974-
visit(node.name2, before: space);
1975-
}
1973+
if (node.name case var name?) visit(name, before: space);
19761974
});
19771975
}
19781976

@@ -2184,10 +2182,10 @@ final class SourceVisitor extends ThrowingAstVisitor {
21842182
token(importPrefix.name);
21852183
soloZeroSplit();
21862184
token(importPrefix.period);
2187-
token(node.name2);
2185+
token(node.name);
21882186
builder.endSpan();
21892187
} else {
2190-
token(node.name2);
2188+
token(node.name);
21912189
}
21922190

21932191
visit(node.typeArguments);

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: dart_style
22
# Note: See tool/grind.dart for how to bump the version.
3-
version: 3.1.1
3+
version: 3.1.2-wip
44
description: >-
55
Opinionated, automatic Dart source code formatter.
66
Provides an API and a CLI tool.
@@ -9,7 +9,7 @@ environment:
99
sdk: ^3.7.0
1010

1111
dependencies:
12-
analyzer: ^7.5.2
12+
analyzer: ^8.0.0
1313
args: ">=1.0.0 <3.0.0"
1414
collection: "^1.17.0"
1515
package_config: ^2.1.0
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
);
70+
>>> Nested call.
71+
.method(.getter,.method(.new(.new())),const.ctor());
72+
<<<
73+
.method(
74+
.getter,
75+
.method(.new(.new())),
76+
const .ctor(),
77+
);
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)