Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix print of block string with leading space and quotation #1190

Merged
merged 1 commit into from
Jan 9, 2018
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
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 ');
Copy link
Member Author

@IvanGoncharov IvanGoncharov Jan 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be replaced with:

return maybeString && maybeString.replace(/^|\n/g, '$&  ');

But I chose easier to read version even those it's more verbose.

}

/**
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"""`;
}