Skip to content

Commit d492cb1

Browse files
committed
Fixes
1 parent 84689d1 commit d492cb1

File tree

4 files changed

+76
-64
lines changed

4 files changed

+76
-64
lines changed

benchmark/benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ function maxBy(array, fn) {
243243

244244
// Prepare all revisions and run benchmarks matching a pattern against them.
245245
async function runBenchmarks(benchmarks, benchmarkProjects) {
246-
for (const benchmark of benchmarks.filter(x => x.includes('visit-') || x.includes('printer-'))) {
246+
for (const benchmark of benchmarks) {
247247
const results = [];
248248
for (let i = 0; i < benchmarkProjects.length; ++i) {
249249
const { revision, projectPath } = benchmarkProjects[i];

benchmark/printer-benchmark.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
const { parse } = require('graphql/language/parser.js');
44
const { print } = require('graphql/language/printer.js');
5-
const { bigDocument } = require('./fixtures')
5+
const { bigDocument } = require('./fixtures');
66

7-
const document = parse(bigDocument)
7+
const document = parse(bigDocument);
88

99
module.exports = {
1010
name: 'Print ktichen-sink query',

src/language/printer.ts

Lines changed: 69 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@ const printDocASTReducer: ASTReducer<string> = {
2323
// Document
2424

2525
Document: {
26-
leave: (node) => truthyJoin(node.definitions, '\n\n'),
26+
leave: (node) => join(node.definitions, '\n\n'),
2727
},
2828

2929
OperationDefinition: {
3030
leave(node) {
31-
const varDefs = wrap('(', truthyJoin(node.variableDefinitions, ', '), ')');
32-
const prefix = join(
31+
const varDefs = wrap('(', join(node.variableDefinitions, ', '), ')');
32+
const prefix = maybeJoin(
3333
[
3434
node.operation,
35-
// TODO: optimize
36-
join([node.name, varDefs]),
37-
truthyJoin(node.directives, ' '),
35+
maybeJoin([node.name, varDefs]),
36+
join(node.directives, ' '),
3837
],
3938
' ',
4039
);
@@ -51,20 +50,20 @@ const printDocASTReducer: ASTReducer<string> = {
5150
': ' +
5251
type +
5352
wrap(' = ', defaultValue) +
54-
wrap(' ', truthyJoin(directives, ' ')),
53+
wrap(' ', join(directives, ' ')),
5554
},
5655
SelectionSet: { leave: ({ selections }) => block(selections) },
5756

5857
Field: {
5958
leave({ alias, name, arguments: args, directives, selectionSet }) {
6059
const prefix = wrap('', alias, ': ') + name;
61-
let argsLine = prefix + wrap('(', truthyJoin(args, ', '), ')');
60+
let argsLine = prefix + wrap('(', join(args, ', '), ')');
6261

6362
if (argsLine.length > MAX_LINE_LENGTH) {
64-
argsLine = prefix + wrap('(\n', indent(truthyJoin(args, '\n')), '\n)');
63+
argsLine = prefix + wrap('(\n', indent(join(args, '\n')), '\n)');
6564
}
6665

67-
return join([argsLine, truthyJoin(directives, ' '), selectionSet], ' ');
66+
return maybeJoin([argsLine, join(directives, ' '), selectionSet], ' ');
6867
},
6968
},
7069

@@ -74,16 +73,16 @@ const printDocASTReducer: ASTReducer<string> = {
7473

7574
FragmentSpread: {
7675
leave: ({ name, directives }) =>
77-
'...' + name + wrap(' ', truthyJoin(directives, ' ')),
76+
'...' + name + wrap(' ', join(directives, ' ')),
7877
},
7978

8079
InlineFragment: {
8180
leave: ({ typeCondition, directives, selectionSet }) =>
82-
join(
81+
maybeJoin(
8382
[
8483
'...',
8584
wrap('on ', typeCondition),
86-
truthyJoin(directives, ' '),
85+
join(directives, ' '),
8786
selectionSet,
8887
],
8988
' ',
@@ -100,8 +99,8 @@ const printDocASTReducer: ASTReducer<string> = {
10099
}) =>
101100
// Note: fragment variable definitions are experimental and may be changed
102101
// or removed in the future.
103-
`fragment ${name}${wrap('(', truthyJoin(variableDefinitions, ', '), ')')} ` +
104-
`on ${typeCondition} ${wrap('', truthyJoin(directives, ' '), ' ')}` +
102+
`fragment ${name}${wrap('(', join(variableDefinitions, ', '), ')')} ` +
103+
`on ${typeCondition} ${wrap('', join(directives, ' '), ' ')}` +
105104
selectionSet,
106105
},
107106

@@ -116,15 +115,15 @@ const printDocASTReducer: ASTReducer<string> = {
116115
BooleanValue: { leave: ({ value }) => (value ? 'true' : 'false') },
117116
NullValue: { leave: () => 'null' },
118117
EnumValue: { leave: ({ value }) => value },
119-
ListValue: { leave: ({ values }) => '[' + truthyJoin(values, ', ') + ']' },
120-
ObjectValue: { leave: ({ fields }) => '{' + truthyJoin(fields, ', ') + '}' },
118+
ListValue: { leave: ({ values }) => '[' + join(values, ', ') + ']' },
119+
ObjectValue: { leave: ({ fields }) => '{' + join(fields, ', ') + '}' },
121120
ObjectField: { leave: ({ name, value }) => name + ': ' + value },
122121

123122
// Directive
124123

125124
Directive: {
126125
leave: ({ name, arguments: args }) =>
127-
'@' + name + wrap('(', truthyJoin(args, ', '), ')'),
126+
'@' + name + wrap('(', join(args, ', '), ')'),
128127
},
129128

130129
// Type
@@ -138,7 +137,10 @@ const printDocASTReducer: ASTReducer<string> = {
138137
SchemaDefinition: {
139138
leave: ({ description, directives, operationTypes }) =>
140139
wrap('', description, '\n') +
141-
join(['schema', join(directives, ' '), block(operationTypes)], ' '),
140+
maybeJoin(
141+
['schema', maybeJoin(directives, ' '), block(operationTypes)],
142+
' ',
143+
),
142144
},
143145

144146
OperationTypeDefinition: {
@@ -148,18 +150,18 @@ const printDocASTReducer: ASTReducer<string> = {
148150
ScalarTypeDefinition: {
149151
leave: ({ description, name, directives }) =>
150152
wrap('', description, '\n') +
151-
join(['scalar', name, truthyJoin(directives, ' ')], ' '),
153+
maybeJoin(['scalar', name, join(directives, ' ')], ' '),
152154
},
153155

154156
ObjectTypeDefinition: {
155157
leave: ({ description, name, interfaces, directives, fields }) =>
156158
wrap('', description, '\n') +
157-
join(
159+
maybeJoin(
158160
[
159161
'type',
160162
name,
161-
wrap('implements ', truthyJoin(interfaces, ' & ')),
162-
truthyJoin(directives, ' '),
163+
wrap('implements ', join(interfaces, ' & ')),
164+
join(directives, ' '),
163165
block(fields),
164166
],
165167
' ',
@@ -171,31 +173,31 @@ const printDocASTReducer: ASTReducer<string> = {
171173
wrap('', description, '\n') +
172174
name +
173175
(hasMultilineItems(args)
174-
? wrap('(\n', indent(truthyJoin(args, '\n')), '\n)')
175-
: wrap('(', truthyJoin(args, ', '), ')')) +
176+
? wrap('(\n', indent(join(args, '\n')), '\n)')
177+
: wrap('(', join(args, ', '), ')')) +
176178
': ' +
177179
type +
178-
wrap(' ', truthyJoin(directives, ' ')),
180+
wrap(' ', join(directives, ' ')),
179181
},
180182

181183
InputValueDefinition: {
182184
leave: ({ description, name, type, defaultValue, directives }) =>
183185
wrap('', description, '\n') +
184-
join(
185-
[name + ': ' + type, wrap('= ', defaultValue), truthyJoin(directives, ' ')],
186+
maybeJoin(
187+
[name + ': ' + type, wrap('= ', defaultValue), join(directives, ' ')],
186188
' ',
187189
),
188190
},
189191

190192
InterfaceTypeDefinition: {
191193
leave: ({ description, name, interfaces, directives, fields }) =>
192194
wrap('', description, '\n') +
193-
join(
195+
maybeJoin(
194196
[
195197
'interface',
196198
name,
197-
wrap('implements ', truthyJoin(interfaces, ' & ')),
198-
truthyJoin(directives, ' '),
199+
wrap('implements ', join(interfaces, ' & ')),
200+
join(directives, ' '),
199201
block(fields),
200202
],
201203
' ',
@@ -205,27 +207,28 @@ const printDocASTReducer: ASTReducer<string> = {
205207
UnionTypeDefinition: {
206208
leave: ({ description, name, directives, types }) =>
207209
wrap('', description, '\n') +
208-
join(
209-
['union', name, truthyJoin(directives, ' '), wrap('= ', truthyJoin(types, ' | '))],
210+
maybeJoin(
211+
['union', name, join(directives, ' '), wrap('= ', join(types, ' | '))],
210212
' ',
211213
),
212214
},
213215

214216
EnumTypeDefinition: {
215217
leave: ({ description, name, directives, values }) =>
216218
wrap('', description, '\n') +
217-
join(['enum', name, truthyJoin(directives, ' '), block(values)], ' '),
219+
maybeJoin(['enum', name, join(directives, ' '), block(values)], ' '),
218220
},
219221

220222
EnumValueDefinition: {
221223
leave: ({ description, name, directives }) =>
222-
wrap('', description, '\n') + join([name, truthyJoin(directives, ' ')], ' '),
224+
wrap('', description, '\n') +
225+
maybeJoin([name, join(directives, ' ')], ' '),
223226
},
224227

225228
InputObjectTypeDefinition: {
226229
leave: ({ description, name, directives, fields }) =>
227230
wrap('', description, '\n') +
228-
join(['input', name, truthyJoin(directives, ' '), block(fields)], ' '),
231+
maybeJoin(['input', name, join(directives, ' '), block(fields)], ' '),
229232
},
230233

231234
DirectiveDefinition: {
@@ -234,34 +237,34 @@ const printDocASTReducer: ASTReducer<string> = {
234237
'directive @' +
235238
name +
236239
(hasMultilineItems(args)
237-
? wrap('(\n', indent(truthyJoin(args, '\n')), '\n)')
238-
: wrap('(', truthyJoin(args, ', '), ')')) +
240+
? wrap('(\n', indent(join(args, '\n')), '\n)')
241+
: wrap('(', join(args, ', '), ')')) +
239242
(repeatable ? ' repeatable' : '') +
240243
' on ' +
241-
truthyJoin(locations, ' | '),
244+
join(locations, ' | '),
242245
},
243246

244247
SchemaExtension: {
245248
leave: ({ directives, operationTypes }) =>
246-
join(
247-
['extend schema', truthyJoin(directives, ' '), block(operationTypes)],
249+
maybeJoin(
250+
['extend schema', join(directives, ' '), block(operationTypes)],
248251
' ',
249252
),
250253
},
251254

252255
ScalarTypeExtension: {
253256
leave: ({ name, directives }) =>
254-
join(['extend scalar', name, truthyJoin(directives, ' ')], ' '),
257+
maybeJoin(['extend scalar', name, join(directives, ' ')], ' '),
255258
},
256259

257260
ObjectTypeExtension: {
258261
leave: ({ name, interfaces, directives, fields }) =>
259-
join(
262+
maybeJoin(
260263
[
261264
'extend type',
262265
name,
263-
wrap('implements ', truthyJoin(interfaces, ' & ')),
264-
truthyJoin(directives, ' '),
266+
wrap('implements ', join(interfaces, ' & ')),
267+
join(directives, ' '),
265268
block(fields),
266269
],
267270
' ',
@@ -270,12 +273,12 @@ const printDocASTReducer: ASTReducer<string> = {
270273

271274
InterfaceTypeExtension: {
272275
leave: ({ name, interfaces, directives, fields }) =>
273-
join(
276+
maybeJoin(
274277
[
275278
'extend interface',
276279
name,
277-
wrap('implements ', truthyJoin(interfaces, ' & ')),
278-
truthyJoin(directives, ' '),
280+
wrap('implements ', join(interfaces, ' & ')),
281+
join(directives, ' '),
279282
block(fields),
280283
],
281284
' ',
@@ -284,37 +287,43 @@ const printDocASTReducer: ASTReducer<string> = {
284287

285288
UnionTypeExtension: {
286289
leave: ({ name, directives, types }) =>
287-
join(
290+
maybeJoin(
288291
[
289292
'extend union',
290293
name,
291-
truthyJoin(directives, ' '),
292-
wrap('= ', truthyJoin(types, ' | ')),
294+
join(directives, ' '),
295+
wrap('= ', join(types, ' | ')),
293296
],
294297
' ',
295298
),
296299
},
297300

298301
EnumTypeExtension: {
299302
leave: ({ name, directives, values }) =>
300-
join(['extend enum', name, truthyJoin(directives, ' '), block(values)], ' '),
303+
maybeJoin(
304+
['extend enum', name, join(directives, ' '), block(values)],
305+
' ',
306+
),
301307
},
302308

303309
InputObjectTypeExtension: {
304310
leave: ({ name, directives, fields }) =>
305-
join(['extend input', name, truthyJoin(directives, ' '), block(fields)], ' '),
311+
maybeJoin(
312+
['extend input', name, join(directives, ' '), block(fields)],
313+
' ',
314+
),
306315
},
307316
};
308317

309318
/**
310319
* Given maybeArray, print an empty string if it is null or empty, otherwise
311320
* print all items together separated by separator if provided
312321
*/
313-
function join(
322+
function maybeJoin(
314323
maybeArray: Maybe<ReadonlyArray<string | undefined>>,
315324
separator = '',
316325
): string {
317-
if (!maybeArray) return ''
326+
if (!maybeArray) return '';
318327

319328
const list = maybeArray.filter((x) => x);
320329
const listLength = list.length;
@@ -323,27 +332,27 @@ function join(
323332
if (i === listLength - 1) return result + list[i];
324333
else result += list[i] + separator;
325334
}
326-
return result
335+
return result;
327336
}
328337

329-
function truthyJoin(
338+
function join(
330339
list: ReadonlyArray<string> | undefined,
331340
separator: string,
332341
): string {
333-
if (!list) return ''
342+
if (!list) return '';
334343
const listLength = list.length;
335344
let result = '';
336345
for (let i = 0; i < listLength; i++) {
337346
if (i === listLength - 1) return result + list[i];
338347
else result += list[i] + separator;
339348
}
340-
return result
349+
return result;
341350
}
342351

343352
/**
344353
* Given array, print each item on its own line, wrapped in an indented `{ }` block.
345354
*/
346-
function block(array: Maybe<ReadonlyArray<string | undefined>>): string {
355+
function block(array: ReadonlyArray<string> | undefined): string {
347356
return wrap('{\n', indent(join(array, '\n')), '\n}');
348357
}
349358

src/language/visitor.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,10 @@ export function visit(
225225
}
226226
}
227227
} else {
228-
node = { ...node }
228+
node = Object.defineProperties(
229+
{},
230+
Object.getOwnPropertyDescriptors(node),
231+
);
229232
for (let i = 0; i < edits.length; i++) {
230233
node[edits[i][0]] = edits[i][1];
231234
}

0 commit comments

Comments
 (0)