Skip to content

Comments and utility types #405

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

Merged
merged 12 commits into from
Feb 23, 2021
Merged
12 changes: 12 additions & 0 deletions test/programs/comments-from-lib/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Use this comment
*/
export type MyObject = Pick<BigThing, "prop1">;

/**
* Not this comment though
*/
interface BigThing {
prop1: string;
prop2: string;
};
13 changes: 13 additions & 0 deletions test/programs/comments-from-lib/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"description": "Use this comment",
"type": "object",
"properties": {
"prop1": {
"type": "string"
}
},
"required": [
"prop1"
],
"$schema": "http://json-schema.org/draft-07/schema#"
}
2 changes: 0 additions & 2 deletions test/programs/dates/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
4 changes: 1 addition & 3 deletions test/programs/type-aliases-partial/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"definitions": {
"__type": {
"type": "object",
"description": "Make all properties in T optional",
"properties": {
"x": {
"type": "number"
Expand All @@ -30,7 +29,6 @@
},
"__type_1": {
"type": "object",
"description": "Make all properties in T optional",
"properties": {
"a": {
"type": "number"
Expand All @@ -45,4 +43,4 @@
}
},
"$schema": "http://json-schema.org/draft-07/schema#"
}
}
1 change: 1 addition & 0 deletions test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ describe("schema", () => {
assertSchema("comments-imports", "MyObject", {
aliasRef: true,
});
assertSchema("comments-from-lib", "MyObject");
});

describe("types", () => {
Expand Down
30 changes: 22 additions & 8 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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
Expand Down Expand Up @@ -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]) {
Expand Down