Skip to content

Commit 45e33ce

Browse files
print: remove indentation inside of block strings (#2932)
1 parent 090ba36 commit 45e33ce

File tree

6 files changed

+12
-17
lines changed

6 files changed

+12
-17
lines changed

src/language/__tests__/blockString-fuzz.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,13 @@ describe('printBlockString', () => {
4848
);
4949

5050
const printedMultilineString = lexValue(
51-
printBlockString(testValue, ' ', true),
51+
printBlockString(testValue, true),
5252
);
5353

5454
invariant(
5555
testValue === printedMultilineString,
5656
dedent`
57-
Expected lexValue(printBlockString(${inspectStr(
58-
testValue,
59-
)}, ' ', true))
57+
Expected lexValue(printBlockString(${inspectStr(testValue)}, true))
6058
to equal ${inspectStr(testValue)}
6159
but got ${inspectStr(printedMultilineString)}
6260
`,

src/language/__tests__/blockString-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ describe('printBlockString', () => {
134134
it('by default print block strings as single line', () => {
135135
const str = 'one liner';
136136
expect(printBlockString(str)).to.equal('"""one liner"""');
137-
expect(printBlockString(str, '', true)).to.equal('"""\none liner\n"""');
137+
expect(printBlockString(str, true)).to.equal('"""\none liner\n"""');
138138
});
139139

140140
it('correctly prints single-line with leading space', () => {
141141
const str = ' space-led string';
142142
expect(printBlockString(str)).to.equal('""" space-led string"""');
143-
expect(printBlockString(str, '', true)).to.equal(
143+
expect(printBlockString(str, true)).to.equal(
144144
'""" space-led string\n"""',
145145
);
146146
});
@@ -152,7 +152,7 @@ describe('printBlockString', () => {
152152
'""" space-led value "quoted string"\n"""',
153153
);
154154

155-
expect(printBlockString(str, '', true)).to.equal(
155+
expect(printBlockString(str, true)).to.equal(
156156
'""" space-led value "quoted string"\n"""',
157157
);
158158
});
@@ -161,7 +161,7 @@ describe('printBlockString', () => {
161161
const str = 'backslash \\';
162162

163163
expect(printBlockString(str)).to.equal('"""\nbackslash \\\n"""');
164-
expect(printBlockString(str, '', true)).to.equal('"""\nbackslash \\\n"""');
164+
expect(printBlockString(str, true)).to.equal('"""\nbackslash \\\n"""');
165165
});
166166

167167
it('correctly prints string with a first line indentation', () => {

src/language/__tests__/printer-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ describe('Printer: Query document', () => {
198198
size: $size
199199
bar: $b
200200
obj: {key: "value", block: """
201-
block string uses \"""
201+
block string uses \"""
202202
"""}
203203
)
204204
}

src/language/blockString.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ export function getBlockStringIndentation(value: string): number {
9393
*/
9494
export function printBlockString(
9595
value: string,
96-
indentation: string = '',
9796
preferMultipleLines: boolean = false,
9897
): string {
9998
const isSingleLine = value.indexOf('\n') === -1;
@@ -109,9 +108,9 @@ export function printBlockString(
109108
let result = '';
110109
// Format a multi-line block quote to account for leading space.
111110
if (printAsMultipleLines && !(isSingleLine && hasLeadingSpace)) {
112-
result += '\n' + indentation;
111+
result += '\n';
113112
}
114-
result += indentation ? value.replace(/\n/g, '\n' + indentation) : value;
113+
result += value;
115114
if (printAsMultipleLines) {
116115
result += '\n';
117116
}

src/language/printer.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,8 @@ const printDocASTReducer: any = {
8484

8585
IntValue: ({ value }) => value,
8686
FloatValue: ({ value }) => value,
87-
StringValue: ({ value, block: isBlockString }, key) =>
88-
isBlockString
89-
? printBlockString(value, key === 'description' ? '' : ' ')
90-
: JSON.stringify(value),
87+
StringValue: ({ value, block: isBlockString }) =>
88+
isBlockString ? printBlockString(value) : JSON.stringify(value),
9189
BooleanValue: ({ value }) => (value ? 'true' : 'false'),
9290
NullValue: () => 'null',
9391
EnumValue: ({ value }) => value,

src/utilities/printSchema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ function printDescription(
314314
}
315315

316316
const preferMultipleLines = description.length > 70;
317-
const blockString = printBlockString(description, '', preferMultipleLines);
317+
const blockString = printBlockString(description, preferMultipleLines);
318318
const prefix =
319319
indentation && !firstInBlock ? '\n' + indentation : indentation;
320320

0 commit comments

Comments
 (0)