Skip to content

Commit 9368c17

Browse files
Format null-aware elements
1 parent 852e54f commit 9368c17

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 3.0.2-wip
22

3+
* Format null-aware elements.
4+
35
* Don't indent conditional branches redundantly after `=`, `:`, and `=>`.
46

57
```dart

lib/src/front_end/ast_node_visitor.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,8 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
12511251

12521252
@override
12531253
void visitMapLiteralEntry(MapLiteralEntry node) {
1254-
writeAssignment(node.key, node.separator, node.value);
1254+
writeAssignment(node.key, node.separator, node.value,
1255+
includeLeftHandSideQuestion: true, includeRightHandSideQuestion: true);
12551256
}
12561257

12571258
@override
@@ -1353,6 +1354,11 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
13531354
writePostfix(node.pattern, node.operator);
13541355
}
13551356

1357+
@override
1358+
void visitNullAwareElement(NullAwareElement node) {
1359+
writePrefix(node.question, node.value);
1360+
}
1361+
13561362
@override
13571363
void visitNullCheckPattern(NullCheckPattern node) {
13581364
writePostfix(node.pattern, node.operator);

lib/src/front_end/piece_factory.dart

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,7 +1338,10 @@ mixin PieceFactory {
13381338
AstNode leftHandSide, Token operator, AstNode rightHandSide,
13391339
{bool includeComma = false,
13401340
bool canBlockSplitLeft = false,
1341-
NodeContext leftHandSideContext = NodeContext.none}) {
1341+
NodeContext leftHandSideContext = NodeContext.none,
1342+
bool includeLeftHandSideQuestion = false,
1343+
bool includeRightHandSideQuestion = false,
1344+
String? rightHandSidePrefix}) {
13421345
// If an operand can have block formatting, then a newline in it doesn't
13431346
// force the operator to split, as in:
13441347
//
@@ -1383,15 +1386,19 @@ mixin PieceFactory {
13831386
_ => false
13841387
};
13851388

1386-
var leftPiece = nodePiece(leftHandSide, context: leftHandSideContext);
1389+
var leftPiece = nodePiece(leftHandSide,
1390+
questionBefore: includeLeftHandSideQuestion,
1391+
context: leftHandSideContext);
13871392

13881393
var operatorPiece = pieces.build(() {
13891394
if (operator.type != TokenType.COLON) pieces.space();
13901395
pieces.token(operator);
13911396
});
13921397

13931398
var rightPiece = nodePiece(rightHandSide,
1394-
commaAfter: includeComma, context: NodeContext.assignment);
1399+
commaAfter: includeComma,
1400+
questionBefore: includeRightHandSideQuestion,
1401+
context: NodeContext.assignment);
13951402

13961403
pieces.add(AssignPiece(
13971404
left: leftPiece,
@@ -1520,7 +1527,9 @@ mixin PieceFactory {
15201527
/// If [commaAfter] is `true`, looks for a comma token after [node] and
15211528
/// writes it to the piece as well.
15221529
Piece nodePiece(AstNode node,
1523-
{bool commaAfter = false, NodeContext context = NodeContext.none}) {
1530+
{bool commaAfter = false,
1531+
bool questionBefore = false,
1532+
NodeContext context = NodeContext.none}) {
15241533
var result = pieces.build(() {
15251534
visitNode(node, context);
15261535
});
@@ -1533,6 +1542,14 @@ mixin PieceFactory {
15331542
}
15341543
}
15351544

1545+
if (questionBefore) {
1546+
var previousToken = node.beginToken.previous!;
1547+
if (previousToken.lexeme == '?') {
1548+
var question = tokenPiece(previousToken);
1549+
result = AdjacentPiece([question, result]);
1550+
}
1551+
}
1552+
15361553
return result;
15371554
}
15381555

test/short/whitespace/collections.stmt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,34 @@ f() async {
9292
<<<
9393
f() async {
9494
var l = [await for (x in y) x];
95-
}
95+
}
96+
>>> null-aware elements
97+
foo1(int? x) => [
98+
?x];
99+
<<<
100+
foo1(int? x) => [?x];
101+
>>>
102+
foo2(int? x) => {
103+
?x};
104+
<<<
105+
foo2(int? x) => {?x};
106+
>>>
107+
bar0() => {
108+
"key": #value};
109+
<<<
110+
bar0() => {"key": #value};
111+
>>>
112+
bar1(int? x) => {
113+
?x: "value"};
114+
<<<
115+
bar1(int? x) => {?x: "value"};
116+
>>>
117+
bar2(String? y) => {
118+
"key": ?y};
119+
<<<
120+
bar2(String? y) => {"key": ?y};
121+
>>>
122+
bar3(int? x, String? y) => {
123+
?x: ?y};
124+
<<<
125+
bar3(int? x, String? y) => {?x: ?y};

0 commit comments

Comments
 (0)