Skip to content

Commit 913fa9b

Browse files
committed
printBlockString: replace 'preferMultipleLines' with 'options.minimize'
1 parent 396a93b commit 913fa9b

File tree

5 files changed

+57
-50
lines changed

5 files changed

+57
-50
lines changed

src/language/__tests__/blockString-fuzz.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ function lexValue(str: string): string {
2121

2222
function testPrintableBlockString(
2323
testValue: string,
24-
preferMultipleLines?: boolean,
24+
options?: { minimize: boolean },
2525
): void {
26-
const blockString = printBlockString(testValue, preferMultipleLines);
26+
const blockString = printBlockString(testValue, options);
2727
const printedValue = lexValue(blockString);
2828
invariant(
2929
testValue === printedValue,
@@ -54,8 +54,7 @@ describe('printBlockString', () => {
5454
invariant(typeof testValue === 'string');
5555

5656
testPrintableBlockString(testValue);
57-
testPrintableBlockString(testValue, true);
58-
testPrintableBlockString(testValue, false);
57+
testPrintableBlockString(testValue, { minimize: true });
5958
}
6059
}).timeout(20000);
6160
});

src/language/__tests__/blockString-test.ts

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -136,66 +136,67 @@ describe('dedentBlockStringLines', () => {
136136
});
137137

138138
describe('printBlockString', () => {
139-
function expectBlockString(str: string, preferMultipleLines?: boolean) {
140-
return expect(printBlockString(str, preferMultipleLines));
139+
function expectBlockString(str: string) {
140+
return {
141+
toEqual(expected: string | { readable: string; minimize: string }) {
142+
const { readable, minimize } =
143+
typeof expected === 'string'
144+
? { readable: expected, minimize: expected }
145+
: expected;
146+
147+
expect(printBlockString(str)).to.equal(readable);
148+
expect(printBlockString(str, { minimize: true })).to.equal(minimize);
149+
},
150+
};
141151
}
142152

143153
it('do not escape characters', () => {
144154
const str = '" \\ / \b \f \n \r \t';
145-
expectBlockString(str).to.equal('"""\n' + str + '\n"""');
155+
expectBlockString(str).toEqual({
156+
readable: '"""\n' + str + '\n"""',
157+
minimize: '"""\n' + str + '"""',
158+
});
146159
});
147160

148161
it('by default print block strings as single line', () => {
149162
const str = 'one liner';
150-
expectBlockString(str).to.equal('"""one liner"""');
151-
expectBlockString(str, true).to.equal('"""\none liner\n"""');
152-
expectBlockString(str, false).to.equal('"""one liner"""');
163+
expectBlockString(str).toEqual('"""one liner"""');
153164
});
154165

155166
it('by default print block strings ending with triple quotation as multi-line', () => {
156167
const str = 'triple quotation """';
157-
expectBlockString(str).to.equal('"""\ntriple quotation \\"""\n"""');
158-
expectBlockString(str, true).to.equal('"""\ntriple quotation \\"""\n"""');
159-
expectBlockString(str, false).to.equal('"""triple quotation \\""""""');
168+
expectBlockString(str).toEqual({
169+
readable: '"""\ntriple quotation \\"""\n"""',
170+
minimize: '"""triple quotation \\""""""',
171+
});
160172
});
161173

162174
it('correctly prints single-line with leading space', () => {
163175
const str = ' space-led string';
164-
expectBlockString(str).to.equal('""" space-led string"""');
165-
expectBlockString(str, true).to.equal('""" space-led string\n"""');
166-
expectBlockString(str, false).to.equal('""" space-led string"""');
176+
expectBlockString(str).toEqual('""" space-led string"""');
167177
});
168178

169179
it('correctly prints single-line with leading space and trailing quotation', () => {
170180
const str = ' space-led value "quoted string"';
171-
172-
expectBlockString(str).to.equal(
173-
'""" space-led value "quoted string"\n"""',
174-
);
175-
176-
expectBlockString(str, true).to.equal(
177-
'""" space-led value "quoted string"\n"""',
178-
);
179-
180-
expectBlockString(str, false).to.equal(
181+
expectBlockString(str).toEqual(
181182
'""" space-led value "quoted string"\n"""',
182183
);
183184
});
184185

185186
it('correctly prints single-line with trailing backslash', () => {
186187
const str = 'backslash \\';
187-
188-
expectBlockString(str).to.equal('"""\nbackslash \\\n"""');
189-
expectBlockString(str, true).to.equal('"""\nbackslash \\\n"""');
190-
expectBlockString(str, false).to.equal('"""backslash \\\n"""');
188+
expectBlockString(str).toEqual({
189+
readable: '"""\nbackslash \\\n"""',
190+
minimize: '"""backslash \\\n"""',
191+
});
191192
});
192193

193194
it('correctly prints multi-line with internal indent', () => {
194195
const str = 'no indent\n with indent';
195-
196-
expectBlockString(str).to.equal('"""\nno indent\n with indent\n"""');
197-
expectBlockString(str, true).to.equal('"""\nno indent\n with indent\n"""');
198-
expectBlockString(str, false).to.equal('"""\nno indent\n with indent"""');
196+
expectBlockString(str).toEqual({
197+
readable: '"""\nno indent\n with indent\n"""',
198+
minimize: '"""\nno indent\n with indent"""',
199+
});
199200
});
200201

201202
it('correctly prints string with a first line indentation', () => {
@@ -206,15 +207,21 @@ describe('printBlockString', () => {
206207
' string',
207208
);
208209

209-
expectBlockString(str).to.equal(
210-
joinLines(
210+
expectBlockString(str).toEqual({
211+
readable: joinLines(
211212
'"""',
212213
' first ',
213214
' line ',
214215
'indentation',
215216
' string',
216217
'"""',
217218
),
218-
);
219+
minimize: joinLines(
220+
'""" first ',
221+
' line ',
222+
'indentation',
223+
' string"""',
224+
),
225+
});
219226
});
220227
});

src/language/blockString.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,24 @@ function leadingWhitespace(str: string): number {
5353
* trailing blank line. However, if a block string starts with whitespace and is
5454
* a single-line, adding a leading blank line would strip that whitespace.
5555
*
56-
* `preferMultipleLines` has following semantics:
57-
* * `true` - add leading and trailing new lines if possible
58-
* * `false` - don't add leading and trailing new lines if possible
59-
* * `undefined` - add leading and trailing new lines only if it improves readability
60-
*
6156
* @internal
6257
*/
6358
export function printBlockString(
6459
value: string,
65-
preferMultipleLines?: boolean,
60+
options?: { minimize?: boolean },
6661
): string {
6762
const escapedValue = value.replace(/"""/g, '\\"""');
6863

6964
// Expand a block string's raw value into independent lines.
7065
const lines = escapedValue.split(/\r\n|[\n\r]/g);
7166
const isSingleLine = lines.length === 1;
7267

73-
// If common identation is found we can fix some of those cases by adding leading new line
68+
// If common indentation is found we can fix some of those cases by adding leading new line
7469
const forceLeadingNewLine =
7570
lines.length > 1 &&
76-
lines.slice(1).every((line) => line.length === 0 || isWhiteSpace(line.charCodeAt(0)));
71+
lines
72+
.slice(1)
73+
.every((line) => line.length === 0 || isWhiteSpace(line.charCodeAt(0)));
7774

7875
// Trailing triple quotes just looks confusing but doesn't force trailing new line
7976
const hasTrailingTripleQuotes = escapedValue.endsWith('\\"""');
@@ -84,9 +81,10 @@ export function printBlockString(
8481
const forceTrailingNewline = hasTrailingQuote || hasTrailingSlash;
8582

8683
const printAsMultipleLines =
87-
preferMultipleLines ??
84+
!options?.minimize &&
8885
// add leading and trailing new lines only if it improves readability
8986
(!isSingleLine ||
87+
value.length > 70 ||
9088
forceTrailingNewline ||
9189
forceLeadingNewLine ||
9290
hasTrailingTripleQuotes);

src/utilities/printSchema.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { Maybe } from '../jsutils/Maybe';
44

55
import { Kind } from '../language/kinds';
66
import { print } from '../language/printer';
7-
import { printBlockString } from '../language/blockString';
87

98
import type { GraphQLSchema } from '../type/schema';
109
import type { GraphQLDirective } from '../type/directives';
@@ -314,8 +313,12 @@ function printDescription(
314313
return '';
315314
}
316315

317-
const preferMultipleLines = description.length > 70;
318-
const blockString = printBlockString(description, preferMultipleLines);
316+
const blockString = print({
317+
kind: Kind.STRING,
318+
value: description,
319+
block: true,
320+
});
321+
319322
const prefix =
320323
indentation && !firstInBlock ? '\n' + indentation : indentation;
321324

src/utilities/stripIgnoredCharacters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export function stripIgnoredCharacters(source: string | Source): string {
8989

9090
const tokenBody = body.slice(currentToken.start, currentToken.end);
9191
if (tokenKind === TokenKind.BLOCK_STRING) {
92-
strippedBody += printBlockString(currentToken.value, false);
92+
strippedBody += printBlockString(currentToken.value, { minimize: true });
9393
} else {
9494
strippedBody += tokenBody;
9595
}

0 commit comments

Comments
 (0)