Skip to content

Commit 80aae99

Browse files
IvanGoncharovleebyron
authored andcommitted
Fix print of block string with leading space and quotation (#1190)
1 parent 50d499e commit 80aae99

File tree

3 files changed

+58
-42
lines changed

3 files changed

+58
-42
lines changed

src/language/__tests__/printer-test.js

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import { print } from '../printer';
1313
import { join } from 'path';
1414
import dedent from '../../jsutils/dedent';
1515

16-
describe('Printer', () => {
16+
describe('Printer: Query document', () => {
17+
const kitchenSink = readFileSync(join(__dirname, '/kitchen-sink.graphql'), {
18+
encoding: 'utf8',
19+
});
20+
1721
it('does not alter ast', () => {
1822
const ast = parse(kitchenSink);
1923
const astBefore = JSON.stringify(ast);
@@ -71,36 +75,53 @@ describe('Printer', () => {
7175
`);
7276
});
7377

74-
it('correctly prints single-line block strings with leading space', () => {
75-
const mutationAstWithArtifacts = parse(
76-
'{ field(arg: """ space-led value""") }',
77-
);
78-
expect(print(mutationAstWithArtifacts)).to.equal(dedent`
79-
{
80-
field(arg: """ space-led value""")
81-
}
82-
`);
83-
});
84-
85-
it('correctly prints block strings with a first line indentation', () => {
86-
const mutationAstWithArtifacts = parse(`
87-
{
88-
field(arg: """
89-
first
90-
line
91-
indentation
92-
""")
93-
}
94-
`);
95-
expect(print(mutationAstWithArtifacts)).to.equal(dedent`
96-
{
97-
field(arg: """
98-
first
99-
line
100-
indentation
101-
""")
102-
}
103-
`);
78+
describe('block string', () => {
79+
it('correctly prints single-line with leading space', () => {
80+
const mutationAstWithArtifacts = parse(
81+
'{ field(arg: """ space-led value""") }',
82+
);
83+
expect(print(mutationAstWithArtifacts)).to.equal(dedent`
84+
{
85+
field(arg: """ space-led value""")
86+
}
87+
`);
88+
});
89+
90+
it('correctly prints string with a first line indentation', () => {
91+
const mutationAstWithArtifacts = parse(`
92+
{
93+
field(arg: """
94+
first
95+
line
96+
indentation
97+
""")
98+
}
99+
`);
100+
expect(print(mutationAstWithArtifacts)).to.equal(dedent`
101+
{
102+
field(arg: """
103+
first
104+
line
105+
indentation
106+
""")
107+
}
108+
`);
109+
});
110+
111+
it('correctly prints single-line with leading space and quotation', () => {
112+
const mutationAstWithArtifacts = parse(`
113+
{
114+
field(arg: """ space-led value "quoted string"
115+
""")
116+
}
117+
`);
118+
expect(print(mutationAstWithArtifacts)).to.equal(dedent`
119+
{
120+
field(arg: """ space-led value "quoted string"
121+
""")
122+
}
123+
`);
124+
});
104125
});
105126

106127
it('Experimental: correctly prints fragment defined variables', () => {
@@ -119,10 +140,6 @@ describe('Printer', () => {
119140
`);
120141
});
121142

122-
const kitchenSink = readFileSync(join(__dirname, '/kitchen-sink.graphql'), {
123-
encoding: 'utf8',
124-
});
125-
126143
it('prints kitchen sink', () => {
127144
const ast = parse(kitchenSink);
128145

src/language/__tests__/schema-printer-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { parse } from '../parser';
1313
import { print } from '../printer';
1414
import dedent from '../../jsutils/dedent';
1515

16-
describe('Printer', () => {
16+
describe('Printer: SDL document', () => {
1717
it('prints minimal ast', () => {
1818
const ast = {
1919
kind: 'ScalarTypeDefinition',

src/language/printer.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ function join(maybeArray, separator) {
236236
*/
237237
function block(array) {
238238
return array && array.length !== 0
239-
? indent('{\n' + join(array, '\n')) + '\n}'
239+
? '{\n' + indent(join(array, '\n')) + '\n}'
240240
: '';
241241
}
242242

@@ -249,7 +249,7 @@ function wrap(start, maybeString, end) {
249249
}
250250

251251
function indent(maybeString) {
252-
return maybeString && maybeString.replace(/\n/g, '\n ');
252+
return maybeString && ' ' + maybeString.replace(/\n/g, '\n ');
253253
}
254254

255255
/**
@@ -258,9 +258,8 @@ function indent(maybeString) {
258258
* a single-line, adding a leading blank line would strip that whitespace.
259259
*/
260260
function printBlockString(value, isDescription) {
261+
const escaped = value.replace(/"""/g, '\\"""');
261262
return (value[0] === ' ' || value[0] === '\t') && value.indexOf('\n') === -1
262-
? `"""${value.replace(/"""/g, '\\"""')}"""`
263-
: isDescription
264-
? '"""\n' + value.replace(/"""/g, '\\"""') + '\n"""'
265-
: indent('"""\n' + value.replace(/"""/g, '\\"""')) + '\n"""';
263+
? `"""${escaped.replace(/"$/, '"\n')}"""`
264+
: `"""\n${isDescription ? escaped : indent(escaped)}\n"""`;
266265
}

0 commit comments

Comments
 (0)