Skip to content

Commit

Permalink
Second pass after #907
Browse files Browse the repository at this point in the history
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
  • Loading branch information
leebyron committed Jun 14, 2017
1 parent 3aa2a73 commit 8523f3c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 42 deletions.
5 changes: 5 additions & 0 deletions src/language/__tests__/schema-kitchen-sink.graphql
Expand Up @@ -76,3 +76,8 @@ directive @include(if: Boolean!)
on FIELD
| FRAGMENT_SPREAD
| INLINE_FRAGMENT

directive @include2(if: Boolean!) on
| FIELD
| FRAGMENT_SPREAD
| INLINE_FRAGMENT
64 changes: 27 additions & 37 deletions src/language/__tests__/schema-parser-test.js
Expand Up @@ -457,7 +457,29 @@ type Hello {
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Union with two types and leading vertical bar', () => {
it('Union with two types', () => {
const body = 'union Hello = Wo | Rld';
const doc = parse(body);
const expected = {
kind: 'Document',
definitions: [
{
kind: 'UnionTypeDefinition',
name: nameNode('Hello', { start: 6, end: 11 }),
directives: [],
types: [
typeNode('Wo', { start: 14, end: 16 }),
typeNode('Rld', { start: 19, end: 22 }),
],
loc: { start: 0, end: 22 },
}
],
loc: { start: 0, end: 22 },
};
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Union with two types and leading pipe', () => {
const body = 'union Hello = | Wo | Rld';
const doc = parse(body);
const expected = {
Expand All @@ -479,58 +501,26 @@ type Hello {
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Union with no types and leading vertical bar', () => {
it('Union fails with no types', () => {
const body = 'union Hello = |';
expect(() => parse(body)).to.throw();
});

it('Union with types and ending vertical bar', () => {
const body = 'union Hello = Wo | Rld |';
expect(() => parse(body)).to.throw();
});

it('Union with types and double vertical bar at the beginning', () => {
it('Union fails with leading douple pipe', () => {
const body = 'union Hello = || Wo | Rld';
expect(() => parse(body)).to.throw();
});

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

it('Union with types and double vertical bar at the end', () => {
const body = 'union Hello = | Wo | Rld ||';
expect(() => parse(body)).to.throw();
});

it('Union with types , leanding and ending vertical bar', () => {
it('Union fails with trailing pipe', () => {
const body = 'union Hello = | Wo | Rld |';
expect(() => parse(body)).to.throw();
});

it('Union with two types', () => {
const body = 'union Hello = Wo | Rld';
const doc = parse(body);
const expected = {
kind: 'Document',
definitions: [
{
kind: 'UnionTypeDefinition',
name: nameNode('Hello', { start: 6, end: 11 }),
directives: [],
types: [
typeNode('Wo', { start: 14, end: 16 }),
typeNode('Rld', { start: 19, end: 22 }),
],
loc: { start: 0, end: 22 },
}
],
loc: { start: 0, end: 22 },
};
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Scalar', () => {
const body = 'scalar Hello';
const doc = parse(body);
Expand Down
2 changes: 2 additions & 0 deletions src/language/__tests__/schema-printer-test.js
Expand Up @@ -119,6 +119,8 @@ type NoFields {}
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
directive @include2(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
`);

});
Expand Down
11 changes: 6 additions & 5 deletions src/language/parser.js
Expand Up @@ -938,14 +938,13 @@ function parseUnionTypeDefinition(lexer: Lexer<*>): UnionTypeDefinitionNode {

/**
* UnionMembers :
* - NamedType
* - `|`? NamedType
* - UnionMembers | NamedType
*/
function parseUnionMembers(lexer: Lexer<*>): Array<NamedTypeNode> {
// Optional leading pipe
skip(lexer, TokenKind.PIPE);
const members = [];
if (peek(lexer, TokenKind.PIPE)) {
skip(lexer, TokenKind.PIPE);
}
do {
members.push(parseNamedType(lexer));
} while (skip(lexer, TokenKind.PIPE));
Expand Down Expand Up @@ -1056,10 +1055,12 @@ function parseDirectiveDefinition(lexer: Lexer<*>): DirectiveDefinitionNode {

/**
* DirectiveLocations :
* - Name
* - `|`? Name
* - DirectiveLocations | Name
*/
function parseDirectiveLocations(lexer: Lexer<*>): Array<NameNode> {
// Optional leading pipe
skip(lexer, TokenKind.PIPE);
const locations = [];
do {
locations.push(parseName(lexer));
Expand Down

0 comments on commit 8523f3c

Please sign in to comment.