Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/language/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ const printDocASTReducer = {
FieldDefinition: addDescription(
({ name, arguments: args, type, directives }) =>
name +
(args.every(arg => arg.indexOf('\n') === -1)
? wrap('(', join(args, ', '), ')')
: wrap('(\n', indent(join(args, '\n')), '\n)')) +
(hasMultilineItems(args)
? wrap('(\n', indent(join(args, '\n')), '\n)')
: wrap('(', join(args, ', '), ')')) +
': ' +
type +
wrap(' ', join(directives, ' ')),
Expand Down Expand Up @@ -184,9 +184,9 @@ const printDocASTReducer = {
({ name, arguments: args, locations }) =>
'directive @' +
name +
(args.every(arg => arg.indexOf('\n') === -1)
? wrap('(', join(args, ', '), ')')
: wrap('(\n', indent(join(args, '\n')), '\n)')) +
(hasMultilineItems(args)
? wrap('(\n', indent(join(args, '\n')), '\n)')
: wrap('(', join(args, ', '), ')')) +
' on ' +
join(locations, ' | '),
),
Expand Down Expand Up @@ -264,14 +264,22 @@ function indent(maybeString) {
return maybeString && ' ' + maybeString.replace(/\n/g, '\n ');
}

function isMultiline(string) {
return string.indexOf('\n') !== -1;
}

function hasMultilineItems(maybeArray) {
return maybeArray && maybeArray.some(isMultiline);
}

/**
* Print a block string in the indented block form by adding a leading and
* trailing blank line. However, if a block string starts with whitespace and is
* a single-line, adding a leading blank line would strip that whitespace.
*/
function printBlockString(value, isDescription) {
const escaped = value.replace(/"""/g, '\\"""');
return (value[0] === ' ' || value[0] === '\t') && value.indexOf('\n') === -1
? `"""${escaped.replace(/"$/, '"\n')}"""`
: `"""\n${isDescription ? escaped : indent(escaped)}\n"""`;
return isMultiline(value) || (value[0] !== ' ' && value[0] !== '\t')
? `"""\n${isDescription ? escaped : indent(escaped)}\n"""`
: `"""${escaped.replace(/"$/, '"\n')}"""`;
}