From b6b27896f96a626aa92ff1fbf192debfd41fc877 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Sun, 21 Feb 2021 02:23:33 +0200 Subject: [PATCH] printer: remove 'addDescription' wrapper (#2933) --- src/language/printer.js | 168 ++++++++++++++++++++++------------------ 1 file changed, 91 insertions(+), 77 deletions(-) diff --git a/src/language/printer.js b/src/language/printer.js index 4f7cc69b9b..8665fee20b 100644 --- a/src/language/printer.js +++ b/src/language/printer.js @@ -106,64 +106,79 @@ const printDocASTReducer: any = { // Type System Definitions - SchemaDefinition: addDescription(({ directives, operationTypes }) => + SchemaDefinition: ({ description, directives, operationTypes }) => + wrap('', description, '\n') + join(['schema', join(directives, ' '), block(operationTypes)], ' '), - ), OperationTypeDefinition: ({ operation, type }) => operation + ': ' + type, - ScalarTypeDefinition: addDescription(({ name, directives }) => + ScalarTypeDefinition: ({ description, name, directives }) => + wrap('', description, '\n') + join(['scalar', name, join(directives, ' ')], ' '), - ), - - ObjectTypeDefinition: addDescription( - ({ name, interfaces, directives, fields }) => - join( - [ - 'type', - name, - wrap('implements ', join(interfaces, ' & ')), - join(directives, ' '), - block(fields), - ], - ' ', - ), - ), - - FieldDefinition: addDescription( - ({ name, arguments: args, type, directives }) => - name + - (hasMultilineItems(args) - ? wrap('(\n', indent(join(args, '\n')), '\n)') - : wrap('(', join(args, ', '), ')')) + - ': ' + - type + - wrap(' ', join(directives, ' ')), - ), - - InputValueDefinition: addDescription( - ({ name, type, defaultValue, directives }) => - join( - [name + ': ' + type, wrap('= ', defaultValue), join(directives, ' ')], - ' ', - ), - ), - - InterfaceTypeDefinition: addDescription( - ({ name, interfaces, directives, fields }) => - join( - [ - 'interface', - name, - wrap('implements ', join(interfaces, ' & ')), - join(directives, ' '), - block(fields), - ], - ' ', - ), - ), - - UnionTypeDefinition: addDescription(({ name, directives, types }) => + + ObjectTypeDefinition: ({ + description, + name, + interfaces, + directives, + fields, + }) => + wrap('', description, '\n') + + join( + [ + 'type', + name, + wrap('implements ', join(interfaces, ' & ')), + join(directives, ' '), + block(fields), + ], + ' ', + ), + + FieldDefinition: ({ description, name, arguments: args, type, directives }) => + wrap('', description, '\n') + + name + + (hasMultilineItems(args) + ? wrap('(\n', indent(join(args, '\n')), '\n)') + : wrap('(', join(args, ', '), ')')) + + ': ' + + type + + wrap(' ', join(directives, ' ')), + + InputValueDefinition: ({ + description, + name, + type, + defaultValue, + directives, + }) => + wrap('', description, '\n') + + join( + [name + ': ' + type, wrap('= ', defaultValue), join(directives, ' ')], + ' ', + ), + + InterfaceTypeDefinition: ({ + description, + name, + interfaces, + directives, + fields, + }) => + wrap('', description, '\n') + + join( + [ + 'interface', + name, + wrap('implements ', join(interfaces, ' & ')), + join(directives, ' '), + block(fields), + ], + ' ', + ), + + UnionTypeDefinition: ({ description, name, directives, types }) => + wrap('', description, '\n') + join( [ 'union', @@ -173,31 +188,34 @@ const printDocASTReducer: any = { ], ' ', ), - ), - EnumTypeDefinition: addDescription(({ name, directives, values }) => + EnumTypeDefinition: ({ description, name, directives, values }) => + wrap('', description, '\n') + join(['enum', name, join(directives, ' '), block(values)], ' '), - ), - EnumValueDefinition: addDescription(({ name, directives }) => - join([name, join(directives, ' ')], ' '), - ), + EnumValueDefinition: ({ description, name, directives }) => + wrap('', description, '\n') + join([name, join(directives, ' ')], ' '), - InputObjectTypeDefinition: addDescription(({ name, directives, fields }) => + InputObjectTypeDefinition: ({ description, name, directives, fields }) => + wrap('', description, '\n') + join(['input', name, join(directives, ' '), block(fields)], ' '), - ), - - DirectiveDefinition: addDescription( - ({ name, arguments: args, repeatable, locations }) => - 'directive @' + - name + - (hasMultilineItems(args) - ? wrap('(\n', indent(join(args, '\n')), '\n)') - : wrap('(', join(args, ', '), ')')) + - (repeatable ? ' repeatable' : '') + - ' on ' + - join(locations, ' | '), - ), + + DirectiveDefinition: ({ + description, + name, + arguments: args, + repeatable, + locations, + }) => + wrap('', description, '\n') + + 'directive @' + + name + + (hasMultilineItems(args) + ? wrap('(\n', indent(join(args, '\n')), '\n)') + : wrap('(', join(args, ', '), ')')) + + (repeatable ? ' repeatable' : '') + + ' on ' + + join(locations, ' | '), SchemaExtension: ({ directives, operationTypes }) => join(['extend schema', join(directives, ' '), block(operationTypes)], ' '), @@ -247,10 +265,6 @@ const printDocASTReducer: any = { join(['extend input', name, join(directives, ' '), block(fields)], ' '), }; -function addDescription(cb) { - return (node) => join([node.description, cb(node)], '\n'); -} - /** * Given maybeArray, print an empty string if it is null or empty, otherwise * print all items together separated by separator if provided