From bede4e516cd690647cd8617977f520967d6222d4 Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Thu, 11 Jan 2024 11:27:32 -1000 Subject: [PATCH] update docs and test --- docs/migrations/version-2-to-3.md | 43 +++++++++++++++++++ .../__snapshots__/index.spec.ts.snap | 20 +++++++++ examples/typescript-use-esm/index.spec.ts | 1 + .../__snapshots__/index.spec.ts.snap | 9 ++-- .../index.spec.ts | 6 +-- .../index.ts | 12 ++---- 6 files changed, 73 insertions(+), 18 deletions(-) diff --git a/docs/migrations/version-2-to-3.md b/docs/migrations/version-2-to-3.md index 0faac990ad..2b394ab93e 100644 --- a/docs/migrations/version-2-to-3.md +++ b/docs/migrations/version-2-to-3.md @@ -21,9 +21,52 @@ const generator = new JavaFileGenerator({ ## TypeScript ### JS reserved keywords are no longer applied by default +By default up until now, JS reserved keywords have been checked for TS as well. Which means that something like: +``` +{ + $schema: 'http://json-schema.org/draft-07/schema#', + type: 'object', + additionalProperties: false, + properties: { + location: { + type: 'string' + } + } +} ``` +Would be default be rendered as: +```ts +class Root { + private _reservedLocation?: string; + + constructor(input: { + reservedLocation?: string, + }) { + this._reservedLocation = input.reservedLocation; + } + + get reservedLocation(): string | undefined { return this._reservedLocation; } + set reservedLocation(reservedLocation: string | undefined) { this._reservedLocation = reservedLocation; } +} +``` + +However, without setting `useJavascriptReservedKeywords: true` by default the following will be generated: + +```ts +class Root { + private _location?: string; + + constructor(input: { + location?: string, + }) { + this._location = input.location; + } + + get location(): string | undefined { return this._location; } + set location(location: string | undefined) { this._location = location; } +} ``` ## JavaScript diff --git a/examples/typescript-use-esm/__snapshots__/index.spec.ts.snap b/examples/typescript-use-esm/__snapshots__/index.spec.ts.snap index a8f7734995..1dad878abf 100644 --- a/examples/typescript-use-esm/__snapshots__/index.spec.ts.snap +++ b/examples/typescript-use-esm/__snapshots__/index.spec.ts.snap @@ -1,6 +1,26 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`Should be able to render models to ESM module system and should log expected output to console 1`] = ` +Array [ + "import Person from './Person'; +class Root { + private _person?: Person; + + constructor(input: { + person?: Person, + }) { + this._person = input.person; + } + + get person(): Person | undefined { return this._person; } + set person(person: Person | undefined) { this._person = person; } +} +export default Root; +", +] +`; + +exports[`Should be able to render models to ESM module system and should log expected output to console 2`] = ` Array [ " class Person { diff --git a/examples/typescript-use-esm/index.spec.ts b/examples/typescript-use-esm/index.spec.ts index 987374c1e9..2c4087b2c2 100644 --- a/examples/typescript-use-esm/index.spec.ts +++ b/examples/typescript-use-esm/index.spec.ts @@ -10,6 +10,7 @@ describe('Should be able to render models to ESM module system', () => { test('and should log expected output to console', async () => { await generate(); expect(spy.mock.calls.length).toEqual(2); + expect(spy.mock.calls[0]).toMatchSnapshot(); expect(spy.mock.calls[1]).toMatchSnapshot(); }); }); diff --git a/examples/typescript-use-js-reserved-keyword/__snapshots__/index.spec.ts.snap b/examples/typescript-use-js-reserved-keyword/__snapshots__/index.spec.ts.snap index fa6aafd56c..db10da51ad 100644 --- a/examples/typescript-use-js-reserved-keyword/__snapshots__/index.spec.ts.snap +++ b/examples/typescript-use-js-reserved-keyword/__snapshots__/index.spec.ts.snap @@ -1,9 +1,8 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Should be able to render models to ESM module system and should log expected output to console 1`] = ` +exports[`Should be able to render models to not use reserved keywords from JS and should log expected output to console 1`] = ` Array [ - " -class Person { + "class Root { private _reservedLocation?: string; constructor(input: { @@ -14,8 +13,6 @@ class Person { get reservedLocation(): string | undefined { return this._reservedLocation; } set reservedLocation(reservedLocation: string | undefined) { this._reservedLocation = reservedLocation; } -} -export default Person; -", +}", ] `; diff --git a/examples/typescript-use-js-reserved-keyword/index.spec.ts b/examples/typescript-use-js-reserved-keyword/index.spec.ts index 987374c1e9..c3457258d5 100644 --- a/examples/typescript-use-js-reserved-keyword/index.spec.ts +++ b/examples/typescript-use-js-reserved-keyword/index.spec.ts @@ -3,13 +3,13 @@ const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { }); import { generate } from './index'; -describe('Should be able to render models to ESM module system', () => { +describe('Should be able to render models to not use reserved keywords from JS', () => { afterAll(() => { jest.restoreAllMocks(); }); test('and should log expected output to console', async () => { await generate(); - expect(spy.mock.calls.length).toEqual(2); - expect(spy.mock.calls[1]).toMatchSnapshot(); + expect(spy.mock.calls.length).toEqual(1); + expect(spy.mock.calls[0]).toMatchSnapshot(); }); }); diff --git a/examples/typescript-use-js-reserved-keyword/index.ts b/examples/typescript-use-js-reserved-keyword/index.ts index a4208f09e0..b297651e06 100644 --- a/examples/typescript-use-js-reserved-keyword/index.ts +++ b/examples/typescript-use-js-reserved-keyword/index.ts @@ -8,20 +8,14 @@ const jsonSchemaDraft7 = { type: 'object', additionalProperties: false, properties: { - person: { - type: 'object', - additionalProperties: false, - properties: { - location: { - type: 'string' - } - } + location: { + type: 'string' } } }; export async function generate(): Promise { - const models = await generator.generateCompleteModels(jsonSchemaDraft7, {}); + const models = await generator.generate(jsonSchemaDraft7); for (const model of models) { console.log(model.result); }