@@ -23,18 +23,17 @@ const printDocASTReducer: ASTReducer<string> = {
23
23
// Document
24
24
25
25
Document : {
26
- leave : ( node ) => truthyJoin ( node . definitions , '\n\n' ) ,
26
+ leave : ( node ) => join ( node . definitions , '\n\n' ) ,
27
27
} ,
28
28
29
29
OperationDefinition : {
30
30
leave ( node ) {
31
- const varDefs = wrap ( '(' , truthyJoin ( node . variableDefinitions , ', ' ) , ')' ) ;
32
- const prefix = join (
31
+ const varDefs = wrap ( '(' , join ( node . variableDefinitions , ', ' ) , ')' ) ;
32
+ const prefix = maybeJoin (
33
33
[
34
34
node . operation ,
35
- // TODO: optimize
36
- join ( [ node . name , varDefs ] ) ,
37
- truthyJoin ( node . directives , ' ' ) ,
35
+ maybeJoin ( [ node . name , varDefs ] ) ,
36
+ join ( node . directives , ' ' ) ,
38
37
] ,
39
38
' ' ,
40
39
) ;
@@ -51,20 +50,20 @@ const printDocASTReducer: ASTReducer<string> = {
51
50
': ' +
52
51
type +
53
52
wrap ( ' = ' , defaultValue ) +
54
- wrap ( ' ' , truthyJoin ( directives , ' ' ) ) ,
53
+ wrap ( ' ' , join ( directives , ' ' ) ) ,
55
54
} ,
56
55
SelectionSet : { leave : ( { selections } ) => block ( selections ) } ,
57
56
58
57
Field : {
59
58
leave ( { alias, name, arguments : args , directives, selectionSet } ) {
60
59
const prefix = wrap ( '' , alias , ': ' ) + name ;
61
- let argsLine = prefix + wrap ( '(' , truthyJoin ( args , ', ' ) , ')' ) ;
60
+ let argsLine = prefix + wrap ( '(' , join ( args , ', ' ) , ')' ) ;
62
61
63
62
if ( argsLine . length > MAX_LINE_LENGTH ) {
64
- argsLine = prefix + wrap ( '(\n' , indent ( truthyJoin ( args , '\n' ) ) , '\n)' ) ;
63
+ argsLine = prefix + wrap ( '(\n' , indent ( join ( args , '\n' ) ) , '\n)' ) ;
65
64
}
66
65
67
- return join ( [ argsLine , truthyJoin ( directives , ' ' ) , selectionSet ] , ' ' ) ;
66
+ return maybeJoin ( [ argsLine , join ( directives , ' ' ) , selectionSet ] , ' ' ) ;
68
67
} ,
69
68
} ,
70
69
@@ -74,16 +73,16 @@ const printDocASTReducer: ASTReducer<string> = {
74
73
75
74
FragmentSpread : {
76
75
leave : ( { name, directives } ) =>
77
- '...' + name + wrap ( ' ' , truthyJoin ( directives , ' ' ) ) ,
76
+ '...' + name + wrap ( ' ' , join ( directives , ' ' ) ) ,
78
77
} ,
79
78
80
79
InlineFragment : {
81
80
leave : ( { typeCondition, directives, selectionSet } ) =>
82
- join (
81
+ maybeJoin (
83
82
[
84
83
'...' ,
85
84
wrap ( 'on ' , typeCondition ) ,
86
- truthyJoin ( directives , ' ' ) ,
85
+ join ( directives , ' ' ) ,
87
86
selectionSet ,
88
87
] ,
89
88
' ' ,
@@ -100,8 +99,8 @@ const printDocASTReducer: ASTReducer<string> = {
100
99
} ) =>
101
100
// Note: fragment variable definitions are experimental and may be changed
102
101
// or removed in the future.
103
- `fragment ${ name } ${ wrap ( '(' , truthyJoin ( variableDefinitions , ', ' ) , ')' ) } ` +
104
- `on ${ typeCondition } ${ wrap ( '' , truthyJoin ( directives , ' ' ) , ' ' ) } ` +
102
+ `fragment ${ name } ${ wrap ( '(' , join ( variableDefinitions , ', ' ) , ')' ) } ` +
103
+ `on ${ typeCondition } ${ wrap ( '' , join ( directives , ' ' ) , ' ' ) } ` +
105
104
selectionSet ,
106
105
} ,
107
106
@@ -116,15 +115,15 @@ const printDocASTReducer: ASTReducer<string> = {
116
115
BooleanValue : { leave : ( { value } ) => ( value ? 'true' : 'false' ) } ,
117
116
NullValue : { leave : ( ) => 'null' } ,
118
117
EnumValue : { leave : ( { value } ) => value } ,
119
- ListValue : { leave : ( { values } ) => '[' + truthyJoin ( values , ', ' ) + ']' } ,
120
- ObjectValue : { leave : ( { fields } ) => '{' + truthyJoin ( fields , ', ' ) + '}' } ,
118
+ ListValue : { leave : ( { values } ) => '[' + join ( values , ', ' ) + ']' } ,
119
+ ObjectValue : { leave : ( { fields } ) => '{' + join ( fields , ', ' ) + '}' } ,
121
120
ObjectField : { leave : ( { name, value } ) => name + ': ' + value } ,
122
121
123
122
// Directive
124
123
125
124
Directive : {
126
125
leave : ( { name, arguments : args } ) =>
127
- '@' + name + wrap ( '(' , truthyJoin ( args , ', ' ) , ')' ) ,
126
+ '@' + name + wrap ( '(' , join ( args , ', ' ) , ')' ) ,
128
127
} ,
129
128
130
129
// Type
@@ -138,7 +137,10 @@ const printDocASTReducer: ASTReducer<string> = {
138
137
SchemaDefinition : {
139
138
leave : ( { description, directives, operationTypes } ) =>
140
139
wrap ( '' , description , '\n' ) +
141
- join ( [ 'schema' , join ( directives , ' ' ) , block ( operationTypes ) ] , ' ' ) ,
140
+ maybeJoin (
141
+ [ 'schema' , maybeJoin ( directives , ' ' ) , block ( operationTypes ) ] ,
142
+ ' ' ,
143
+ ) ,
142
144
} ,
143
145
144
146
OperationTypeDefinition : {
@@ -148,18 +150,18 @@ const printDocASTReducer: ASTReducer<string> = {
148
150
ScalarTypeDefinition : {
149
151
leave : ( { description, name, directives } ) =>
150
152
wrap ( '' , description , '\n' ) +
151
- join ( [ 'scalar' , name , truthyJoin ( directives , ' ' ) ] , ' ' ) ,
153
+ maybeJoin ( [ 'scalar' , name , join ( directives , ' ' ) ] , ' ' ) ,
152
154
} ,
153
155
154
156
ObjectTypeDefinition : {
155
157
leave : ( { description, name, interfaces, directives, fields } ) =>
156
158
wrap ( '' , description , '\n' ) +
157
- join (
159
+ maybeJoin (
158
160
[
159
161
'type' ,
160
162
name ,
161
- wrap ( 'implements ' , truthyJoin ( interfaces , ' & ' ) ) ,
162
- truthyJoin ( directives , ' ' ) ,
163
+ wrap ( 'implements ' , join ( interfaces , ' & ' ) ) ,
164
+ join ( directives , ' ' ) ,
163
165
block ( fields ) ,
164
166
] ,
165
167
' ' ,
@@ -171,31 +173,31 @@ const printDocASTReducer: ASTReducer<string> = {
171
173
wrap ( '' , description , '\n' ) +
172
174
name +
173
175
( hasMultilineItems ( args )
174
- ? wrap ( '(\n' , indent ( truthyJoin ( args , '\n' ) ) , '\n)' )
175
- : wrap ( '(' , truthyJoin ( args , ', ' ) , ')' ) ) +
176
+ ? wrap ( '(\n' , indent ( join ( args , '\n' ) ) , '\n)' )
177
+ : wrap ( '(' , join ( args , ', ' ) , ')' ) ) +
176
178
': ' +
177
179
type +
178
- wrap ( ' ' , truthyJoin ( directives , ' ' ) ) ,
180
+ wrap ( ' ' , join ( directives , ' ' ) ) ,
179
181
} ,
180
182
181
183
InputValueDefinition : {
182
184
leave : ( { description, name, type, defaultValue, directives } ) =>
183
185
wrap ( '' , description , '\n' ) +
184
- join (
185
- [ name + ': ' + type , wrap ( '= ' , defaultValue ) , truthyJoin ( directives , ' ' ) ] ,
186
+ maybeJoin (
187
+ [ name + ': ' + type , wrap ( '= ' , defaultValue ) , join ( directives , ' ' ) ] ,
186
188
' ' ,
187
189
) ,
188
190
} ,
189
191
190
192
InterfaceTypeDefinition : {
191
193
leave : ( { description, name, interfaces, directives, fields } ) =>
192
194
wrap ( '' , description , '\n' ) +
193
- join (
195
+ maybeJoin (
194
196
[
195
197
'interface' ,
196
198
name ,
197
- wrap ( 'implements ' , truthyJoin ( interfaces , ' & ' ) ) ,
198
- truthyJoin ( directives , ' ' ) ,
199
+ wrap ( 'implements ' , join ( interfaces , ' & ' ) ) ,
200
+ join ( directives , ' ' ) ,
199
201
block ( fields ) ,
200
202
] ,
201
203
' ' ,
@@ -205,27 +207,28 @@ const printDocASTReducer: ASTReducer<string> = {
205
207
UnionTypeDefinition : {
206
208
leave : ( { description, name, directives, types } ) =>
207
209
wrap ( '' , description , '\n' ) +
208
- join (
209
- [ 'union' , name , truthyJoin ( directives , ' ' ) , wrap ( '= ' , truthyJoin ( types , ' | ' ) ) ] ,
210
+ maybeJoin (
211
+ [ 'union' , name , join ( directives , ' ' ) , wrap ( '= ' , join ( types , ' | ' ) ) ] ,
210
212
' ' ,
211
213
) ,
212
214
} ,
213
215
214
216
EnumTypeDefinition : {
215
217
leave : ( { description, name, directives, values } ) =>
216
218
wrap ( '' , description , '\n' ) +
217
- join ( [ 'enum' , name , truthyJoin ( directives , ' ' ) , block ( values ) ] , ' ' ) ,
219
+ maybeJoin ( [ 'enum' , name , join ( directives , ' ' ) , block ( values ) ] , ' ' ) ,
218
220
} ,
219
221
220
222
EnumValueDefinition : {
221
223
leave : ( { description, name, directives } ) =>
222
- wrap ( '' , description , '\n' ) + join ( [ name , truthyJoin ( directives , ' ' ) ] , ' ' ) ,
224
+ wrap ( '' , description , '\n' ) +
225
+ maybeJoin ( [ name , join ( directives , ' ' ) ] , ' ' ) ,
223
226
} ,
224
227
225
228
InputObjectTypeDefinition : {
226
229
leave : ( { description, name, directives, fields } ) =>
227
230
wrap ( '' , description , '\n' ) +
228
- join ( [ 'input' , name , truthyJoin ( directives , ' ' ) , block ( fields ) ] , ' ' ) ,
231
+ maybeJoin ( [ 'input' , name , join ( directives , ' ' ) , block ( fields ) ] , ' ' ) ,
229
232
} ,
230
233
231
234
DirectiveDefinition : {
@@ -234,34 +237,34 @@ const printDocASTReducer: ASTReducer<string> = {
234
237
'directive @' +
235
238
name +
236
239
( hasMultilineItems ( args )
237
- ? wrap ( '(\n' , indent ( truthyJoin ( args , '\n' ) ) , '\n)' )
238
- : wrap ( '(' , truthyJoin ( args , ', ' ) , ')' ) ) +
240
+ ? wrap ( '(\n' , indent ( join ( args , '\n' ) ) , '\n)' )
241
+ : wrap ( '(' , join ( args , ', ' ) , ')' ) ) +
239
242
( repeatable ? ' repeatable' : '' ) +
240
243
' on ' +
241
- truthyJoin ( locations , ' | ' ) ,
244
+ join ( locations , ' | ' ) ,
242
245
} ,
243
246
244
247
SchemaExtension : {
245
248
leave : ( { directives, operationTypes } ) =>
246
- join (
247
- [ 'extend schema' , truthyJoin ( directives , ' ' ) , block ( operationTypes ) ] ,
249
+ maybeJoin (
250
+ [ 'extend schema' , join ( directives , ' ' ) , block ( operationTypes ) ] ,
248
251
' ' ,
249
252
) ,
250
253
} ,
251
254
252
255
ScalarTypeExtension : {
253
256
leave : ( { name, directives } ) =>
254
- join ( [ 'extend scalar' , name , truthyJoin ( directives , ' ' ) ] , ' ' ) ,
257
+ maybeJoin ( [ 'extend scalar' , name , join ( directives , ' ' ) ] , ' ' ) ,
255
258
} ,
256
259
257
260
ObjectTypeExtension : {
258
261
leave : ( { name, interfaces, directives, fields } ) =>
259
- join (
262
+ maybeJoin (
260
263
[
261
264
'extend type' ,
262
265
name ,
263
- wrap ( 'implements ' , truthyJoin ( interfaces , ' & ' ) ) ,
264
- truthyJoin ( directives , ' ' ) ,
266
+ wrap ( 'implements ' , join ( interfaces , ' & ' ) ) ,
267
+ join ( directives , ' ' ) ,
265
268
block ( fields ) ,
266
269
] ,
267
270
' ' ,
@@ -270,12 +273,12 @@ const printDocASTReducer: ASTReducer<string> = {
270
273
271
274
InterfaceTypeExtension : {
272
275
leave : ( { name, interfaces, directives, fields } ) =>
273
- join (
276
+ maybeJoin (
274
277
[
275
278
'extend interface' ,
276
279
name ,
277
- wrap ( 'implements ' , truthyJoin ( interfaces , ' & ' ) ) ,
278
- truthyJoin ( directives , ' ' ) ,
280
+ wrap ( 'implements ' , join ( interfaces , ' & ' ) ) ,
281
+ join ( directives , ' ' ) ,
279
282
block ( fields ) ,
280
283
] ,
281
284
' ' ,
@@ -284,66 +287,83 @@ const printDocASTReducer: ASTReducer<string> = {
284
287
285
288
UnionTypeExtension : {
286
289
leave : ( { name, directives, types } ) =>
287
- join (
290
+ maybeJoin (
288
291
[
289
292
'extend union' ,
290
293
name ,
291
- truthyJoin ( directives , ' ' ) ,
292
- wrap ( '= ' , truthyJoin ( types , ' | ' ) ) ,
294
+ join ( directives , ' ' ) ,
295
+ wrap ( '= ' , join ( types , ' | ' ) ) ,
293
296
] ,
294
297
' ' ,
295
298
) ,
296
299
} ,
297
300
298
301
EnumTypeExtension : {
299
302
leave : ( { name, directives, values } ) =>
300
- join ( [ 'extend enum' , name , truthyJoin ( directives , ' ' ) , block ( values ) ] , ' ' ) ,
303
+ maybeJoin (
304
+ [ 'extend enum' , name , join ( directives , ' ' ) , block ( values ) ] ,
305
+ ' ' ,
306
+ ) ,
301
307
} ,
302
308
303
309
InputObjectTypeExtension : {
304
310
leave : ( { name, directives, fields } ) =>
305
- join ( [ 'extend input' , name , truthyJoin ( directives , ' ' ) , block ( fields ) ] , ' ' ) ,
311
+ maybeJoin (
312
+ [ 'extend input' , name , join ( directives , ' ' ) , block ( fields ) ] ,
313
+ ' ' ,
314
+ ) ,
306
315
} ,
307
316
} ;
308
317
309
318
/**
310
319
* Given maybeArray, print an empty string if it is null or empty, otherwise
311
320
* print all items together separated by separator if provided
312
321
*/
313
- function join (
322
+ function maybeJoin (
314
323
maybeArray : Maybe < ReadonlyArray < string | undefined > > ,
315
324
separator = '' ,
316
325
) : string {
317
- if ( ! maybeArray ) return ''
326
+ if ( ! maybeArray ) {
327
+ return '' ;
328
+ }
318
329
319
330
const list = maybeArray . filter ( ( x ) => x ) ;
320
331
const listLength = list . length ;
321
332
let result = '' ;
322
333
for ( let i = 0 ; i < listLength ; i ++ ) {
323
- if ( i === listLength - 1 ) return result + list [ i ] ;
324
- else result += list [ i ] + separator ;
334
+ if ( i === listLength - 1 ) {
335
+ result += list [ i ] ;
336
+ } else {
337
+ result += list [ i ] + separator ;
338
+ }
325
339
}
326
- return result
340
+ return result ;
327
341
}
328
342
329
- function truthyJoin (
343
+ function join (
330
344
list : ReadonlyArray < string > | undefined ,
331
345
separator : string ,
332
346
) : string {
333
- if ( ! list ) return ''
347
+ if ( ! list ) {
348
+ return '' ;
349
+ }
334
350
const listLength = list . length ;
335
351
let result = '' ;
336
352
for ( let i = 0 ; i < listLength ; i ++ ) {
337
- if ( i === listLength - 1 ) return result + list [ i ] ;
338
- else result += list [ i ] + separator ;
353
+ if ( i === listLength - 1 ) {
354
+ result += list [ i ] ;
355
+ } else {
356
+ result += list [ i ] + separator ;
357
+ }
339
358
}
340
- return result
359
+
360
+ return result ;
341
361
}
342
362
343
363
/**
344
364
* Given array, print each item on its own line, wrapped in an indented `{ }` block.
345
365
*/
346
- function block ( array : Maybe < ReadonlyArray < string | undefined > > ) : string {
366
+ function block ( array : ReadonlyArray < string > | undefined ) : string {
347
367
return wrap ( '{\n' , indent ( join ( array , '\n' ) ) , '\n}' ) ;
348
368
}
349
369
0 commit comments