Skip to content
This repository was archived by the owner on May 14, 2026. It is now read-only.

Commit 6cb5d11

Browse files
authored
[ggj][ast] feat: enable postfix operator for ExprStatement (#282)
* feat: enable postfix operator for ExprStatement * fix: merge master * fix: merge master
1 parent 1fc408a commit 6cb5d11

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/main/java/com/google/api/generator/engine/ast/ExprStatement.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ public ExprStatement build() {
5353
|| (expr instanceof ReferenceConstructorExpr)
5454
|| (expr instanceof AssignmentExpr)
5555
|| (expr instanceof ThrowExpr)
56-
|| (expr instanceof ReturnExpr),
56+
|| (expr instanceof ReturnExpr)
57+
|| (expr instanceof UnaryOperationExpr
58+
&& ((UnaryOperationExpr) expr).isPostfixIncrement()),
5759
"Expression statements must be either a method invocation, assignment, throw, "
58-
+ "this/super constructor, or return expression");
60+
+ "this/super constructor, return, or unary post-fix operation expression");
5961
}
6062
return exprStatement;
6163
}

src/main/java/com/google/api/generator/engine/ast/UnaryOperationExpr.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public void accept(AstNodeVisitor visitor) {
3030
visitor.visit(this);
3131
}
3232

33+
public boolean isPostfixIncrement() {
34+
return operatorKind().equals(OperatorKind.UNARY_POST_INCREMENT);
35+
}
36+
3337
public static UnaryOperationExpr postfixIncrementWithExpr(Expr expr) {
3438
return builder()
3539
.setExpr(expr)

src/test/java/com/google/api/generator/engine/ast/ExprStatementTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public void validExprStatement_return() {
7171
ReturnExpr.withExpr(ValueExpr.withValue(StringObjectValue.withValue("asdf"))));
7272
}
7373

74+
@Test
75+
public void validExprStatement_unaryOperation() {
76+
assertValidExprStatement(
77+
UnaryOperationExpr.postfixIncrementWithExpr(
78+
VariableExpr.withVariable(
79+
Variable.builder().setType(TypeNode.INT).setName("i").build())));
80+
}
81+
7482
@Test
7583
public void invalidExprStatement_variable() {
7684
Variable variable = Variable.builder().setType(TypeNode.INT).setName("libraryClient").build();
@@ -85,6 +93,15 @@ public void invalidExprStatement_value() {
8593
assertInvalidExprStatement(valueExpr);
8694
}
8795

96+
@Test
97+
public void invalidExprStatement_logicalNotUnaryOperator() {
98+
Expr logicalNotExpr =
99+
UnaryOperationExpr.logicalNotWithExpr(
100+
VariableExpr.withVariable(
101+
Variable.builder().setType(TypeNode.BOOLEAN).setName("foo").build()));
102+
assertInvalidExprStatement(logicalNotExpr);
103+
}
104+
88105
private static void assertInvalidExprStatement(Expr expr) {
89106
assertThrows(
90107
IllegalStateException.class,

0 commit comments

Comments
 (0)