Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to remove comment about interface referencer #575

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
| style | object | `{ bracketSpacing: false, printWidth: 120, semi: true, singleQuote: false, tabWidth: 2, trailingComma: 'none', useTabs: false }` | A [Prettier](https://prettier.io/docs/en/options.html) configuration |
| unknownAny | boolean | `true` | Use `unknown` instead of `any` where possible |
| unreachableDefinitions | boolean | `false` | Generates code for `$defs` that aren't referenced by the schema. |
| interfaceReferenceComment | boolean | `true` | Generate comment for: defintions that aren't referenced by schema, and pattern properties; pointing to referencer. |
| $refOptions | object | `{}` | [$RefParser](https://github.com/BigstickCarpet/json-schema-ref-parser) Options, used when resolving `$ref`s |
## CLI

Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export interface Options {
* Generate code for `definitions` that aren't referenced by the schema?
*/
unreachableDefinitions: boolean
/**
* Generate comment for: defintions that aren't referenced by schema, and pattern properties; pointing to referencer
*/
interfaceReferenceComment: boolean
/**
* Generate unknown type instead of any
*/
Expand Down Expand Up @@ -104,6 +108,7 @@ export const DEFAULT_OPTIONS: Options = {
useTabs: false,
},
unreachableDefinitions: false,
interfaceReferenceComment: true,
unknownAny: true,
}

Expand Down
16 changes: 12 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,13 @@ function parseSchema(
asts = asts.concat(
map(schema.patternProperties, (value, key: string) => {
const ast = parse(value, options, key, processed, usedNames)
const comment = `This interface was referenced by \`${parentSchemaName}\`'s JSON-Schema definition

if (options.interfaceReferenceComment) {
const comment = `This interface was referenced by \`${parentSchemaName}\`'s JSON-Schema definition
via the \`patternProperty\` "${key.replace('*/', '*\\/')}".`
ast.comment = ast.comment ? `${ast.comment}\n\n${comment}` : comment
ast.comment = ast.comment ? `${ast.comment}\n\n${comment}` : comment
}

return {
ast,
isPatternProperty: !singlePatternProperty,
Expand All @@ -431,9 +435,13 @@ via the \`patternProperty\` "${key.replace('*/', '*\\/')}".`
asts = asts.concat(
map(schema.$defs, (value, key: string) => {
const ast = parse(value, options, key, processed, usedNames)
const comment = `This interface was referenced by \`${parentSchemaName}\`'s JSON-Schema

if (options.interfaceReferenceComment) {
const comment = `This interface was referenced by \`${parentSchemaName}\`'s JSON-Schema
via the \`definition\` "${key}".`
ast.comment = ast.comment ? `${ast.comment}\n\n${comment}` : comment
ast.comment = ast.comment ? `${ast.comment}\n\n${comment}` : comment
}

return {
ast,
isPatternProperty: false,
Expand Down
40 changes: 40 additions & 0 deletions test/__snapshots__/test/test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -2213,6 +2213,26 @@ Generated by [AVA](https://avajs.dev).
}␊
`

## options.interfaceReferenceComment.js

> Expected output to match snapshot for e2e test: options.interfaceReferenceComment.js

`/* eslint-disable */␊
/**␊
* This file was automatically generated by json-schema-to-typescript.␊
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
* and run json-schema-to-typescript to regenerate this file.␊
*/␊
export interface UnreachableDefinition {␊
b?: {␊
likesDogs?: boolean;␊
[k: string]: unknown;␊
};␊
[k: string]: unknown;␊
}␊
`

## options.strictIndexSignatures.js

> Expected output to match snapshot for e2e test: options.strictIndexSignatures.js
Expand Down Expand Up @@ -449648,3 +449668,23 @@ Generated by [AVA](https://avajs.dev).
g?: number;␊
}␊
`

## options.referenceSourceComment.js

> Expected output to match snapshot for e2e test: options.referenceSourceComment.js

`/* eslint-disable */␊
/**␊
* This file was automatically generated by json-schema-to-typescript.␊
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
* and run json-schema-to-typescript to regenerate this file.␊
*/␊
export interface UnreachableDefinition {␊
b?: {␊
likesDogs?: boolean;␊
[k: string]: unknown;␊
};␊
[k: string]: unknown;␊
}␊
`
Binary file modified test/__snapshots__/test/test.ts.snap
Binary file not shown.
30 changes: 30 additions & 0 deletions test/e2e/options.interfaceReferenceComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export const input = {
title: 'UnreachableDefinition',
type: 'object',
definitions: {
a: {
properties: {
firstName: {
type: 'string',
},
lastName: {
id: 'lastName',
type: 'string',
},
},
},
},
properties: {
b: {
properties: {
likesDogs: {
type: 'boolean',
},
},
},
},
}

export const options = {
interfaceReferenceComment: false,
}