Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format postfix expressions: !, ++, and --. #1352

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1098,7 +1098,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 @@ -1408,12 +1417,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 @@ -1434,7 +1453,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;