@@ -23,18 +23,17 @@ const printDocASTReducer: ASTReducer<string> = {
2323 // Document
2424
2525 Document : {
26- leave : ( node ) => truthyJoin ( node . definitions , '\n\n' ) ,
26+ leave : ( node ) => join ( node . definitions , '\n\n' ) ,
2727 } ,
2828
2929 OperationDefinition : {
3030 leave ( node ) {
31- const varDefs = wrap ( '(' , truthyJoin ( node . variableDefinitions , ', ' ) , ')' ) ;
32- const prefix = join (
31+ const varDefs = wrap ( '(' , join ( node . variableDefinitions , ', ' ) , ')' ) ;
32+ const prefix = maybeJoin (
3333 [
3434 node . operation ,
35- // TODO: optimize
36- join ( [ node . name , varDefs ] ) ,
37- truthyJoin ( node . directives , ' ' ) ,
35+ maybeJoin ( [ node . name , varDefs ] ) ,
36+ join ( node . directives , ' ' ) ,
3837 ] ,
3938 ' ' ,
4039 ) ;
@@ -51,20 +50,20 @@ const printDocASTReducer: ASTReducer<string> = {
5150 ': ' +
5251 type +
5352 wrap ( ' = ' , defaultValue ) +
54- wrap ( ' ' , truthyJoin ( directives , ' ' ) ) ,
53+ wrap ( ' ' , join ( directives , ' ' ) ) ,
5554 } ,
5655 SelectionSet : { leave : ( { selections } ) => block ( selections ) } ,
5756
5857 Field : {
5958 leave ( { alias, name, arguments : args , directives, selectionSet } ) {
6059 const prefix = wrap ( '' , alias , ': ' ) + name ;
61- let argsLine = prefix + wrap ( '(' , truthyJoin ( args , ', ' ) , ')' ) ;
60+ let argsLine = prefix + wrap ( '(' , join ( args , ', ' ) , ')' ) ;
6261
6362 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)' ) ;
6564 }
6665
67- return join ( [ argsLine , truthyJoin ( directives , ' ' ) , selectionSet ] , ' ' ) ;
66+ return maybeJoin ( [ argsLine , join ( directives , ' ' ) , selectionSet ] , ' ' ) ;
6867 } ,
6968 } ,
7069
@@ -74,16 +73,16 @@ const printDocASTReducer: ASTReducer<string> = {
7473
7574 FragmentSpread : {
7675 leave : ( { name, directives } ) =>
77- '...' + name + wrap ( ' ' , truthyJoin ( directives , ' ' ) ) ,
76+ '...' + name + wrap ( ' ' , join ( directives , ' ' ) ) ,
7877 } ,
7978
8079 InlineFragment : {
8180 leave : ( { typeCondition, directives, selectionSet } ) =>
82- join (
81+ maybeJoin (
8382 [
8483 '...' ,
8584 wrap ( 'on ' , typeCondition ) ,
86- truthyJoin ( directives , ' ' ) ,
85+ join ( directives , ' ' ) ,
8786 selectionSet ,
8887 ] ,
8988 ' ' ,
@@ -100,8 +99,8 @@ const printDocASTReducer: ASTReducer<string> = {
10099 } ) =>
101100 // Note: fragment variable definitions are experimental and may be changed
102101 // 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 , ' ' ) , ' ' ) } ` +
105104 selectionSet ,
106105 } ,
107106
@@ -116,15 +115,15 @@ const printDocASTReducer: ASTReducer<string> = {
116115 BooleanValue : { leave : ( { value } ) => ( value ? 'true' : 'false' ) } ,
117116 NullValue : { leave : ( ) => 'null' } ,
118117 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 , ', ' ) + '}' } ,
121120 ObjectField : { leave : ( { name, value } ) => name + ': ' + value } ,
122121
123122 // Directive
124123
125124 Directive : {
126125 leave : ( { name, arguments : args } ) =>
127- '@' + name + wrap ( '(' , truthyJoin ( args , ', ' ) , ')' ) ,
126+ '@' + name + wrap ( '(' , join ( args , ', ' ) , ')' ) ,
128127 } ,
129128
130129 // Type
@@ -138,7 +137,7 @@ const printDocASTReducer: ASTReducer<string> = {
138137 SchemaDefinition : {
139138 leave : ( { description, directives, operationTypes } ) =>
140139 wrap ( '' , description , '\n' ) +
141- join ( [ 'schema' , join ( directives , ' ' ) , block ( operationTypes ) ] , ' ' ) ,
140+ maybeJoin ( [ 'schema' , maybeJoin ( directives , ' ' ) , block ( operationTypes ) ] , ' ' ) ,
142141 } ,
143142
144143 OperationTypeDefinition : {
@@ -148,18 +147,18 @@ const printDocASTReducer: ASTReducer<string> = {
148147 ScalarTypeDefinition : {
149148 leave : ( { description, name, directives } ) =>
150149 wrap ( '' , description , '\n' ) +
151- join ( [ 'scalar' , name , truthyJoin ( directives , ' ' ) ] , ' ' ) ,
150+ maybeJoin ( [ 'scalar' , name , join ( directives , ' ' ) ] , ' ' ) ,
152151 } ,
153152
154153 ObjectTypeDefinition : {
155154 leave : ( { description, name, interfaces, directives, fields } ) =>
156155 wrap ( '' , description , '\n' ) +
157- join (
156+ maybeJoin (
158157 [
159158 'type' ,
160159 name ,
161- wrap ( 'implements ' , truthyJoin ( interfaces , ' & ' ) ) ,
162- truthyJoin ( directives , ' ' ) ,
160+ wrap ( 'implements ' , join ( interfaces , ' & ' ) ) ,
161+ join ( directives , ' ' ) ,
163162 block ( fields ) ,
164163 ] ,
165164 ' ' ,
@@ -171,31 +170,31 @@ const printDocASTReducer: ASTReducer<string> = {
171170 wrap ( '' , description , '\n' ) +
172171 name +
173172 ( 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 , ', ' ) , ')' ) ) +
176175 ': ' +
177176 type +
178- wrap ( ' ' , truthyJoin ( directives , ' ' ) ) ,
177+ wrap ( ' ' , join ( directives , ' ' ) ) ,
179178 } ,
180179
181180 InputValueDefinition : {
182181 leave : ( { description, name, type, defaultValue, directives } ) =>
183182 wrap ( '' , description , '\n' ) +
184- join (
185- [ name + ': ' + type , wrap ( '= ' , defaultValue ) , truthyJoin ( directives , ' ' ) ] ,
183+ maybeJoin (
184+ [ name + ': ' + type , wrap ( '= ' , defaultValue ) , join ( directives , ' ' ) ] ,
186185 ' ' ,
187186 ) ,
188187 } ,
189188
190189 InterfaceTypeDefinition : {
191190 leave : ( { description, name, interfaces, directives, fields } ) =>
192191 wrap ( '' , description , '\n' ) +
193- join (
192+ maybeJoin (
194193 [
195194 'interface' ,
196195 name ,
197- wrap ( 'implements ' , truthyJoin ( interfaces , ' & ' ) ) ,
198- truthyJoin ( directives , ' ' ) ,
196+ wrap ( 'implements ' , join ( interfaces , ' & ' ) ) ,
197+ join ( directives , ' ' ) ,
199198 block ( fields ) ,
200199 ] ,
201200 ' ' ,
@@ -205,27 +204,27 @@ const printDocASTReducer: ASTReducer<string> = {
205204 UnionTypeDefinition : {
206205 leave : ( { description, name, directives, types } ) =>
207206 wrap ( '' , description , '\n' ) +
208- join (
209- [ 'union' , name , truthyJoin ( directives , ' ' ) , wrap ( '= ' , truthyJoin ( types , ' | ' ) ) ] ,
207+ maybeJoin (
208+ [ 'union' , name , join ( directives , ' ' ) , wrap ( '= ' , join ( types , ' | ' ) ) ] ,
210209 ' ' ,
211210 ) ,
212211 } ,
213212
214213 EnumTypeDefinition : {
215214 leave : ( { description, name, directives, values } ) =>
216215 wrap ( '' , description , '\n' ) +
217- join ( [ 'enum' , name , truthyJoin ( directives , ' ' ) , block ( values ) ] , ' ' ) ,
216+ maybeJoin ( [ 'enum' , name , join ( directives , ' ' ) , block ( values ) ] , ' ' ) ,
218217 } ,
219218
220219 EnumValueDefinition : {
221220 leave : ( { description, name, directives } ) =>
222- wrap ( '' , description , '\n' ) + join ( [ name , truthyJoin ( directives , ' ' ) ] , ' ' ) ,
221+ wrap ( '' , description , '\n' ) + maybeJoin ( [ name , join ( directives , ' ' ) ] , ' ' ) ,
223222 } ,
224223
225224 InputObjectTypeDefinition : {
226225 leave : ( { description, name, directives, fields } ) =>
227226 wrap ( '' , description , '\n' ) +
228- join ( [ 'input' , name , truthyJoin ( directives , ' ' ) , block ( fields ) ] , ' ' ) ,
227+ maybeJoin ( [ 'input' , name , join ( directives , ' ' ) , block ( fields ) ] , ' ' ) ,
229228 } ,
230229
231230 DirectiveDefinition : {
@@ -234,34 +233,34 @@ const printDocASTReducer: ASTReducer<string> = {
234233 'directive @' +
235234 name +
236235 ( 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 , ', ' ) , ')' ) ) +
239238 ( repeatable ? ' repeatable' : '' ) +
240239 ' on ' +
241- truthyJoin ( locations , ' | ' ) ,
240+ join ( locations , ' | ' ) ,
242241 } ,
243242
244243 SchemaExtension : {
245244 leave : ( { directives, operationTypes } ) =>
246- join (
247- [ 'extend schema' , truthyJoin ( directives , ' ' ) , block ( operationTypes ) ] ,
245+ maybeJoin (
246+ [ 'extend schema' , join ( directives , ' ' ) , block ( operationTypes ) ] ,
248247 ' ' ,
249248 ) ,
250249 } ,
251250
252251 ScalarTypeExtension : {
253252 leave : ( { name, directives } ) =>
254- join ( [ 'extend scalar' , name , truthyJoin ( directives , ' ' ) ] , ' ' ) ,
253+ maybeJoin ( [ 'extend scalar' , name , join ( directives , ' ' ) ] , ' ' ) ,
255254 } ,
256255
257256 ObjectTypeExtension : {
258257 leave : ( { name, interfaces, directives, fields } ) =>
259- join (
258+ maybeJoin (
260259 [
261260 'extend type' ,
262261 name ,
263- wrap ( 'implements ' , truthyJoin ( interfaces , ' & ' ) ) ,
264- truthyJoin ( directives , ' ' ) ,
262+ wrap ( 'implements ' , join ( interfaces , ' & ' ) ) ,
263+ join ( directives , ' ' ) ,
265264 block ( fields ) ,
266265 ] ,
267266 ' ' ,
@@ -270,12 +269,12 @@ const printDocASTReducer: ASTReducer<string> = {
270269
271270 InterfaceTypeExtension : {
272271 leave : ( { name, interfaces, directives, fields } ) =>
273- join (
272+ maybeJoin (
274273 [
275274 'extend interface' ,
276275 name ,
277- wrap ( 'implements ' , truthyJoin ( interfaces , ' & ' ) ) ,
278- truthyJoin ( directives , ' ' ) ,
276+ wrap ( 'implements ' , join ( interfaces , ' & ' ) ) ,
277+ join ( directives , ' ' ) ,
279278 block ( fields ) ,
280279 ] ,
281280 ' ' ,
@@ -284,33 +283,33 @@ const printDocASTReducer: ASTReducer<string> = {
284283
285284 UnionTypeExtension : {
286285 leave : ( { name, directives, types } ) =>
287- join (
286+ maybeJoin (
288287 [
289288 'extend union' ,
290289 name ,
291- truthyJoin ( directives , ' ' ) ,
292- wrap ( '= ' , truthyJoin ( types , ' | ' ) ) ,
290+ join ( directives , ' ' ) ,
291+ wrap ( '= ' , join ( types , ' | ' ) ) ,
293292 ] ,
294293 ' ' ,
295294 ) ,
296295 } ,
297296
298297 EnumTypeExtension : {
299298 leave : ( { name, directives, values } ) =>
300- join ( [ 'extend enum' , name , truthyJoin ( directives , ' ' ) , block ( values ) ] , ' ' ) ,
299+ maybeJoin ( [ 'extend enum' , name , join ( directives , ' ' ) , block ( values ) ] , ' ' ) ,
301300 } ,
302301
303302 InputObjectTypeExtension : {
304303 leave : ( { name, directives, fields } ) =>
305- join ( [ 'extend input' , name , truthyJoin ( directives , ' ' ) , block ( fields ) ] , ' ' ) ,
304+ maybeJoin ( [ 'extend input' , name , join ( directives , ' ' ) , block ( fields ) ] , ' ' ) ,
306305 } ,
307306} ;
308307
309308/**
310309 * Given maybeArray, print an empty string if it is null or empty, otherwise
311310 * print all items together separated by separator if provided
312311 */
313- function join (
312+ function maybeJoin (
314313 maybeArray : Maybe < ReadonlyArray < string | undefined > > ,
315314 separator = '' ,
316315) : string {
@@ -326,7 +325,7 @@ function join(
326325 return result
327326}
328327
329- function truthyJoin (
328+ function join (
330329 list : ReadonlyArray < string > | undefined ,
331330 separator : string ,
332331) : string {
@@ -343,7 +342,7 @@ function truthyJoin(
343342/**
344343 * Given array, print each item on its own line, wrapped in an indented `{ }` block.
345344 */
346- function block ( array : Maybe < ReadonlyArray < string | undefined > > ) : string {
345+ function block ( array : ReadonlyArray < string > | undefined ) : string {
347346 return wrap ( '{\n' , indent ( join ( array , '\n' ) ) , '\n}' ) ;
348347}
349348
0 commit comments