From 411dc3ccef25682601f77d3d83c1ded0054d64a8 Mon Sep 17 00:00:00 2001 From: Patrick McElhaney Date: Wed, 13 Mar 2024 18:40:17 -0400 Subject: [PATCH] add comment to the top of component (schema type) files --- .../operation-type-coder.js | 7 ++---- .../read-only-comments.js | 5 ++++ src/typescript-generator/schema-type-coder.js | 2 ++ test/server/code-generator.test.ts | 4 ++-- .../schema-type-coder.test.js | 24 +++++++++---------- 5 files changed, 23 insertions(+), 19 deletions(-) create mode 100644 src/typescript-generator/read-only-comments.js diff --git a/src/typescript-generator/operation-type-coder.js b/src/typescript-generator/operation-type-coder.js index e001b02a..f5223fad 100644 --- a/src/typescript-generator/operation-type-coder.js +++ b/src/typescript-generator/operation-type-coder.js @@ -3,6 +3,7 @@ import nodePath from "node:path"; import { Coder } from "./coder.js"; import { CONTEXT_FILE_TOKEN } from "./context-file-token.js"; import { ParametersTypeCoder } from "./parameters-type-coder.js"; +import { READ_ONLY_COMMENTS } from "./read-only-comments.js"; import { ResponseTypeCoder } from "./response-type-coder.js"; import { SchemaTypeCoder } from "./schema-type-coder.js"; @@ -70,11 +71,7 @@ export class OperationTypeCoder extends Coder { // eslint-disable-next-line max-statements write(script) { // eslint-disable-next-line no-param-reassign - script.comments = [ - "This code was automatically generated from an OpenAPI description.", - "Do not edit this file. Edit the OpenAPI file instead.", - "For more information, see https://github.com/pmcelhaney/counterfact/blob/main/docs/faq-generated-code.md", - ]; + script.comments = READ_ONLY_COMMENTS; const contextTypeImportName = script.importExternalType( "Context", diff --git a/src/typescript-generator/read-only-comments.js b/src/typescript-generator/read-only-comments.js new file mode 100644 index 00000000..a19d62d7 --- /dev/null +++ b/src/typescript-generator/read-only-comments.js @@ -0,0 +1,5 @@ +export const READ_ONLY_COMMENTS = [ + "This code was automatically generated from an OpenAPI description.", + "Do not edit this file. Edit the OpenAPI file instead.", + "For more information, see https://github.com/pmcelhaney/counterfact/blob/main/docs/faq-generated-code.md", +]; diff --git a/src/typescript-generator/schema-type-coder.js b/src/typescript-generator/schema-type-coder.js index 5567cc90..bd9de52a 100644 --- a/src/typescript-generator/schema-type-coder.js +++ b/src/typescript-generator/schema-type-coder.js @@ -116,6 +116,8 @@ export class SchemaTypeCoder extends Coder { } write(script) { + // script.comments = READ_ONLY_COMMENTS; + if (this.requirement.isReference) { return script.importType(this); } diff --git a/test/server/code-generator.test.ts b/test/server/code-generator.test.ts index f1dc89be..7ca85dbb 100644 --- a/test/server/code-generator.test.ts +++ b/test/server/code-generator.test.ts @@ -44,7 +44,7 @@ describe("a CodeGenerator", () => { await generator.stopWatching(); - expect(exampleComponent).toEqual("export type Example = string;\n"); + expect(exampleComponent).toContain("export type Example = string;\n"); }); }); @@ -69,7 +69,7 @@ describe("a CodeGenerator", () => { await generator.stopWatching(); - expect(exampleComponent).toEqual("export type Example = number;\n"); + expect(exampleComponent).toContain("export type Example = number;\n"); }); }); }); diff --git a/test/typescript-generator/schema-type-coder.test.js b/test/typescript-generator/schema-type-coder.test.js index fc095ab5..96b2542c 100644 --- a/test/typescript-generator/schema-type-coder.test.js +++ b/test/typescript-generator/schema-type-coder.test.js @@ -97,7 +97,7 @@ describe("a SchemaTypeCoder", () => { "type x = { age?: number, name?: string, [key: string]: unknown };", ); - await expect(format(`type x = ${coder.write()}`)).resolves.toStrictEqual( + await expect(format(`type x = ${coder.write({})}`)).resolves.toStrictEqual( expected, ); }); @@ -127,7 +127,7 @@ describe("a SchemaTypeCoder", () => { `, ); - await expect(format(`type x = ${coder.write()}`)).resolves.toStrictEqual( + await expect(format(`type x = ${coder.write({})}`)).resolves.toStrictEqual( expected, ); }); @@ -150,7 +150,7 @@ describe("a SchemaTypeCoder", () => { "type x = { age?: number, name?: string, [key: string]: unknown };", ); - await expect(format(`type x = ${coder.write()}`)).resolves.toStrictEqual( + await expect(format(`type x = ${coder.write({})}`)).resolves.toStrictEqual( expected, ); }); @@ -171,7 +171,7 @@ describe("a SchemaTypeCoder", () => { const expected = await format("type x = { age?: number, name?: string};"); - await expect(format(`type x = ${coder.write()}`)).resolves.toStrictEqual( + await expect(format(`type x = ${coder.write({})}`)).resolves.toStrictEqual( expected, ); }); @@ -187,7 +187,7 @@ describe("a SchemaTypeCoder", () => { const expected = await format("type x = { [key: string]: boolean };"); - await expect(format(`type x = ${coder.write()}`)).resolves.toStrictEqual( + await expect(format(`type x = ${coder.write({})}`)).resolves.toStrictEqual( expected, ); }); @@ -210,7 +210,7 @@ describe("a SchemaTypeCoder", () => { "type x = { anotherNumber?: number; aNumber?: number; [key: string]: number };", ); - await expect(format(`type x = ${coder.write()}`)).resolves.toStrictEqual( + await expect(format(`type x = ${coder.write({})}`)).resolves.toStrictEqual( expected, ); }); @@ -226,7 +226,7 @@ describe("a SchemaTypeCoder", () => { const expected = await format("type x = Array;"); - await expect(format(`type x = ${coder.write()}`)).resolves.toStrictEqual( + await expect(format(`type x = ${coder.write({})}`)).resolves.toStrictEqual( expected, ); }); @@ -240,7 +240,7 @@ describe("a SchemaTypeCoder", () => { const expected = await format("type x = string & number;"); - await expect(format(`type x = ${coder.write()}`)).resolves.toStrictEqual( + await expect(format(`type x = ${coder.write({})}`)).resolves.toStrictEqual( expected, ); }); @@ -254,7 +254,7 @@ describe("a SchemaTypeCoder", () => { const expected = await format("type x = string | number;"); - await expect(format(`type x = ${coder.write()}`)).resolves.toStrictEqual( + await expect(format(`type x = ${coder.write({})}`)).resolves.toStrictEqual( expected, ); }); @@ -268,7 +268,7 @@ describe("a SchemaTypeCoder", () => { const expected = await format("type x = string | number;"); - await expect(format(`type x = ${coder.write()}`)).resolves.toStrictEqual( + await expect(format(`type x = ${coder.write({})}`)).resolves.toStrictEqual( expected, ); }); @@ -284,7 +284,7 @@ describe("a SchemaTypeCoder", () => { // The best we could do is Exclude, but that doesn't actually exclude strings const expected = await format("type x = unknown;"); - await expect(format(`type x = ${coder.write()}`)).resolves.toStrictEqual( + await expect(format(`type x = ${coder.write({})}`)).resolves.toStrictEqual( expected, ); }); @@ -299,7 +299,7 @@ describe("a SchemaTypeCoder", () => { const expected = await format('type x = 1 | "two" | null;'); - await expect(format(`type x = ${coder.write()}`)).resolves.toStrictEqual( + await expect(format(`type x = ${coder.write({})}`)).resolves.toStrictEqual( expected, ); });