Skip to content

Commit 15e9033

Browse files
authoredFeb 22, 2024
feat: jsdoc block tags (#1231)
1 parent 4f95efb commit 15e9033

File tree

7 files changed

+151
-15
lines changed

7 files changed

+151
-15
lines changed
 

‎packages/core/src/utils/doc.ts

+67-10
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,30 @@ export function jsDoc(
88
description,
99
deprecated,
1010
summary,
11+
minLength,
12+
maxLength,
13+
minimum,
14+
maximum,
15+
exclusiveMinimum,
16+
exclusiveMaximum,
17+
minItems,
18+
maxItems,
19+
nullable,
20+
pattern,
1121
}: {
1222
description?: string[] | string;
1323
deprecated?: boolean;
1424
summary?: string;
25+
minLength?: number;
26+
maxLength?: number;
27+
minimum?: number;
28+
maximum?: number;
29+
exclusiveMinimum?: boolean;
30+
exclusiveMaximum?: boolean;
31+
minItems?: number;
32+
maxItems?: number;
33+
nullable?: boolean;
34+
pattern?: string;
1535
},
1636
tryOneLine = false,
1737
): string {
@@ -22,10 +42,21 @@ export function jsDoc(
2242
: [description || '']
2343
).map((line) => line.replace(regex, replacement));
2444

25-
const count = [description, deprecated, summary].reduce(
26-
(acc, it) => (it ? acc + 1 : acc),
27-
0,
28-
);
45+
const count = [
46+
description,
47+
deprecated,
48+
summary,
49+
minLength?.toString(),
50+
maxLength?.toString(),
51+
minimum?.toString(),
52+
maximum?.toString(),
53+
exclusiveMinimum?.toString(),
54+
exclusiveMaximum?.toString(),
55+
minItems?.toString(),
56+
maxItems?.toString(),
57+
nullable?.toString(),
58+
pattern,
59+
].reduce((acc, it) => (it ? acc + 1 : acc), 0);
2960

3061
if (!count) {
3162
return '';
@@ -46,20 +77,46 @@ export function jsDoc(
4677
doc += ` ${lines.join('\n * ')}`;
4778
}
4879

49-
if (deprecated) {
80+
function appendPrefix() {
5081
if (!oneLine) {
5182
doc += `\n${tryOneLine ? ' ' : ''} *`;
5283
}
53-
doc += ' @deprecated';
5484
}
5585

56-
if (summary) {
57-
if (!oneLine) {
58-
doc += `\n${tryOneLine ? ' ' : ''} *`;
86+
function tryAppendStringDocLine(key: string, value?: string) {
87+
if (value) {
88+
appendPrefix();
89+
doc += ` @${key} ${value}`;
90+
}
91+
}
92+
93+
function tryAppendBooleanDocLine(key: string, value?: boolean) {
94+
if (value === true) {
95+
appendPrefix();
96+
doc += ` @${key}`;
97+
}
98+
}
99+
100+
function tryAppendNumberDocLine(key: string, value?: number) {
101+
if (value !== undefined) {
102+
appendPrefix();
103+
doc += ` @${key} ${value}`;
59104
}
60-
doc += ` @summary ${summary.replace(regex, replacement)}`;
61105
}
62106

107+
tryAppendBooleanDocLine('deprecated', deprecated);
108+
tryAppendStringDocLine('summary', summary?.replace(regex, replacement));
109+
tryAppendNumberDocLine('minLength', minLength);
110+
tryAppendNumberDocLine('maxLength', maxLength);
111+
tryAppendNumberDocLine('minimum', minimum);
112+
tryAppendNumberDocLine('maximum', maximum);
113+
tryAppendBooleanDocLine('exclusiveMinimum', exclusiveMinimum);
114+
tryAppendBooleanDocLine('exclusiveMaximum', exclusiveMaximum);
115+
tryAppendNumberDocLine('minItems', minItems);
116+
tryAppendNumberDocLine('maxItems', maxItems);
117+
tryAppendBooleanDocLine('nullable', nullable);
118+
tryAppendStringDocLine('pattern', pattern);
119+
63120
doc += !oneLine ? `\n ${tryOneLine ? ' ' : ''}` : ' ';
64121

65122
doc += '*/\n';

‎samples/basic/api/endpoints/petstoreFromFileSpec.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,31 @@ export interface Error {
2424
}
2525

2626
export interface Pet {
27+
/**
28+
* @minimum 0
29+
* @maximum 30
30+
* @exclusiveMinimum
31+
* @exclusiveMaximum
32+
*/
33+
age?: number;
2734
id: number;
35+
/**
36+
* Name of pet
37+
* @minLength 40
38+
* @maxLength 0
39+
*/
2840
name: string;
29-
tag?: string;
41+
/**
42+
* @nullable
43+
* @pattern ^\\d{3}-\\d{2}-\\d{4}$
44+
*/
45+
tag?: string | null;
3046
}
3147

48+
/**
49+
* @minItems 1
50+
* @maxItems 20
51+
*/
3252
export type Pets = Pet[];
3353

3454
/**

‎samples/basic/api/endpoints/petstoreFromFileSpecWithConfig.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,31 @@ export interface Error {
2424
}
2525

2626
export interface Pet {
27+
/**
28+
* @minimum 0
29+
* @maximum 30
30+
* @exclusiveMinimum
31+
* @exclusiveMaximum
32+
*/
33+
age?: number;
2734
id: number;
35+
/**
36+
* Name of pet
37+
* @minLength 40
38+
* @maxLength 0
39+
*/
2840
name: string;
29-
tag?: string;
41+
/**
42+
* @nullable
43+
* @pattern ^\\d{3}-\\d{2}-\\d{4}$
44+
*/
45+
tag?: string | null;
3046
}
3147

48+
/**
49+
* @minItems 1
50+
* @maxItems 20
51+
*/
3252
export type Pets = Pet[];
3353

3454
/**

‎samples/basic/api/endpoints/petstoreFromFileSpecWithTransformer.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@ export const getListPetsResponseMock = (overrideResponse: any = {}): Pets =>
5858
{ length: faker.number.int({ min: 1, max: 10 }) },
5959
(_, i) => i + 1,
6060
).map(() => ({
61+
age: faker.helpers.arrayElement([
62+
faker.number.int({ min: 0, max: 30 }),
63+
undefined,
64+
]),
6165
id: faker.number.int({ min: undefined, max: undefined }),
6266
name: 'jon',
63-
tag: 'jon',
67+
tag: faker.helpers.arrayElement(['jon', null]),
6468
...overrideResponse,
6569
}));
6670

67-
export const getShowPetByIdResponseMock = (): Pet =>
71+
export const getShowPetByIdResponseMock = () =>
6872
(() => ({
6973
id: faker.number.int({ min: 1, max: 99 }),
7074
name: faker.person.firstName(),

‎samples/basic/api/model/pet.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,23 @@
66
*/
77

88
export interface Pet {
9+
/**
10+
* @minimum 0
11+
* @maximum 30
12+
* @exclusiveMinimum
13+
* @exclusiveMaximum
14+
*/
15+
age?: number;
916
id: number;
17+
/**
18+
* Name of pet
19+
* @minLength 40
20+
* @maxLength 0
21+
*/
1022
name: string;
11-
tag?: string;
23+
/**
24+
* @nullable
25+
* @pattern ^\\d{3}-\\d{2}-\\d{4}$
26+
*/
27+
tag?: string | null;
1228
}

‎samples/basic/api/model/pets.ts

+4
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
*/
77
import type { Pet } from './pet';
88

9+
/**
10+
* @minItems 1
11+
* @maxItems 20
12+
*/
913
export type Pets = Pet[];

‎samples/basic/petstore.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,25 @@ components:
111111
format: int64
112112
name:
113113
type: string
114+
description: 'Name of pet'
115+
maxLength: 0
116+
minLength: 40
117+
age:
118+
type: integer
119+
format: int32
120+
minimum: 0
121+
maximum: 30
122+
exclusiveMinimum: true
123+
exclusiveMaximum: true
114124
tag:
115125
type: string
126+
pattern: '^\\d{3}-\\d{2}-\\d{4}$'
127+
nullable: true
128+
116129
Pets:
117130
type: array
131+
minItems: 1
132+
maxItems: 20
118133
items:
119134
$ref: '#/components/schemas/Pet'
120135
Error:

0 commit comments

Comments
 (0)
Please sign in to comment.