Skip to content

Commit c8ed552

Browse files
authored
Format enhanced enums. (#1096)
* Format enhanced enums. Fix #1075. * Include constructor names in enum values. * Migrate off deprecated analyzer APIs.
1 parent 4de83d3 commit c8ed552

File tree

8 files changed

+541
-28
lines changed

8 files changed

+541
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 2.2.2
22

33
* Format named arguments anywhere (#1072).
4+
* Format enhanced enums (#1075).
45

56
# 2.2.1
67

lib/src/source_visitor.dart

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ class SourceVisitor extends ThrowingAstVisitor {
802802
token(node.equals);
803803
space();
804804

805-
visit(node.superclass2);
805+
visit(node.superclass);
806806

807807
builder.startRule(CombinatorRule());
808808
visit(node.withClause);
@@ -834,11 +834,12 @@ class SourceVisitor extends ThrowingAstVisitor {
834834

835835
var needsDouble = true;
836836
for (var declaration in node.declarations) {
837-
var hasClassBody = declaration is ClassDeclaration ||
837+
var hasBody = declaration is ClassDeclaration ||
838+
declaration is EnumDeclaration ||
838839
declaration is ExtensionDeclaration;
839840

840-
// Add a blank line before declarations with class-like bodies.
841-
if (hasClassBody) needsDouble = true;
841+
// Add a blank line before types with bodies.
842+
if (hasBody) needsDouble = true;
842843

843844
if (needsDouble) {
844845
twoNewlines();
@@ -851,8 +852,8 @@ class SourceVisitor extends ThrowingAstVisitor {
851852
visit(declaration);
852853

853854
needsDouble = false;
854-
if (hasClassBody) {
855-
// Add a blank line after declarations with class-like bodies.
855+
if (hasBody) {
856+
// Add a blank line after types declarations with bodies.
856857
needsDouble = true;
857858
} else if (declaration is FunctionDeclaration) {
858859
// Add a blank line after non-empty block functions.
@@ -1068,7 +1069,7 @@ class SourceVisitor extends ThrowingAstVisitor {
10681069

10691070
@override
10701071
void visitConstructorName(ConstructorName node) {
1071-
visit(node.type2);
1072+
visit(node.type);
10721073
token(node.period);
10731074
visit(node.name);
10741075
}
@@ -1165,25 +1166,80 @@ class SourceVisitor extends ThrowingAstVisitor {
11651166
void visitEnumConstantDeclaration(EnumConstantDeclaration node) {
11661167
visitMetadata(node.metadata);
11671168
visit(node.name);
1169+
1170+
var arguments = node.arguments;
1171+
if (arguments != null) {
1172+
builder.nestExpression();
1173+
visit(arguments.typeArguments);
1174+
1175+
var constructor = arguments.constructorSelector;
1176+
if (constructor != null) {
1177+
token(constructor.period);
1178+
visit(constructor.name);
1179+
}
1180+
1181+
visitArgumentList(arguments.argumentList, nestExpression: false);
1182+
builder.unnest();
1183+
}
11681184
}
11691185

11701186
@override
11711187
void visitEnumDeclaration(EnumDeclaration node) {
11721188
visitMetadata(node.metadata);
11731189

1190+
builder.nestExpression();
11741191
token(node.enumKeyword);
11751192
space();
11761193
visit(node.name);
1194+
visit(node.typeParameters);
1195+
1196+
builder.startRule(CombinatorRule());
1197+
visit(node.withClause);
1198+
visit(node.implementsClause);
1199+
builder.endRule();
11771200
space();
11781201

1202+
builder.unnest();
1203+
11791204
_beginBody(node.leftBracket, space: true);
11801205
visitCommaSeparatedNodes(node.constants, between: splitOrTwoNewlines);
11811206

11821207
// If there is a trailing comma, always force the constants to split.
1183-
if (hasCommaAfter(node.constants.last)) {
1208+
Token? trailingComma = _commaAfter(node.constants.last);
1209+
if (trailingComma != null) {
11841210
builder.forceRules();
11851211
}
11861212

1213+
// The ";" after the constants, which may occur after a trailing comma.
1214+
Token afterConstants = node.constants.last.endToken.next!;
1215+
Token? semicolon;
1216+
if (afterConstants.type == TokenType.SEMICOLON) {
1217+
semicolon = node.constants.last.endToken.next!;
1218+
} else if (trailingComma != null &&
1219+
trailingComma.next!.type == TokenType.SEMICOLON) {
1220+
semicolon = afterConstants.next!;
1221+
}
1222+
1223+
if (semicolon != null) {
1224+
// If there is both a trailing comma and a semicolon, move the semicolon
1225+
// to the next line. This doesn't look great but it's less bad than being
1226+
// next to the comma.
1227+
// TODO(rnystrom): If the formatter starts making non-whitespace changes
1228+
// like adding/removing trailing commas, then it should fix this too.
1229+
if (trailingComma != null) newline();
1230+
1231+
token(semicolon);
1232+
1233+
// Always split the constants if they end in ";", even if there aren't
1234+
// any members.
1235+
builder.forceRules();
1236+
1237+
// Put a blank line between the constants and members.
1238+
if (node.members.isNotEmpty) twoNewlines();
1239+
}
1240+
1241+
_visitMembers(node.members);
1242+
11871243
_endBody(node.rightBracket, space: true);
11881244
}
11891245

@@ -1373,7 +1429,7 @@ class SourceVisitor extends ThrowingAstVisitor {
13731429
soloSplit();
13741430
token(node.extendsKeyword);
13751431
space();
1376-
visit(node.superclass2);
1432+
visit(node.superclass);
13771433
}
13781434

13791435
@override
@@ -1999,7 +2055,7 @@ class SourceVisitor extends ThrowingAstVisitor {
19992055

20002056
@override
20012057
void visitImplementsClause(ImplementsClause node) {
2002-
_visitCombinator(node.implementsKeyword, node.interfaces2);
2058+
_visitCombinator(node.implementsKeyword, node.interfaces);
20032059
}
20042060

20052061
@override
@@ -2264,18 +2320,18 @@ class SourceVisitor extends ThrowingAstVisitor {
22642320
// If there is only a single superclass constraint, format it like an
22652321
// "extends" in a class.
22662322
var onClause = node.onClause;
2267-
if (onClause != null && onClause.superclassConstraints2.length == 1) {
2323+
if (onClause != null && onClause.superclassConstraints.length == 1) {
22682324
soloSplit();
22692325
token(onClause.onKeyword);
22702326
space();
2271-
visit(onClause.superclassConstraints2.single);
2327+
visit(onClause.superclassConstraints.single);
22722328
}
22732329

22742330
builder.startRule(CombinatorRule());
22752331

22762332
// If there are multiple superclass constraints, format them like the
22772333
// "implements" clause.
2278-
if (onClause != null && onClause.superclassConstraints2.length > 1) {
2334+
if (onClause != null && onClause.superclassConstraints.length > 1) {
22792335
visit(onClause);
22802336
}
22812337

@@ -2326,7 +2382,7 @@ class SourceVisitor extends ThrowingAstVisitor {
23262382

23272383
@override
23282384
void visitOnClause(OnClause node) {
2329-
_visitCombinator(node.onKeyword, node.superclassConstraints2);
2385+
_visitCombinator(node.onKeyword, node.superclassConstraints);
23302386
}
23312387

23322388
@override
@@ -2730,7 +2786,7 @@ class SourceVisitor extends ThrowingAstVisitor {
27302786

27312787
@override
27322788
void visitWithClause(WithClause node) {
2733-
_visitCombinator(node.withKeyword, node.mixinTypes2);
2789+
_visitCombinator(node.withKeyword, node.mixinTypes);
27342790
}
27352791

27362792
@override

pubspec.lock

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ packages:
77
name: _fe_analyzer_shared
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "34.0.0"
10+
version: "36.0.0"
1111
analyzer:
1212
dependency: "direct main"
1313
description:
1414
name: analyzer
1515
url: "https://pub.dartlang.org"
1616
source: hosted
17-
version: "3.2.0"
17+
version: "3.3.1"
1818
args:
1919
dependency: "direct main"
2020
description:
@@ -70,7 +70,7 @@ packages:
7070
name: coverage
7171
url: "https://pub.dartlang.org"
7272
source: hosted
73-
version: "1.0.3"
73+
version: "1.1.0"
7474
crypto:
7575
dependency: transitive
7676
description:
@@ -112,7 +112,7 @@ packages:
112112
name: http_multi_server
113113
url: "https://pub.dartlang.org"
114114
source: hosted
115-
version: "3.0.1"
115+
version: "3.2.0"
116116
http_parser:
117117
dependency: transitive
118118
description:
@@ -133,7 +133,7 @@ packages:
133133
name: js
134134
url: "https://pub.dartlang.org"
135135
source: hosted
136-
version: "0.6.3"
136+
version: "0.6.4"
137137
lints:
138138
dependency: "direct dev"
139139
description:
@@ -189,7 +189,7 @@ packages:
189189
name: path
190190
url: "https://pub.dartlang.org"
191191
source: hosted
192-
version: "1.8.0"
192+
version: "1.8.1"
193193
pool:
194194
dependency: transitive
195195
description:
@@ -252,7 +252,7 @@ packages:
252252
name: source_span
253253
url: "https://pub.dartlang.org"
254254
source: hosted
255-
version: "1.8.1"
255+
version: "1.8.2"
256256
stack_trace:
257257
dependency: transitive
258258
description:
@@ -287,7 +287,7 @@ packages:
287287
name: test
288288
url: "https://pub.dartlang.org"
289289
source: hosted
290-
version: "1.20.0"
290+
version: "1.20.1"
291291
test_api:
292292
dependency: transitive
293293
description:
@@ -301,7 +301,7 @@ packages:
301301
name: test_core
302302
url: "https://pub.dartlang.org"
303303
source: hosted
304-
version: "0.4.10"
304+
version: "0.4.11"
305305
test_descriptor:
306306
dependency: "direct dev"
307307
description:
@@ -329,7 +329,7 @@ packages:
329329
name: vm_service
330330
url: "https://pub.dartlang.org"
331331
source: hosted
332-
version: "7.5.0"
332+
version: "8.2.0"
333333
watcher:
334334
dependency: transitive
335335
description:
@@ -359,4 +359,4 @@ packages:
359359
source: hosted
360360
version: "3.1.0"
361361
sdks:
362-
dart: ">=2.14.0 <3.0.0"
362+
dart: ">=2.16.0-100.0.dev <3.0.0"

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ environment:
99
sdk: ">=2.12.0-0 <3.0.0"
1010

1111
dependencies:
12-
analyzer: ">=3.2.0 <4.0.0"
12+
analyzer: ^3.3.1
1313
args: ">=1.0.0 <3.0.0"
1414
path: ^1.0.0
1515
pub_semver: ">=1.4.4 <3.0.0"

test/comments/enums.unit

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,25 @@ enum A {
5151
enum A {
5252
// comment
5353
B
54+
}
55+
>>> ensure blank line above doc comments
56+
enum Foo {/// doc
57+
a,/// doc
58+
b;/// doc
59+
var x = 1;
60+
/// doc
61+
void y() {}}
62+
<<<
63+
enum Foo {
64+
/// doc
65+
a,
66+
67+
/// doc
68+
b;
69+
70+
/// doc
71+
var x = 1;
72+
73+
/// doc
74+
void y() {}
5475
}

0 commit comments

Comments
 (0)