Skip to content

Commit 5fd69d4

Browse files
committed
consolidate parser changes
1 parent e7d6890 commit 5fd69d4

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

src/language/__tests__/parser-test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ describe('Parser', () => {
5858
locations: [{ line: 1, column: 1 }],
5959
});
6060

61+
// Throws on first error, the unexpected description.
62+
expectSyntaxError(`
63+
"Unexpected description"
64+
notAnOperation Foo { field }
65+
`).to.deep.include({
66+
message: 'Syntax Error: Unexpected description, only GraphQL definitions support descriptions.',
67+
locations: [{ line: 2, column: 7 }],
68+
});
69+
6170
expectSyntaxError('...').to.deep.include({
6271
message: 'Syntax Error: Unexpected "...".',
6372
locations: [{ line: 1, column: 1 }],
@@ -751,7 +760,7 @@ describe('Parser', () => {
751760
}
752761
`),
753762
).to.throw(
754-
'Syntax Error: Unexpected description, descriptions are not supported on shorthand queries.',
763+
'Syntax Error: Unexpected description, shorthand queries do not support descriptions.',
755764
);
756765
});
757766
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ describe('Schema Parser', () => {
331331
}
332332
`).to.deep.equal({
333333
message:
334-
'Syntax Error: Unexpected description, descriptions are not supported on type extensions.',
334+
'Syntax Error: Unexpected description, only GraphQL definitions support descriptions.',
335335
locations: [{ line: 2, column: 7 }],
336336
});
337337

@@ -353,7 +353,7 @@ describe('Schema Parser', () => {
353353
}
354354
`).to.deep.equal({
355355
message:
356-
'Syntax Error: Unexpected description, descriptions are not supported on type extensions.',
356+
'Syntax Error: Unexpected description, only GraphQL definitions support descriptions.',
357357
locations: [{ line: 2, column: 7 }],
358358
});
359359

src/language/parser.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,25 @@ export class Parser {
258258
* - InputObjectTypeDefinition
259259
*/
260260
parseDefinition(): DefinitionNode {
261-
if (this.peek(TokenKind.BRACE_L)) {
262-
return this.parseOperationDefinition();
263-
}
264261

265262
// Many definitions begin with a description and require a lookahead.
266263
const hasDescription = this.peekDescription();
267264
const keywordToken = hasDescription
268265
? this._lexer.lookahead()
269266
: this._lexer.token;
270267

268+
if (keywordToken.kind === TokenKind.BRACE_L) {
269+
// Check for shorthand query with description
270+
if (hasDescription) {
271+
throw syntaxError(
272+
this._lexer.source,
273+
this._lexer.token.start,
274+
'Unexpected description, shorthand queries do not support descriptions.',
275+
);
276+
}
277+
return this.parseOperationDefinition();
278+
}
279+
271280
if (keywordToken.kind === TokenKind.NAME) {
272281
switch (keywordToken.value) {
273282
case 'schema':
@@ -286,37 +295,27 @@ export class Parser {
286295
return this.parseInputObjectTypeDefinition();
287296
case 'directive':
288297
return this.parseDirectiveDefinition();
298+
case 'query':
299+
case 'mutation':
300+
case 'subscription':
301+
return this.parseOperationDefinition();
302+
case 'fragment':
303+
return this.parseFragmentDefinition();
289304
}
290305

291-
if (hasDescription && keywordToken.value === 'extend') {
306+
if (hasDescription) {
292307
throw syntaxError(
293308
this._lexer.source,
294309
this._lexer.token.start,
295-
'Unexpected description, descriptions are not supported on type extensions.',
310+
'Unexpected description, only GraphQL definitions support descriptions.',
296311
);
297312
}
298313

299-
switch (keywordToken.value) {
300-
case 'query':
301-
case 'mutation':
302-
case 'subscription':
303-
return this.parseOperationDefinition();
304-
case 'fragment':
305-
return this.parseFragmentDefinition();
306-
case 'extend':
307-
return this.parseTypeSystemExtension();
314+
if (keywordToken.value == 'extend') {
315+
return this.parseTypeSystemExtension();
308316
}
309317
}
310318

311-
// Check for shorthand query with description
312-
if (hasDescription && keywordToken.kind === TokenKind.BRACE_L) {
313-
throw syntaxError(
314-
this._lexer.source,
315-
this._lexer.token.start,
316-
'Unexpected description, descriptions are not supported on shorthand queries.',
317-
);
318-
}
319-
320319
throw this.unexpected(keywordToken);
321320
}
322321

0 commit comments

Comments
 (0)