Skip to content
Merged
Show file tree
Hide file tree
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
148 changes: 148 additions & 0 deletions src/language/__tests__/blockString-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

import { expect } from 'chai';
import { describe, it } from 'mocha';
import { dedentBlockStringValue, printBlockString } from '../blockString';

function joinLines(...args) {
return args.join('\n');
}

describe('dedentBlockStringValue', () => {
it('removes uniform indentation from a string', () => {
const rawValue = joinLines(
'',
' Hello,',
' World!',
'',
' Yours,',
' GraphQL.',
);
expect(dedentBlockStringValue(rawValue)).to.equal(
joinLines('Hello,', ' World!', '', 'Yours,', ' GraphQL.'),
);
});

it('removes empty leading and trailing lines', () => {
const rawValue = joinLines(
'',
'',
' Hello,',
' World!',
'',
' Yours,',
' GraphQL.',
'',
'',
);
expect(dedentBlockStringValue(rawValue)).to.equal(
joinLines('Hello,', ' World!', '', 'Yours,', ' GraphQL.'),
);
});

it('removes blank leading and trailing lines', () => {
const rawValue = joinLines(
' ',
' ',
' Hello,',
' World!',
'',
' Yours,',
' GraphQL.',
' ',
' ',
);
expect(dedentBlockStringValue(rawValue)).to.equal(
joinLines('Hello,', ' World!', '', 'Yours,', ' GraphQL.'),
);
});

it('retains indentation from first line', () => {
const rawValue = joinLines(
' Hello,',
' World!',
'',
' Yours,',
' GraphQL.',
);
expect(dedentBlockStringValue(rawValue)).to.equal(
joinLines(' Hello,', ' World!', '', 'Yours,', ' GraphQL.'),
);
});

it('does not alter trailing spaces', () => {
const rawValue = joinLines(
' ',
' Hello, ',
' World! ',
' ',
' Yours, ',
' GraphQL. ',
' ',
);
expect(dedentBlockStringValue(rawValue)).to.equal(
joinLines(
'Hello, ',
' World! ',
' ',
'Yours, ',
' GraphQL. ',
),
);
});
});

describe('printBlockString', () => {
it('by default print block strings as single line', () => {
const str = 'one liner';
expect(printBlockString(str)).to.equal('"""one liner"""');
expect(printBlockString(str, '', true)).to.equal('"""\none liner\n"""');
});

it('correctly prints single-line with leading space', () => {
const str = ' space-led string';
expect(printBlockString(str)).to.equal('""" space-led string"""');
expect(printBlockString(str, '', true)).to.equal(
'""" space-led string\n"""',
);
});

it('correctly prints single-line with leading space and quotation', () => {
const str = ' space-led value "quoted string"';

expect(printBlockString(str)).to.equal(
'""" space-led value "quoted string"\n"""',
);

expect(printBlockString(str, '', true)).to.equal(
'""" space-led value "quoted string"\n"""',
);
});

it('correctly prints string with a first line indentation', () => {
const str = joinLines(
' first ',
' line ',
'indentation',
' string',
);

expect(printBlockString(str)).to.equal(
joinLines(
'"""',
' first ',
' line ',
'indentation',
' string',
'"""',
),
);
});
});
96 changes: 0 additions & 96 deletions src/language/__tests__/blockStringValue-test.js

This file was deleted.

49 changes: 0 additions & 49 deletions src/language/__tests__/printer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,55 +98,6 @@ describe('Printer: Query document', () => {
`);
});

describe('block string', () => {
it('correctly prints single-line with leading space', () => {
const mutationASTWithArtifacts = parse(
'{ field(arg: """ space-led value""") }',
);
expect(print(mutationASTWithArtifacts)).to.equal(dedent`
{
field(arg: """ space-led value""")
}
`);
});

it('correctly prints string with a first line indentation', () => {
const mutationASTWithArtifacts = parse(`
{
field(arg: """
first
line
indentation
""")
}
`);
expect(print(mutationASTWithArtifacts)).to.equal(dedent`
{
field(arg: """
first
line
indentation
""")
}
`);
});

it('correctly prints single-line with leading space and quotation', () => {
const mutationASTWithArtifacts = parse(`
{
field(arg: """ space-led value "quoted string"
""")
}
`);
expect(print(mutationASTWithArtifacts)).to.equal(dedent`
{
field(arg: """ space-led value "quoted string"
""")
}
`);
});
});

it('Experimental: correctly prints fragment defined variables', () => {
const fragmentWithVariable = parse(
`
Expand Down
24 changes: 6 additions & 18 deletions src/language/__tests__/schema-printer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,12 @@ describe('Printer: SDL document', () => {
type Foo implements Bar & Baz {
"Description of the \`one\` field."
one: Type
"""
This is a description of the \`two\` field.
"""
"""This is a description of the \`two\` field."""
two(
"""
This is a description of the \`argument\` argument.
"""
"""This is a description of the \`argument\` argument."""
argument: InputType!
): Type
"""
This is a description of the \`three\` field.
"""
"""This is a description of the \`three\` field."""
three(argument: InputType, other: String): Int
four(argument: String = "string"): String
five(argument: [String] = ["string", "string"]): String
Expand Down Expand Up @@ -121,13 +115,9 @@ describe('Printer: SDL document', () => {
extend scalar CustomScalar @onScalar

enum Site {
"""
This is a description of the \`DESKTOP\` value
"""
"""This is a description of the \`DESKTOP\` value"""
DESKTOP
"""
This is a description of the \`MOBILE\` value
"""
"""This is a description of the \`MOBILE\` value"""
MOBILE
"This is a description of the \`WEB\` value"
WEB
Expand Down Expand Up @@ -163,9 +153,7 @@ describe('Printer: SDL document', () => {

extend input InputType @onInputObject

"""
This is a description of the \`@skip\` directive
"""
"""This is a description of the \`@skip\` directive"""
directive @skip(if: Boolean! @onArgumentDefinition) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
Expand Down
31 changes: 30 additions & 1 deletion src/language/blockStringValue.js → src/language/blockString.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* This implements the GraphQL spec's BlockStringValue() static algorithm.
*/
export default function blockStringValue(rawString: string): string {
export function dedentBlockStringValue(rawString: string): string {
// Expand a block string's raw value into independent lines.
const lines = rawString.split(/\r\n|[\n\r]/g);

Expand Down Expand Up @@ -62,3 +62,32 @@ function leadingWhitespace(str) {
function isBlank(str) {
return leadingWhitespace(str) === str.length;
}

/**
* 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.
*/
export function printBlockString(
value: string,
indentation?: string = '',
preferMultipleLines?: ?boolean = false,
): string {
const isSingleLine = value.indexOf('\n') === -1;
const hasLeadingSpace = value[0] === ' ' || value[0] === '\t';
const hasTrailingQuote = value[value.length - 1] === '"';
const printAsMultipleLines =
!isSingleLine || hasTrailingQuote || preferMultipleLines;

let result = '';
// Format a multi-line block quote to account for leading space.
if (printAsMultipleLines && !(isSingleLine && hasLeadingSpace)) {
result += '\n' + indentation;
}
result += indentation ? value.replace(/\n/g, '\n' + indentation) : value;
if (printAsMultipleLines) {
result += '\n';
}

return '"""' + result.replace(/"""/g, '\\"""') + '"""';
}
Loading