Skip to content
Open
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
16 changes: 16 additions & 0 deletions libs/java-ast/src/lib/core/Writer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,20 @@ describe("Writer", () => {
const formatted = writer.formatCode("public void method() {");
expect(formatted).toBe("public void method() {");
});

it("should correctly de-duplicate imports and references", () => {
writer.addReference(
new ClassReference({ name: "ArrayList", packageName: "java.util" }),
);
writer.addReference(
new ClassReference({ name: "ArrayList", packageName: "java.util" }),
);
writer.addImport("java.util.ArrayList");
writer.addImport("java.util.ArrayList");

const output = writer.toString();
expect(output.trim()).toBe(`package com.example;

import java.util.ArrayList;`);
});
});
15 changes: 8 additions & 7 deletions libs/java-ast/src/lib/core/Writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class Writer implements IWriter {
private indentLevel = 0;
private buffer = "";
private imports: Set<string> = new Set();
private references: Set<ClassReference> = new Set();
private references: Set<string> = new Set();
private packageName: string;
private skipPackageDeclaration: boolean;

Expand Down Expand Up @@ -111,7 +111,9 @@ export class Writer implements IWriter {
if (reference.packageName === this.packageName) {
return;
}
this.references.add(reference);
// Sets compare objects by reference, so we need to
// convert to string before adding
this.references.add(`${reference.packageName}.${reference.name}`);
}

/**
Expand Down Expand Up @@ -155,11 +157,10 @@ export class Writer implements IWriter {
result += `package ${this.packageName};\n\n`;
}

// Write imports
const allImports = [...this.imports];
this.references.forEach((ref) => {
allImports.push(`${ref.packageName}.${ref.name}`);
});
// Combine imports with references, and remove duplicates
const allImports = [
...new Set<string>([...this.imports, ...this.references]),
];

// Sort imports
allImports.sort().forEach((importName) => {
Expand Down
14 changes: 0 additions & 14 deletions libs/java-ast/tests/__snapshots__/ComplexExample.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,23 @@
exports[`Complex Example should generate a complex Spring Boot controller 1`] = `
"package com.example.controller;

import com.example.dto.UserDTO;
import com.example.dto.UserDTO;
import com.example.dto.UserDTO;
import com.example.dto.UserDTO;
import com.example.dto.UserDTO;
import com.example.dto.UserDTO;
import com.example.dto.UserDTO;
import com.example.exception.DuplicateResourceException;
import com.example.service.UserService;
import com.example.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import java.lang.String;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.List;
import java.util.Map;
import java.util.Map;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down