Skip to content

Commit a8dd95c

Browse files
authored
Format prefix operators. (#1306)
This was an easy one. :)
1 parent ba88de7 commit a8dd95c

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

lib/src/front_end/ast_node_visitor.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,16 @@ class AstNodeVisitor extends ThrowingAstVisitor<void>
863863

864864
@override
865865
void visitPrefixExpression(PrefixExpression node) {
866-
throw UnimplementedError();
866+
token(node.operator);
867+
868+
// Edge case: put a space after "-" if the operand is "-" or "--" so that
869+
// we don't merge the operator tokens.
870+
if (node.operand
871+
case PrefixExpression(operator: Token(lexeme: '-' || '--'))) {
872+
space();
873+
}
874+
875+
visit(node.operand);
867876
}
868877

869878
@override

test/expression/prefix.stmt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
40 columns |
2+
>>> Unary negate.
3+
- foo ;
4+
<<<
5+
-foo;
6+
>>> Bitwise not.
7+
~ foo ;
8+
<<<
9+
~foo;
10+
>>> Boolean not.
11+
! foo ;
12+
<<<
13+
!foo;
14+
>>> Prefix increment.
15+
++ foo ;
16+
<<<
17+
++foo;
18+
>>> Prefix decrement.
19+
-- foo ;
20+
<<<
21+
--foo;
22+
>>> Multiple prefix operators.
23+
- ~ ! foo;
24+
<<<
25+
-~!foo;
26+
>>> Sequential `-` operators are not joined.
27+
- - - -foo;
28+
<<<
29+
- - - -foo;
30+
>>> A `-` operator before a negative integer is not joined.
31+
- -1;
32+
<<<
33+
- -1;
34+
>>> A `-` operator before a negative floating point number is not joined.
35+
- -1.2;
36+
<<<
37+
- -1.2;
38+
>>> A `-` before a `--` is not joined.
39+
- -- foo;
40+
<<<
41+
- --foo;

0 commit comments

Comments
 (0)