Skip to content

Commit 053efcc

Browse files
committed
Second pass after #907
This adds a bit of cleanup after #907 for post-commit review: * Removes unnecessary `peek()` * Adds leading pipe support to directive locations * Removes some redundant tests * Orders tests so similar tests remain together
1 parent 3aa2a73 commit 053efcc

File tree

4 files changed

+39
-41
lines changed

4 files changed

+39
-41
lines changed

src/language/__tests__/schema-kitchen-sink.graphql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,8 @@ directive @include(if: Boolean!)
7676
on FIELD
7777
| FRAGMENT_SPREAD
7878
| INLINE_FRAGMENT
79+
80+
directive @include2(if: Boolean!) on
81+
| FIELD
82+
| FRAGMENT_SPREAD
83+
| INLINE_FRAGMENT

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

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,28 @@ type Hello {
457457
expect(printJson(doc)).to.equal(printJson(expected));
458458
});
459459

460+
it('Union with two types', () => {
461+
const body = 'union Hello = Wo | Rld';
462+
const doc = parse(body);
463+
const expected = {
464+
kind: 'Document',
465+
definitions: [
466+
{
467+
kind: 'UnionTypeDefinition',
468+
name: nameNode('Hello', { start: 6, end: 11 }),
469+
directives: [],
470+
types: [
471+
typeNode('Wo', { start: 14, end: 16 }),
472+
typeNode('Rld', { start: 19, end: 22 }),
473+
],
474+
loc: { start: 0, end: 22 },
475+
}
476+
],
477+
loc: { start: 0, end: 22 },
478+
};
479+
expect(printJson(doc)).to.equal(printJson(expected));
480+
});
481+
460482
it('Union with two types and leading vertical bar', () => {
461483
const body = 'union Hello = | Wo | Rld';
462484
const doc = parse(body);
@@ -479,58 +501,26 @@ type Hello {
479501
expect(printJson(doc)).to.equal(printJson(expected));
480502
});
481503

482-
it('Union with no types and leading vertical bar', () => {
504+
it('Union fails with no types', () => {
483505
const body = 'union Hello = |';
484506
expect(() => parse(body)).to.throw();
485507
});
486508

487-
it('Union with types and ending vertical bar', () => {
488-
const body = 'union Hello = Wo | Rld |';
489-
expect(() => parse(body)).to.throw();
490-
});
491-
492-
it('Union with types and double vertical bar at the beginning', () => {
509+
it('Union fails with leading douple pipe', () => {
493510
const body = 'union Hello = || Wo | Rld';
494511
expect(() => parse(body)).to.throw();
495512
});
496513

497-
it('Union with types and double vertical bar in the middle', () => {
514+
it('Union fails with double pipe', () => {
498515
const body = 'union Hello = Wo || Rld';
499516
expect(() => parse(body)).to.throw();
500517
});
501518

502-
it('Union with types and double vertical bar at the end', () => {
503-
const body = 'union Hello = | Wo | Rld ||';
504-
expect(() => parse(body)).to.throw();
505-
});
506-
507-
it('Union with types , leanding and ending vertical bar', () => {
519+
it('Union fails with trailing pipe', () => {
508520
const body = 'union Hello = | Wo | Rld |';
509521
expect(() => parse(body)).to.throw();
510522
});
511523

512-
it('Union with two types', () => {
513-
const body = 'union Hello = Wo | Rld';
514-
const doc = parse(body);
515-
const expected = {
516-
kind: 'Document',
517-
definitions: [
518-
{
519-
kind: 'UnionTypeDefinition',
520-
name: nameNode('Hello', { start: 6, end: 11 }),
521-
directives: [],
522-
types: [
523-
typeNode('Wo', { start: 14, end: 16 }),
524-
typeNode('Rld', { start: 19, end: 22 }),
525-
],
526-
loc: { start: 0, end: 22 },
527-
}
528-
],
529-
loc: { start: 0, end: 22 },
530-
};
531-
expect(printJson(doc)).to.equal(printJson(expected));
532-
});
533-
534524
it('Scalar', () => {
535525
const body = 'scalar Hello';
536526
const doc = parse(body);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ type NoFields {}
119119
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
120120
121121
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
122+
123+
directive @include2(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
122124
`);
123125

124126
});

src/language/parser.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -938,14 +938,13 @@ function parseUnionTypeDefinition(lexer: Lexer<*>): UnionTypeDefinitionNode {
938938

939939
/**
940940
* UnionMembers :
941-
* - NamedType
941+
* - `|`? NamedType
942942
* - UnionMembers | NamedType
943943
*/
944944
function parseUnionMembers(lexer: Lexer<*>): Array<NamedTypeNode> {
945+
// Optional leading pipe
946+
skip(lexer, TokenKind.PIPE);
945947
const members = [];
946-
if (peek(lexer, TokenKind.PIPE)) {
947-
skip(lexer, TokenKind.PIPE);
948-
}
949948
do {
950949
members.push(parseNamedType(lexer));
951950
} while (skip(lexer, TokenKind.PIPE));
@@ -1056,10 +1055,12 @@ function parseDirectiveDefinition(lexer: Lexer<*>): DirectiveDefinitionNode {
10561055

10571056
/**
10581057
* DirectiveLocations :
1059-
* - Name
1058+
* - `|`? Name
10601059
* - DirectiveLocations | Name
10611060
*/
10621061
function parseDirectiveLocations(lexer: Lexer<*>): Array<NameNode> {
1062+
// Optional leading pipe
1063+
skip(lexer, TokenKind.PIPE);
10631064
const locations = [];
10641065
do {
10651066
locations.push(parseName(lexer));

0 commit comments

Comments
 (0)