@@ -465,22 +465,19 @@ function parseFragment(
465
465
) : FragmentSpreadNode | InlineFragmentNode {
466
466
const start = lexer . token ;
467
467
expect ( lexer , TokenKind . SPREAD ) ;
468
- if ( peek ( lexer , TokenKind . NAME ) && lexer . token . value !== 'on' ) {
468
+
469
+ const hasTypeCondition = skipKeyword ( lexer , 'on' ) ;
470
+ if ( ! hasTypeCondition && peek ( lexer , TokenKind . NAME ) ) {
469
471
return {
470
472
kind : Kind . FRAGMENT_SPREAD ,
471
473
name : parseFragmentName ( lexer ) ,
472
474
directives : parseDirectives ( lexer , false ) ,
473
475
loc : loc ( lexer , start ) ,
474
476
} ;
475
477
}
476
- let typeCondition ;
477
- if ( lexer . token . value === 'on' ) {
478
- lexer . advance ( ) ;
479
- typeCondition = parseNamedType ( lexer ) ;
480
- }
481
478
return {
482
479
kind : Kind . INLINE_FRAGMENT ,
483
- typeCondition,
480
+ typeCondition : hasTypeCondition ? parseNamedType ( lexer ) : undefined ,
484
481
directives : parseDirectives ( lexer , false ) ,
485
482
selectionSet : parseSelectionSet ( lexer ) ,
486
483
loc : loc ( lexer , start ) ,
@@ -889,8 +886,7 @@ function parseObjectTypeDefinition(lexer: Lexer<*>): ObjectTypeDefinitionNode {
889
886
*/
890
887
function parseImplementsInterfaces ( lexer : Lexer < * > ) : Array < NamedTypeNode > {
891
888
const types = [ ] ;
892
- if ( lexer . token . value === 'implements' ) {
893
- lexer . advance ( ) ;
889
+ if ( skipKeyword ( lexer , 'implements' ) ) {
894
890
// Optional leading ampersand
895
891
skip ( lexer , TokenKind . AMP ) ;
896
892
do {
@@ -1464,11 +1460,11 @@ function peek(lexer: Lexer<*>, kind: TokenKindEnum): boolean {
1464
1460
* the lexer. Otherwise, do not change the parser state and return false.
1465
1461
*/
1466
1462
function skip ( lexer : Lexer < * > , kind : TokenKindEnum ) : boolean {
1467
- const match = lexer . token . kind === kind ;
1468
- if ( match ) {
1463
+ if ( lexer . token . kind === kind ) {
1469
1464
lexer . advance ( ) ;
1465
+ return true ;
1470
1466
}
1471
- return match ;
1467
+ return false ;
1472
1468
}
1473
1469
1474
1470
/**
@@ -1489,21 +1485,31 @@ function expect(lexer: Lexer<*>, kind: TokenKindEnum): Token {
1489
1485
}
1490
1486
1491
1487
/**
1492
- * If the next token is a keyword with the given value, return that token after
1493
- * advancing the lexer. Otherwise, do not change the parser state and return
1494
- * false.
1488
+ * If the next token is a keyword with the given value, return true after advancing
1489
+ * the lexer. Otherwise, do not change the parser state and return false.
1495
1490
*/
1496
- function expectKeyword ( lexer : Lexer < * > , value : string ) : Token {
1491
+ function skipKeyword ( lexer : Lexer < * > , value : string ) : boolean {
1497
1492
const token = lexer . token ;
1498
1493
if ( token . kind === TokenKind . NAME && token . value === value ) {
1499
1494
lexer . advance ( ) ;
1500
- return token ;
1495
+ return true ;
1496
+ }
1497
+ return false ;
1498
+ }
1499
+
1500
+ /**
1501
+ * If the next token is a keyword with the given value, return that token after
1502
+ * advancing the lexer. Otherwise, do not change the parser state and throw
1503
+ * an error.
1504
+ */
1505
+ function expectKeyword ( lexer : Lexer < * > , value : string ) : void {
1506
+ if ( ! skipKeyword ( lexer , value ) ) {
1507
+ throw syntaxError (
1508
+ lexer . source ,
1509
+ lexer . token . start ,
1510
+ `Expected "${ value } ", found ${ getTokenDesc ( lexer . token ) } ` ,
1511
+ ) ;
1501
1512
}
1502
- throw syntaxError (
1503
- lexer . source ,
1504
- token . start ,
1505
- `Expected "${ value } ", found ${ getTokenDesc ( token ) } ` ,
1506
- ) ;
1507
1513
}
1508
1514
1509
1515
/**
0 commit comments