Skip to content

Commit

Permalink
print: remove indentation inside of block strings (#2932)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Feb 21, 2021
1 parent 090ba36 commit 45e33ce
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 17 deletions.
6 changes: 2 additions & 4 deletions src/language/__tests__/blockString-fuzz.js
Expand Up @@ -48,15 +48,13 @@ describe('printBlockString', () => {
);

const printedMultilineString = lexValue(
printBlockString(testValue, ' ', true),
printBlockString(testValue, true),
);

invariant(
testValue === printedMultilineString,
dedent`
Expected lexValue(printBlockString(${inspectStr(
testValue,
)}, ' ', true))
Expected lexValue(printBlockString(${inspectStr(testValue)}, true))
to equal ${inspectStr(testValue)}
but got ${inspectStr(printedMultilineString)}
`,
Expand Down
8 changes: 4 additions & 4 deletions src/language/__tests__/blockString-test.js
Expand Up @@ -134,13 +134,13 @@ 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"""');
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(
expect(printBlockString(str, true)).to.equal(
'""" space-led string\n"""',
);
});
Expand All @@ -152,7 +152,7 @@ describe('printBlockString', () => {
'""" space-led value "quoted string"\n"""',
);

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

expect(printBlockString(str)).to.equal('"""\nbackslash \\\n"""');
expect(printBlockString(str, '', true)).to.equal('"""\nbackslash \\\n"""');
expect(printBlockString(str, true)).to.equal('"""\nbackslash \\\n"""');
});

it('correctly prints string with a first line indentation', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/language/__tests__/printer-test.js
Expand Up @@ -198,7 +198,7 @@ describe('Printer: Query document', () => {
size: $size
bar: $b
obj: {key: "value", block: """
block string uses \"""
block string uses \"""
"""}
)
}
Expand Down
5 changes: 2 additions & 3 deletions src/language/blockString.js
Expand Up @@ -93,7 +93,6 @@ export function getBlockStringIndentation(value: string): number {
*/
export function printBlockString(
value: string,
indentation: string = '',
preferMultipleLines: boolean = false,
): string {
const isSingleLine = value.indexOf('\n') === -1;
Expand All @@ -109,9 +108,9 @@ export function printBlockString(
let result = '';
// Format a multi-line block quote to account for leading space.
if (printAsMultipleLines && !(isSingleLine && hasLeadingSpace)) {
result += '\n' + indentation;
result += '\n';
}
result += indentation ? value.replace(/\n/g, '\n' + indentation) : value;
result += value;
if (printAsMultipleLines) {
result += '\n';
}
Expand Down
6 changes: 2 additions & 4 deletions src/language/printer.js
Expand Up @@ -84,10 +84,8 @@ const printDocASTReducer: any = {

IntValue: ({ value }) => value,
FloatValue: ({ value }) => value,
StringValue: ({ value, block: isBlockString }, key) =>
isBlockString
? printBlockString(value, key === 'description' ? '' : ' ')
: JSON.stringify(value),
StringValue: ({ value, block: isBlockString }) =>
isBlockString ? printBlockString(value) : JSON.stringify(value),
BooleanValue: ({ value }) => (value ? 'true' : 'false'),
NullValue: () => 'null',
EnumValue: ({ value }) => value,
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/printSchema.js
Expand Up @@ -314,7 +314,7 @@ function printDescription(
}

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

Expand Down

0 comments on commit 45e33ce

Please sign in to comment.