Skip to content

Commit

Permalink
Format postfix expressions: !, ++, and --. (#1352)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
munificent authored Jan 8, 2024
1 parent 93c7913 commit b3b4321
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
34 changes: 30 additions & 4 deletions lib/src/front_end/ast_node_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,16 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {

@override
Piece visitIndexExpression(IndexExpression node) {
throw UnimplementedError();
// TODO(tall): Allow splitting before and/or after the `[` when method
// chain formatting is fully implemented. For now, we just output the code
// so that tests of other language features that contain index expressions
// can run.
return buildPiece((b) {
b.visit(node.target);
b.token(node.leftBracket);
b.visit(node.index);
b.token(node.rightBracket);
});
}

@override
Expand Down Expand Up @@ -1403,12 +1412,22 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {

@override
Piece visitPostfixExpression(PostfixExpression node) {
throw UnimplementedError();
return buildPiece((b) {
b.visit(node.operand);
b.token(node.operator);
});
}

@override
Piece visitPrefixedIdentifier(PrefixedIdentifier node) {
throw UnimplementedError();
// TODO(tall): Allow splitting before the `.` when method chain formatting
// is fully implemented. For now, we just output the code so that tests
// of other language features that contain prefixed identifiers can run.
return buildPiece((b) {
b.visit(node.prefix);
b.token(node.period);
b.visit(node.identifier);
});
}

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

@override
Piece visitPropertyAccess(PropertyAccess node) {
throw UnimplementedError();
// TODO(tall): Allow splitting before the `.` when method chain formatting
// is fully implemented. For now, we just output the code so that tests
// of other language features that contain property accesses can run.
return buildPiece((b) {
b.visit(node.target);
b.token(node.operator);
b.visit(node.propertyName);
});
}

@override
Expand Down
33 changes: 33 additions & 0 deletions test/expression/postfix.stmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
40 columns |
>>> Postfix increment.
value ++ ;
<<<
value++;
>>> Postfix decrement.
value -- ;
<<<
value--;
>>> Increment and decrement as subexpressions.
value ++ - other --;
<<<
value++ - other--;
>>> Null-assert.
obj ! ;
<<<
obj!;
>>> Null-assert after method call.
obj . method() ! ;
<<<
obj.method()!;
>>> Null-assert after property.
obj . prop ! ;
<<<
obj.prop!;
>>> Null-assert inside method chain.
obj ! . getter ! . method ( arg ) ! + 3;
<<<
obj!.getter!.method(arg)! + 3;
>>> Null-assert before index and call operators.
obj ! [ index ] ! ( call ) ! + 3;
<<<
obj![index]!(call)! + 3;

0 comments on commit b3b4321

Please sign in to comment.