Skip to content

Commit 8e0c599

Browse files
authored
Read-only AST types (#1121)
**Potential breaking change: Where previously AST would produce `null`, it now produces `undefined`** This tightens up the AST types to produce readonly covariant fields, $ReadOnlyArray in a couple places (not AST yet, perhaps in a future PR), and replaces `field?: ?Rule` with `field?: Rule` to exclude the possibility of `null` from `undefined` for optional rules, reducing complexity.
1 parent f2c96b0 commit 8e0c599

File tree

10 files changed

+210
-212
lines changed

10 files changed

+210
-212
lines changed

src/execution/values.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export function getArgumentValues(
186186
*/
187187
export function getDirectiveValues(
188188
directiveDef: GraphQLDirective,
189-
node: { directives?: ?Array<DirectiveNode> },
189+
node: { +directives?: $ReadOnlyArray<DirectiveNode> },
190190
variableValues?: ?ObjMap<mixed>,
191191
): void | { [argument: string]: mixed } {
192192
const directiveNode =

src/jsutils/find.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
export default function find<T>(
11-
list: Array<T>,
11+
list: $ReadOnlyArray<T>,
1212
predicate: (item: T) => boolean,
1313
): ?T {
1414
for (let i = 0; i < list.length; i++) {

src/jsutils/quotedOrList.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const MAX_LENGTH = 5;
1212
/**
1313
* Given [ A, B, C ] return '"A", "B", or "C"'.
1414
*/
15-
export default function quotedOrList(items: Array<string>): string {
15+
export default function quotedOrList(items: $ReadOnlyArray<string>): string {
1616
const selected = items.slice(0, MAX_LENGTH);
1717
return selected
1818
.map(item => `"${item}"`)

src/jsutils/suggestionList.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
export default function suggestionList(
1515
input: string,
16-
options: Array<string>,
16+
options: $ReadOnlyArray<string>,
1717
): Array<string> {
1818
const optionsByDistance = Object.create(null);
1919
const oLength = options.length;

src/language/__tests__/parser-test.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ describe('Parser', () => {
221221
kind: Kind.OPERATION_DEFINITION,
222222
loc: { start: 0, end: 40 },
223223
operation: 'query',
224-
name: null,
224+
name: undefined,
225225
variableDefinitions: [],
226226
directives: [],
227227
selectionSet: {
@@ -231,7 +231,7 @@ describe('Parser', () => {
231231
{
232232
kind: Kind.FIELD,
233233
loc: { start: 4, end: 38 },
234-
alias: null,
234+
alias: undefined,
235235
name: {
236236
kind: Kind.NAME,
237237
loc: { start: 4, end: 8 },
@@ -261,28 +261,28 @@ describe('Parser', () => {
261261
{
262262
kind: Kind.FIELD,
263263
loc: { start: 22, end: 24 },
264-
alias: null,
264+
alias: undefined,
265265
name: {
266266
kind: Kind.NAME,
267267
loc: { start: 22, end: 24 },
268268
value: 'id',
269269
},
270270
arguments: [],
271271
directives: [],
272-
selectionSet: null,
272+
selectionSet: undefined,
273273
},
274274
{
275275
kind: Kind.FIELD,
276276
loc: { start: 30, end: 34 },
277-
alias: null,
277+
alias: undefined,
278278
name: {
279279
kind: Kind.NAME,
280280
loc: { start: 30, end: 34 },
281281
value: 'name',
282282
},
283283
arguments: [],
284284
directives: [],
285-
selectionSet: null,
285+
selectionSet: undefined,
286286
},
287287
],
288288
},
@@ -311,7 +311,7 @@ describe('Parser', () => {
311311
kind: Kind.OPERATION_DEFINITION,
312312
loc: { start: 0, end: 29 },
313313
operation: 'query',
314-
name: null,
314+
name: undefined,
315315
variableDefinitions: [],
316316
directives: [],
317317
selectionSet: {
@@ -321,7 +321,7 @@ describe('Parser', () => {
321321
{
322322
kind: Kind.FIELD,
323323
loc: { start: 10, end: 27 },
324-
alias: null,
324+
alias: undefined,
325325
name: {
326326
kind: Kind.NAME,
327327
loc: { start: 10, end: 14 },
@@ -336,15 +336,15 @@ describe('Parser', () => {
336336
{
337337
kind: Kind.FIELD,
338338
loc: { start: 21, end: 23 },
339-
alias: null,
339+
alias: undefined,
340340
name: {
341341
kind: Kind.NAME,
342342
loc: { start: 21, end: 23 },
343343
value: 'id',
344344
},
345345
arguments: [],
346346
directives: [],
347-
selectionSet: null,
347+
selectionSet: undefined,
348348
},
349349
],
350350
},

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ type Hello {
399399
inputValueNode(
400400
nameNode('flag', { start: 22, end: 26 }),
401401
typeNode('Boolean', { start: 28, end: 35 }),
402-
null,
402+
undefined,
403403
{ start: 22, end: 35 },
404404
),
405405
],
@@ -481,7 +481,7 @@ type Hello {
481481
type: typeNode('String', { start: 31, end: 37 }),
482482
loc: { start: 30, end: 38 },
483483
},
484-
null,
484+
undefined,
485485
{ start: 22, end: 38 },
486486
),
487487
],
@@ -518,13 +518,13 @@ type Hello {
518518
inputValueNode(
519519
nameNode('argOne', { start: 22, end: 28 }),
520520
typeNode('Boolean', { start: 30, end: 37 }),
521-
null,
521+
undefined,
522522
{ start: 22, end: 37 },
523523
),
524524
inputValueNode(
525525
nameNode('argTwo', { start: 39, end: 45 }),
526526
typeNode('Int', { start: 47, end: 50 }),
527-
null,
527+
undefined,
528528
{ start: 39, end: 50 },
529529
),
530530
],
@@ -657,7 +657,7 @@ input Hello {
657657
inputValueNode(
658658
nameNode('world', { start: 17, end: 22 }),
659659
typeNode('String', { start: 24, end: 30 }),
660-
null,
660+
undefined,
661661
{ start: 17, end: 30 },
662662
),
663663
],

0 commit comments

Comments
 (0)