@@ -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,7 @@ 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 ( [ 'schema' , maybeJoin ( directives , ' ' ) , block ( operationTypes ) ] , ' ' ) ,
142
141
} ,
143
142
144
143
OperationTypeDefinition : {
@@ -148,18 +147,18 @@ const printDocASTReducer: ASTReducer<string> = {
148
147
ScalarTypeDefinition : {
149
148
leave : ( { description, name, directives } ) =>
150
149
wrap ( '' , description , '\n' ) +
151
- join ( [ 'scalar' , name , truthyJoin ( directives , ' ' ) ] , ' ' ) ,
150
+ maybeJoin ( [ 'scalar' , name , join ( directives , ' ' ) ] , ' ' ) ,
152
151
} ,
153
152
154
153
ObjectTypeDefinition : {
155
154
leave : ( { description, name, interfaces, directives, fields } ) =>
156
155
wrap ( '' , description , '\n' ) +
157
- join (
156
+ maybeJoin (
158
157
[
159
158
'type' ,
160
159
name ,
161
- wrap ( 'implements ' , truthyJoin ( interfaces , ' & ' ) ) ,
162
- truthyJoin ( directives , ' ' ) ,
160
+ wrap ( 'implements ' , join ( interfaces , ' & ' ) ) ,
161
+ join ( directives , ' ' ) ,
163
162
block ( fields ) ,
164
163
] ,
165
164
' ' ,
@@ -171,31 +170,31 @@ const printDocASTReducer: ASTReducer<string> = {
171
170
wrap ( '' , description , '\n' ) +
172
171
name +
173
172
( hasMultilineItems ( args )
174
- ? wrap ( '(\n' , indent ( truthyJoin ( args , '\n' ) ) , '\n)' )
175
- : wrap ( '(' , truthyJoin ( args , ', ' ) , ')' ) ) +
173
+ ? wrap ( '(\n' , indent ( join ( args , '\n' ) ) , '\n)' )
174
+ : wrap ( '(' , join ( args , ', ' ) , ')' ) ) +
176
175
': ' +
177
176
type +
178
- wrap ( ' ' , truthyJoin ( directives , ' ' ) ) ,
177
+ wrap ( ' ' , join ( directives , ' ' ) ) ,
179
178
} ,
180
179
181
180
InputValueDefinition : {
182
181
leave : ( { description, name, type, defaultValue, directives } ) =>
183
182
wrap ( '' , description , '\n' ) +
184
- join (
185
- [ name + ': ' + type , wrap ( '= ' , defaultValue ) , truthyJoin ( directives , ' ' ) ] ,
183
+ maybeJoin (
184
+ [ name + ': ' + type , wrap ( '= ' , defaultValue ) , join ( directives , ' ' ) ] ,
186
185
' ' ,
187
186
) ,
188
187
} ,
189
188
190
189
InterfaceTypeDefinition : {
191
190
leave : ( { description, name, interfaces, directives, fields } ) =>
192
191
wrap ( '' , description , '\n' ) +
193
- join (
192
+ maybeJoin (
194
193
[
195
194
'interface' ,
196
195
name ,
197
- wrap ( 'implements ' , truthyJoin ( interfaces , ' & ' ) ) ,
198
- truthyJoin ( directives , ' ' ) ,
196
+ wrap ( 'implements ' , join ( interfaces , ' & ' ) ) ,
197
+ join ( directives , ' ' ) ,
199
198
block ( fields ) ,
200
199
] ,
201
200
' ' ,
@@ -205,27 +204,27 @@ const printDocASTReducer: ASTReducer<string> = {
205
204
UnionTypeDefinition : {
206
205
leave : ( { description, name, directives, types } ) =>
207
206
wrap ( '' , description , '\n' ) +
208
- join (
209
- [ 'union' , name , truthyJoin ( directives , ' ' ) , wrap ( '= ' , truthyJoin ( types , ' | ' ) ) ] ,
207
+ maybeJoin (
208
+ [ 'union' , name , join ( directives , ' ' ) , wrap ( '= ' , join ( types , ' | ' ) ) ] ,
210
209
' ' ,
211
210
) ,
212
211
} ,
213
212
214
213
EnumTypeDefinition : {
215
214
leave : ( { description, name, directives, values } ) =>
216
215
wrap ( '' , description , '\n' ) +
217
- join ( [ 'enum' , name , truthyJoin ( directives , ' ' ) , block ( values ) ] , ' ' ) ,
216
+ maybeJoin ( [ 'enum' , name , join ( directives , ' ' ) , block ( values ) ] , ' ' ) ,
218
217
} ,
219
218
220
219
EnumValueDefinition : {
221
220
leave : ( { description, name, directives } ) =>
222
- wrap ( '' , description , '\n' ) + join ( [ name , truthyJoin ( directives , ' ' ) ] , ' ' ) ,
221
+ wrap ( '' , description , '\n' ) + maybeJoin ( [ name , join ( directives , ' ' ) ] , ' ' ) ,
223
222
} ,
224
223
225
224
InputObjectTypeDefinition : {
226
225
leave : ( { description, name, directives, fields } ) =>
227
226
wrap ( '' , description , '\n' ) +
228
- join ( [ 'input' , name , truthyJoin ( directives , ' ' ) , block ( fields ) ] , ' ' ) ,
227
+ maybeJoin ( [ 'input' , name , join ( directives , ' ' ) , block ( fields ) ] , ' ' ) ,
229
228
} ,
230
229
231
230
DirectiveDefinition : {
@@ -234,34 +233,34 @@ const printDocASTReducer: ASTReducer<string> = {
234
233
'directive @' +
235
234
name +
236
235
( hasMultilineItems ( args )
237
- ? wrap ( '(\n' , indent ( truthyJoin ( args , '\n' ) ) , '\n)' )
238
- : wrap ( '(' , truthyJoin ( args , ', ' ) , ')' ) ) +
236
+ ? wrap ( '(\n' , indent ( join ( args , '\n' ) ) , '\n)' )
237
+ : wrap ( '(' , join ( args , ', ' ) , ')' ) ) +
239
238
( repeatable ? ' repeatable' : '' ) +
240
239
' on ' +
241
- truthyJoin ( locations , ' | ' ) ,
240
+ join ( locations , ' | ' ) ,
242
241
} ,
243
242
244
243
SchemaExtension : {
245
244
leave : ( { directives, operationTypes } ) =>
246
- join (
247
- [ 'extend schema' , truthyJoin ( directives , ' ' ) , block ( operationTypes ) ] ,
245
+ maybeJoin (
246
+ [ 'extend schema' , join ( directives , ' ' ) , block ( operationTypes ) ] ,
248
247
' ' ,
249
248
) ,
250
249
} ,
251
250
252
251
ScalarTypeExtension : {
253
252
leave : ( { name, directives } ) =>
254
- join ( [ 'extend scalar' , name , truthyJoin ( directives , ' ' ) ] , ' ' ) ,
253
+ maybeJoin ( [ 'extend scalar' , name , join ( directives , ' ' ) ] , ' ' ) ,
255
254
} ,
256
255
257
256
ObjectTypeExtension : {
258
257
leave : ( { name, interfaces, directives, fields } ) =>
259
- join (
258
+ maybeJoin (
260
259
[
261
260
'extend type' ,
262
261
name ,
263
- wrap ( 'implements ' , truthyJoin ( interfaces , ' & ' ) ) ,
264
- truthyJoin ( directives , ' ' ) ,
262
+ wrap ( 'implements ' , join ( interfaces , ' & ' ) ) ,
263
+ join ( directives , ' ' ) ,
265
264
block ( fields ) ,
266
265
] ,
267
266
' ' ,
@@ -270,12 +269,12 @@ const printDocASTReducer: ASTReducer<string> = {
270
269
271
270
InterfaceTypeExtension : {
272
271
leave : ( { name, interfaces, directives, fields } ) =>
273
- join (
272
+ maybeJoin (
274
273
[
275
274
'extend interface' ,
276
275
name ,
277
- wrap ( 'implements ' , truthyJoin ( interfaces , ' & ' ) ) ,
278
- truthyJoin ( directives , ' ' ) ,
276
+ wrap ( 'implements ' , join ( interfaces , ' & ' ) ) ,
277
+ join ( directives , ' ' ) ,
279
278
block ( fields ) ,
280
279
] ,
281
280
' ' ,
@@ -284,33 +283,33 @@ const printDocASTReducer: ASTReducer<string> = {
284
283
285
284
UnionTypeExtension : {
286
285
leave : ( { name, directives, types } ) =>
287
- join (
286
+ maybeJoin (
288
287
[
289
288
'extend union' ,
290
289
name ,
291
- truthyJoin ( directives , ' ' ) ,
292
- wrap ( '= ' , truthyJoin ( types , ' | ' ) ) ,
290
+ join ( directives , ' ' ) ,
291
+ wrap ( '= ' , join ( types , ' | ' ) ) ,
293
292
] ,
294
293
' ' ,
295
294
) ,
296
295
} ,
297
296
298
297
EnumTypeExtension : {
299
298
leave : ( { name, directives, values } ) =>
300
- join ( [ 'extend enum' , name , truthyJoin ( directives , ' ' ) , block ( values ) ] , ' ' ) ,
299
+ maybeJoin ( [ 'extend enum' , name , join ( directives , ' ' ) , block ( values ) ] , ' ' ) ,
301
300
} ,
302
301
303
302
InputObjectTypeExtension : {
304
303
leave : ( { name, directives, fields } ) =>
305
- join ( [ 'extend input' , name , truthyJoin ( directives , ' ' ) , block ( fields ) ] , ' ' ) ,
304
+ maybeJoin ( [ 'extend input' , name , join ( directives , ' ' ) , block ( fields ) ] , ' ' ) ,
306
305
} ,
307
306
} ;
308
307
309
308
/**
310
309
* Given maybeArray, print an empty string if it is null or empty, otherwise
311
310
* print all items together separated by separator if provided
312
311
*/
313
- function join (
312
+ function maybeJoin (
314
313
maybeArray : Maybe < ReadonlyArray < string | undefined > > ,
315
314
separator = '' ,
316
315
) : string {
@@ -326,7 +325,7 @@ function join(
326
325
return result
327
326
}
328
327
329
- function truthyJoin (
328
+ function join (
330
329
list : ReadonlyArray < string > | undefined ,
331
330
separator : string ,
332
331
) : string {
@@ -343,7 +342,7 @@ function truthyJoin(
343
342
/**
344
343
* Given array, print each item on its own line, wrapped in an indented `{ }` block.
345
344
*/
346
- function block ( array : Maybe < ReadonlyArray < string | undefined > > ) : string {
345
+ function block ( array : ReadonlyArray < string > | undefined ) : string {
347
346
return wrap ( '{\n' , indent ( join ( array , '\n' ) ) , '\n}' ) ;
348
347
}
349
348
0 commit comments