diff --git a/README.md b/README.md index 52db849d..0231ef83 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/index.ts b/src/index.ts index 20a078de..948ed419 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 */ @@ -104,6 +108,7 @@ export const DEFAULT_OPTIONS: Options = { useTabs: false, }, unreachableDefinitions: false, + interfaceReferenceComment: true, unknownAny: true, } diff --git a/src/parser.ts b/src/parser.ts index a52479fc..acfc2d32 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -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, @@ -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, diff --git a/test/__snapshots__/test/test.ts.md b/test/__snapshots__/test/test.ts.md index 43fcff5a..f987b37d 100644 --- a/test/__snapshots__/test/test.ts.md +++ b/test/__snapshots__/test/test.ts.md @@ -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 @@ -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;␊ + }␊ + ` diff --git a/test/__snapshots__/test/test.ts.snap b/test/__snapshots__/test/test.ts.snap index cae91b34..e3afb804 100644 Binary files a/test/__snapshots__/test/test.ts.snap and b/test/__snapshots__/test/test.ts.snap differ diff --git a/test/e2e/options.interfaceReferenceComment.ts b/test/e2e/options.interfaceReferenceComment.ts new file mode 100644 index 00000000..e187a63b --- /dev/null +++ b/test/e2e/options.interfaceReferenceComment.ts @@ -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, +}