diff --git a/libs/java-ast/src/lib/ast/Class.spec.ts b/libs/java-ast/src/lib/ast/Class.spec.ts index 02e0b33..ec8d48c 100644 --- a/libs/java-ast/src/lib/ast/Class.spec.ts +++ b/libs/java-ast/src/lib/ast/Class.spec.ts @@ -102,6 +102,83 @@ describe("Class", () => { expect(output).toContain("this.id = id;"); }); + it("should write constructor documentation if the constructor has it", () => { + const cls = new Class({ + name: "ClassWithConstructor", + packageName: "com.example", + access: Access.Public, + }); + + cls.addConstructor({ + access: Access.Public, + javadoc: "Some constructor", + parameters: [ + new Parameter({ + name: "id", + docs: "The id to set", + type: Type.long(), + }), + ], + body: new CodeBlock({ code: "this.id = id;" }), + }); + + cls.write(writer); + const output = writer.toString(); + + expect(output).toContain(" * Some constructor"); + expect(output).toContain(" * @param id The id to set"); + }); + + it("should write constructor documentation if the constructor doesn't, but the parameters do", () => { + const cls = new Class({ + name: "ClassWithConstructor", + packageName: "com.example", + access: Access.Public, + }); + + cls.addConstructor({ + access: Access.Public, + parameters: [ + new Parameter({ + name: "id", + docs: "The id to set", + type: Type.long(), + }), + ], + body: new CodeBlock({ code: "this.id = id;" }), + }); + + cls.write(writer); + const output = writer.toString(); + + expect(output).toContain(" * @param id The id to set"); + }); + + it("should write constructor documentation if the constructor does but the parameters don't", () => { + const cls = new Class({ + name: "ClassWithConstructor", + packageName: "com.example", + javadoc: "Some constructor", + access: Access.Public, + }); + + cls.addConstructor({ + access: Access.Public, + parameters: [ + new Parameter({ + name: "id", + type: Type.long(), + }), + ], + body: new CodeBlock({ code: "this.id = id;" }), + }); + + cls.write(writer); + const output = writer.toString(); + + expect(output).toContain(" * Some constructor"); + }); + it("should write a class with inheritance", () => { const cls = new Class({ name: "Child", diff --git a/libs/java-ast/src/lib/ast/Class.ts b/libs/java-ast/src/lib/ast/Class.ts index d6f0855..3c77bb5 100644 --- a/libs/java-ast/src/lib/ast/Class.ts +++ b/libs/java-ast/src/lib/ast/Class.ts @@ -58,6 +58,8 @@ export declare namespace Class { parameters: Parameter[]; /** The access level of the constructor */ access: Access; + /** Documentation/JavaDoc for the constructor */ + javadoc?: string; /** Constructor annotations */ annotations?: Annotation[]; /** Whether this constructor calls another constructor with super() */ @@ -316,6 +318,25 @@ export class Class extends AstNode { writer: Writer, constructor: Class.Constructor, ): void { + // Write JavaDoc if provided + if ( + constructor.javadoc || + constructor.parameters.some((param) => param.docs) + ) { + writer.writeLine("/**"); + if (constructor.javadoc) { + constructor.javadoc.split("\n").forEach((line) => { + writer.writeLine(` * ${line}`); + }); + } + constructor.parameters.forEach((param) => { + if (param.docs) { + writer.writeLine(` * @param ${param.name} ${param.docs}`); + } + }); + writer.writeLine(" */"); + } + // Write annotations if (constructor.annotations) { constructor.annotations.forEach((annotation) => {