From 14d18af13a466a7ecf669b0f598f34862d176d5e Mon Sep 17 00:00:00 2001 From: Mikael Arneborn Date: Mon, 22 Feb 2021 16:14:13 -0800 Subject: [PATCH 01/12] don't use comments from declarations whose parent is hasNoDefaultLib --- test/programs/comments-from-lib/main.ts | 9 +++++++++ test/programs/comments-from-lib/schema.json | 13 +++++++++++++ test/schema.test.ts | 1 + 3 files changed, 23 insertions(+) create mode 100644 test/programs/comments-from-lib/main.ts create mode 100644 test/programs/comments-from-lib/schema.json diff --git a/test/programs/comments-from-lib/main.ts b/test/programs/comments-from-lib/main.ts new file mode 100644 index 00000000..29bd78f8 --- /dev/null +++ b/test/programs/comments-from-lib/main.ts @@ -0,0 +1,9 @@ +/** + * Use this comment + */ +export type MyObject = Pick; + +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/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", () => { From 5df1ec15524213f3fb82728570b32571b4befda2 Mon Sep 17 00:00:00 2001 From: Mikael Arneborn Date: Mon, 22 Feb 2021 17:24:15 -0800 Subject: [PATCH 02/12] more tests --- test/programs/comments-from-lib/main.ts | 3 +++ test/programs/comments-from-lib2/main.ts | 4 ++++ test/programs/comments-from-lib2/schema.json | 13 +++++++++++++ test/schema.test.ts | 3 ++- 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 test/programs/comments-from-lib2/main.ts create mode 100644 test/programs/comments-from-lib2/schema.json diff --git a/test/programs/comments-from-lib/main.ts b/test/programs/comments-from-lib/main.ts index 29bd78f8..a09df518 100644 --- a/test/programs/comments-from-lib/main.ts +++ b/test/programs/comments-from-lib/main.ts @@ -3,6 +3,9 @@ */ export type MyObject = Pick; +/** + * Not this comment though + */ interface BigThing { prop1: string; prop2: string; diff --git a/test/programs/comments-from-lib2/main.ts b/test/programs/comments-from-lib2/main.ts new file mode 100644 index 00000000..2371b067 --- /dev/null +++ b/test/programs/comments-from-lib2/main.ts @@ -0,0 +1,4 @@ +/** + * Use this comment + */ +export type MyObject2 = string; diff --git a/test/programs/comments-from-lib2/schema.json b/test/programs/comments-from-lib2/schema.json new file mode 100644 index 00000000..2457b93c --- /dev/null +++ b/test/programs/comments-from-lib2/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/schema.test.ts b/test/schema.test.ts index 2d92a3f1..882f2e53 100644 --- a/test/schema.test.ts +++ b/test/schema.test.ts @@ -42,7 +42,7 @@ export function assertSchema( const files = [resolve(BASE + group + "/main.ts")]; const actual = TJS.generateSchema(TJS.getProgramFromFiles(files, compilerOptions), type, settings, files); - + console.log('actual', JSON.stringify(actual, null, 2)) // writeFileSync(BASE + group + "/schema.json", stringify(actual, {space: 4}) + "\n\n"); const file = readFileSync(BASE + group + "/schema.json", "utf8"); @@ -298,6 +298,7 @@ describe("schema", () => { aliasRef: true, }); assertSchema("comments-from-lib", "MyObject"); + assertSchema("comments-from-lib2", "MyObject2"); }); describe("types", () => { From c96c8d086038eb17bf9fa46deb5fb6c353090815 Mon Sep 17 00:00:00 2001 From: Mikael Arneborn Date: Mon, 22 Feb 2021 18:14:18 -0800 Subject: [PATCH 03/12] add comment if utility type --- test/programs/comments-from-lib2/schema.json | 10 +--------- typescript-json-schema.ts | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/test/programs/comments-from-lib2/schema.json b/test/programs/comments-from-lib2/schema.json index 2457b93c..b198ffdc 100644 --- a/test/programs/comments-from-lib2/schema.json +++ b/test/programs/comments-from-lib2/schema.json @@ -1,13 +1,5 @@ { "description": "Use this comment", - "type": "object", - "properties": { - "prop1": { - "type": "string" - } - }, - "required": [ - "prop1" - ], + "type": "string", "$schema": "http://json-schema.org/draft-07/schema#" } diff --git a/typescript-json-schema.ts b/typescript-json-schema.ts index 87236ad4..9b6607e5 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. */ @@ -522,8 +530,7 @@ export class JsonSchemaGenerator { // the comments for a symbol const comments = symbol.getDocumentationComment(this.tc); - - if (comments.length) { + if (comments.length && !this.isFromDefaultLib(symbol)) { definition.description = comments .map((comment) => comment.kind === "lineBreak" ? comment.text : comment.text.trim().replace(/\r\n/g, "\n") @@ -1262,6 +1269,11 @@ 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]) { From 81e7c2dc3f1701175809f161cddcd94ff8e77304 Mon Sep 17 00:00:00 2001 From: Mikael Arneborn Date: Mon, 22 Feb 2021 18:14:33 -0800 Subject: [PATCH 04/12] add comment if utility type --- test/schema.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/schema.test.ts b/test/schema.test.ts index 882f2e53..5d04df96 100644 --- a/test/schema.test.ts +++ b/test/schema.test.ts @@ -42,7 +42,6 @@ export function assertSchema( const files = [resolve(BASE + group + "/main.ts")]; const actual = TJS.generateSchema(TJS.getProgramFromFiles(files, compilerOptions), type, settings, files); - console.log('actual', JSON.stringify(actual, null, 2)) // writeFileSync(BASE + group + "/schema.json", stringify(actual, {space: 4}) + "\n\n"); const file = readFileSync(BASE + group + "/schema.json", "utf8"); From 267c41042b8ed2dc1663b7e728c48163e46d29c1 Mon Sep 17 00:00:00 2001 From: Mikael Arneborn Date: Mon, 22 Feb 2021 18:19:34 -0800 Subject: [PATCH 05/12] cleanup --- test/schema.test.ts | 1 + typescript-json-schema.ts | 17 ++++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/test/schema.test.ts b/test/schema.test.ts index 5d04df96..31adb9ef 100644 --- a/test/schema.test.ts +++ b/test/schema.test.ts @@ -42,6 +42,7 @@ export function assertSchema( const files = [resolve(BASE + group + "/main.ts")]; const actual = TJS.generateSchema(TJS.getProgramFromFiles(files, compilerOptions), type, settings, files); + // writeFileSync(BASE + group + "/schema.json", stringify(actual, {space: 4}) + "\n\n"); const file = readFileSync(BASE + group + "/schema.json", "utf8"); diff --git a/typescript-json-schema.ts b/typescript-json-schema.ts index 9b6607e5..c53c8e96 100644 --- a/typescript-json-schema.ts +++ b/typescript-json-schema.ts @@ -529,13 +529,16 @@ export class JsonSchemaGenerator { } // the comments for a symbol - const comments = symbol.getDocumentationComment(this.tc); - if (comments.length && !this.isFromDefaultLib(symbol)) { - definition.description = comments - .map((comment) => - comment.kind === "lineBreak" ? comment.text : comment.text.trim().replace(/\r\n/g, "\n") - ) - .join(""); + if (!this.isFromDefaultLib(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(""); + } } // jsdocs are separate from comments From 0e3997e980f6873e6774962f8d9c30471936e6f4 Mon Sep 17 00:00:00 2001 From: Mikael Arneborn Date: Mon, 22 Feb 2021 18:20:33 -0800 Subject: [PATCH 06/12] revert --- test/schema.test.ts | 1 - typescript-json-schema.ts | 5 ----- 2 files changed, 6 deletions(-) diff --git a/test/schema.test.ts b/test/schema.test.ts index 31adb9ef..2d92a3f1 100644 --- a/test/schema.test.ts +++ b/test/schema.test.ts @@ -298,7 +298,6 @@ describe("schema", () => { aliasRef: true, }); assertSchema("comments-from-lib", "MyObject"); - assertSchema("comments-from-lib2", "MyObject2"); }); describe("types", () => { diff --git a/typescript-json-schema.ts b/typescript-json-schema.ts index c53c8e96..439b0665 100644 --- a/typescript-json-schema.ts +++ b/typescript-json-schema.ts @@ -1272,11 +1272,6 @@ 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]) { From 6f92eada70ea11ae0f498fc59edd690324c207ef Mon Sep 17 00:00:00 2001 From: Mikael Arneborn Date: Mon, 22 Feb 2021 18:21:02 -0800 Subject: [PATCH 07/12] removed tmp test --- test/programs/comments-from-lib2/main.ts | 4 ---- test/programs/comments-from-lib2/schema.json | 5 ----- 2 files changed, 9 deletions(-) delete mode 100644 test/programs/comments-from-lib2/main.ts delete mode 100644 test/programs/comments-from-lib2/schema.json diff --git a/test/programs/comments-from-lib2/main.ts b/test/programs/comments-from-lib2/main.ts deleted file mode 100644 index 2371b067..00000000 --- a/test/programs/comments-from-lib2/main.ts +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Use this comment - */ -export type MyObject2 = string; diff --git a/test/programs/comments-from-lib2/schema.json b/test/programs/comments-from-lib2/schema.json deleted file mode 100644 index b198ffdc..00000000 --- a/test/programs/comments-from-lib2/schema.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "description": "Use this comment", - "type": "string", - "$schema": "http://json-schema.org/draft-07/schema#" -} From ec32fc32a5558576b7aab0834ad6724f2f2fac5e Mon Sep 17 00:00:00 2001 From: Mikael Arneborn Date: Mon, 22 Feb 2021 18:24:00 -0800 Subject: [PATCH 08/12] changed dates test to not have comment from lib --- test/programs/dates/schema.json | 2 -- 1 file changed, 2 deletions(-) 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" } From 37e52047c693516d1720bde3f0d055c04eb9bed0 Mon Sep 17 00:00:00 2001 From: Mikael Arneborn Date: Mon, 22 Feb 2021 18:26:42 -0800 Subject: [PATCH 09/12] more tests that have comments from lib --- test/programs/comments-from-lib/main.ts | 4 ++-- test/programs/comments-from-lib/schema.json | 1 - test/programs/type-aliases-partial/schema.json | 4 +--- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/test/programs/comments-from-lib/main.ts b/test/programs/comments-from-lib/main.ts index a09df518..307ec5cb 100644 --- a/test/programs/comments-from-lib/main.ts +++ b/test/programs/comments-from-lib/main.ts @@ -1,10 +1,10 @@ /** - * Use this comment + * Want this comment, but doesn't work at the moment */ export type MyObject = Pick; /** - * Not this comment though + * This comment should be ignored */ interface BigThing { prop1: string; diff --git a/test/programs/comments-from-lib/schema.json b/test/programs/comments-from-lib/schema.json index 2457b93c..5244f28b 100644 --- a/test/programs/comments-from-lib/schema.json +++ b/test/programs/comments-from-lib/schema.json @@ -1,5 +1,4 @@ { - "description": "Use this comment", "type": "object", "properties": { "prop1": { 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 +} From 5ebe8875a8f1073ebf5523836e7a89610e3bdbfd Mon Sep 17 00:00:00 2001 From: Mikael Arneborn Date: Mon, 22 Feb 2021 18:35:30 -0800 Subject: [PATCH 10/12] lint --- typescript-json-schema.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/typescript-json-schema.ts b/typescript-json-schema.ts index 439b0665..3ff8c030 100644 --- a/typescript-json-schema.ts +++ b/typescript-json-schema.ts @@ -528,17 +528,17 @@ export class JsonSchemaGenerator { return; } - // the comments for a symbol - if (!this.isFromDefaultLib(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 (!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(""); + } } // jsdocs are separate from comments From 070d9c70ad9ddc43c6902bf6aebc0c0ccb5ba612 Mon Sep 17 00:00:00 2001 From: Mikael Arneborn Date: Tue, 23 Feb 2021 11:28:26 -0800 Subject: [PATCH 11/12] use comment even if type is a utility type --- test/programs/comments-from-lib/main.ts | 4 ++-- test/programs/comments-from-lib/schema.json | 1 + typescript-json-schema.ts | 4 ++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/test/programs/comments-from-lib/main.ts b/test/programs/comments-from-lib/main.ts index 307ec5cb..a09df518 100644 --- a/test/programs/comments-from-lib/main.ts +++ b/test/programs/comments-from-lib/main.ts @@ -1,10 +1,10 @@ /** - * Want this comment, but doesn't work at the moment + * Use this comment */ export type MyObject = Pick; /** - * This comment should be ignored + * Not this comment though */ interface BigThing { prop1: string; diff --git a/test/programs/comments-from-lib/schema.json b/test/programs/comments-from-lib/schema.json index 5244f28b..2457b93c 100644 --- a/test/programs/comments-from-lib/schema.json +++ b/test/programs/comments-from-lib/schema.json @@ -1,4 +1,5 @@ { + "description": "Use this comment", "type": "object", "properties": { "prop1": { diff --git a/typescript-json-schema.ts b/typescript-json-schema.ts index 3ff8c030..905b2578 100644 --- a/typescript-json-schema.ts +++ b/typescript-json-schema.ts @@ -1272,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]) { From b9c393f46453d0adbdaa8501c557e6a97ea55219 Mon Sep 17 00:00:00 2001 From: Mikael Arneborn Date: Tue, 23 Feb 2021 11:53:11 -0800 Subject: [PATCH 12/12] removed bang --- typescript-json-schema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typescript-json-schema.ts b/typescript-json-schema.ts index 905b2578..4ca1b769 100644 --- a/typescript-json-schema.ts +++ b/typescript-json-schema.ts @@ -1273,7 +1273,7 @@ export class JsonSchemaGenerator { this.parseCommentsIntoDefinition(prop, returnedDefinition, otherAnnotations); } if (pairedSymbol && symbol && this.isFromDefaultLib(symbol)) { - this.parseCommentsIntoDefinition(pairedSymbol!, definition, otherAnnotations); + this.parseCommentsIntoDefinition(pairedSymbol, definition, otherAnnotations); } // Create the actual definition only if is an inline definition, or