Skip to content

Commit 4b00b07

Browse files
authored
Comments and utility types (#405)
1 parent 3d0a280 commit 4b00b07

File tree

6 files changed

+49
-13
lines changed

6 files changed

+49
-13
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Use this comment
3+
*/
4+
export type MyObject = Pick<BigThing, "prop1">;
5+
6+
/**
7+
* Not this comment though
8+
*/
9+
interface BigThing {
10+
prop1: string;
11+
prop2: string;
12+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"description": "Use this comment",
3+
"type": "object",
4+
"properties": {
5+
"prop1": {
6+
"type": "string"
7+
}
8+
},
9+
"required": [
10+
"prop1"
11+
],
12+
"$schema": "http://json-schema.org/draft-07/schema#"
13+
}

test/programs/dates/schema.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
"$schema": "http://json-schema.org/draft-07/schema#",
33
"properties": {
44
"var1": {
5-
"description": "Enables basic storage and retrieval of dates and times.",
65
"format": "date-time",
76
"type": "string"
87
},
98
"var2": {
10-
"description": "Enables basic storage and retrieval of dates and times.",
119
"format": "date-time",
1210
"type": "string"
1311
}

test/programs/type-aliases-partial/schema.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"definitions": {
1616
"__type": {
1717
"type": "object",
18-
"description": "Make all properties in T optional",
1918
"properties": {
2019
"x": {
2120
"type": "number"
@@ -30,7 +29,6 @@
3029
},
3130
"__type_1": {
3231
"type": "object",
33-
"description": "Make all properties in T optional",
3432
"properties": {
3533
"a": {
3634
"type": "number"
@@ -45,4 +43,4 @@
4543
}
4644
},
4745
"$schema": "http://json-schema.org/draft-07/schema#"
48-
}
46+
}

test/schema.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ describe("schema", () => {
297297
assertSchema("comments-imports", "MyObject", {
298298
aliasRef: true,
299299
});
300+
assertSchema("comments-from-lib", "MyObject");
300301
});
301302

302303
describe("types", () => {

typescript-json-schema.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,14 @@ export class JsonSchemaGenerator {
512512
return this.reffedDefinitions;
513513
}
514514

515+
private isFromDefaultLib(symbol: ts.Symbol) {
516+
const declarations = symbol.getDeclarations();
517+
if (declarations && declarations.length > 0) {
518+
return declarations[0].parent.getSourceFile().hasNoDefaultLib;
519+
}
520+
return false;
521+
}
522+
515523
/**
516524
* Parse the comments of a symbol into the definition and other annotations.
517525
*/
@@ -520,15 +528,17 @@ export class JsonSchemaGenerator {
520528
return;
521529
}
522530

523-
// the comments for a symbol
524-
const comments = symbol.getDocumentationComment(this.tc);
531+
if (!this.isFromDefaultLib(symbol)) {
532+
// the comments for a symbol
533+
const comments = symbol.getDocumentationComment(this.tc);
525534

526-
if (comments.length) {
527-
definition.description = comments
528-
.map((comment) =>
529-
comment.kind === "lineBreak" ? comment.text : comment.text.trim().replace(/\r\n/g, "\n")
530-
)
531-
.join("");
535+
if (comments.length) {
536+
definition.description = comments
537+
.map((comment) =>
538+
comment.kind === "lineBreak" ? comment.text : comment.text.trim().replace(/\r\n/g, "\n")
539+
)
540+
.join("");
541+
}
532542
}
533543

534544
// jsdocs are separate from comments
@@ -1262,6 +1272,10 @@ export class JsonSchemaGenerator {
12621272
if (prop) {
12631273
this.parseCommentsIntoDefinition(prop, returnedDefinition, otherAnnotations);
12641274
}
1275+
if (pairedSymbol && symbol && this.isFromDefaultLib(symbol)) {
1276+
this.parseCommentsIntoDefinition(pairedSymbol, definition, otherAnnotations);
1277+
}
1278+
12651279
// Create the actual definition only if is an inline definition, or
12661280
// if it will be a $ref and it is not yet created
12671281
if (!asRef || !this.reffedDefinitions[fullTypeName]) {

0 commit comments

Comments
 (0)