Skip to content

Commit

Permalink
Read-only AST types
Browse files Browse the repository at this point in the history
**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.
  • Loading branch information
leebyron committed Dec 6, 2017
1 parent f2c96b0 commit 67018ae
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 212 deletions.
2 changes: 1 addition & 1 deletion src/execution/values.js
Expand Up @@ -186,7 +186,7 @@ export function getArgumentValues(
*/
export function getDirectiveValues(
directiveDef: GraphQLDirective,
node: { directives?: ?Array<DirectiveNode> },
node: { +directives?: $ReadOnlyArray<DirectiveNode> },
variableValues?: ?ObjMap<mixed>,
): void | { [argument: string]: mixed } {
const directiveNode =
Expand Down
2 changes: 1 addition & 1 deletion src/jsutils/find.js
Expand Up @@ -8,7 +8,7 @@
*/

export default function find<T>(
list: Array<T>,
list: $ReadOnlyArray<T>,
predicate: (item: T) => boolean,
): ?T {
for (let i = 0; i < list.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/jsutils/quotedOrList.js
Expand Up @@ -12,7 +12,7 @@ const MAX_LENGTH = 5;
/**
* Given [ A, B, C ] return '"A", "B", or "C"'.
*/
export default function quotedOrList(items: Array<string>): string {
export default function quotedOrList(items: $ReadOnlyArray<string>): string {
const selected = items.slice(0, MAX_LENGTH);
return selected
.map(item => `"${item}"`)
Expand Down
2 changes: 1 addition & 1 deletion src/jsutils/suggestionList.js
Expand Up @@ -13,7 +13,7 @@
*/
export default function suggestionList(
input: string,
options: Array<string>,
options: $ReadOnlyArray<string>,
): Array<string> {
const optionsByDistance = Object.create(null);
const oLength = options.length;
Expand Down
20 changes: 10 additions & 10 deletions src/language/__tests__/parser-test.js
Expand Up @@ -221,7 +221,7 @@ describe('Parser', () => {
kind: Kind.OPERATION_DEFINITION,
loc: { start: 0, end: 40 },
operation: 'query',
name: null,
name: undefined,
variableDefinitions: [],
directives: [],
selectionSet: {
Expand All @@ -231,7 +231,7 @@ describe('Parser', () => {
{
kind: Kind.FIELD,
loc: { start: 4, end: 38 },
alias: null,
alias: undefined,
name: {
kind: Kind.NAME,
loc: { start: 4, end: 8 },
Expand Down Expand Up @@ -261,28 +261,28 @@ describe('Parser', () => {
{
kind: Kind.FIELD,
loc: { start: 22, end: 24 },
alias: null,
alias: undefined,
name: {
kind: Kind.NAME,
loc: { start: 22, end: 24 },
value: 'id',
},
arguments: [],
directives: [],
selectionSet: null,
selectionSet: undefined,
},
{
kind: Kind.FIELD,
loc: { start: 30, end: 34 },
alias: null,
alias: undefined,
name: {
kind: Kind.NAME,
loc: { start: 30, end: 34 },
value: 'name',
},
arguments: [],
directives: [],
selectionSet: null,
selectionSet: undefined,
},
],
},
Expand Down Expand Up @@ -311,7 +311,7 @@ describe('Parser', () => {
kind: Kind.OPERATION_DEFINITION,
loc: { start: 0, end: 29 },
operation: 'query',
name: null,
name: undefined,
variableDefinitions: [],
directives: [],
selectionSet: {
Expand All @@ -321,7 +321,7 @@ describe('Parser', () => {
{
kind: Kind.FIELD,
loc: { start: 10, end: 27 },
alias: null,
alias: undefined,
name: {
kind: Kind.NAME,
loc: { start: 10, end: 14 },
Expand All @@ -336,15 +336,15 @@ describe('Parser', () => {
{
kind: Kind.FIELD,
loc: { start: 21, end: 23 },
alias: null,
alias: undefined,
name: {
kind: Kind.NAME,
loc: { start: 21, end: 23 },
value: 'id',
},
arguments: [],
directives: [],
selectionSet: null,
selectionSet: undefined,
},
],
},
Expand Down
10 changes: 5 additions & 5 deletions src/language/__tests__/schema-parser-test.js
Expand Up @@ -399,7 +399,7 @@ type Hello {
inputValueNode(
nameNode('flag', { start: 22, end: 26 }),
typeNode('Boolean', { start: 28, end: 35 }),
null,
undefined,
{ start: 22, end: 35 },
),
],
Expand Down Expand Up @@ -481,7 +481,7 @@ type Hello {
type: typeNode('String', { start: 31, end: 37 }),
loc: { start: 30, end: 38 },
},
null,
undefined,
{ start: 22, end: 38 },
),
],
Expand Down Expand Up @@ -518,13 +518,13 @@ type Hello {
inputValueNode(
nameNode('argOne', { start: 22, end: 28 }),
typeNode('Boolean', { start: 30, end: 37 }),
null,
undefined,
{ start: 22, end: 37 },
),
inputValueNode(
nameNode('argTwo', { start: 39, end: 45 }),
typeNode('Int', { start: 47, end: 50 }),
null,
undefined,
{ start: 39, end: 50 },
),
],
Expand Down Expand Up @@ -657,7 +657,7 @@ input Hello {
inputValueNode(
nameNode('world', { start: 17, end: 22 }),
typeNode('String', { start: 24, end: 30 }),
null,
undefined,
{ start: 17, end: 30 },
),
],
Expand Down

0 comments on commit 67018ae

Please sign in to comment.