Skip to content

Commit

Permalink
update docs and test
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Jan 11, 2024
1 parent ce7835d commit bede4e5
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 18 deletions.
43 changes: 43 additions & 0 deletions docs/migrations/version-2-to-3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions examples/typescript-use-esm/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
1 change: 1 addition & 0 deletions examples/typescript-use-esm/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -14,8 +13,6 @@ class Person {
get reservedLocation(): string | undefined { return this._reservedLocation; }
set reservedLocation(reservedLocation: string | undefined) { this._reservedLocation = reservedLocation; }
}
export default Person;
",
}",
]
`;
6 changes: 3 additions & 3 deletions examples/typescript-use-js-reserved-keyword/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
12 changes: 3 additions & 9 deletions examples/typescript-use-js-reserved-keyword/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const models = await generator.generateCompleteModels(jsonSchemaDraft7, {});
const models = await generator.generate(jsonSchemaDraft7);
for (const model of models) {
console.log(model.result);
}
Expand Down

0 comments on commit bede4e5

Please sign in to comment.