diff --git a/test/programs/comments-from-lib/main.ts b/test/programs/comments-from-lib/main.ts new file mode 100644 index 00000000..a09df518 --- /dev/null +++ b/test/programs/comments-from-lib/main.ts @@ -0,0 +1,12 @@ +/** + * Use this comment + */ +export type MyObject = Pick; + +/** + * Not this comment though + */ +interface BigThing { + prop1: string; + prop2: string; +}; diff --git a/test/programs/comments-from-lib/schema.json b/test/programs/comments-from-lib/schema.json new file mode 100644 index 00000000..2457b93c --- /dev/null +++ b/test/programs/comments-from-lib/schema.json @@ -0,0 +1,13 @@ +{ + "description": "Use this comment", + "type": "object", + "properties": { + "prop1": { + "type": "string" + } + }, + "required": [ + "prop1" + ], + "$schema": "http://json-schema.org/draft-07/schema#" +} diff --git a/test/programs/dates/schema.json b/test/programs/dates/schema.json index 39a42929..bfcdb3ac 100644 --- a/test/programs/dates/schema.json +++ b/test/programs/dates/schema.json @@ -2,12 +2,10 @@ "$schema": "http://json-schema.org/draft-07/schema#", "properties": { "var1": { - "description": "Enables basic storage and retrieval of dates and times.", "format": "date-time", "type": "string" }, "var2": { - "description": "Enables basic storage and retrieval of dates and times.", "format": "date-time", "type": "string" } diff --git a/test/programs/type-aliases-partial/schema.json b/test/programs/type-aliases-partial/schema.json index de8e7901..798ddab5 100644 --- a/test/programs/type-aliases-partial/schema.json +++ b/test/programs/type-aliases-partial/schema.json @@ -15,7 +15,6 @@ "definitions": { "__type": { "type": "object", - "description": "Make all properties in T optional", "properties": { "x": { "type": "number" @@ -30,7 +29,6 @@ }, "__type_1": { "type": "object", - "description": "Make all properties in T optional", "properties": { "a": { "type": "number" @@ -45,4 +43,4 @@ } }, "$schema": "http://json-schema.org/draft-07/schema#" -} \ No newline at end of file +} diff --git a/test/schema.test.ts b/test/schema.test.ts index 26352eb6..2d92a3f1 100644 --- a/test/schema.test.ts +++ b/test/schema.test.ts @@ -297,6 +297,7 @@ describe("schema", () => { assertSchema("comments-imports", "MyObject", { aliasRef: true, }); + assertSchema("comments-from-lib", "MyObject"); }); describe("types", () => { diff --git a/typescript-json-schema.ts b/typescript-json-schema.ts index 87236ad4..4ca1b769 100644 --- a/typescript-json-schema.ts +++ b/typescript-json-schema.ts @@ -512,6 +512,14 @@ export class JsonSchemaGenerator { return this.reffedDefinitions; } + private isFromDefaultLib(symbol: ts.Symbol) { + const declarations = symbol.getDeclarations(); + if (declarations && declarations.length > 0) { + return declarations[0].parent.getSourceFile().hasNoDefaultLib; + } + return false; + } + /** * Parse the comments of a symbol into the definition and other annotations. */ @@ -520,15 +528,17 @@ export class JsonSchemaGenerator { return; } - // the comments for a symbol - const comments = symbol.getDocumentationComment(this.tc); + if (!this.isFromDefaultLib(symbol)) { + // the comments for a symbol + const comments = symbol.getDocumentationComment(this.tc); - if (comments.length) { - definition.description = comments - .map((comment) => - comment.kind === "lineBreak" ? comment.text : comment.text.trim().replace(/\r\n/g, "\n") - ) - .join(""); + if (comments.length) { + definition.description = comments + .map((comment) => + comment.kind === "lineBreak" ? comment.text : comment.text.trim().replace(/\r\n/g, "\n") + ) + .join(""); + } } // jsdocs are separate from comments @@ -1262,6 +1272,10 @@ export class JsonSchemaGenerator { if (prop) { this.parseCommentsIntoDefinition(prop, returnedDefinition, otherAnnotations); } + if (pairedSymbol && symbol && this.isFromDefaultLib(symbol)) { + this.parseCommentsIntoDefinition(pairedSymbol, definition, otherAnnotations); + } + // Create the actual definition only if is an inline definition, or // if it will be a $ref and it is not yet created if (!asRef || !this.reffedDefinitions[fullTypeName]) {