Skip to content

Commit b3b4321

Browse files
authored
Format postfix expressions: !, ++, and --. (#1352)
This also includes temporary formatting support for a few syntactic forms that the postfix tests needed to use. I left TODOs in there so that we know that support for those isn't complete yet.
1 parent 93c7913 commit b3b4321

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

lib/src/front_end/ast_node_visitor.dart

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,16 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
10931093

10941094
@override
10951095
Piece visitIndexExpression(IndexExpression node) {
1096-
throw UnimplementedError();
1096+
// TODO(tall): Allow splitting before and/or after the `[` when method
1097+
// chain formatting is fully implemented. For now, we just output the code
1098+
// so that tests of other language features that contain index expressions
1099+
// can run.
1100+
return buildPiece((b) {
1101+
b.visit(node.target);
1102+
b.token(node.leftBracket);
1103+
b.visit(node.index);
1104+
b.token(node.rightBracket);
1105+
});
10971106
}
10981107

10991108
@override
@@ -1403,12 +1412,22 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
14031412

14041413
@override
14051414
Piece visitPostfixExpression(PostfixExpression node) {
1406-
throw UnimplementedError();
1415+
return buildPiece((b) {
1416+
b.visit(node.operand);
1417+
b.token(node.operator);
1418+
});
14071419
}
14081420

14091421
@override
14101422
Piece visitPrefixedIdentifier(PrefixedIdentifier node) {
1411-
throw UnimplementedError();
1423+
// TODO(tall): Allow splitting before the `.` when method chain formatting
1424+
// is fully implemented. For now, we just output the code so that tests
1425+
// of other language features that contain prefixed identifiers can run.
1426+
return buildPiece((b) {
1427+
b.visit(node.prefix);
1428+
b.token(node.period);
1429+
b.visit(node.identifier);
1430+
});
14121431
}
14131432

14141433
@override
@@ -1429,7 +1448,14 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
14291448

14301449
@override
14311450
Piece visitPropertyAccess(PropertyAccess node) {
1432-
throw UnimplementedError();
1451+
// TODO(tall): Allow splitting before the `.` when method chain formatting
1452+
// is fully implemented. For now, we just output the code so that tests
1453+
// of other language features that contain property accesses can run.
1454+
return buildPiece((b) {
1455+
b.visit(node.target);
1456+
b.token(node.operator);
1457+
b.visit(node.propertyName);
1458+
});
14331459
}
14341460

14351461
@override

test/expression/postfix.stmt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
40 columns |
2+
>>> Postfix increment.
3+
value ++ ;
4+
<<<
5+
value++;
6+
>>> Postfix decrement.
7+
value -- ;
8+
<<<
9+
value--;
10+
>>> Increment and decrement as subexpressions.
11+
value ++ - other --;
12+
<<<
13+
value++ - other--;
14+
>>> Null-assert.
15+
obj ! ;
16+
<<<
17+
obj!;
18+
>>> Null-assert after method call.
19+
obj . method() ! ;
20+
<<<
21+
obj.method()!;
22+
>>> Null-assert after property.
23+
obj . prop ! ;
24+
<<<
25+
obj.prop!;
26+
>>> Null-assert inside method chain.
27+
obj ! . getter ! . method ( arg ) ! + 3;
28+
<<<
29+
obj!.getter!.method(arg)! + 3;
30+
>>> Null-assert before index and call operators.
31+
obj ! [ index ] ! ( call ) ! + 3;
32+
<<<
33+
obj![index]!(call)! + 3;

0 commit comments

Comments
 (0)