Skip to content

Commit

Permalink
add comment to the top of component (schema type) files
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcelhaney committed Mar 13, 2024
1 parent 9ed5fd3 commit 411dc3c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
7 changes: 2 additions & 5 deletions src/typescript-generator/operation-type-coder.js
Expand Up @@ -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";

Expand Down Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions 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",
];
2 changes: 2 additions & 0 deletions src/typescript-generator/schema-type-coder.js
Expand Up @@ -116,6 +116,8 @@ export class SchemaTypeCoder extends Coder {
}

write(script) {
// script.comments = READ_ONLY_COMMENTS;

if (this.requirement.isReference) {
return script.importType(this);
}
Expand Down
4 changes: 2 additions & 2 deletions test/server/code-generator.test.ts
Expand Up @@ -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");
});
});

Expand All @@ -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");
});
});
});
24 changes: 12 additions & 12 deletions test/typescript-generator/schema-type-coder.test.js
Expand Up @@ -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,
);
});
Expand Down Expand Up @@ -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,
);
});
Expand All @@ -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,
);
});
Expand All @@ -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,
);
});
Expand All @@ -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,
);
});
Expand All @@ -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,
);
});
Expand All @@ -226,7 +226,7 @@ describe("a SchemaTypeCoder", () => {

const expected = await format("type x = Array<string>;");

await expect(format(`type x = ${coder.write()}`)).resolves.toStrictEqual(
await expect(format(`type x = ${coder.write({})}`)).resolves.toStrictEqual(
expected,
);
});
Expand All @@ -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,
);
});
Expand All @@ -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,
);
});
Expand All @@ -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,
);
});
Expand All @@ -284,7 +284,7 @@ describe("a SchemaTypeCoder", () => {
// The best we could do is Exclude<any, string>, 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,
);
});
Expand All @@ -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,
);
});
Expand Down

0 comments on commit 411dc3c

Please sign in to comment.