Skip to content

Commit

Permalink
Fix print of block string with leading space and quotation (#1190)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored and leebyron committed Jan 9, 2018
1 parent 50d499e commit 80aae99
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 42 deletions.
87 changes: 52 additions & 35 deletions src/language/__tests__/printer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import { print } from '../printer';
import { join } from 'path';
import dedent from '../../jsutils/dedent';

describe('Printer', () => {
describe('Printer: Query document', () => {
const kitchenSink = readFileSync(join(__dirname, '/kitchen-sink.graphql'), {
encoding: 'utf8',
});

it('does not alter ast', () => {
const ast = parse(kitchenSink);
const astBefore = JSON.stringify(ast);
Expand Down Expand Up @@ -71,36 +75,53 @@ describe('Printer', () => {
`);
});

it('correctly prints single-line block strings 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 block strings with a first line indentation', () => {
const mutationAstWithArtifacts = parse(`
{
field(arg: """
first
line
indentation
""")
}
`);
expect(print(mutationAstWithArtifacts)).to.equal(dedent`
{
field(arg: """
first
line
indentation
""")
}
`);
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', () => {
Expand All @@ -119,10 +140,6 @@ describe('Printer', () => {
`);
});

const kitchenSink = readFileSync(join(__dirname, '/kitchen-sink.graphql'), {
encoding: 'utf8',
});

it('prints kitchen sink', () => {
const ast = parse(kitchenSink);

Expand Down
2 changes: 1 addition & 1 deletion src/language/__tests__/schema-printer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { parse } from '../parser';
import { print } from '../printer';
import dedent from '../../jsutils/dedent';

describe('Printer', () => {
describe('Printer: SDL document', () => {
it('prints minimal ast', () => {
const ast = {
kind: 'ScalarTypeDefinition',
Expand Down
11 changes: 5 additions & 6 deletions src/language/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ function join(maybeArray, separator) {
*/
function block(array) {
return array && array.length !== 0
? indent('{\n' + join(array, '\n')) + '\n}'
? '{\n' + indent(join(array, '\n')) + '\n}'
: '';
}

Expand All @@ -249,7 +249,7 @@ function wrap(start, maybeString, end) {
}

function indent(maybeString) {
return maybeString && maybeString.replace(/\n/g, '\n ');
return maybeString && ' ' + maybeString.replace(/\n/g, '\n ');
}

/**
Expand All @@ -258,9 +258,8 @@ function indent(maybeString) {
* 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
? `"""${value.replace(/"""/g, '\\"""')}"""`
: isDescription
? '"""\n' + value.replace(/"""/g, '\\"""') + '\n"""'
: indent('"""\n' + value.replace(/"""/g, '\\"""')) + '\n"""';
? `"""${escaped.replace(/"$/, '"\n')}"""`
: `"""\n${isDescription ? escaped : indent(escaped)}\n"""`;
}

0 comments on commit 80aae99

Please # to comment.