Skip to content

Commit

Permalink
fix(ast): add import generation for annotations on VariableExpr (#1076)
Browse files Browse the repository at this point in the history
* This PR is a follow-up patch to #1012 which added support for annotations on VariableExpr. It updates ImportWriterVisitor so that import generation also covers these annotations.
  • Loading branch information
emmileaf authored Nov 3, 2022
1 parent c34f2af commit 78b15ab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public void visit(TernaryExpr ternaryExpr) {
@Override
public void visit(VariableExpr variableExpr) {
variableExpr.variable().type().accept(this);
annotations(variableExpr.annotations());
if (variableExpr.exprReferenceExpr() != null) {
variableExpr.exprReferenceExpr().accept(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,32 @@ public void writeVariableExprImports_nestedReference() {
writerVisitor.write());
}

@Test
public void writeVariableExprImports_withAnnotations() {
Variable variable =
Variable.builder()
.setName("expr")
.setType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class)))
.build();

VariableExpr variableExpr =
VariableExpr.builder()
.setVariable(variable)
.setIsDecl(true)
.setAnnotations(
Arrays.asList(
AnnotationNode.withType(
TypeNode.withReference(ConcreteReference.withClazz(Generated.class)))))
.build();

variableExpr.accept(writerVisitor);
assertEquals(
LineFormatter.lines(
"import com.google.api.generator.engine.ast.Expr;\n",
"import javax.annotation.Generated;\n\n"),
writerVisitor.write());
}

@Test
public void writeAnonymousClassExprImports() {
// [Constructing] Function<List<IOException>, MethodDefinition>
Expand Down

0 comments on commit 78b15ab

Please sign in to comment.