Skip to content

Commit 27d5197

Browse files
sadfsdfdsaArtem Shuvaev
and
Artem Shuvaev
authored
feat: new jsdoc comments formatter (#797)
* feat: create utils function for jsdoc comments * feat: change comment formatting for schema obj * feat: add examples with jsdoc notations Co-authored-by: Artem Shuvaev <[email protected]>
1 parent 9849d90 commit 27d5197

File tree

4 files changed

+451
-6
lines changed

4 files changed

+451
-6
lines changed

src/transform/schema.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import { GlobalContext } from "../types";
2-
import { comment, nodeType, tsArrayOf, tsIntersectionOf, tsPartial, tsReadonly, tsTupleOf, tsUnionOf } from "../utils";
2+
import {
3+
prepareComment,
4+
nodeType,
5+
tsArrayOf,
6+
tsIntersectionOf,
7+
tsPartial,
8+
tsReadonly,
9+
tsTupleOf,
10+
tsUnionOf,
11+
} from "../utils";
312

413
interface TransformSchemaObjOptions extends GlobalContext {
514
required: Set<string>;
@@ -18,11 +27,9 @@ export function transformSchemaObjMap(obj: Record<string, any>, options: Transfo
1827
for (const k of Object.keys(obj)) {
1928
const v = obj[k];
2029

21-
// 1. JSDoc comment (goes above property)
22-
let schemaComment = "";
23-
if (v.deprecated) schemaComment += `@deprecated `;
24-
if (v.description) schemaComment += v.description;
25-
if (schemaComment) output += comment(schemaComment);
30+
// 1. Add comment in jsdoc notation
31+
const comment = prepareComment(v);
32+
if (comment) output += comment;
2633

2734
// 2. name (with “?” if optional property)
2835
const readonly = tsReadonly(options.immutableTypes);

src/utils.ts

+37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
import { OpenAPI2, OpenAPI3, ReferenceObject } from "./types";
22

3+
type CommentObject = {
4+
title?: string; // not jsdoc
5+
format?: string; // not jsdoc
6+
deprecated?: boolean; // jsdoc without value
7+
description?: string; // jsdoc with value
8+
default?: string; // jsdoc with value
9+
example?: string; // jsdoc with value
10+
};
11+
12+
/**
13+
* Preparing comments from fields
14+
* @see {comment} for output examples
15+
* @returns void if not comments or jsdoc format comment string
16+
*/
17+
export function prepareComment(v: CommentObject): string | void {
18+
const commentsArray: Array<string> = [];
19+
20+
// * Not JSDOC tags: [title, format]
21+
if (v.title) commentsArray.push(`${v.title} `);
22+
if (v.format) commentsArray.push(`Format: ${v.format} `);
23+
24+
// * JSDOC tags without value
25+
// 'Deprecated' without value
26+
if (v.deprecated) commentsArray.push(`@deprecated `);
27+
28+
// * JSDOC tags with value
29+
const supportedJsDocTags: Array<keyof CommentObject> = ["description", "default", "example"];
30+
for (let index = 0; index < supportedJsDocTags.length; index++) {
31+
const field = supportedJsDocTags[index];
32+
if (v[field]) commentsArray.push(`@${field} ${v[field]} `);
33+
}
34+
35+
if (!commentsArray.length) return;
36+
37+
return comment(commentsArray.join("\n"));
38+
}
39+
340
export function comment(text: string): string {
441
const commentText = text.trim().replace(/\*\//g, "*\\/");
542

tests/v3/expected/jsdoc.ts

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/**
2+
* This file was auto-generated by openapi-typescript.
3+
* Do not make direct changes to the file.
4+
*/
5+
6+
export interface paths {
7+
"/contacts": {
8+
get: operations["getContacts"];
9+
};
10+
"/contacts/{userUid}": {
11+
get: operations["getContactInfo"];
12+
};
13+
"/contacts/{userUid}/icon": {
14+
get: operations["getContactIcon"];
15+
};
16+
"/contacts/me": {
17+
get: operations["getMyInfo"];
18+
};
19+
"/contacts/me/icon": {
20+
get: operations["getMyIcon"];
21+
delete: operations["deleteMyIcon"];
22+
};
23+
}
24+
25+
export interface components {
26+
schemas: {
27+
/** Parent of most important objects */
28+
BaseEntity: {
29+
/**
30+
* Format: nanoid
31+
* @deprecated
32+
* @description Test description with deprecated
33+
* @example njbusD52k6YoRG346tPgD
34+
*/
35+
uid?: string;
36+
/**
37+
* Format: date-time
38+
* @description It's date example
39+
* @example 1999-03-31 15:00:00.000
40+
*/
41+
created_at?: string;
42+
/**
43+
* Format: date-time
44+
* @example 2020-07-10 10:10:00.000
45+
*/
46+
updated_at?: string;
47+
deleted?: boolean;
48+
};
49+
/** Image for preview */
50+
Image: {
51+
/** @example https://shantichat.com/data/V1StGXR8_Z5jdHi6B-myT/white-rabbit.png */
52+
url: string;
53+
/** @example 128 */
54+
width: unknown;
55+
/** @example 128 */
56+
height: unknown;
57+
/** @example LEHV6nWB2yk8pyo0adR*.7kCMdnj */
58+
blurhash?: string;
59+
};
60+
/** User object */
61+
User: components["schemas"]["BaseEntity"] & {
62+
/** @example Thomas A. Anderson */
63+
name?: string;
64+
/**
65+
* @default test
66+
* @example The One
67+
*/
68+
description?: string;
69+
icon?: components["schemas"]["Image"];
70+
/** @example America/Chicago */
71+
timezone?: string;
72+
/**
73+
* Format: date-time
74+
* @example 2020-07-10 15:00:00.000
75+
*/
76+
last_online_at?: string;
77+
};
78+
};
79+
}
80+
81+
export interface operations {
82+
getContacts: {
83+
responses: {
84+
/** OK */
85+
200: {
86+
content: {
87+
"application/json": components["schemas"]["User"][];
88+
};
89+
};
90+
};
91+
};
92+
getContactInfo: {
93+
parameters: {
94+
path: {
95+
userUid: string;
96+
};
97+
};
98+
responses: {
99+
/** OK */
100+
200: {
101+
content: {
102+
"application/json": components["schemas"]["User"];
103+
};
104+
};
105+
};
106+
};
107+
getContactIcon: {
108+
parameters: {
109+
path: {
110+
userUid: string;
111+
};
112+
};
113+
responses: {
114+
/** OK */
115+
200: {
116+
content: {
117+
"application/json": components["schemas"]["Image"];
118+
};
119+
};
120+
};
121+
};
122+
getMyInfo: {
123+
responses: {
124+
/** OK */
125+
200: {
126+
content: {
127+
"application/json": components["schemas"]["User"];
128+
};
129+
};
130+
};
131+
};
132+
getMyIcon: {
133+
responses: {
134+
/** OK */
135+
200: {
136+
content: {
137+
"application/json": components["schemas"]["Image"];
138+
};
139+
};
140+
};
141+
};
142+
deleteMyIcon: {
143+
responses: {
144+
/** OK */
145+
200: unknown;
146+
};
147+
};
148+
}
149+
150+
export interface external {}

0 commit comments

Comments
 (0)