Skip to content

Commit 5833e01

Browse files
authored
Fix single-line comments (#449)
1 parent 48787ee commit 5833e01

File tree

19 files changed

+2754
-925
lines changed

19 files changed

+2754
-925
lines changed

examples/stripe-openapi2.ts

Lines changed: 189 additions & 92 deletions
Large diffs are not rendered by default.

examples/stripe-openapi3.ts

Lines changed: 629 additions & 193 deletions
Large diffs are not rendered by default.

src/utils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ export function comment(text: string): string {
1111
// if multi-line comment
1212
return `/**
1313
* ${commentText.replace(/\n/g, "\n * ")}
14-
*/
15-
`;
14+
*/\n`;
1615
}
1716

1817
/** shim for Object.fromEntries() for Node < 13 */
@@ -27,7 +26,7 @@ export function nodeType(obj: any): SchemaObjectType | undefined {
2726
return undefined;
2827
}
2928

30-
if (obj["$ref"]) {
29+
if (obj.$ref) {
3130
return "ref";
3231
}
3332

@@ -111,6 +110,7 @@ export function tsTupleOf(types: string[]): string {
111110

112111
/** Convert T, U into T & U; */
113112
export function tsIntersectionOf(types: string[]): string {
113+
if (types.length === 1) return types[0]; // don’t add parentheses around one thing
114114
return `(${types.join(") & (")})`;
115115
}
116116

@@ -121,6 +121,7 @@ export function tsPartial(type: string): string {
121121

122122
/** Convert [X, Y, Z] into X | Y | Z */
123123
export function tsUnionOf(types: string[]): string {
124+
if (types.length === 1) return types[0]; // don’t add parentheses around one thing
124125
return `(${types.join(") | (")})`;
125126
}
126127

src/v2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export default function generateTypesV2(input: OpenAPI2 | OpenAPI2Schemas, optio
8080

8181
return tsIntersectionOf([
8282
...(node.allOf ? (node.allOf as any[]).map(transform) : []), // append allOf first
83-
...(properties ? [`{ ${properties} }`] : []), // then properties + additionalProperties
83+
...(properties ? [`{\n${properties}\n}`] : []), // then properties + additionalProperties
8484
]);
8585
break;
8686
}

src/v3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export default function generateTypesV3(input: OpenAPI3 | OpenAPI3Schemas, optio
104104

105105
return tsIntersectionOf([
106106
...(node.allOf ? (node.allOf as any[]).map(transform) : []), // append allOf first
107-
...(properties ? [`{ ${properties} }`] : []), // then properties
107+
...(properties ? [`{\n${properties}\n}`] : []), // then properties
108108
...(additionalProperties ? [additionalProperties] : []), // then additional properties
109109
]);
110110
}

tests/bin/expected/prettier-js.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ export interface operations {
144144
}
145145
requestBody: {
146146
'application/x-www-form-urlencoded': {
147-
/** Updated name of the pet */ name?: string
147+
/** Updated name of the pet */
148+
name?: string
148149
/** Updated status of the pet */
149150
status?: string
150151
}
@@ -180,7 +181,8 @@ export interface operations {
180181
}
181182
requestBody: {
182183
'multipart/form-data': {
183-
/** Additional data to pass to server */ additionalMetadata?: string
184+
/** Additional data to pass to server */
185+
additionalMetadata?: string
184186
/** file to upload */
185187
file?: string
186188
}
@@ -368,7 +370,10 @@ export interface components {
368370
status?: 'placed' | 'approved' | 'delivered'
369371
complete?: boolean
370372
}
371-
Category: { id?: number; name?: string }
373+
Category: {
374+
id?: number
375+
name?: string
376+
}
372377
User: {
373378
id?: number
374379
username?: string
@@ -380,7 +385,10 @@ export interface components {
380385
/** User Status */
381386
userStatus?: number
382387
}
383-
Tag: { id?: number; name?: string }
388+
Tag: {
389+
id?: number
390+
name?: string
391+
}
384392
Pet: {
385393
id?: number
386394
category?: components['schemas']['Category']
@@ -390,6 +398,10 @@ export interface components {
390398
/** pet status in the store */
391399
status?: 'available' | 'pending' | 'sold'
392400
}
393-
ApiResponse: { code?: number; type?: string; message?: string }
401+
ApiResponse: {
402+
code?: number
403+
type?: string
404+
message?: string
405+
}
394406
}
395407
}

tests/bin/expected/prettier-json.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ export interface operations {
144144
}
145145
requestBody: {
146146
'application/x-www-form-urlencoded': {
147-
/** Updated name of the pet */ name?: string
147+
/** Updated name of the pet */
148+
name?: string
148149
/** Updated status of the pet */
149150
status?: string
150151
}
@@ -180,7 +181,8 @@ export interface operations {
180181
}
181182
requestBody: {
182183
'multipart/form-data': {
183-
/** Additional data to pass to server */ additionalMetadata?: string
184+
/** Additional data to pass to server */
185+
additionalMetadata?: string
184186
/** file to upload */
185187
file?: string
186188
}
@@ -368,7 +370,10 @@ export interface components {
368370
status?: 'placed' | 'approved' | 'delivered'
369371
complete?: boolean
370372
}
371-
Category: { id?: number; name?: string }
373+
Category: {
374+
id?: number
375+
name?: string
376+
}
372377
User: {
373378
id?: number
374379
username?: string
@@ -380,7 +385,10 @@ export interface components {
380385
/** User Status */
381386
userStatus?: number
382387
}
383-
Tag: { id?: number; name?: string }
388+
Tag: {
389+
id?: number
390+
name?: string
391+
}
384392
Pet: {
385393
id?: number
386394
category?: components['schemas']['Category']
@@ -390,6 +398,10 @@ export interface components {
390398
/** pet status in the store */
391399
status?: 'available' | 'pending' | 'sold'
392400
}
393-
ApiResponse: { code?: number; type?: string; message?: string }
401+
ApiResponse: {
402+
code?: number
403+
type?: string
404+
message?: string
405+
}
394406
}
395407
}

tests/bin/expected/stdout.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ export interface operations {
144144
};
145145
requestBody: {
146146
"application/x-www-form-urlencoded": {
147-
/** Updated name of the pet */ name?: string;
147+
/** Updated name of the pet */
148+
name?: string;
148149
/** Updated status of the pet */
149150
status?: string;
150151
};
@@ -180,7 +181,8 @@ export interface operations {
180181
};
181182
requestBody: {
182183
"multipart/form-data": {
183-
/** Additional data to pass to server */ additionalMetadata?: string;
184+
/** Additional data to pass to server */
185+
additionalMetadata?: string;
184186
/** file to upload */
185187
file?: string;
186188
};
@@ -368,7 +370,10 @@ export interface components {
368370
status?: "placed" | "approved" | "delivered";
369371
complete?: boolean;
370372
};
371-
Category: { id?: number; name?: string };
373+
Category: {
374+
id?: number;
375+
name?: string;
376+
};
372377
User: {
373378
id?: number;
374379
username?: string;
@@ -380,7 +385,10 @@ export interface components {
380385
/** User Status */
381386
userStatus?: number;
382387
};
383-
Tag: { id?: number; name?: string };
388+
Tag: {
389+
id?: number;
390+
name?: string;
391+
};
384392
Pet: {
385393
id?: number;
386394
category?: components["schemas"]["Category"];
@@ -390,6 +398,10 @@ export interface components {
390398
/** pet status in the store */
391399
status?: "available" | "pending" | "sold";
392400
};
393-
ApiResponse: { code?: number; type?: string; message?: string };
401+
ApiResponse: {
402+
code?: number;
403+
type?: string;
404+
message?: string;
405+
};
394406
};
395407
}

tests/v2/expected/http.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@ export interface definitions {
5050
version: 1;
5151
body: definitions["RegionBody"];
5252
};
53-
CreateRegion: { body: definitions["RegionBody"] };
54-
UpdateRegion: { name: string };
53+
CreateRegion: {
54+
body: definitions["RegionBody"];
55+
};
56+
UpdateRegion: {
57+
name: string;
58+
};
5559
ProviderBody: {
5660
owner_id?: definitions["OptionalFlexID"];
5761
team_id?: definitions["OptionalID"];
@@ -76,7 +80,9 @@ export interface definitions {
7680
type: "provider";
7781
body: definitions["ProviderBody"];
7882
};
79-
CreateProvider: { body: definitions["ProviderBody"] };
83+
CreateProvider: {
84+
body: definitions["ProviderBody"];
85+
};
8086
UpdateProvider: {
8187
id: definitions["ID"];
8288
body: definitions["UpdateProviderBody"];
@@ -121,7 +127,10 @@ export interface definitions {
121127
platform_ids?: definitions["ID"][];
122128
tags?: definitions["ProductTags"];
123129
};
124-
UpdatePlan: { id: definitions["ID"]; body: definitions["UpdatePlanBody"] };
130+
UpdatePlan: {
131+
id: definitions["ID"];
132+
body: definitions["UpdatePlanBody"];
133+
};
125134
UpdatePlanBody: {
126135
name?: definitions["Name"];
127136
label?: definitions["Label"];
@@ -254,7 +263,8 @@ export interface definitions {
254263
value: definitions["FeatureValueLabel"];
255264
};
256265
ValueProp: {
257-
/** Heading of a value proposition. */ header: string;
266+
/** Heading of a value proposition. */
267+
header: string;
258268
/** Body of a value proposition. */
259269
body: string;
260270
};
@@ -369,7 +379,10 @@ export interface definitions {
369379
* URL to this Product's Terms of Service. If provided is true, then
370380
* a url must be set. Otherwise, provided is false.
371381
*/
372-
terms: { url?: string; provided: boolean };
382+
terms: {
383+
url?: string;
384+
provided: boolean;
385+
};
373386
feature_types: definitions["FeatureType"][];
374387
billing: {
375388
type: "monthly-prorated" | "monthly-anniversary" | "annual-anniversary";
@@ -390,7 +403,9 @@ export interface definitions {
390403
type: "product";
391404
body: definitions["ProductBody"];
392405
};
393-
CreateProduct: { body: definitions["ProductBody"] };
406+
CreateProduct: {
407+
body: definitions["ProductBody"];
408+
};
394409
/** Array of Plan IDs that this Plan can be resized to, if null all will be assumed */
395410
PlanResizeList: definitions["ID"][];
396411
PlanBody: {
@@ -441,10 +456,13 @@ export interface definitions {
441456
type: "plan";
442457
body: definitions["ExpandedPlanBody"];
443458
};
444-
CreatePlan: { body: definitions["PlanBody"] };
459+
CreatePlan: {
460+
body: definitions["PlanBody"];
461+
};
445462
/** Unexpected error */
446463
Error: {
447-
/** The error type */ type: string;
464+
/** The error type */
465+
type: string;
448466
/** Explanation of the errors */
449467
message: string[];
450468
};

tests/v2/expected/manifold.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@ export interface definitions {
5050
version: 1;
5151
body: definitions["RegionBody"];
5252
};
53-
CreateRegion: { body: definitions["RegionBody"] };
54-
UpdateRegion: { name: string };
53+
CreateRegion: {
54+
body: definitions["RegionBody"];
55+
};
56+
UpdateRegion: {
57+
name: string;
58+
};
5559
ProviderBody: {
5660
owner_id?: definitions["OptionalFlexID"];
5761
team_id?: definitions["OptionalID"];
@@ -76,7 +80,9 @@ export interface definitions {
7680
type: "provider";
7781
body: definitions["ProviderBody"];
7882
};
79-
CreateProvider: { body: definitions["ProviderBody"] };
83+
CreateProvider: {
84+
body: definitions["ProviderBody"];
85+
};
8086
UpdateProvider: {
8187
id: definitions["ID"];
8288
body: definitions["UpdateProviderBody"];
@@ -121,7 +127,10 @@ export interface definitions {
121127
platform_ids?: definitions["ID"][];
122128
tags?: definitions["ProductTags"];
123129
};
124-
UpdatePlan: { id: definitions["ID"]; body: definitions["UpdatePlanBody"] };
130+
UpdatePlan: {
131+
id: definitions["ID"];
132+
body: definitions["UpdatePlanBody"];
133+
};
125134
UpdatePlanBody: {
126135
name?: definitions["Name"];
127136
label?: definitions["Label"];
@@ -254,7 +263,8 @@ export interface definitions {
254263
value: definitions["FeatureValueLabel"];
255264
};
256265
ValueProp: {
257-
/** Heading of a value proposition. */ header: string;
266+
/** Heading of a value proposition. */
267+
header: string;
258268
/** Body of a value proposition. */
259269
body: string;
260270
};
@@ -369,7 +379,10 @@ export interface definitions {
369379
* URL to this Product's Terms of Service. If provided is true, then
370380
* a url must be set. Otherwise, provided is false.
371381
*/
372-
terms: { url?: string; provided: boolean };
382+
terms: {
383+
url?: string;
384+
provided: boolean;
385+
};
373386
feature_types: definitions["FeatureType"][];
374387
billing: {
375388
type: "monthly-prorated" | "monthly-anniversary" | "annual-anniversary";
@@ -390,7 +403,9 @@ export interface definitions {
390403
type: "product";
391404
body: definitions["ProductBody"];
392405
};
393-
CreateProduct: { body: definitions["ProductBody"] };
406+
CreateProduct: {
407+
body: definitions["ProductBody"];
408+
};
394409
/** Array of Plan IDs that this Plan can be resized to, if null all will be assumed */
395410
PlanResizeList: definitions["ID"][];
396411
PlanBody: {
@@ -441,10 +456,13 @@ export interface definitions {
441456
type: "plan";
442457
body: definitions["ExpandedPlanBody"];
443458
};
444-
CreatePlan: { body: definitions["PlanBody"] };
459+
CreatePlan: {
460+
body: definitions["PlanBody"];
461+
};
445462
/** Unexpected error */
446463
Error: {
447-
/** The error type */ type: string;
464+
/** The error type */
465+
type: string;
448466
/** Explanation of the errors */
449467
message: string[];
450468
};

0 commit comments

Comments
 (0)